Message ID | bdeb8aef-571b-1622-c19c-536cf2948ad3@ericsson.com |
---|---|
State | New, archived |
Headers |
Received: (qmail 105211 invoked by alias); 27 Aug 2018 21:26:20 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: <gdb-patches.sourceware.org> List-Unsubscribe: <mailto:gdb-patches-unsubscribe-##L=##H@sourceware.org> List-Subscribe: <mailto:gdb-patches-subscribe@sourceware.org> List-Archive: <http://sourceware.org/ml/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-help@sourceware.org>, <http://sourceware.org/ml/#faqs> Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 105201 invoked by uid 89); 27 Aug 2018 21:26:19 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.2 spammy=Private X-HELO: sessmg23.ericsson.net Received: from sessmg23.ericsson.net (HELO sessmg23.ericsson.net) (193.180.251.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 27 Aug 2018 21:26:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/simple; q=dns/txt; i=@ericsson.com; t=1535405175; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=sMC0JQmE1VUVf1z4ihqi8xFnTveZTi4OLGL+mcunV3A=; b=OwBOPq2n1oAxxGhFKGZgq+0yGUqCfLUPLbvW38nZOwLCDkyegfO+XJISGjYyL9XZ WDdAHcUQ6OmP1qdap18rdibrzdmaABKVF7ysm8kKjJMByD/ka+q6oY91oRtjj7P6 bjftXguajwin+MbfMZ6yvqBo5NC5Jg1tffcNjdFrf+g=; Received: from ESESSMB502.ericsson.se (Unknown_Domain [153.88.183.120]) by sessmg23.ericsson.net (Symantec Mail Security) with SMTP id B4.F3.22015.77C648B5; Mon, 27 Aug 2018 23:26:15 +0200 (CEST) Received: from ESESBMB503.ericsson.se (153.88.183.170) by ESESSMB502.ericsson.se (153.88.183.163) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Mon, 27 Aug 2018 23:26:13 +0200 Received: from NAM03-BY2-obe.outbound.protection.outlook.com (153.88.183.157) by ESESBMB503.ericsson.se (153.88.183.170) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3 via Frontend Transport; Mon, 27 Aug 2018 23:26:13 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5Z8i6BWR8PnA7PpwYNhVAXtVcAJGeq/SGpVdvCzEaQo=; b=O1Pe/Rdu56pM3WGtCFRHuhg7vELjazScfqN54KzV9ELRFHYHejK8LH1x6YaeKgBV/tbA3rmI+etLKqtk+b0XJCxnsX1Lesdxz1keI1IyeqmQtY3Kaf73Lxy32NFT2GoiMroFj+mdfE7iEkpzhPO3vvXjqOiAmP0zrIXcUGIg9I0= Received: from [142.133.48.188] (192.75.88.130) by BN7PR15MB2387.namprd15.prod.outlook.com (2603:10b6:406:8c::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1080.15; Mon, 27 Aug 2018 21:26:10 +0000 Subject: Re: [PATCH 2/9] Use unsigned as base type for some enums To: Tom Tromey <tom@tromey.com> CC: <gdb-patches@sourceware.org> References: <20180827145620.11055-1-tom@tromey.com> <20180827145620.11055-3-tom@tromey.com> <86e5c223-9fcc-ad6c-7c29-63037f4bdb74@ericsson.com> <87r2ijy2zm.fsf@tromey.com> From: Simon Marchi <simon.marchi@ericsson.com> Message-ID: <bdeb8aef-571b-1622-c19c-536cf2948ad3@ericsson.com> Date: Mon, 27 Aug 2018 17:26:00 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <87r2ijy2zm.fsf@tromey.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Return-Path: simon.marchi@ericsson.com Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=simon.marchi@ericsson.com; Received-SPF: None (protection.outlook.com: ericsson.com does not designate permitted sender hosts) X-IsSubscribed: yes |
Commit Message
Simon Marchi
Aug. 27, 2018, 9:26 p.m. UTC
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: would enforce it at compile time, which is preferable than finding it at runtime. Simon
Comments
On 08/27/2018 10:26 PM, Simon Marchi wrote: > 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); > Yeah. I've been meaning to find time to repost by enum-flags v2 fixes, but, it's never happened... One of the things that I wanted to look at is exactly this issue, seeing about coming up with some solution that is compatible with C++98/C++03, given that the GCC folks expressed interest in sharing the enum-flags.h header. Off hand, all I can think of is to introduce a "clear(unsigned mask)" method that would be used instead of &= and ~... But maybe really best is to ignore that (C++98), expecting that GCC will come around to requiring C++11 in some reasonable timeframe... > 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. Yes, I think that we should add that. Thanks, Pedro Alves
>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
Simon> Doing a static assert like this:
Simon> + gdb_static_assert (std::is_unsigned<underlying_type>::value);
Simon> would enforce it at compile time, which is preferable than finding it at
Simon> runtime.
I made this change.
Tom
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes: >>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes: Simon> Doing a static assert like this: Simon> + gdb_static_assert (std::is_unsigned<underlying_type>::value); Simon> would enforce it at compile time, which is preferable than finding it at Simon> runtime. Tom> I made this change. This change turns out to be a bit too eager. It requires changing all the enums that are used by DEF_ENUM_FLAGS_TYPE; including gcc_qualifiers, which is maintained in gcc (and theoretically at least is C compatible) -- but operator~ is never actually used with these other enums, so there isn't any UB to avoid. However, moving the static assert to operator~ seems to work. Tom
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: