From: Kees Cook <keescook@chromium.org>
To: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: airlied@linux.ie, trix@redhat.com, dlatypov@google.com,
dri-devel@lists.freedesktop.org, gustavoars@kernel.org,
linux-hardening@vger.kernel.org, linux-sparse@vger.kernel.org,
llvm@lists.linux.dev, arnd@kernel.org,
intel-gfx@lists.freedesktop.org, nathan@kernel.org,
rodrigo.vivi@intel.com, mchehab@kernel.org,
ndesaulniers@google.com, linux-kernel@vger.kernel.org,
daniel@ffwll.ch, vitor@massaru.org, luc.vanoostenryck@gmail.com
Subject: Re: [Intel-gfx] [PATCH v5] overflow: Introduce overflows_type() and castable_to_type()
Date: Wed, 2 Nov 2022 12:33:33 -0700 [thread overview]
Message-ID: <202211021233.02BF0D38@keescook> (raw)
In-Reply-To: <307b90cb-b80d-6ce3-14ae-4a0b2ee5e447@prevas.dk>
On Wed, Nov 02, 2022 at 12:52:32PM +0100, Rasmus Villemoes wrote:
> On 24/10/2022 22.11, Gwan-gyeong Mun wrote:
> > From: Kees Cook <keescook@chromium.org>
> >
> > Implement a robust overflows_type() macro to test if a variable or
> > constant value would overflow another variable or type. This can be
> > used as a constant expression for static_assert() (which requires a
> > constant expression[1][2]) when used on constant values. This must be
> > constructed manually, since __builtin_add_overflow() does not produce
> > a constant expression[3].
> >
> > Additionally adds castable_to_type(), similar to __same_type(), but for
> > checking if a constant value would overflow if cast to a given type.
> >
>
> > +#define __overflows_type_constexpr(x, T) ( \
> > + is_unsigned_type(typeof(x)) ? \
> > + (x) > type_max(typeof(T)) ? 1 : 0 \
> > + : is_unsigned_type(typeof(T)) ? \
> > + (x) < 0 || (x) > type_max(typeof(T)) ? 1 : 0 \
> > + : (x) < type_min(typeof(T)) || \
> > + (x) > type_max(typeof(T)) ? 1 : 0)
> > +
>
> Can't all these instances of "foo ? 1 : 0" be simplified to "foo"? That
> would improve the readability of this thing somewhat IMO.
Oh, good point. :P I'll fix these.
--
Kees Cook
WARNING: multiple messages have this Message-ID (diff)
From: Kees Cook <keescook@chromium.org>
To: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>,
luc.vanoostenryck@gmail.com, nathan@kernel.org,
ndesaulniers@google.com, trix@redhat.com, dlatypov@google.com,
vitor@massaru.org, gustavoars@kernel.org,
linux-hardening@vger.kernel.org, llvm@lists.linux.dev,
jani.nikula@linux.intel.com, joonas.lahtinen@linux.intel.com,
rodrigo.vivi@intel.com, tvrtko.ursulin@linux.intel.com,
airlied@linux.ie, daniel@ffwll.ch, linux-kernel@vger.kernel.org,
intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-sparse@vger.kernel.org, arnd@kernel.org,
mchehab@kernel.org, mauro.chehab@linux.intel.com
Subject: Re: [PATCH v5] overflow: Introduce overflows_type() and castable_to_type()
Date: Wed, 2 Nov 2022 12:33:33 -0700 [thread overview]
Message-ID: <202211021233.02BF0D38@keescook> (raw)
In-Reply-To: <307b90cb-b80d-6ce3-14ae-4a0b2ee5e447@prevas.dk>
On Wed, Nov 02, 2022 at 12:52:32PM +0100, Rasmus Villemoes wrote:
> On 24/10/2022 22.11, Gwan-gyeong Mun wrote:
> > From: Kees Cook <keescook@chromium.org>
> >
> > Implement a robust overflows_type() macro to test if a variable or
> > constant value would overflow another variable or type. This can be
> > used as a constant expression for static_assert() (which requires a
> > constant expression[1][2]) when used on constant values. This must be
> > constructed manually, since __builtin_add_overflow() does not produce
> > a constant expression[3].
> >
> > Additionally adds castable_to_type(), similar to __same_type(), but for
> > checking if a constant value would overflow if cast to a given type.
> >
>
> > +#define __overflows_type_constexpr(x, T) ( \
> > + is_unsigned_type(typeof(x)) ? \
> > + (x) > type_max(typeof(T)) ? 1 : 0 \
> > + : is_unsigned_type(typeof(T)) ? \
> > + (x) < 0 || (x) > type_max(typeof(T)) ? 1 : 0 \
> > + : (x) < type_min(typeof(T)) || \
> > + (x) > type_max(typeof(T)) ? 1 : 0)
> > +
>
> Can't all these instances of "foo ? 1 : 0" be simplified to "foo"? That
> would improve the readability of this thing somewhat IMO.
Oh, good point. :P I'll fix these.
--
Kees Cook
WARNING: multiple messages have this Message-ID (diff)
From: Kees Cook <keescook@chromium.org>
To: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: airlied@linux.ie, trix@redhat.com, dlatypov@google.com,
dri-devel@lists.freedesktop.org, gustavoars@kernel.org,
linux-hardening@vger.kernel.org, linux-sparse@vger.kernel.org,
llvm@lists.linux.dev, arnd@kernel.org,
intel-gfx@lists.freedesktop.org,
Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>,
nathan@kernel.org, rodrigo.vivi@intel.com, mchehab@kernel.org,
tvrtko.ursulin@linux.intel.com, mauro.chehab@linux.intel.com,
ndesaulniers@google.com, linux-kernel@vger.kernel.org,
vitor@massaru.org, luc.vanoostenryck@gmail.com
Subject: Re: [PATCH v5] overflow: Introduce overflows_type() and castable_to_type()
Date: Wed, 2 Nov 2022 12:33:33 -0700 [thread overview]
Message-ID: <202211021233.02BF0D38@keescook> (raw)
In-Reply-To: <307b90cb-b80d-6ce3-14ae-4a0b2ee5e447@prevas.dk>
On Wed, Nov 02, 2022 at 12:52:32PM +0100, Rasmus Villemoes wrote:
> On 24/10/2022 22.11, Gwan-gyeong Mun wrote:
> > From: Kees Cook <keescook@chromium.org>
> >
> > Implement a robust overflows_type() macro to test if a variable or
> > constant value would overflow another variable or type. This can be
> > used as a constant expression for static_assert() (which requires a
> > constant expression[1][2]) when used on constant values. This must be
> > constructed manually, since __builtin_add_overflow() does not produce
> > a constant expression[3].
> >
> > Additionally adds castable_to_type(), similar to __same_type(), but for
> > checking if a constant value would overflow if cast to a given type.
> >
>
> > +#define __overflows_type_constexpr(x, T) ( \
> > + is_unsigned_type(typeof(x)) ? \
> > + (x) > type_max(typeof(T)) ? 1 : 0 \
> > + : is_unsigned_type(typeof(T)) ? \
> > + (x) < 0 || (x) > type_max(typeof(T)) ? 1 : 0 \
> > + : (x) < type_min(typeof(T)) || \
> > + (x) > type_max(typeof(T)) ? 1 : 0)
> > +
>
> Can't all these instances of "foo ? 1 : 0" be simplified to "foo"? That
> would improve the readability of this thing somewhat IMO.
Oh, good point. :P I'll fix these.
--
Kees Cook
next prev parent reply other threads:[~2022-11-02 19:33 UTC|newest]
Thread overview: 71+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-26 19:11 [Intel-gfx] [PATCH v2] overflow: Introduce overflows_type() and castable_to_type() Kees Cook
2022-09-26 19:11 ` Kees Cook
2022-09-26 19:11 ` Kees Cook
2022-09-26 20:17 ` [Intel-gfx] " Nick Desaulniers
2022-09-26 20:17 ` Nick Desaulniers
2022-09-26 20:17 ` Nick Desaulniers
2022-09-26 21:07 ` [Intel-gfx] " Kees Cook
2022-09-26 21:07 ` Kees Cook
2022-09-26 21:07 ` Kees Cook
2022-09-27 6:45 ` [Intel-gfx] " Arnd Bergmann
2022-09-27 6:45 ` Arnd Bergmann
2022-09-27 6:45 ` Arnd Bergmann
2022-09-27 0:27 ` [Intel-gfx] ✗ Fi.CI.BUILD: failure for " Patchwork
2022-09-27 8:29 ` [Intel-gfx] [PATCH v2] " Gwan-gyeong Mun
2022-09-27 8:29 ` Gwan-gyeong Mun
2022-09-27 8:29 ` Gwan-gyeong Mun
2022-09-27 23:36 ` [Intel-gfx] " kernel test robot
2022-09-27 23:36 ` kernel test robot
2022-09-27 23:36 ` kernel test robot
2022-09-28 8:19 ` Gwan-gyeong Mun
2022-09-28 8:19 ` Gwan-gyeong Mun
2022-09-28 8:19 ` Gwan-gyeong Mun
2022-09-29 3:24 ` [Intel-gfx] " kernel test robot
2022-09-29 3:24 ` kernel test robot
2022-09-29 3:24 ` kernel test robot
2022-09-29 8:32 ` kernel test robot
2022-09-29 8:32 ` kernel test robot
2022-09-29 8:32 ` kernel test robot
2022-10-13 6:49 ` [Intel-gfx] [PATCH v3] " Gwan-gyeong Mun
2022-10-13 6:49 ` Gwan-gyeong Mun
2022-10-13 6:49 ` Gwan-gyeong Mun
2022-10-13 8:12 ` [Intel-gfx] " Jani Nikula
2022-10-13 8:12 ` Jani Nikula
2022-10-13 8:12 ` Jani Nikula
2022-10-13 8:17 ` [Intel-gfx] ✗ Fi.CI.BUILD: failure for overflow: Introduce overflows_type() and castable_to_type() (rev2) Patchwork
2022-10-21 8:33 ` [Intel-gfx] [PATCH v4] overflow: Introduce overflows_type() and castable_to_type() Gwan-gyeong Mun
2022-10-21 8:33 ` Gwan-gyeong Mun
2022-10-21 8:33 ` Gwan-gyeong Mun
2022-10-21 16:06 ` [Intel-gfx] " Nathan Chancellor
2022-10-21 16:06 ` Nathan Chancellor
2022-10-21 16:06 ` Nathan Chancellor
2022-10-21 12:29 ` [Intel-gfx] ✗ Fi.CI.CHECKPATCH: warning for overflow: Introduce overflows_type() and castable_to_type() (rev3) Patchwork
2022-10-21 12:29 ` [Intel-gfx] ✗ Fi.CI.SPARSE: " Patchwork
2022-10-21 12:48 ` [Intel-gfx] ✓ Fi.CI.BAT: success " Patchwork
2022-10-21 19:46 ` [Intel-gfx] ✗ Fi.CI.IGT: failure " Patchwork
2022-10-24 20:11 ` [Intel-gfx] [PATCH v5] overflow: Introduce overflows_type() and castable_to_type() Gwan-gyeong Mun
2022-10-24 20:11 ` Gwan-gyeong Mun
2022-10-24 20:11 ` Gwan-gyeong Mun
2022-10-29 5:55 ` [Intel-gfx] " Gwan-gyeong Mun
2022-10-29 5:55 ` Gwan-gyeong Mun
2022-10-29 5:55 ` Gwan-gyeong Mun
2022-10-29 7:32 ` [Intel-gfx] " Kees Cook
2022-10-29 7:32 ` Kees Cook
2022-10-29 7:32 ` Kees Cook
2022-10-29 8:01 ` [Intel-gfx] " Gwan-gyeong Mun
2022-10-29 8:01 ` Gwan-gyeong Mun
2022-10-29 8:01 ` Gwan-gyeong Mun
2022-11-01 23:06 ` [Intel-gfx] " Kees Cook
2022-11-01 23:06 ` Kees Cook
2022-11-02 11:27 ` [Intel-gfx] " Gwan-gyeong Mun
2022-11-02 11:27 ` Gwan-gyeong Mun
2022-11-02 11:27 ` Gwan-gyeong Mun
2022-11-02 11:52 ` [Intel-gfx] " Rasmus Villemoes
2022-11-02 11:52 ` Rasmus Villemoes
2022-11-02 11:52 ` Rasmus Villemoes
2022-11-02 19:33 ` Kees Cook [this message]
2022-11-02 19:33 ` Kees Cook
2022-11-02 19:33 ` Kees Cook
2022-10-25 0:31 ` [Intel-gfx] ✗ Fi.CI.CHECKPATCH: warning for overflow: Introduce overflows_type() and castable_to_type() (rev4) Patchwork
2022-10-25 0:31 ` [Intel-gfx] ✗ Fi.CI.SPARSE: " Patchwork
2022-10-25 0:53 ` [Intel-gfx] ✗ Fi.CI.BAT: failure " Patchwork
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=202211021233.02BF0D38@keescook \
--to=keescook@chromium.org \
--cc=airlied@linux.ie \
--cc=arnd@kernel.org \
--cc=daniel@ffwll.ch \
--cc=dlatypov@google.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gustavoars@kernel.org \
--cc=intel-gfx@lists.freedesktop.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=luc.vanoostenryck@gmail.com \
--cc=mchehab@kernel.org \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=rasmus.villemoes@prevas.dk \
--cc=rodrigo.vivi@intel.com \
--cc=trix@redhat.com \
--cc=vitor@massaru.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.