* [PATCH 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper @ 2025-04-22 17:20 Gustavo A. R. Silva 2025-04-22 17:21 ` [PATCH 1/2][next] overflow: " Gustavo A. R. Silva 2025-04-22 17:22 ` [PATCH 2/2][next] kunit/overflow: Add tests for " Gustavo A. R. Silva 0 siblings, 2 replies; 6+ messages in thread From: Gustavo A. R. Silva @ 2025-04-22 17:20 UTC (permalink / raw) To: Kees Cook, Gustavo A. R. Silva; +Cc: linux-hardening, linux-kernel Add new STACK_FLEX_ARRAY_SIZE() helper to get the size of a flexible-array member defined using DEFINE_FLEX()/DEFINE_RAW_FLEX() at compile time. This is essentially the same as ARRAY_SIZE() but for on-stack flexible-array members. Gustavo A. R. Silva (2): overflow: Add STACK_FLEX_ARRAY_SIZE() helper kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper include/linux/overflow.h | 13 +++++++++++++ lib/tests/overflow_kunit.c | 3 +++ 2 files changed, 16 insertions(+) -- 2.43.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/2][next] overflow: Add STACK_FLEX_ARRAY_SIZE() helper 2025-04-22 17:20 [PATCH 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper Gustavo A. R. Silva @ 2025-04-22 17:21 ` Gustavo A. R. Silva 2025-04-22 20:39 ` Kees Cook 2025-04-22 17:22 ` [PATCH 2/2][next] kunit/overflow: Add tests for " Gustavo A. R. Silva 1 sibling, 1 reply; 6+ messages in thread From: Gustavo A. R. Silva @ 2025-04-22 17:21 UTC (permalink / raw) To: Kees Cook, Gustavo A. R. Silva; +Cc: linux-hardening, linux-kernel Add new STACK_FLEX_ARRAY_SIZE() helper to get the size of a flexible-array member defined using DEFINE_FLEX()/DEFINE_RAW_FLEX() at compile time. This is essentially the same as ARRAY_SIZE() but for on-stack flexible-array members. Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> --- include/linux/overflow.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 6ee67c20b575..5b2e0cc9aba2 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -420,6 +420,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) * flexible array member. * Use __struct_size(@name) to get compile-time size of it afterwards. * Use __member_size(@name->member) to get compile-time size of @name members. + * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member. */ #define DEFINE_RAW_FLEX(type, name, member, count) \ _DEFINE_FLEX(type, name, member, count, = {}) @@ -438,8 +439,20 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) * flexible array member. * Use __struct_size(@NAME) to get compile-time size of it afterwards. * Use __member_size(@NAME->member) to get compile-time size of @NAME members. + * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member. */ #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT) \ _DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, }) +/** + * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family. + * Returns the number of elements in @array. + * + * @name: Name for a variable defined in DEFINE_RAW_FLEX()/DEFINE_FLEX(). + * @array: Name of the array member. + */ +#define STACK_FLEX_ARRAY_SIZE(name, array) \ + (__member_size((name)->array) / sizeof(*(name)->array) + \ + __must_be_array((name)->array)) + #endif /* __LINUX_OVERFLOW_H */ -- 2.43.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2][next] overflow: Add STACK_FLEX_ARRAY_SIZE() helper 2025-04-22 17:21 ` [PATCH 1/2][next] overflow: " Gustavo A. R. Silva @ 2025-04-22 20:39 ` Kees Cook 0 siblings, 0 replies; 6+ messages in thread From: Kees Cook @ 2025-04-22 20:39 UTC (permalink / raw) To: Gustavo A. R. Silva; +Cc: linux-hardening, linux-kernel On Tue, Apr 22, 2025 at 11:21:36AM -0600, Gustavo A. R. Silva wrote: > Add new STACK_FLEX_ARRAY_SIZE() helper to get the size of a > flexible-array member defined using DEFINE_FLEX()/DEFINE_RAW_FLEX() > at compile time. > > This is essentially the same as ARRAY_SIZE() but for on-stack > flexible-array members. > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > --- > include/linux/overflow.h | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/include/linux/overflow.h b/include/linux/overflow.h > index 6ee67c20b575..5b2e0cc9aba2 100644 > --- a/include/linux/overflow.h > +++ b/include/linux/overflow.h > @@ -420,6 +420,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) > * flexible array member. > * Use __struct_size(@name) to get compile-time size of it afterwards. > * Use __member_size(@name->member) to get compile-time size of @name members. > + * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member. Instead of "size of" how about "number of elements in" > */ > #define DEFINE_RAW_FLEX(type, name, member, count) \ > _DEFINE_FLEX(type, name, member, count, = {}) > @@ -438,8 +439,20 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) > * flexible array member. > * Use __struct_size(@NAME) to get compile-time size of it afterwards. > * Use __member_size(@NAME->member) to get compile-time size of @NAME members. > + * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member. Same here. > */ > #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT) \ > _DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, }) > > +/** > + * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family. > + * Returns the number of elements in @array. Which matches these docs. > + * > + * @name: Name for a variable defined in DEFINE_RAW_FLEX()/DEFINE_FLEX(). > + * @array: Name of the array member. > + */ > +#define STACK_FLEX_ARRAY_SIZE(name, array) \ > + (__member_size((name)->array) / sizeof(*(name)->array) + \ > + __must_be_array((name)->array)) > + > #endif /* __LINUX_OVERFLOW_H */ > -- > 2.43.0 > -- Kees Cook ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/2][next] kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper 2025-04-22 17:20 [PATCH 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper Gustavo A. R. Silva 2025-04-22 17:21 ` [PATCH 1/2][next] overflow: " Gustavo A. R. Silva @ 2025-04-22 17:22 ` Gustavo A. R. Silva 2025-04-22 20:39 ` Kees Cook 1 sibling, 1 reply; 6+ messages in thread From: Gustavo A. R. Silva @ 2025-04-22 17:22 UTC (permalink / raw) To: Kees Cook, Gustavo A. R. Silva; +Cc: linux-hardening, linux-kernel Add a couple of tests for new STACK_FLEX_ARRAY_SIZE() helper. Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> --- lib/tests/overflow_kunit.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/tests/overflow_kunit.c b/lib/tests/overflow_kunit.c index 894691b4411a..3beb497a44be 100644 --- a/lib/tests/overflow_kunit.c +++ b/lib/tests/overflow_kunit.c @@ -1210,6 +1210,9 @@ static void DEFINE_FLEX_test(struct kunit *test) KUNIT_EXPECT_EQ(test, __struct_size(empty->array), 0); KUNIT_EXPECT_EQ(test, __member_size(empty->array), 0); + KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(two, array), 2); + KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(eight, array), 8); + /* If __counted_by is not being used, array size will have the on-stack size. */ if (!IS_ENABLED(CONFIG_CC_HAS_COUNTED_BY)) array_size_override = 2 * sizeof(s16); -- 2.43.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2][next] kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper 2025-04-22 17:22 ` [PATCH 2/2][next] kunit/overflow: Add tests for " Gustavo A. R. Silva @ 2025-04-22 20:39 ` Kees Cook 2025-04-22 20:48 ` Gustavo A. R. Silva 0 siblings, 1 reply; 6+ messages in thread From: Kees Cook @ 2025-04-22 20:39 UTC (permalink / raw) To: Gustavo A. R. Silva; +Cc: linux-hardening, linux-kernel On Tue, Apr 22, 2025 at 11:22:08AM -0600, Gustavo A. R. Silva wrote: > Add a couple of tests for new STACK_FLEX_ARRAY_SIZE() helper. > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > --- > lib/tests/overflow_kunit.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/lib/tests/overflow_kunit.c b/lib/tests/overflow_kunit.c > index 894691b4411a..3beb497a44be 100644 > --- a/lib/tests/overflow_kunit.c > +++ b/lib/tests/overflow_kunit.c > @@ -1210,6 +1210,9 @@ static void DEFINE_FLEX_test(struct kunit *test) > KUNIT_EXPECT_EQ(test, __struct_size(empty->array), 0); > KUNIT_EXPECT_EQ(test, __member_size(empty->array), 0); > > + KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(two, array), 2); > + KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(eight, array), 8); Nice! Can you add a 0 test for "empty" as well? > + > /* If __counted_by is not being used, array size will have the on-stack size. */ > if (!IS_ENABLED(CONFIG_CC_HAS_COUNTED_BY)) > array_size_override = 2 * sizeof(s16); > -- > 2.43.0 > -- Kees Cook ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2][next] kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper 2025-04-22 20:39 ` Kees Cook @ 2025-04-22 20:48 ` Gustavo A. R. Silva 0 siblings, 0 replies; 6+ messages in thread From: Gustavo A. R. Silva @ 2025-04-22 20:48 UTC (permalink / raw) To: Kees Cook, Gustavo A. R. Silva; +Cc: linux-hardening, linux-kernel On 22/04/25 14:39, Kees Cook wrote: > On Tue, Apr 22, 2025 at 11:22:08AM -0600, Gustavo A. R. Silva wrote: >> Add a couple of tests for new STACK_FLEX_ARRAY_SIZE() helper. >> >> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> >> --- >> lib/tests/overflow_kunit.c | 3 +++ >> 1 file changed, 3 insertions(+) >> >> diff --git a/lib/tests/overflow_kunit.c b/lib/tests/overflow_kunit.c >> index 894691b4411a..3beb497a44be 100644 >> --- a/lib/tests/overflow_kunit.c >> +++ b/lib/tests/overflow_kunit.c >> @@ -1210,6 +1210,9 @@ static void DEFINE_FLEX_test(struct kunit *test) >> KUNIT_EXPECT_EQ(test, __struct_size(empty->array), 0); >> KUNIT_EXPECT_EQ(test, __member_size(empty->array), 0); >> >> + KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(two, array), 2); >> + KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(eight, array), 8); > > Nice! Can you add a 0 test for "empty" as well? Sure thing. I had actually added it, but for some reason I removed at the last minute. -Gustavo ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-04-22 20:48 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-04-22 17:20 [PATCH 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper Gustavo A. R. Silva 2025-04-22 17:21 ` [PATCH 1/2][next] overflow: " Gustavo A. R. Silva 2025-04-22 20:39 ` Kees Cook 2025-04-22 17:22 ` [PATCH 2/2][next] kunit/overflow: Add tests for " Gustavo A. R. Silva 2025-04-22 20:39 ` Kees Cook 2025-04-22 20:48 ` Gustavo A. R. Silva
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.