From: David Laight <David.Laight@ACULAB.COM>
To: 'Kees Cook' <keescook@chromium.org>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: "Gustavo A . R . Silva" <gustavoars@kernel.org>,
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>,
Jani Nikula <jani.nikula@linux.intel.com>,
Nick Desaulniers <ndesaulniers@google.com>,
Martin Uecker <Martin.Uecker@med.uni-goettingen.de>,
Jonathan Corbet <corbet@lwn.net>,
"linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>,
Miguel Ojeda <ojeda@kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-hardening@vger.kernel.org"
<linux-hardening@vger.kernel.org>
Subject: RE: [PATCH] compiler.h: Explain how __is_constexpr() works
Date: Fri, 1 Mar 2024 09:32:16 +0000 [thread overview]
Message-ID: <af0eff12e6bc41039614add550406c11@AcuMS.aculab.com> (raw)
In-Reply-To: <20240301044428.work.411-kees@kernel.org>
From: Kees Cook
> Sent: 01 March 2024 04:45
> To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
>
> The __is_constexpr() macro is dark magic. Shed some light on it with
> a comment to explain how and why it works.
All the 8s don't help...
I don't think you need that much explanation.
Perhaps just saying that the type of ?: depends on the types
of the values and is independent of the condition.
The type of (0 ? (void *)p : (foo *)q) is normally 'void *'
(so that both values can be assigned to it).
But if 'p' is 'an integer constant expression with value 0'
then (void *)p is NULL and the type is 'foo *'.
The type can then be checked to find out it 'p' is constant 0.
A non-zero constant 'p' can be multiples by 0.
I need to replace the definition with (the more portable):
#define __if_constexpr(cond, if_const, if_not_const) \
_Generic(0 ? (void *)((long)(cond) * 0) : (char *)0, \
char *: (if_const), \
void *: (if_not_const))
which is arguably less cryptic.
#define __is_constexpr(cond) __if_constexpr(cond, 1, 0)
So that I can write:
#define is_non_neg_const(x) (__if_constexpr(x, x , -1) >= 0)
and avoid the compiler bleating about some comparisons
in unreachable code.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2024-03-01 9:32 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-01 4:44 [PATCH] compiler.h: Explain how __is_constexpr() works Kees Cook
2024-03-01 8:16 ` Jani Nikula
2024-03-01 9:32 ` David Laight [this message]
2024-03-01 13:21 ` [+externe Mail+] " Uecker, Martin
2024-03-01 13:43 ` David Laight
2024-03-01 13:56 ` Uecker, Martin
2024-03-02 1:17 ` Andy Shevchenko
2024-03-02 1:46 ` Kees Cook
2024-03-04 16:52 ` Nick Desaulniers
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=af0eff12e6bc41039614add550406c11@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=Martin.Uecker@med.uni-goettingen.de \
--cc=corbet@lwn.net \
--cc=gustavoars@kernel.org \
--cc=jani.nikula@linux.intel.com \
--cc=keescook@chromium.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=ndesaulniers@google.com \
--cc=ojeda@kernel.org \
/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