[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [XEN PATCH v6] xen/include: avoid using a compiler extension for BUILD_BUG_ON_ZERO


  • To: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Tue, 4 Jul 2023 16:24:57 +0200
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=bfp/sOB1eMVA5o4DqiLd457KZuGKzZ1y3xs5SL2BAFo=; b=lKSM/fa+Sd1ibqpJgfSMER7W+8z0AR1PpS+hjQ9pFGf13cMsPfSOAxSH01KfgqcXOCsXdqQPxWXKslabVXFjNSJn0ArBOXn4aXkpv1h2Dz0Dlgg1rv1HtG9gnkir3rAh3ucgDpkfA9KpK5ZZQJnmZzijPeI/xRAnJQVyks7/Lnf3V7EWOKzAUrlWll3n6H8VXTmOPdGtnQpnHkNoedw/Rhj5tDgXQyXLSq++qRyJygo6tqRiWe63xZS/SsRPA4rTsdH+BFovTijv9Dkw4FkNY26ArTZmWdFmOvNlrKNoi4NXRX35pNyu6K5ZkFUh12icAuREvfpI/yLeAjw33j2ofQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l50cU5Bx04b/1Gu+l9ZnNbAo8ndlMOMVvE3c7UIqmm2ghsd43qCsTdrmGv5N8MiSqgeWUmuIA2lBqNCVIt9zqoWUqfhiUEsY+bMaTqmQi/sdFhUvv7jKc3JL9fNV1EOZpKFSd9e6AS2sxoNFKgBFNzmMhmxxShFFGfS20z2k26JBXbF7DybS4/c2T/pezjQZxYeqS6qVNHWcrHjzmw8fGFIGZLPjf/kfvyoTXNvT6sE9cXVfy3c6IrRhUTF+AWcDT3abkEG8PaBsqcbQYoY8sS6HZHnvpFFTsqx6H9ElBl9WKCloz9RlDZUtrYR6PY/DQZzl00F/PucNtbBFajptxA==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com;
  • Cc: sstabellini@xxxxxxxxxx, michal.orzel@xxxxxxx, xenia.ragiadakou@xxxxxxx, ayan.kumar.halder@xxxxxxx, consulting@xxxxxxxxxxx, andrew.cooper3@xxxxxxxxxx, roger.pau@xxxxxxxxxx, bertrand.marquis@xxxxxxx, julien@xxxxxxx, George Dunlap <george.dunlap@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Tue, 04 Jul 2023 14:25:02 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 26.06.2023 17:37, Nicola Vetrini wrote:
> Redefine BUILD_BUG_ON_ZERO to avoid using a compiler extension
> that gives an acceptable semantics to C99 undefined behavior 58
> ("A structure or union is defined as containing no named members
> (6.7.2.1)").
> 
> The first definition includes an additional named field of type
> char.
> 
> The chosen ill-formed construct for the second definition is a struct
> with a named bitfield of width 0 when the condition is true,
> which prevents the UB without using the compiler extension while keeping
> the semantic of the construct.
> 
> The choice of the bitwise AND operation to bring the result to 0
> when cond is false boils down to possibly better portability.
> 
> Signed-off-by: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>

The code change looks okay to me now, but I'd like to double check
towards the testing you did with this change: While it is clear that
you will have checked that the tree builds with the adjustments, I
expect that would have been with a compiler supporting _Static_assert.
Did you also check with an older compiler, using the alternative
implementation? Plus did you also check both constructs for actually
triggering when the supplied condition turns out to be true?

Jan

> --- a/xen/include/xen/lib.h
> +++ b/xen/include/xen/lib.h
> @@ -51,9 +51,10 @@
>     e.g. in a structure initializer (or where-ever else comma expressions
>     aren't permitted). */
>  #define BUILD_BUG_ON_ZERO(cond) \
> -    sizeof(struct { _Static_assert(!(cond), "!(" #cond ")"); })
> +    (sizeof(struct { char c; _Static_assert(!(cond), "!(" #cond ")"); }) & 0)
>  #else
> -#define BUILD_BUG_ON_ZERO(cond) sizeof(struct { int:-!!(cond); })
> +#define BUILD_BUG_ON_ZERO(cond) \
> +    (sizeof(struct { unsigned u : !(cond); }) & 0)
>  #define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond))
>  #endif
>  




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.