Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

bwendling
Copy link
Collaborator

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]

bwendling added 13 commits April 8, 2025 12:38
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]>
Copy link

github-actions bot commented Apr 24, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@bwendling bwendling requested a review from devincoughlin April 25, 2025 19:42
@kees
Copy link
Contributor

kees commented Apr 30, 2025

This is looking good, but needs to support void * struct members (they are treated as byte-sized for pointer arithmetic and memcpy). GCC has added support for this. Otherwise, yes, this is building for Linux with some (non-void *) annotations added and is operating correctly.

@bwendling
Copy link
Collaborator Author

Friendly ping. :-)

@AaronBallman AaronBallman requested a review from rjmccall May 1, 2025 13:59
Copy link
Collaborator

@AaronBallman AaronBallman left a 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.

@bwendling
Copy link
Collaborator Author

Friendly ping x 2 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants