Skip to content

Commit 81cee91

Browse files
committed
compiler_types: Introduce __flex_counter() and family
Introduce __flex_counter() which wraps __builtin_counted_by_ref(), as newly introduced by GCC[1] and Clang[2]. Use of __flex_counter() allows access to the counter member of a struct's flexible array member when it has been annotated with __counted_by(). Introduce typeof_flex_counter(), overflows_flex_counter_type(), and __set_flex_counter() to provide the needed _Generic() wrappers to get sane results out of __flex_counter(). For example, with: struct foo { int counter; short array[] __counted_by(counter); } *p; __flex_counter(p->array) will resolve to: &p->counter typeof_flex_counter(p->array) will resolve to "int". (If p->array was not annotated, it would resolve to "size_t".) overflows_flex_counter_type(typeof(*p), array, COUNT) is the same as: COUNT <= type_max(p->counter) && COUNT >= type_min(p->counter) (If p->array was not annotated it would return true since everything fits in size_t.) __set_flex_counter(p->array, COUNT) is the same as: p->counter = COUNT; (It is a no-op if p->array is not annotated with __counted_by().) Link: https://patch.msgid.link/20251203233036.3212363-3-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 070580b commit 81cee91

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

include/linux/compiler_types.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,37 @@ struct ftrace_likely_data {
535535
#define __annotated(var, attr) __builtin_has_attribute(var, attr)
536536
#endif
537537

538+
/*
539+
* Optional: only supported since gcc >= 15, clang >= 19
540+
*
541+
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fcounted_005fby_005fref
542+
* clang: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-counted-by-ref
543+
*/
544+
#if __has_builtin(__builtin_counted_by_ref)
545+
/**
546+
* __flex_counter() - Get pointer to counter member for the given
547+
* flexible array, if it was annotated with __counted_by()
548+
* @FAM: Pointer to flexible array member of an addressable struct instance
549+
*
550+
* For example, with:
551+
*
552+
* struct foo {
553+
* int counter;
554+
* short array[] __counted_by(counter);
555+
* } *p;
556+
*
557+
* __flex_counter(p->array) will resolve to &p->counter.
558+
*
559+
* Note that Clang may not allow this to be assigned to a separate
560+
* variable; it must be used directly.
561+
*
562+
* If p->array is unannotated, this returns (void *)NULL.
563+
*/
564+
#define __flex_counter(FAM) __builtin_counted_by_ref(FAM)
565+
#else
566+
#define __flex_counter(FAM) ((void *)NULL)
567+
#endif
568+
538569
/*
539570
* Some versions of gcc do not mark 'asm goto' volatile:
540571
*

include/linux/overflow.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,46 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
552552
(__member_size((name)->array) / sizeof(*(name)->array) + \
553553
__must_be_array((name)->array))
554554

555+
/**
556+
* typeof_flex_counter() - Return the type of the counter variable of a given
557+
* flexible array member annotated by __counted_by().
558+
* @FAM: Instance of flexible array member within a given struct.
559+
*
560+
* Returns: "size_t" if no annotation exists.
561+
*/
562+
#define typeof_flex_counter(FAM) \
563+
typeof(_Generic(__flex_counter(FAM), \
564+
void *: (size_t)0, \
565+
default: *__flex_counter(FAM)))
566+
567+
/**
568+
* overflows_flex_counter_type() - Check if the counter associated with the
569+
* given flexible array member can represent
570+
* a value.
571+
* @TYPE: Type of the struct that contains the @FAM.
572+
* @FAM: Member name of the FAM within @TYPE.
573+
* @COUNT: Value to check against the __counted_by annotated @FAM's counter.
574+
*
575+
* Returns: true if @COUNT can be represented in the @FAM's counter. When
576+
* @FAM is not annotated with __counted_by(), always returns true.
577+
*/
578+
#define overflows_flex_counter_type(TYPE, FAM, COUNT) \
579+
(!overflows_type(COUNT, typeof_flex_counter(((TYPE *)NULL)->FAM)))
580+
581+
/**
582+
* __set_flex_counter() - Set the counter associated with the given flexible
583+
* array member that has been annoated by __counted_by().
584+
* @FAM: Instance of flexible array member within a given struct.
585+
* @COUNT: Value to store to the __counted_by annotated @FAM_PTR's counter.
586+
*
587+
* This is a no-op if no annotation exists. Count needs to be checked with
588+
* overflows_flex_counter_type() before using this function.
589+
*/
590+
#define __set_flex_counter(FAM, COUNT) \
591+
({ \
592+
*_Generic(__flex_counter(FAM), \
593+
void *: &(size_t){ 0 }, \
594+
default: __flex_counter(FAM)) = (COUNT); \
595+
})
596+
555597
#endif /* __LINUX_OVERFLOW_H */

0 commit comments

Comments
 (0)