Issue 241284
Summary [clang][CodeGen] Constant for [[no_unique_address]] class is larger than the class (wrong code at -O0)
Labels clang
Assignees
Reporter jeffchen006
    Value-initialization of a class with `[[no_unique_address]]` members makes a constant of 72 bytes for a 64-byte object. Clang emits `memset(ptr, 0, 72)` on a 64-byte alloca, which overwrites the neighbour variable at -O0.

**Reproducer** (`repro.cpp`)
```cpp
// C c{} must zero exactly sizeof(C) == 64 bytes. Clang emits memset(&c, 0, 72),
// so at -O0 it also zeroes the local g next to c.  Correct output: 1
// (The two locals must be in this order; "return 0" keeps g next to c.)
extern "C" int printf(const char *, ...);
struct A { long long x; alignas(16) int y; };   // sizeof 32, data size 20
struct C { [[no_unique_address]] A a, b; };     // sizeof 64
int main() {
  long long g = 1;
  C c{};
  printf("%lld\n", g);
  return 0;
}
```

**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang++ -std=c++20 -O0 repro.cpp -o a0
$ ./a0
0
$ clang++ -std=c++20 -O3 repro.cpp -o a3
$ ./a3
1
$ clang++ -std=c++20 -O0 -S -emit-llvm repro.cpp -o - | grep -E 'alloca %struct.C|llvm.memset.p0.i64\(ptr'
  %3 = alloca %struct.C, align 16
 call void @llvm.memset.p0.i64(ptr align 16 %3, i8 0, i64 72, i1 false)
declare void @llvm.memset.p0.i64(ptr writeonly captures(none), i8, i64, i1 immarg) #1
```
Expected (as `-O3`): `1`. `-O0` prints `0`. The IR has a 64-byte `alloca %struct.C` and `memset(..., i64 72)`.

**Why the expected output is right**
Value-initialization of a class with `[[no_unique_address]]` members; reads of initialized objects only. UBSan and MSan at -O0 are clean.

**How the -O0/-O3 difference arises**
Here -O0 is the wrong one: the front-end memset writes 8 bytes past the object (ASan at -O0 reports stack-buffer-overflow inside that memset, the bug itself) and the neighbour variable sits there. At -O3 the layout differs and the overrun hits nothing that is read.

**Potential Root cause**
`clang/lib/CodeGen/CGExprConstant.cpp`, `ConstantAggregateBuilder::buildFrom`: `AlignedSize = Size.alignTo(Align)` and padding `DesiredSize - Size` use the tracked `Size` (52, the data end of a potentially-overlapping member), but the element as laid out ends at 56. The padding overshoots: the constant type is 72 bytes > `sizeof(C)` = 64. `CGDecl.cpp` `emitStoresForConstant` uses `getTypeAllocSize(constant type)` = 72 as the memset size. `condense` and `add()` have the same bookkeeping gap.

**Possible Proposed fix**
1. `buildFrom`: compute the real end of the laid-out elements and use it for `AlignedSize` and the padding; if it is past `DesiredSize`, use the packed path.
2. `condense`: after `replace(...)`, set `Size = std::max(Size, Offset + getSize(Replacement))`.
3. Array path in `buildFrom`: require `getSize(CommonType) == ElemSize`.
4. Guard in `emitStoresForConstant`: assert/clamp `ConstantSize <= sizeof(D.getType())`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to