<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54845>54845</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Undefined Sanitizer emits alignment requirement of 8 bytes for double inside struct on i386
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ADKaster
      </td>
    </tr>
</table>

<pre>
    The following program generates an -fsanitize=alignment error with clang++-14.0.1 but not gcc-11.2.0

Is this code actually undefined behavior? Or is the clang code in clang::CodeGen::CodeGenFunction::EmitCXXNewExpr  improperly emitting the wrong sanitizer metadata? I've seen this same error with more complex code that tries to create the equivalent of std::aligned_storage using `alignof(T)` and being surprised that alignof(double) is actually 4 on i386 but UBSAN is complaining that my storage is supposed to have 8 byte alignment.

aligntest.cpp
```cpp
#include <new>
 
struct S {
    short b;
    double c;
};
 
__attribute__((noinline)) void foo(double& d) {
    new (&d) double(0);
}
 
int main() {
    S s {};
    foo(s.c);
}
```

clang++-14 output:

```console
~/dev$ clang++ --version
Ubuntu clang version 14.0.1-++20220407063131+ec13fed5867f-1~exp1~20220407063214.111
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
~/dev$ clang++ -m32 -Og -ggdb3 -fsanitize=undefined aligntest.cpp
~/dev$ ./a.out
aligntest.cpp:9:14: runtime error: constructor call on misaligned address 0xfffc2a7c for type 'double', which requires 8 byte alignment
0xfffc2a7c: note: pointer points here
  00 00 00 00 00 00 00 00  00 00 00 00 98 19 f0 f7  00 30 bc f7 ca c4 9b f7  01 00 00 00 54 2b fc ff
              ^ 
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior aligntest.cpp:9:14 in 
```

g++-11 output:
```console
~/dev$ g++ --version
g++ (Ubuntu 11.2.0-7ubuntu2) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

~/dev$ g++ -m32 -Og -ggdb3 -fsanitize=undefined aligntest.cpp
~/dev$ ./a.out
~/dev$ 
```

The i386 psABI has an alignment requirement of 4 bytes for the double here, as shown by pahole

```console
$ pahole ./a.out 
struct S {
        short                      b;                    /*     0     2 */

        /* XXX 2 bytes hole, try to pack */

        double                     c;                    /*     4     8 */

        /* size: 12, cachelines: 1, members: 2 */
        /* sum members: 10, holes: 1, sum holes: 2 */
        /* last cacheline: 12 bytes */
};
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1VktzozgQ_jX40oWLl18HHxzH3k3tTDIVO1WZU0oIAdoFxErCjufXb0tgg2eczGkpikd3q9Wvr1uxSE7Lfc4gFUUhjrzKoJYik6SEjFVMEs0UkArcVJGKa_6DOeE9KXhWlazSwKQUEo5c50ALUmVOcIe360djb-xD3GiohIaMUtf3x8HYc7x7x1u1zwcFOucKqEgYEKobUhQnaKqEpbxiCcQsJwcupBNu4UkCN-Ks3aZdw6tu03CF9xpJf7Dq6mfbVFRz0RE3Jdfr19dHdty81xKAl-hqzSTuypCljfNmi6MU-HX2V0LJNEmIJsaQByeYHRgoxqrWekVKNgxDKSQaKcq6YO-tmTonGrTkGEgtgEqGMbX7sH8bfiCFiaNIQemkNdNGlyVvSgtJMgaNMoY5U88yROoE870TLJCAiTFhMmzVyFpyhWGz2_WiiWjigqG8CeAlyhGICng4n9ocvdztVo9gU4F2E161kUA95QnOZhhfm7oWdg8BmBsGc4hP6MylHsbD_Foqlo8e07ruGFOvvXtKEPKKFg3GyQnXFTs64ablQPtSWjZUww6c2V3HwEvlQmqInXBAaz0FeiE6s_teoH29vRGNqUCn2dsbRgfvSvCqwIIzIcUoHQRPEAxiELopJIZzZQBaCnb91PLOonPPqBkaMNydY6ZLDK9d-JPCHShLGNqMV2uJGtObei_xHMb9GoggGl032pTWQKZPhKi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