Skip to content

Commit 150a04d

Browse files
bwendlingkees
authored andcommitted
compiler_types.h: Attributes: Add __counted_by_ptr macro
Introduce __counted_by_ptr(), which works like __counted_by(), but for pointer struct members. struct foo { int a, b, c; char *buffer __counted_by_ptr(bytes); short nr_bars; struct bar *bars __counted_by_ptr(nr_bars); size_t bytes; }; Because "counted_by" can only be applied to pointer members in very recent compiler versions, its application ends up needing to be distinct from flexibe array "counted_by" annotations, hence a separate macro. This is a reworking of Kees' previous patch [1]. Link: https://lore.kernel.org/all/20251020220118.1226740-1-kees@kernel.org/ [1] Co-developed-by: Kees Cook <kees@kernel.org> Signed-off-by: Bill Wendling <morbo@google.com> Link: https://patch.msgid.link/20260116005838.2419118-1-morbo@google.com Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 9f54ab8 commit 150a04d

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,12 @@ KBUILD_CFLAGS += $(CC_AUTO_VAR_INIT_ZERO_ENABLER)
952952
endif
953953
endif
954954

955+
ifdef CONFIG_CC_IS_CLANG
956+
ifdef CONFIG_CC_HAS_COUNTED_BY_PTR
957+
KBUILD_CFLAGS += -fexperimental-late-parse-attributes
958+
endif
959+
endif
960+
955961
# Explicitly clear padding bits during variable initialization
956962
KBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
957963

include/linux/compiler_types.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ struct ftrace_likely_data {
369369
* Optional: only supported since clang >= 18
370370
*
371371
* gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
372-
* clang: https://github.com/llvm/llvm-project/pull/76348
372+
* clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
373373
*
374374
* __bdos on clang < 19.1.2 can erroneously return 0:
375375
* https://github.com/llvm/llvm-project/pull/110497
@@ -383,6 +383,22 @@ struct ftrace_likely_data {
383383
# define __counted_by(member)
384384
#endif
385385

386+
/*
387+
* Runtime track number of objects pointed to by a pointer member for use by
388+
* CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS.
389+
*
390+
* Optional: only supported since gcc >= 16
391+
* Optional: only supported since clang >= 22
392+
*
393+
* gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html
394+
* clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
395+
*/
396+
#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR
397+
#define __counted_by_ptr(member) __attribute__((__counted_by__(member)))
398+
#else
399+
#define __counted_by_ptr(member)
400+
#endif
401+
386402
/*
387403
* Optional: only supported since gcc >= 15
388404
* Optional: not supported by Clang

include/uapi/linux/stddef.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
#define __counted_by_be(m)
7373
#endif
7474

75+
#ifndef __counted_by_ptr
76+
#define __counted_by_ptr(m)
77+
#endif
78+
7579
#ifdef __KERNEL__
7680
#define __kernel_nonstring __nonstring
7781
#else

init/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ config CC_HAS_COUNTED_BY
143143
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
144144
default y if CC_IS_GCC && GCC_VERSION >= 150100
145145

146+
config CC_HAS_COUNTED_BY_PTR
147+
bool
148+
# supported since clang 22
149+
default y if CC_IS_CLANG && CLANG_VERSION >= 220000
150+
# supported since gcc 16.0.0
151+
default y if CC_IS_GCC && GCC_VERSION >= 160000
152+
146153
config CC_HAS_MULTIDIMENSIONAL_NONSTRING
147154
def_bool $(success,echo 'char tag[][4] __attribute__((__nonstring__)) = { };' | $(CC) $(CLANG_FLAGS) -x c - -c -o /dev/null -Werror)
148155

0 commit comments

Comments
 (0)