[clang] [Clang][CodeGen] Emit load of GEP after EmitMemberExpr (PR #110487)
Bill Wendling via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 1 17:17:39 PDT 2024
bwendling wrote:
> > I reverted my last commit. This leaves the original patch, which seems to work. @efriedma-quic, would you be okay with this patch while I work to improve the code in follow-up?
>
> The original (and current) patch in this PR still introduces a regression. So it should not be merged in my opinion.
>
> Look at the following C file (`test.c`):
>
> ```c
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> struct variable {
> int a;
> int b;
> int length;
> short array[] __attribute__((counted_by(length)));
> };
>
> struct bucket {
> int a;
> struct variable growable;
> };
>
> int main(int argc, char *argv[])
> {
> struct bucket *p;
>
> p = malloc(sizeof(*p) + sizeof(*p->growable.array) * 32);
> p->growable.length = 32;
>
> printf("%zu\n", __builtin_dynamic_object_size(p->growable.array, 1));
>
> return 0;
> }
> ```
>
> Compiling this file with clang 19.1.0 and running it:
>
> ```shell
> $ clang --version
> clang version 19.1.0 (Fedora 19.1.0-1.fc41)
> Target: x86_64-redhat-linux-gnu
> Thread model: posix
> InstalledDir: /usr/bin
> Configuration file: /etc/clang/x86_64-redhat-linux-gnu-clang.cfg
>
> $ clang test.c
>
> $ ./a.out
> 64
> ```
>
> Correctly prints `64`.
>
> But compiling and running it with the original (and current) patch in this PR:
>
> ```shell
> $ clang --version
> clang version 20.0.0git (git at github.com:llvm/llvm-project.git 2de76472e0d1417b64da5aa2c1138eb365685d3a)
> Target: x86_64-unknown-linux-gnu
> Thread model: posix
> InstalledDir: /home/jan/llvm-project/build2/bin
>
> $ clang test.c
>
> $ ./a.out
> Segmentation fault (core dumped)
> ```
>
> Will result in a binary that crashes with a segmentation fault.
>
> My PR #110497 adds a test case for this scenario and does not have the same issue: https://github.com/Cydox/llvm-project/blob/15b69329a97706ada7d5e8cbeb76508aa55b418f/clang/test/CodeGen/attr-counted-by-pr110385.c#L61
This is what I get:
```
kiff ~/llvm/llvm-project (builtin_get_counted_by) $ cat > /tmp/y.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct variable {
int a;
int b;
int length;
short array[] __attribute__((counted_by(length)));
};
struct bucket {
int a;
struct variable growable;
};
int main(int argc, char *argv[])
{
struct bucket *p;
p = malloc(sizeof(*p) + sizeof(*p->growable.array) * 32);
p->growable.length = 32;
printf("%zu\n", __builtin_dynamic_object_size(p->growable.array, 1));
return 0;
}
kiff ~/llvm/llvm-project (builtin_get_counted_by) $ ./build.opt/bin/clang -O2 /tmp/y.c && ./a.out
64
```
https://github.com/llvm/llvm-project/pull/110487
More information about the cfe-commits
mailing list