From: Kees Cook <keescook@chromium.org>
To: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
Cc: intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org,
dri-devel@lists.freedesktop.org, mchehab@kernel.org,
chris@chris-wilson.co.uk, matthew.auld@intel.com,
thomas.hellstrom@linux.intel.com, jani.nikula@intel.com,
nirmoy.das@intel.com, airlied@redhat.com, daniel@ffwll.ch,
andi.shyti@linux.intel.com, andrzej.hajda@intel.com,
mauro.chehab@linux.intel.com, linux@rasmusvillemoes.dk,
vitor@massaru.org, dlatypov@google.com, ndesaulniers@google.com
Subject: Re: [PATCH v11 3/9] compiler_types.h: Add assert_same_type to catch type mis-match while compiling
Date: Sun, 25 Sep 2022 17:37:10 -0700 [thread overview]
Message-ID: <202209251032.71251A8@keescook> (raw)
In-Reply-To: <20220923082628.3061408-4-gwan-gyeong.mun@intel.com>
On Fri, Sep 23, 2022 at 11:26:22AM +0300, Gwan-gyeong Mun wrote:
> Adds assert_same_type and assert_same_typable macros to catch type
> mis-match while compiling. The existing typecheck() macro outputs build
> warnings, but the newly added assert_same_type() macro uses the
> static_assert macro (which uses _Static_assert keyword and it introduced
> in C11) to generate a build break when the types are different and can be
> used to detect explicit build errors. Unlike the assert_same_type() macro,
> assert_same_typable() macro allows a constant value as the second argument.
> Since static_assert is used at compile time and it requires
> constant-expression as an argument [1][2], overflows_type_ret_const_expr()
> is newly added. There is overflows_type() that has the same behavior, but
> the macro uses __builtin_add_overflow() internally, and
> __builtin_add_overflows returns a bool type [3], so it is difficult to use
> as an argument of _Static_assert. The assert_same_type and
> assert_same_typable macros have been added to compiler_types.h, but the
> overflows_type_ret_const_expr macro has been added to overflow.h
> So, overflow.h has to be included to use assert_same_typable which
> internally uses overflows_type_ret_const_expr.
> And it adds unit tests for overflows_type, overflows_type_ret_const_expr,
> assert_same_type and assert_same_typable. The overflows_type has been added
> as well to compare whether the overflows_type_ret_const_expr unit test has
> the same as the result.
I spent some time rewriting the code in this patch. I think it's really
close, but I wanted to tweak how things were being defined, naming, etc.
Notes below, and I'll send my proposed patch separately...
> [...]
> +#define overflows_type_ret_const_expr(x,T) ( \
For the "overflows_type" defines, I think this reads a bit better:
#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 )
#define __overflows_type(x, T) ({ \
typeof(T) v = 0; \
check_add_overflow((x), v, &v); \
})
#define overflows_type(n, T) \
__builtin_choose_expr(__is_constexpr(n), \
__overflows_type_constexpr(n, T), \
__overflows_type(n, T))
> [...]
> +/**
> + * assert_same_type - abort compilation if the first argument's data type and
> + * the second argument's data type are not the same
> + * @t1: data type or variable
> + * @t2: data type or variable
> + *
> + * The first and second arguments can be data types or variables or mixed (the
> + * first argument is the data type and the second argument is variable or vice
> + * versa). It determines whether the first argument's data type and the second
> + * argument's data type are the same while compiling, and it aborts compilation
> + * if the two types are not the same.
> + * See also assert_same_typable().
> + */
> +#define assert_same_type(t1, t2) static_assert(__same_type(t1, t2))
I still think I'd rather avoid a define for this. It doesn't seem worth
4 characters of savings to just have to type it out:
static_assert(__same_type(a, b))
> [...]
> +#define assert_same_typable(t, n) static_assert( \
> + __builtin_choose_expr(__builtin_constant_p(n), \
> + overflows_type_ret_const_expr(n,t) == 0, \
> + __same_type(t, n)))
This one I'd like to convert into something closer in naming convention to
"__same_type". Also note that "__builtin_constant_p()" doesn't actually
work here: it needs to be __is_constexpr(). So, I propose:
#define __castable_to_type(n, T) \
__builtin_choose_expr(__is_constexpr(n), \
__overflows_type_constexpr(n, T), \
__same_type(n, T))
Then we can do:
static_assert(__castable_to_type(INT_MAX, size_t));
> [...[
> +static void overflows_type_test(struct kunit *test)
> +{
> +/* Args are: first type, secound type, value, overflow expected */
> +#define TEST_OVERFLOWS_TYPE(t1, t2, v, of) do { \
> + t1 __t1 = v; \
> + t2 __t2; \
> + bool __of; \
> + __of = overflows_type(v, t2); \
> + if (__of != of) { \
> + KUNIT_EXPECT_EQ_MSG(test, __of, of, \
> + "expected overflows_type(%s, %s) to%s overflow\n", \
> + #v, #t2, of ? "" : " not"); \
> + } \
> [...]
> + __of = overflows_type_ret_const_expr(__t1, __t2) ? true : false;\
> + if (__of != of) { \
> + KUNIT_EXPECT_EQ_MSG(test, __of, of, \
> + "expected overflows_type_ret_const_expr(%s, %s) to%s overflow\n", \
> + #t1" __t1 = "#v, #t2" __t2", of ? "" : " not"); \
> + } \
These tests are excellent! I've adapted them a little bit to avoid some
of their internal redundancy. (i.e. the above blocks are basically
almost entire the same, etc).
-Kees
--
Kees Cook
next prev parent reply other threads:[~2022-09-26 0:37 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-23 8:26 [PATCH v11 0/9] Fixes integer overflow or integer truncation issues in page lookups, ttm place configuration and scatterlist creation Gwan-gyeong Mun
2022-09-23 8:26 ` [PATCH v11 1/9] overflow: Allow mixed type arguments Gwan-gyeong Mun
2022-09-23 8:26 ` [PATCH v11 2/9] overflow: Move and add few utility macros into overflow Gwan-gyeong Mun
2022-09-23 8:26 ` [PATCH v11 3/9] compiler_types.h: Add assert_same_type to catch type mis-match while compiling Gwan-gyeong Mun
2022-09-24 4:21 ` kernel test robot
2022-09-24 14:52 ` kernel test robot
2022-09-26 0:37 ` Kees Cook [this message]
2022-09-26 0:37 ` [PATCH v11.5] overflow: Introduce overflows_type() and __castable_to_type() Kees Cook
2022-09-26 9:53 ` Gwan-gyeong Mun
2022-09-26 15:57 ` Gwan-gyeong Mun
2022-09-26 17:49 ` Kees Cook
2022-09-23 8:26 ` [PATCH v11 4/9] drm/i915/gem: Typecheck page lookups Gwan-gyeong Mun
2022-09-23 8:26 ` [PATCH v11 5/9] drm/i915: Check for integer truncation on scatterlist creation Gwan-gyeong Mun
2022-09-23 8:26 ` [PATCH v11 6/9] drm/i915: Check for integer truncation on the configuration of ttm place Gwan-gyeong Mun
2022-09-23 8:26 ` [PATCH v11 7/9] drm/i915: Check if the size is too big while creating shmem file Gwan-gyeong Mun
2022-09-23 8:26 ` [PATCH v11 8/9] drm/i915: Use error code as -E2BIG when the size of gem ttm object is too large Gwan-gyeong Mun
2022-09-23 8:26 ` [PATCH v11 9/9] drm/i915: Remove truncation warning for large objects Gwan-gyeong Mun
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=202209251032.71251A8@keescook \
--to=keescook@chromium.org \
--cc=airlied@redhat.com \
--cc=andi.shyti@linux.intel.com \
--cc=andrzej.hajda@intel.com \
--cc=chris@chris-wilson.co.uk \
--cc=daniel@ffwll.ch \
--cc=dlatypov@google.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gwan-gyeong.mun@intel.com \
--cc=intel-gfx@lists.freedesktop.org \
--cc=jani.nikula@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=matthew.auld@intel.com \
--cc=mauro.chehab@linux.intel.com \
--cc=mchehab@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nirmoy.das@intel.com \
--cc=thomas.hellstrom@linux.intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox