[clang] [Clang] Fix 'counted_by' for nested struct pointers (PR #110497)
Jan Hendrik Farr via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 4 14:32:37 PDT 2024
Cydox wrote:
> > Yeah so the problem is if you do `__builtin_dynamic_object_size(v, 0)`
> > In that case it's a `DeclRefExpr`, a pointer, and an `LValue`.
>
> Can you give a more complete example? I just tried the following, and I see an lvaluetorvalue cast.
>
> ```
> int f(const void *p) { return __builtin_dynamic_object_size(p, 0); }
> ```
```C
// test2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct variable {
int a;
int b;
int length;
short array[] __attribute__((counted_by(length)));
};
int main(int argc, char *argv[])
{
struct variable *v;
v = malloc(sizeof(struct variable) + sizeof(short) * 32);
v->length = 32;
printf("%zu\n", __builtin_dynamic_object_size(v, 0));
return 0;
}
```
I added this `StructBase->dump`:
```
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 4aca60685f37..7a06819f1a67 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1162,6 +1162,8 @@ llvm::Value *CodeGenFunction::EmitLoadOfCountedByField(
if (!StructBase || StructBase->HasSideEffects(getContext()))
return nullptr;
+ StructBase->dump();
+
llvm::Value *Res = nullptr;
if (StructBase->getType()->isPointerType()) {
LValueBaseInfo BaseInfo;
```
```
$ clang test2.c
DeclRefExpr 0x34b302c8 'struct variable *' lvalue Var 0x34b2fce8 'v' 'struct variable *'
$ ./a.out
76
```
In my testing I also added a print statement that printed a dump when both `StructBase->getType()->isPointerType()` and `StructBase->isLValue()` were true and there were a bunch of hits when compiling the kernel.
https://github.com/llvm/llvm-project/pull/110497
More information about the cfe-commits
mailing list