Addition of function parameter attribute to help __builtin_object_size

Hello friends,

I plan to add a function parameter attribute called carry_size (name suggestions welcome). The goal of this attribute is to help Clang/LLVM answer __builtin_object_size(Ptr, Type) calls when the object pointed to by Ptr isn’t visible in the caller of __builtin_object_size.

An example of its use:
#define CS(N) attribute((carry_size(N)))
void attribute((noinline)) Func(void *const p CS(0)) {
printf(“%d\n”, __builtin_object_size(p, 0));
}

int main() {
char c[10];
Func(c); // prints “10\n”
}

For more info, please see the doc here: https://docs.google.com/document/d/14TwNLxFL_jhiYn7_kppNUDiaTopRAIY5bc-B8NcnKMM/edit?usp=sharing

Thanks!
George

i must be missing something here but why do you need a manually applied
attr at all when the compiler can just figure it out itself whenever it
sees a call to __builtin_object_size?

It’s more or less meant to be implemented by passing extra arguments, which changes how the function is called. This is also meant to work across TUs. e.g. in

// foo.h
int doIt(void *const p attribute((carry_size(0))));

// foo.c
int doIt(void *const p attribute((carry_size(0)))) { return __builtin_object_size(p, 0); }

// main.c
#include “foo.h”
int main() {
char c[10];
return doIt(c);
}

Also, the common case for how this is foreseen to be used is “call function A with the object size if __builtin_object_size succeeds, and function B if __builtin_object_size fails,” so being able to overload on the attribute may be mildly convenient. :slight_smile: