Define is_signed_type() once
cover Define is_signed_type() once
Hi Kees,
The changes in this patch series are as follows:
- Add a unit test for the is_signed_type() macro.
- Define the is_signed_type() macro once.
Please consider these patches for the next merge window.
Thanks,
Bart.
Bart Van Assche (2):
testing/selftests: Add tests for the is_signed_type() macro overflow, tracing: Define the is_signed_type() macro once
include/linux/compiler.h | 6 +++++ include/linux/overflow.h | 1 - include/linux/trace_events.h | 2 -- lib/Kconfig.debug | 12 +++++++++ lib/Makefile | 1 + lib/is_signed_type_test.c | 48 ++++++++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 lib/is_signed_type_test.c
PATCH 1/2 testing/selftests: Add tests for the is_signed_type() macro
Although not documented, is_signed_type() must support the 'bool' and pointer types next to scalar and enumeration types. Add a selftest that verifies that this macro handles all supported types correctly.
--- lib/Kconfig.debug | 12 ++++++++++ lib/Makefile | 1 + lib/is_signed_type_test.c | 48 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 lib/is_signed_type_test.c
lib/Kconfig.debug +12 -0
@@ -2506,6 +2506,18 @@ config MEMCPY_KUNIT_TEST If unsure, say N. +config IS_SIGNED_TYPE_KUNIT_TEST + tristate "Test is_signed_type() macro" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Builds unit tests for the is_signed_type() macro. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + config OVERFLOW_KUNIT_TEST tristate "Test check_*_overflow() functions at runtime" if !KUNIT_ALL_TESTS depends on KUNIT
lib/Makefile +1 -0
@@ -377,6 +377,7 @@ obj-$(CONFIG_BITS_TEST) += test_bits.o obj-$(CONFIG_CMDLINE_KUNIT_TEST) += cmdline_kunit.o obj-$(CONFIG_SLUB_KUNIT_TEST) += slub_kunit.o obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o +obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_test.o obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable) obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
lib/is_signed_type_test.c new file +48 -0
48 lines not on this page · raw message
Re: [PATCH 1/2] testing/selftests: Add tests for the is_signed_type() macro
Quoted 14 lines · Bart Van Assche
Although not documented,
is_signed_type()must support the 'bool' and pointer types next to scalar and enumeration types. Add a selftest that verifies that this macro handles all supported types correctly.Cc Andrew Morton <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Dan Williams <[email protected]>Cc Eric Dumazet <[email protected]>Cc Ingo Molnar <[email protected]>Cc Isabella Basso <[email protected]>Cc "Jason A. Donenfeld" <[email protected]>Cc Josh Poimboeuf <[email protected]>Cc Luc Van Oostenryck <[email protected]>Cc Masami Hiramatsu <[email protected]>
Quoted 8 lines
Cc Nathan Chancellor <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Rasmus Villemoes <[email protected]>Cc Sander Vanheule <[email protected]>Cc Steven Rostedt <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Yury Norov <[email protected]>Signed-off-by Bart Van Assche <[email protected]>
Signature · 1 line
Kees Cook
Re: [PATCH 1/2] testing/selftests: Add tests for the is_signed_type() macro
In reply to Bart Van AsscheHi, Bart,
Glad to see some more KUnit tests making it inside of the kernel’s lib folder :).
Quoted 72 lines
Am 26/08/2022 um 1:21 PM schrieb Bart Van Assche <[email protected]>:
Although not documented,
is_signed_type()must support the 'bool' and pointer types next to scalar and enumeration types. Add a selftest that verifies that this macro handles all supported types correctly.Cc Andrew Morton <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Dan Williams <[email protected]>Cc Eric Dumazet <[email protected]>Cc Ingo Molnar <[email protected]>Cc Isabella Basso <[email protected]>Cc "Jason A. Donenfeld" <[email protected]>Cc Josh Poimboeuf <[email protected]>Cc Luc Van Oostenryck <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Nathan Chancellor <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Rasmus Villemoes <[email protected]>Cc Sander Vanheule <[email protected]>Cc Steven Rostedt <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Yury Norov <[email protected]>Signed-off-by Bart Van Assche <[email protected]>---
lib/Kconfig.debug | 12 ++++++++++ lib/Makefile | 1 + lib/is_signed_type_test.c | 48 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+)create mode 100644
lib/is_signed_type_test.clib/Kconfig.debug +12
-0@@ -2506,6 +2506,18 @@ config MEMCPY_KUNIT_TEST If unsure, say N. +config IS_SIGNED_TYPE_KUNIT_TEST + tristate "Test is_signed_type() macro" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Builds unit tests for the is_signed_type() macro. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. +config
OVERFLOW_KUNIT_TEST
tristate "Test check_*_overflow() functions at runtime" if !KUNIT_ALL_TESTS depends on KUNITlib/Makefile +0
-0@@ -377,6 +377,7 @@ obj-$(CONFIG_BITS_TEST) += test_bits.oobj-$(CONFIG_CMDLINE_KUNIT_TEST) += cmdline_kunit.o obj-$(CONFIG_SLUB_KUNIT_TEST) += slub_kunit.o obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o +obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_test.o obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable) obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
lib/is_signed_type_test.c new file +0
-0
lib/is_signed_type_test.c line 8
Quoted 8 lines
lib/is_signed_type_test.c new file +8
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* +* ./tools/testing/kunit/kunit.py run is_signed_type [--raw_output] +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <kunit/test.h> +#include <linux/overflow.h>
Nit: I don’t know if that makes a huge difference but you might include <linux/compiler.h> directly to make the final object smaller. Of course, that would ideally be a change happening in 2/2 but that was already merged :).
Quoted 30 lines
lib/is_signed_type_test.c new file +30
-0+ +enum unsigned_enum { + constant_a =3 , +}; + +enum signed_enum { + constant_b =-1 , + constant_c =2 , +}; + +static void is_signed_type_test (struct kunit *test) +{ +KUNIT_EXPECT_EQ (test,is_signed_type (bool), false); + KUNIT_EXPECT_EQ(test, is_signed_type(signed char), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned char), false); +KUNIT_EXPECT_EQ (test,is_signed_type (int), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned int), false); +KUNIT_EXPECT_EQ (test,is_signed_type (long), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned long), false); + KUNIT_EXPECT_EQ(test, is_signed_type(long long), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned long long), false); + KUNIT_EXPECT_EQ(test, is_signed_type(enum unsigned_enum), false); + KUNIT_EXPECT_EQ(test, is_signed_type(enum signed_enum), true); + KUNIT_EXPECT_EQ(test, is_signed_type(void *), false); + KUNIT_EXPECT_EQ(test, is_signed_type(const char *), false); +} + +static struct kunit_case is_signed_type_test_cases[] = { +KUNIT_CASE (is_signed_type_test), + {}
lib/is_signed_type_test.c
Quoted 10 lines
lib/is_signed_type_test.c new file +10
-0+}; + +static struct kunit_suite is_signed_type_test_suite = { + .name ="is_signed_type" , + .test_cases = is_signed_type_test_cases, +}; + +kunit_test_suite (is_signed_type_test_suite); + +MODULE_LICENSE("Dual MIT/GPL“);
Cheers,
--
Isabella Basso
Re: [PATCH 1/2] testing/selftests: Add tests for the is_signed_type() macro
Quoted 6 lines · Isabella Basso
lib/is_signed_type_test.c line 8
Quoted 2 lines
lib/is_signed_type_test.c new file +2
-0+#include <kunit/test.h> +#include <linux/overflow.h> Nit: I don’t know if that makes a huge difference but you might include
<linux/compiler.h>directly to make the final object smaller. Of course, that would ideally be a change happening in 2/2 but that was already merged :).
Right, that could have been done in patch 2/2 but I think this can also be done as a follow-up patch. I'm not sure what Kees prefers?
Thanks,
Bart.
Re: [PATCH 1/2] testing/selftests: Add tests for the is_signed_type() macro
Quoted 10 lines · Bart Van Assche
Quoted 6 lines · Isabella Basso
lib/is_signed_type_test.c line 8
Quoted 2 lines
lib/is_signed_type_test.c new file +2
-0+#include <kunit/test.h> +#include <linux/overflow.h> Nit: I don’t know if that makes a huge difference but you might include
<linux/compiler.h>directly to make the final object smaller. Of course, that would ideally be a change happening in 2/2 but that was already merged :).Right, that could have been done in patch 2/2 but I think this can also be done as a follow-up patch. I'm not sure what Kees prefers?
A follow-up would be easier for me. And perhaps could include Rasmus's suggestions too?
Signature · 1 line
Kees Cook
Re: [PATCH 1/2] testing/selftests: Add tests for the is_signed_type() macro
In reply to Bart Van Asschelib/is_signed_type_test.c line 23
Quoted 10 lines · Bart Van Assche
Although not documented,
is_signed_type()must support the 'bool' and pointer types next to scalar and enumeration types. Add a selftest that verifies that this macro handles all supported types correctly.lib/is_signed_type_test.c new file +5
-0+static void is_signed_type_test (struct kunit *test) +{ +KUNIT_EXPECT_EQ (test,is_signed_type (bool), false); + KUNIT_EXPECT_EQ(test, is_signed_type(signed char), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned char), false);
Nice. You could consider adding
#ifdef __UNSIGNED_CHAR__ KUNIT_EXPECT_EQ(test, is_signed_type(char), false); #elseKUNIT_EXPECT_EQ(test, is_signed_type(char), true); #endif
The kernel depends on the compiler providing __UNSIGNED_CHAR__ in two places (one in ext4, one in printf test suite).
lib/is_signed_type_test.c line 31
Quoted 8 lines
lib/is_signed_type_test.c new file +8
-0+KUNIT_EXPECT_EQ (test,is_signed_type (int), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned int), false); +KUNIT_EXPECT_EQ (test,is_signed_type (long), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned long), false); + KUNIT_EXPECT_EQ(test, is_signed_type(long long), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned long long), false); + KUNIT_EXPECT_EQ(test, is_signed_type(enum unsigned_enum), false); + KUNIT_EXPECT_EQ(test, is_signed_type(enum signed_enum), true);
Yeah. But enum types are one of the weirdest aspects of C. Taking your example and expanding with a positive value that doesn't fit an int:
#include <stdio.h>
#define is_signed_type(t) ((t)-1 < (t)1)
#define typeinfo(t) printf("%-24s signed %d, size %zu\n", #t, is_signed_type(t), sizeof(t))
enum unsigned_enum {
constant_a = 3,
constant_d = 3000000000,
};enum signed_enum {
constant_b = -1,
constant_c = 2,
};int main(int argc, char *argv[])
{
enum unsigned_enum a = constant_a;
enum unsigned_enum d = constant_d;
enum signed_enum b = constant_b;
enum signed_enum c = constant_c;
typeinfo(enum unsigned_enum);
typeinfo(enum signed_enum);
typeinfo(typeof(constant_a));
typeinfo(typeof(constant_b));
typeinfo(typeof(constant_c));
typeinfo(typeof(constant_d));
typeinfo(typeof(a));
typeinfo(typeof(b));
typeinfo(typeof(c));
typeinfo(typeof(d));
return 0;
}This gives me
enum unsigned_enum signed 0, size 4 enum signed_enum signed 1, size 4 typeof(constant_a) signed 1, size 4 typeof(constant_b) signed 1, size 4 typeof(constant_c) signed 1, size 4 typeof(constant_d) signed 0, size 4 typeof(a) signed 0, size 4 typeof(b) signed 1, size 4 typeof(c) signed 1, size 4 typeof(d) signed 0, size 4
That is, typeof(constant_a) is not the same type (different signedness) as enum unsigned_enum! While both constant_d (due to its size) and variables declared as 'enum unsigned_enum' do indeed have that underlying unsigned type.
At least gcc and clang agree on this weirdness, but I haven't been able to find a spec mandating this. Anyway, this was just an aside.
Re: [PATCH 1/2] testing/selftests: Add tests for the is_signed_type() macro
Quoted 14 lines · Rasmus Villemoes
lib/is_signed_type_test.c line 23
Quoted 10 lines · Bart Van Assche
Although not documented,
is_signed_type()must support the 'bool' and pointer types next to scalar and enumeration types. Add a selftest that verifies that this macro handles all supported types correctly.lib/is_signed_type_test.c new file +5
-0+static void is_signed_type_test (struct kunit *test) +{ +KUNIT_EXPECT_EQ (test,is_signed_type (bool), false); + KUNIT_EXPECT_EQ(test, is_signed_type(signed char), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned char), false);Nice. You could consider adding
Quoted 8 lines
#ifdef
__UNSIGNED_CHAR__KUNIT_EXPECT_EQ(test, is_signed_type(char), false); #elseKUNIT_EXPECT_EQ(test, is_signed_type(char), true); #endifThe kernel depends on the compiler providing
__UNSIGNED_CHAR__in two places (one in ext4, one in printf test suite).
(reduced Cc-list)
Hi Rasmus,
Since I would like to implement the above suggestion I tried to look up other uses of the __UNSIGNED_CHAR__ macro. However, I couldn't find any. Did I perhaps do something wrong?
$ git grep -w __UNSIGNED_CHAR__ origin/master | wc
0 0 0Thanks,
Bart.
Re: [PATCH 1/2] testing/selftests: Add tests for the is_signed_type() macro
Quoted 5 lines · Bart Van Assche
Since I would like to implement the above suggestion I tried to look up other uses of the
__UNSIGNED_CHAR__macro. However, I couldn't find any. Did I perhaps do something wrong?$ git grep -w __UNSIGNED_CHAR__ origin/master | wc 0 0 0
No, sorry, I did. It's __CHAR_UNSIGNED__ . Here's the description from 'info cpp':
'__CHAR_UNSIGNED__'
GCC defines this macro if and only if the data type 'char' is unsigned on the target machine.
Rasmus
PATCH 2/2 overflow, tracing: Define the is_signed_type() macro once
In reply to Bart Van AsscheThere are two definitions of the is_signed_type() macro: one in <linux/overflow.h> and a second definition in <linux/trace_events.h>.
As suggested by Linus Torvalds, move the definition of the is_signed_type() macro into the <linux/compiler.h> header file. Change the definition of the is_signed_type() macro to make sure that it does not trigger any sparse warnings with future versions of sparse for bitwise types. See also:
https://lore.kernel.org/all/CAHk-=whjH6p+qzwUdx5SOVVHjS3WvzJQr6mDUwhEyTf6pJWzaQ@mail.gmail.com/
https://lore.kernel.org/all/CAHk-=wjQGnVfb4jehFR0XyZikdQvCZouE96xR_nnf5kqaM5qqQ@mail.gmail.com/
--- include/linux/compiler.h | 6 ++++++ include/linux/overflow.h | 1 - include/linux/trace_events.h | 2 -- 3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/compiler.h +6 -0
@@ -239,6 +239,12 @@ static inline void *offset_to_ptr(const int *off)/* &a[0] degrades to a pointer: a different type from an array */ #define __must_be_array (a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) +/* +* Whether 'type' is a signed type or an unsigned type. Supports scalar types, +* bool and also pointer types. +*/ +#define is_signed_type (type) (((type)(-1)) < (__force type)1) +/* * This is needed in functions which generate the stack canary, see * arch/x86/kernel/smpboot.c::start_secondary() for an example.
include/linux/overflow.h +0 -1
@@ -30,7 +30,6 @@* https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html - * credit to Christian Biere. */ -#define is_signed_type (type) (((type)(-1)) < (type)1)#define __type_half_max (type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))#define type_max (T ) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))#define type_min (T ) ((T)((T)-type_max(T)-(T)1))
include/linux/trace_events.h +0 -2
@@ -814,8 +814,6 @@ extern int trace_add_event_call(struct trace_event_call *call);extern int trace_remove_event_call (struct trace_event_call *call);extern int trace_event_get_offsets (struct trace_event_call *call); -#define is_signed_type (type) (((type)(-1)) < (type)1) -int ftrace_set_clr_event (struct trace_array *tr,char *buf,int set);int trace_set_clr_event (const char *system,const char *event,int set); int trace_array_set_clr_event(struct trace_array *tr, const char *system,
Re: [PATCH 2/2] overflow, tracing: Define the is_signed_type() macro once
Quoted 69 lines · Bart Van Assche
There are two definitions of the
is_signed_type()macro: one in <linux/overflow.h> and a second definition in <linux/trace_events.h>.As suggested by Linus Torvalds, move the definition of the
is_signed_type()macro into the <linux/compiler.h> header file. Change the definition of theis_signed_type()macro to make sure that it does not trigger any sparse warnings with future versions of sparse for bitwise types. See also:
https://lore.kernel.org/all/CAHk-=whjH6p+qzwUdx5SOVVHjS3WvzJQr6mDUwhEyTf6pJWzaQ@mail.gmail.com/
https://lore.kernel.org/all/CAHk-=wjQGnVfb4jehFR0XyZikdQvCZouE96xR_nnf5kqaM5qqQ@mail.gmail.com/Cc Andrew Morton <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Dan Williams <[email protected]>Cc Eric Dumazet <[email protected]>Cc Ingo Molnar <[email protected]>Cc Isabella Basso <[email protected]>Cc "Jason A. Donenfeld" <[email protected]>Cc Josh Poimboeuf <[email protected]>Cc Luc Van Oostenryck <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Nathan Chancellor <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Rasmus Villemoes <[email protected]>Cc Sander Vanheule <[email protected]>Cc Steven Rostedt <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Yury Norov <[email protected]>Signed-off-by Bart Van Assche <[email protected]>--- include/linux/compiler.h | 6 ++++++ include/linux/overflow.h | 1 - include/linux/trace_events.h | 2 -- 3 files changed, 6 insertions(+), 3 deletions(-)include/linux/compiler.h +6
-0@@ -239,6 +239,12 @@ static inline void *offset_to_ptr(const int *off)/* &a[0] degrades to a pointer: a different type from an array */ #define __must_be_array (a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) +/* +* Whether 'type' is a signed type or an unsigned type. Supports scalar types, +* bool and also pointer types. +*/ +#define is_signed_type (type) (((type)(-1)) < (__force type)1) +/* * This is needed in functions which generate the stack canary, see * arch/x86/kernel/smpboot.c::start_secondary() for an example. include/linux/overflow.h +0
-1@@ -30,7 +30,6 @@* https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html - * credit to Christian Biere. */ -#define is_signed_type (type) (((type)(-1)) < (type)1)#define __type_half_max (type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))#define type_max (T ) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))#define type_min (T ) ((T)((T)-type_max(T)-(T)1))include/linux/trace_events.h +0
-0
include/linux/trace_events.h extern int trace_add_event_call(struct trace_event_call *call); · line 819
Quoted 8 lines
include/linux/trace_events.h +0
-2@@ -814,8 +814,6 @@ extern int trace_add_event_call(struct trace_event_call *call);extern int trace_remove_event_call (struct trace_event_call *call);extern int trace_event_get_offsets (struct trace_event_call *call); -#define is_signed_type (type) (((type)(-1)) < (type)1) -int ftrace_set_clr_event (struct trace_array *tr,char *buf,int set);int trace_set_clr_event (const char *system,const char *event,int set); int trace_array_set_clr_event(struct trace_array *tr, const char *system,
Yeah, this looks good. I'll take these as part of the hardening tree since it's touching overflow.h, unless I hear otherwise. :)
Signature · 1 line
Kees Cook
Kees Cook
AppliedQuoted 9 lines · Bart Van Assche
The changes in this patch series are as follows:
- Add a unit test for theis_signed_type()macro.
- Define theis_signed_type()macro once.Please consider these patches for the next merge window.
Thanks,
[...]
Applied to for-next/hardening, thanks!
[1/2] testing/selftests: Add tests for the is_signed_type() macro
https://git.kernel.org/kees/c/5e3ad11bfc5a [2/2] overflow, tracing: Define the is_signed_type() macro once https://git.kernel.org/kees/c/6bf7edc1e6f0
I tweaked the kunit test to have a matching filename to the other kunit tests (i.e. ending with _kunit.c instead of _test.c)
Signature · 1 line
Kees Cook