diff options
author | Bill Wendling <morbo@google.com> | 2024-11-07 22:03:55 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-07 22:03:55 +0000 |
commit | 7475156d49406785a974b1205d11fe3de9c1553e (patch) | |
tree | cc50e8cd3f3d2c9f82e5d52c13f1eb976db6a194 /llvm/lib/CodeGen/MachineFunction.cpp | |
parent | ae9d0623ad65d84022bb4ed8446b6491451ae575 (diff) | |
download | llvm-7475156d49406785a974b1205d11fe3de9c1553e.zip llvm-7475156d49406785a974b1205d11fe3de9c1553e.tar.gz llvm-7475156d49406785a974b1205d11fe3de9c1553e.tar.bz2 |
[Clang] Add __builtin_counted_by_ref builtin (#114495)
The __builtin_counted_by_ref builtin is used on a flexible array
pointer and returns a pointer to the "counted_by" attribute's COUNT
argument, which is a field in the same non-anonymous struct as the
flexible array member. This is useful for automatically setting the
count field without needing the programmer's intervention. Otherwise
it's possible to get this anti-pattern:
ptr = alloc(<ty>, ..., COUNT);
ptr->FAM[9] = 42; /* <<< Sanitizer will complain */
ptr->count = COUNT;
To prevent this anti-pattern, the user can create an allocator that
automatically performs the assignment:
#define alloc(TY, FAM, COUNT) ({ \
TY __p = alloc(get_size(TY, COUNT)); \
if (__builtin_counted_by_ref(__p->FAM)) \
*__builtin_counted_by_ref(__p->FAM) = COUNT; \
__p; \
})
The builtin's behavior is heavily dependent upon the "counted_by"
attribute existing. It's main utility is during allocation to avoid
the above anti-pattern. If the flexible array member doesn't have that
attribute, the builtin becomes a no-op. Therefore, if the flexible
array member has a "count" field not referenced by "counted_by", it
must be set explicitly after the allocation as this builtin will
return a "nullptr" and the assignment will most likely be elided.
---------
Co-authored-by: Bill Wendling <isanbard@gmail.com>
Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
Diffstat (limited to 'llvm/lib/CodeGen/MachineFunction.cpp')
0 files changed, 0 insertions, 0 deletions