From: Simon Marchi <simon.marchi@ericsson.com>
To: Tom Tromey <tom@tromey.com>
Cc: <gdb-patches@sourceware.org>
Subject: Re: [PATCH 2/9] Use unsigned as base type for some enums
Date: Mon, 27 Aug 2018 21:26:00 -0000 [thread overview]
Message-ID: <bdeb8aef-571b-1622-c19c-536cf2948ad3@ericsson.com> (raw)
In-Reply-To: <87r2ijy2zm.fsf@tromey.com>
On 2018-08-27 04:21 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
>
> Simon> Can you give an example of how the error manifests itself (I'm not really
> Simon> familiar with -fsanitize=undefined). Is the error reported at compile-time
> Simon> or run-time? I'm not able to make a synthetic standalone example to reproduce
> Simon> the error.
>
> You will get an error at runtime, and with the flags added by the last
> patch in the series, a crash.
>
> The error looks somewhat like the error from the expression dumping
> patch:
>
> runtime error: load of value 2887952, which is not a valid value for type 'exp_opcode'
>
> (I don't have an exact error handy, this was just taken from the other
> patch.)
>
> Simon> In any case, that LGTM if that makes the compiler happy. If the error reported
> Simon> by -fsanitize=undefined is at run-time, could we add a static assert in there
> Simon> to make sure the underlying types of types used with DEF_ENUM_FLAGS_TYPE are
> Simon> unsigned, to get a compilation error?
>
> With the final patch, any UB will cause gdb to crash (in development
> mode), presumably leading to a test suite failure. I think it isn't
> necessary to require unsigned as the underlying type -- any type will
> do. However I don't know how to assert that.
Indeed, it's only necessary if the ~ operator is used. OTOH, it doesn't really
make sense to use a signed type for flags, so we wouldn't lose anything by enforcing
unsigned types. If I understand correctly, the errors come from code like this, when
making a bit mask to clear some bits:
btinfo->flags &= ~(BTHR_MOVE | BTHR_STOP);
Doing a static assert like this:
diff --git a/gdb/common/enum-flags.h b/gdb/common/enum-flags.h
index 82568a5..c82970c 100644
--- a/gdb/common/enum-flags.h
+++ b/gdb/common/enum-flags.h
@@ -92,6 +92,7 @@ class enum_flags
public:
typedef E enum_type;
typedef typename enum_underlying_type<enum_type>::type underlying_type;
+ gdb_static_assert (std::is_unsigned<underlying_type>::value);
private:
/* Private type used to support initializing flag types with zero:
would enforce it at compile time, which is preferable than finding it at
runtime.
Simon
next prev parent reply other threads:[~2018-08-27 21:26 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-27 14:57 [PATCH 0/9] Add UBSan to the build Tom Tromey
2018-08-27 14:57 ` [PATCH 2/9] Use unsigned as base type for some enums Tom Tromey
2018-08-27 19:22 ` Simon Marchi
2018-08-27 20:22 ` Tom Tromey
2018-08-27 21:26 ` Simon Marchi [this message]
2018-08-28 18:54 ` Pedro Alves
2018-08-28 22:42 ` Tom Tromey
2018-08-29 0:01 ` Tom Tromey
2018-08-27 14:57 ` [PATCH 5/9] Avoid undefined behavior in parse_number Tom Tromey
2018-08-27 14:57 ` [PATCH 1/9] Do not pass NULL to memcpy Tom Tromey
2018-08-27 19:12 ` Simon Marchi
2018-08-28 22:40 ` Tom Tromey
2018-08-27 14:57 ` [PATCH 7/9] Avoid undefined behavior in ada_operator_length Tom Tromey
2018-08-27 14:57 ` [PATCH 3/9] Avoid undefined behavior in extract_integer Tom Tromey
2018-08-28 18:39 ` Pedro Alves
2018-08-29 0:11 ` Tom Tromey
2018-08-27 14:57 ` [PATCH 6/9] Avoid undefined behavior in read_signed_leb128 Tom Tromey
2018-08-27 14:59 ` [PATCH 8/9] Avoid undefined behavior in expression dumping Tom Tromey
2018-08-28 18:49 ` Pedro Alves
2018-08-29 0:27 ` Tom Tromey
2018-08-27 14:59 ` [PATCH 4/9] Avoid undefined behavior in read_subrange_type Tom Tromey
2018-08-27 14:59 ` [PATCH 9/9] Add --enable-ubsan Tom Tromey
2018-08-28 19:03 ` [PATCH 0/9] Add UBSan to the build Pedro Alves
2018-08-29 0:45 ` Tom Tromey
2018-08-30 18:41 ` Pedro Alves
2018-08-30 18:56 ` Tom Tromey
2018-08-30 19:06 ` Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bdeb8aef-571b-1622-c19c-536cf2948ad3@ericsson.com \
--to=simon.marchi@ericsson.com \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).