* [PATCH v2 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper
@ 2025-04-22 21:04 Gustavo A. R. Silva
2025-04-22 21:05 ` [PATCH v2 1/2][next] overflow: " Gustavo A. R. Silva
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2025-04-22 21:04 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 | 15 +++++++++++++++
lib/tests/overflow_kunit.c | 4 ++++
2 files changed, 19 insertions(+)
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/2][next] overflow: Add STACK_FLEX_ARRAY_SIZE() helper
2025-04-22 21:04 [PATCH v2 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper Gustavo A. R. Silva
@ 2025-04-22 21:05 ` Gustavo A. R. Silva
2025-04-22 21:07 ` [PATCH v2 2/2][next] kunit/overflow: Add tests for " Gustavo A. R. Silva
2025-04-30 21:29 ` [PATCH v2 0/2][next] Add " Kees Cook
2 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2025-04-22 21:05 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>
---
Changes in v2:
- Use "number of elements" instead of "size of" in kernel-doc blocks.
v1:
- Link: https://lore.kernel.org/linux-hardening/8f9ab8fcd26ce59c0e0e25e095b446a77849c08e.1745342381.git.gustavoars@kernel.org/
include/linux/overflow.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 6ee67c20b575..f33d74dac06f 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -420,6 +420,8 @@ 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 number of
+ * elements in array @member.
*/
#define DEFINE_RAW_FLEX(type, name, member, count) \
_DEFINE_FLEX(type, name, member, count, = {})
@@ -438,8 +440,21 @@ 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 number of
+ * elements in 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] 4+ messages in thread
* [PATCH v2 2/2][next] kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper
2025-04-22 21:04 [PATCH v2 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper Gustavo A. R. Silva
2025-04-22 21:05 ` [PATCH v2 1/2][next] overflow: " Gustavo A. R. Silva
@ 2025-04-22 21:07 ` Gustavo A. R. Silva
2025-04-30 21:29 ` [PATCH v2 0/2][next] Add " Kees Cook
2 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2025-04-22 21:07 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>
---
Changes v2:
- Add 0 test for `empty` instance.
v1:
- Link: https://lore.kernel.org/linux-hardening/8cf48c3f9d8ef9b999d87cc0a822ffe539bf7a64.1745342381.git.gustavoars@kernel.org/
lib/tests/overflow_kunit.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/lib/tests/overflow_kunit.c b/lib/tests/overflow_kunit.c
index 894691b4411a..19cb03b25dc5 100644
--- a/lib/tests/overflow_kunit.c
+++ b/lib/tests/overflow_kunit.c
@@ -1210,6 +1210,10 @@ 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);
+ KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(empty, array), 0);
+
/* 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] 4+ messages in thread
* Re: [PATCH v2 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper
2025-04-22 21:04 [PATCH v2 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper Gustavo A. R. Silva
2025-04-22 21:05 ` [PATCH v2 1/2][next] overflow: " Gustavo A. R. Silva
2025-04-22 21:07 ` [PATCH v2 2/2][next] kunit/overflow: Add tests for " Gustavo A. R. Silva
@ 2025-04-30 21:29 ` Kees Cook
2 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2025-04-30 21:29 UTC (permalink / raw)
To: Gustavo A. R. Silva; +Cc: Kees Cook, linux-hardening, linux-kernel
On Tue, 22 Apr 2025 15:04:48 -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.
>
> [...]
Applied to for-next/hardening, thanks!
[1/2] overflow: Add STACK_FLEX_ARRAY_SIZE() helper
https://git.kernel.org/kees/c/9cc4498acf3e
[2/2] kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper
https://git.kernel.org/kees/c/90961958f48f
Take care,
--
Kees Cook
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-04-30 21:29 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-22 21:04 [PATCH v2 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper Gustavo A. R. Silva
2025-04-22 21:05 ` [PATCH v2 1/2][next] overflow: " Gustavo A. R. Silva
2025-04-22 21:07 ` [PATCH v2 2/2][next] kunit/overflow: Add tests for " Gustavo A. R. Silva
2025-04-30 21:29 ` [PATCH v2 0/2][next] Add " Kees Cook
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.