-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[Clang][counted_by] Add support for 'counted_by' on struct pointers #137250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The 'counted_by' attribute is now available for structs. It generates code for sanity checks as well as __builtin_dynamic_object_size() calculations. For example: struct annotated_ptr { int count; void *buf __attribute__((counted_by(count))); }; The attribute can be applied to a 'void *'. In that case, use the 'sized_by' attribute instead: struct annotated_ptr { void *buf __attribute__((sized_by(count))); size_t count; }; The 'count' field member may occur after the pointer. If it does, use the '-fexperimental-late-parse-attributes' flag (which will hopefully be made the default in future Clang versions). Note that 'counted_by' cannot be applied to a pointer to an incomplete type, because the size isn't known. struct foo; struct annotated_ptr { int count; struct foo *buf __attribute__((counted_by(count))); /* invalid */ }; Signed-off-by: Bill Wendling <[email protected]>
✅ With the latest revision this PR passed the C/C++ code formatter. |
… redundant 'isCounteAttributedType'.
This is looking good, but needs to support |
Friendly ping. :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the changes look reasonable but I'd appreciate final sign-off from @efriedma-quic or @rjmccall given this is all codegen work.
Friendly ping x 2 :) |
The 'counted_by' attribute is now available for pointers in structs.
It generates code for sanity checks as well as __builtin_dynamic_object_size()
calculations. For example:
struct annotated_ptr {
int count;
char *buf attribute((counted_by(count)));
};
If the pointer's type is 'void *', use the 'sized_by' attribute, which
works similarly to 'counted_by', but can handle the 'void' base type:
struct annotated_ptr {
int count;
void *buf attribute((sized_by(count)));
};
If the 'count' field member occurs after the pointer, use the
'-fexperimental-late-parse-attributes' flag during compilation.
Note that 'counted_by' cannot be applied to a pointer to an incomplete
type, because the size isn't known.
struct foo;
struct annotated_ptr {
int count;
struct foo buf attribute((counted_by(count))); / invalid */
};
Signed-off-by: Bill Wendling [email protected]