[clang] [llvm] [Clang] Correct __builtin_dynamic_object_size for subobject types (PR #78526)

Bill Wendling via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 25 12:56:03 PST 2024


bwendling wrote:

> > ```c
> > struct x {
> >     int a;
> >     char foo[2][40];
> >     int b;
> >     int c;
> > };
> > 
> > size_t f(struct x *p, int idx) {
> >     return __builtin_dynamic_object_size(&p->foo[idx], 1);
> > }
> > ```
> 
> If I'm following correctly, the return here is 0, 40, or 80, depending on the value of idx? That's not a constant, but the computation is entirely syntactic; it doesn't matter what "p" actually points to. So clang can lower the builtin itself. Currently it doesn't, I think, because all the relevant code is in ExprConstant, but the code could be adapted.

Right. That's what I want to add to the front-end.

> The problem, really, is that we can't easily extend that approach to stuff like the following:
> 
> ```c
> size_t f(struct x *p, int idx) {
>     char *c = &p->foo[idx];
>     return __builtin_dynamic_object_size(c, 1);
> }
> ```

Yup! I've been forbidden from doing this in the back-end, so I have to jump through hoops now and do partial solutions and hope that it works for most people and that when we get it wrong it doesn't hurt security (spoilers: it will).

https://github.com/llvm/llvm-project/pull/78526


More information about the cfe-commits mailing list