<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/93563>93563</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang][codegen] clang generates bad code for placement new of aggregate with `[[no_unique_address]]` and `-O2`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ericniebler
</td>
</tr>
</table>
<pre>
Compiling the following code with `-O2` yields an executable that prints the incorrect result and then segfaults:
```c++
#include <new>
#include <cstdio>
template <class T>
struct box {
[[no_unique_address]]
T value;
};
template <class... Ts>
struct tuple : box<Ts>... {};
struct empty {};
int main() {
using T = tuple<empty, int>;
alignas(T) unsigned char buffer[sizeof(T)];
std::puts("before"); std::fflush(stdout);
auto* p = new(buffer) T{{empty{}}, {42}}; // bad codegen here
std::puts("after"); std::fflush(stdout);
p->~T();
}
```
Building and running this program with clang-trunk results in:
```
before
before
Segmentation fault (core dumped)
```
With ASAN, I get:
```
before
before
AddressSanitizer:DEADLYSIGNAL
=================================================================
==796460==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000000000 bp 0x000000000001 sp 0x7ffc0b326b38 T0)
==796460==Hint: pc points to the zero page.
==796460==The signal is caused by a READ memory access.
==796460==Hint: address points to the zero page.
#0 0x0 (<unknown module>)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (<unknown module>)
==796460==ABORTING
```
Interestingly, if I try to build with `-O0`, clang hangs and eventually aborts with:
```
fatal error: error in backend: SmallVector unable to grow. Requested capacity (4294967296) is larger than maximum value for size type (4294967295)
clang++-17: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Ubuntu clang version 17.0.6 (++20231209124227+6009708b4367-1~exp1~20231209124336.77)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang++-17: note: diagnostic msg:
********************
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++-17: note: diagnostic msg: /tmp/hello_world-eac76f.cpp
clang++-17: note: diagnostic msg: /tmp/hello_world-eac76f.sh
clang++-17: note: diagnostic msg:
********************
```
See: https://godbolt.org/z/jPr4e5Kco
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUV81y2zgSfhr40iUVBUqkdNCBsiTHtZ44JSkzNacUCDYpTECAgx9bziHPvgWQlu3E9tZm57IqFkWCDfTXX_-gwawVjUJcktmKzNYXzLujNks0giuBpURzUerqYXmp205IoRpwR4RaS6nvwxvXFcK9cEcgWTK6pSRL4EGgrCwwBXhC7h0rJYI7MgedEcrZuIRQXBuD3IFB66UDpqrwQYHFpmZeOkvSgiRrkjzes6S_OKGrcPWjNBWKS18hkPRS4T1JN6994dZVQj99jHeHbSeZ6wUksxYOZwnrjOcOSn0Ckg_KILK0UvqLV-Jvj19YVRm0lszW4epl4AB3THok6SPEfP30_Lre8XgMB_ujbuc7GWSKgIKkl1EiiAZAPy06TMK2cw-vSwjloGVCETondPHMLPA2OPMAJF33Wkl6GRci9BKEcgFZepZmUjSKWULnh7COVzGGKuBHZqD0dY2GzFZWfENdD0KBnpdgAKyrgovTovPORky0xFobJJSGKenqSaSupbdHQufWVdq7_vPL5Zh3mtACumhFiAQ6H8DQBRwiI6veqJ6cfB2sI_lqSofXdAWEbgndQsmqGNsNKjiiwf-InNUuKPoF4N2IpJvvh94pL4LmZdg_n7TyQlbBZSFrjFeqz0xhoTO6Maztc5JLppqRM159HdLMglBv5VX_Orjgp-c9Ni0qx5zQCmKCAqFzrg1C5dsOq4D-bcR_BDzFvvgYOL-GBt2vwSj6jNszJZz4hoakxXpTrG_-3F9ffSxuhrXS9f_99cyQfJFNs6R_3ux2t7tQEl4hAvabq99BK_Dqq9L3CobyBMkpefYLfuv4j4Nl93JkAjaM5HXNkzKlWZnO4ZA8OfknZB9inSig49Dpvs7rWOq_odHQsQbHb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