Skip to content

[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
wants to merge 15 commits into from

Conversation

bwendling
Copy link
Collaborator

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).

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>
Copy link

github-actions bot commented Feb 13, 2025

✅ 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;
  }
@bwendling bwendling closed this Apr 8, 2025
@bwendling bwendling deleted the counted-by-on-struct-pointers branch April 8, 2025 19:30
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.

1 participant