Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

Kees Cook 81cee916 070580b0

+73
+31
include/linux/compiler_types.h
··· 536 536 #endif 537 537 538 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 + 569 + /* 539 570 * Some versions of gcc do not mark 'asm goto' volatile: 540 571 * 541 572 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103979
+42
include/linux/overflow.h
··· 552 552 (__member_size((name)->array) / sizeof(*(name)->array) + \ 553 553 __must_be_array((name)->array)) 554 554 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 + 555 597 #endif /* __LINUX_OVERFLOW_H */