Skip to content

Commit dd06e72

Browse files
committed
Compiler Attributes: Add __counted_by macro
In an effort to annotate all flexible array members with their run-time size information, the "element_count" attribute is being introduced by Clang[1] and GCC[2] in future releases. This annotation will provide the CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE features the ability to perform run-time bounds checking on otherwise unknown-size flexible arrays. Even though the attribute is under development, we can start the annotation process in the kernel. This requires defining a macro for it, even if we have to change the name of the actual attribute later. Since it is likely that this attribute may change its name to "counted_by" in the future (to better align with a future total bytes "sized_by" attribute), name the wrapper macro "__counted_by", which also reads more clearly (and concisely) in structure definitions. [1] https://reviews.llvm.org/D148381 [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896 Cc: Bill Wendling <[email protected]> Cc: Qing Zhao <[email protected]> Cc: Gustavo A. R. Silva <[email protected]> Cc: Nick Desaulniers <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Tom Rix <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Gustavo A. R. Silva <[email protected]> Reviewed-by: Nathan Chancellor <[email protected]> Acked-by: Miguel Ojeda <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent e910c8e commit dd06e72

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

include/linux/compiler_attributes.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@
123123
# define __designated_init
124124
#endif
125125

126+
/*
127+
* Optional: only supported since gcc >= 14
128+
* Optional: only supported since clang >= 17
129+
*
130+
* gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
131+
* clang: https://reviews.llvm.org/D148381
132+
*/
133+
#if __has_attribute(__element_count__)
134+
# define __counted_by(member) __attribute__((__element_count__(#member)))
135+
#else
136+
# define __counted_by(member)
137+
#endif
138+
126139
/*
127140
* Optional: only supported since clang >= 14.0
128141
*

0 commit comments

Comments
 (0)