-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[Clang][counted_by] Add support for 'counted_by' on struct pointers #127116
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
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; char *buf __counted_by(count); }; 'counted_by' cannot be applied to a pointer to an incomplete type. So this is invalid: struct foo; struct annotated_ptr { int count; struct foo *buf __counted_by(count); /* invalid */ }; 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). Signed-off-by: Bill Wendling <morbo@google.com>
✅ With the latest revision this PR passed the C/C++ code formatter. |
We automatically add the appropriate argument to the function call. The function must be marked either 'pure' or 'const'. There's a small issue still if the function is marked as 'static'. If it is, add the 'used' attribute to it. An example of use: const unsigned long size = 42; static const unsigned long inline __attribute__((__pure__, __used__)) buf_size(void *ptr); struct X { unsigned int flags; int *buf __attribute__((counted_by(buf_size))); int count; /* int *buf2 __attribute__((counted_by(buf_size((void *)0)))); */ } *ptr; static const unsigned long inline __attribute__((__pure__, __used__)) buf_size(void *ptr) { struct X *p = (struct X *)ptr; return p->count + size; } int __attribute__((__noinline__)) foo(struct X *ptr, int index) { return ptr->buf[index]; } int main(void) { struct X p; p.buf = __builtin_malloc(sizeof(int) * size + 37); p.count = 37; p.buf[3] = 927; __builtin_printf("output = %d\n", foo(&p, 3)); return 0; }
…ction definition.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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;
char *buf __counted_by(count);
};
'counted_by' cannot be applied to a pointer to an incomplete type. So this is invalid:
struct foo;
struct annotated_ptr {
int count;
struct foo buf __counted_by(count); / invalid */
};
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).