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

    <tr>
        <th>Summary</th>
        <td>
            clang 17 fails to align stack struct with -fstack-protector-strong, building smartmontools
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          citrus-it
      </td>
    </tr>
</table>

<pre>
    I searched for any similar issues to this but didn't find anything, so apologies if it's a known issue.

We recently switched the default clang compiler in the OmniOS operating system distribution from 16 to 17, and we had a report that the `smartmontools` package was no longer working correctly.

There is more detail in https://github.com/omniosorg/omnios-extra/issues/1418 but the relevant parts are included below.

The short version is that clang 17 is not aligning `drive` on the stack for some reason in the following code if `-fstack-protector-strong` is in the build flags:

```c
 ata_identify_device drive;
  memset(&drive, 0, sizeof(drive));
 unsigned char raw_drive[sizeof(drive)];
  memset(&raw_drive, 0, sizeof(raw_drive));
```

Here's [the context](https://github.com/smartmontools/smartmontools/blob/5feaa21b8989dc32f3dc76d4d7d3b163cef30e41/smartmontools/ataprint.cpp#L3513).

When building with clang 17.0.6, the resulting disassembly shows the misalignment:

```
_Z12ataPrintMainP10ata_deviceRK17ata_print_options+0x3ef: leaq   0xfffffffffffffdc6(%rbp),%rbx
```
which is supported by gdb's interpretation of the DWARF - note that `raw_drive` is aligned as expected.
```
Symbol drive is a variable at frame base reg $rbp offset 0+-570, length 512.
Symbol raw_drive is a variable at frame base reg $rbp offset 0+-1088, length 512.
```

Building with either clang 16 or 18 does not cause this, in both of those cases the variable is properly aligned, as shown below.

```
_Z12ataPrintMainP10ata_deviceRK17ata_print_options+0x3ef: leaq   0xfffffffffffffdc0(%rbp),%rbx
```

Unfortunately I have not been able to come up with a standalone reproducer for this, but I did build the same smartmontools source on Ubuntu 24.04 with clang 17.0.6, and got similar results:

clang17
```
   0x000000000000049c <+1068>: lea    0x6ec7(%rsp),%rbx
```

clang18
```
 0x0000000000000496 <+1062>:    lea    0x6ef0(%rsp),%rbx
```

If there is any more data I can gather on this that would help, please let me know.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vk9v474R_TT0ZWBDoizJPvgQJw0atMUudrtYoJeAIkcSG4pUyVEc99MXpBwn3ngX29_hZxi2ZZIzbx7f_BEh6M4i7li5Z-XdQkzUO7-TmvwUlpoWjVPH3QMEFF72qKB1HoQ9QtCDNsKDDmHCAOSAeh2gmQiUVpbxmqDVVsXN1GvbMX4LwYEYnXGdxgC6BU2M1wEEPFl3sLOtFcvuWHYzf35H8CjRkjlCOGhKEKhHUNiKyRBII2wH0g2jNuhB27T6abD601dwI3pB2nYQjoFwAKUDed1MpJ2F1rsB8ipCz-uITlgFB4ReKBDgcXSegHpBySSrsjAIT4Oz5JwJrMpgFPJJdAgHEcA6MM526OHg_JNOoLxHSeZ4EdE_e_QIOsDgfAyDhDYRdk80BlbcMH7P-H2nqZ-alXQD4_dusNoF57vz7yW-kBeM38_sM36fr_NNIj9i9WjwWViCUXgKIKJDK82kUEGDxh1-RAShj8E-ow-RGR3muGdy8zr-YR2BMLqzMTRWZcrrZ4wkuJnyQEI-JXUEN0QIIkRL82LrjHGHmRSF8eZZlS3bdGY5ekcoyfllIO9sF43q8Hq0mbRR0BrRJXbe4WZVNr_l_AyCxKNWaEm3x0eFz1oizDCL_WkLDDgEJMY3jFfzGr-FLGlT_xddy_jm9Pc2vs8HJ5vyRIHshQcvDo_ztnL_8Vx59xOHb8c-OH23dOH4HOT7yP-KHlPisHIfOZLOEr5QdMw3vxDSpYA_PjfGNYzfly0KwfNms91slSx4WyhZV2qtalU0eVVIbIsM1_kVC4LE6LWllRxHxou_F2VeML69zOke7XytURAHTf1ZaKtsVUVaZhGHyaTkVTqIEHBoYhHo3SGk9UGHpMcBLf1MGfPj479yLkh8jsD-IbT9nGdRKrNCvvwtr-NTgv3oxlgaAuP77KX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