Issue 241293
Summary [clang][CodeGen] Value-init of [[no_unique_address]] member zeroes sizeof, not data size (wrong code at all -O levels)
Labels clang
Assignees
Reporter jeffchen006
    `D() : b()` value-initializes a `[[no_unique_address]]` member `b` (data size 5, sizeof 8). Clang zero-fills 8 bytes before the constructor and destroys the virtual base `V` that lives in `b`'s tail padding.

**Reproducer** (`repro.cpp`)
```cpp
// V lies in the tail padding of the [[no_unique_address]] member b (sizeof(B) 8,
// data size 5). V() runs first and sets v = 'V' (86); then b() may zero only b's
// 5 data bytes. Correct output: 86 1  (grows(INT_MAX - 86) does not overflow).
// clang zeroes all 8 bytes, so v == 0 and grows(INT_MAX) overflows: -O0 and -O3 differ.
extern "C" int printf(const char *, ...);
struct B { int a; char c; B() = default; B(const B &o) : a(o.a), c(o.c) {} };
struct V { char v; V() : v('V') {} };
struct D : virtual V { [[no_unique_address]] B b; D() : b() {} };
int grows(int x) { return x + 1 > x; }
int main() {
  D d;
 printf("%d %d\n", d.v, grows(2147483647 - d.v));
}
```

**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 o0
$ ./o0
0 0
$ clang++ -std=c++20 -O3 repro.cpp -o o3
$ ./o3
0 1
$ clang++ -std=c++20 -O0 -S -emit-llvm repro.cpp -o - | grep 'memset.p0.i64(ptr'
  call void @llvm.memset.p0.i64(ptr align 8 %5, i8 0, i64 8, i1 false)
declare void @llvm.memset.p0.i64(ptr writeonly captures(none), i8, i64, i1 immarg) #4
```
Correct: `86 1`. Clang `-O0`: `0 0`; `-O3`: `0 1`. The IR zeroes 8 bytes for the 5-byte member.

**Why the expected output is right**
`D() : b()` value-initializes `b` ([dcl.init]/9: zero-initialize b, i.e. its own data, then the trivial default ctor). `V` is a distinct object placed in b's tail padding ([intro.object]); its value must stay `'V'` (86). ASan and MSan at -O0 are clean.

**How the -O0/-O3 difference arises**
The front-end bug (v = 0) is wrong at all levels. The second output line comes from a `grows()` signed-overflow check that overflows only with the wrong v = 0, so -O0 and -O3 differ. UBSan reports that overflow, but with the correct v = 86 there is none.

**Potential Root cause**
`clang/lib/CodeGen/CGExprCXX.cpp`, `CodeGenFunction::EmitCXXConstructExpr`: when `E->requiresZeroInitialization()`, `case CXXConstructionKind::Complete:` calls `EmitNullInitialization(Dest.getAddress(), E->getType())`, which zeroes `sizeof(B)` = 8 bytes. It ignores `Dest.mayOverlap()`; for a potentially-overlapping member only the data size (5) belongs to `b`. -O0 IR: `memset(ptr %5, 0, i64 8)`. `AggValueSlot::getPreferredSize` already gives the right size (used by the copy path, PR36992).

**Possible Proposed fix**
```diff
     case CXXConstructionKind::Complete:
- EmitNullInitialization(Dest.getAddress(), E->getType());
+      if (Dest.mayOverlap())
+        EmitNullInitialization(Dest.getAddress(), E->getType(),
+ Dest.getPreferredSize(getContext(), E->getType()));
+      else
+ EmitNullInitialization(Dest.getAddress(), E->getType());
```
with a new optional size argument in `EmitNullInitialization`.

Related: #234795 and open PR #235011 (same switch, but they change only the `Delegating` case) and the fixed copy-path sibling #36340 (PR36992). This report is the `Complete` case, which they do not change.

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

Reply via email to