enhancement: add __builtin_set_counted_by(P->FAM, COUNT) or equivalent
With the wonderful addition of the 'counted_by' attribute and its wide roll-out within the Linux kernel, we have found a use case that would be very nice to have for object allocators: being able to set the counted_by counter variable without knowing its name.
For example, given:
struct foo {
...
int counter;
...
struct bar array[] __attribute__((counted_by(counter)));
} *p;
one thought was to have __builtin_set_counted_by(P->FAM, COUNT), which would have the behavior of:
if has_counted_by_attribute(P->FAM):
P->COUNT_MEMBER = COUNT
else
do nothing
The existing Linux object allocators are roughly:
#define alloc(P, FAM, COUNT) ({ \
size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \
kmalloc(__size, GFP); \
})
Right now, any addition of a counted_by annotation must also include an open-coded assignment of the counter variable after the allocation:
p = alloc(p, array, how_many);
p->counter = how_many;
Instead, the central allocator could be updated to:
#define alloc(P, FAM, COUNT) ({ \
typeof(P) __p; \
size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \
__p = kmalloc(__size, GFP); \
__builtin_set_counted_by(__p->FAM, COUNT); \
__p; \
})
And now structs can gain the counted_by attribute without needing additional open-coded counter assignments for each struct, and unannotated structs could still use the same allocator. (i.e. we are able to more cleanly continue to migrate FAM structs.)
GCC feature request exists as well: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016
@bwendling
The feature has changed a bit. Here's the current doc from Qing:
Builtin-in Function: type __builtin_get_counted_by (ptr)
The argument PTR to the built-in function __builtin_get_counted_by is a pointer to an array object, which should have a counted_by attribute associated with it. This function returns a pointer to the corresponding object that holds the number of elements in the array object through counted_by attribute.
The TYPE of the returned value should be a pointer type pointing to an integral type.
It's the user's responsibility to make sure that the PTR has a counted-by attribute associated with it. (GCC provides another built-in __builtin_has_attribute() to make sure this)
For example:
for the following:
struct foo {
int counter;
struct bar1 array[] __attribute__((counted_by(counter)));
} *p;
__builtin_get_counted_by (p->array)
returns:
&p->counter
This is fine, however we need to add __builtin_has_attribute first.
On second thought, the __builtin_has_attribute may not be necessary for this feature.
On second thought, the
__builtin_has_attributemay not be necessary for this feature.
Right, it'd be nice to have once the nonstring attribute exists, but just checking for a NULL return should be sufficient for detecting the presence of counted_by on the FAM.
Despite what I wrote in the GCC bug, I'm going to go ahead with this feature so that we don't fall behind, but I do have some misgivings. I'm not entirely against this feature, but ... well my thoughts are in that bug report:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016
See https://github.com/bwendling/llvm-project/tree/builtin_get_counted_by for a WIP patch.
Here's my RFC for this feature: https://discourse.llvm.org/t/rfc-introducing-new-clang-builtin-builtin-get-counted-by/80836?u=gwelymernans
CC @patrykstefanski @hnrklssn @delcypher @apple-fcloutier @devincoughlin
Added 7475156d49406785a974b1205d11fe3de9c1553e
@llvm/issue-subscribers-clang-codegen
Author: Kees Cook (kees)
For example, given:
struct foo {
...
int counter;
...
struct bar array[] __attribute__((counted_by(counter)));
} *p;
one thought was to have __builtin_set_counted_by(P->FAM, COUNT), which would have the behavior of:
if has_counted_by_attribute(P->FAM):
P->COUNT_MEMBER = COUNT
else
do nothing
The existing Linux object allocators are roughly:
#define alloc(P, FAM, COUNT) ({ \
size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \
kmalloc(__size, GFP); \
})
Right now, any addition of a counted_by annotation must also include an open-coded assignment of the counter variable after the allocation:
p = alloc(p, array, how_many);
p->counter = how_many;
Instead, the central allocator could be updated to:
#define alloc(P, FAM, COUNT) ({ \
typeof(P) __p; \
size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \
__p = kmalloc(__size, GFP); \
__builtin_set_counted_by(__p->FAM, COUNT); \
__p; \
})
And now structs can gain the counted_by attribute without needing additional open-coded counter assignments for each struct, and unannotated structs could still use the same allocator. (i.e. we are able to more cleanly continue to migrate FAM structs.)
GCC feature request exists as well: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016
@llvm/issue-subscribers-clang-frontend
Author: Kees Cook (kees)
For example, given:
struct foo {
...
int counter;
...
struct bar array[] __attribute__((counted_by(counter)));
} *p;
one thought was to have __builtin_set_counted_by(P->FAM, COUNT), which would have the behavior of:
if has_counted_by_attribute(P->FAM):
P->COUNT_MEMBER = COUNT
else
do nothing
The existing Linux object allocators are roughly:
#define alloc(P, FAM, COUNT) ({ \
size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \
kmalloc(__size, GFP); \
})
Right now, any addition of a counted_by annotation must also include an open-coded assignment of the counter variable after the allocation:
p = alloc(p, array, how_many);
p->counter = how_many;
Instead, the central allocator could be updated to:
#define alloc(P, FAM, COUNT) ({ \
typeof(P) __p; \
size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \
__p = kmalloc(__size, GFP); \
__builtin_set_counted_by(__p->FAM, COUNT); \
__p; \
})
And now structs can gain the counted_by attribute without needing additional open-coded counter assignments for each struct, and unannotated structs could still use the same allocator. (i.e. we are able to more cleanly continue to migrate FAM structs.)
GCC feature request exists as well: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016