Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang 17 fails to align stack struct with -fstack-protector-strong, building smartmontools #83673

Open
citrus-it opened this issue Mar 2, 2024 · 1 comment

Comments

@citrus-it
Copy link

citrus-it commented Mar 2, 2024

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 omniosorg/omnios-extra#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:

  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.

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:

The build line for these was:

clang-17 -DHAVE_CONFIG_H -I.  -DBUILD_INFO='"(local build)"' -DSMARTMONTOOLS_SYSCONFDIR='"/usr/local/etc"' -DSMARTMONTOOLS_SMARTDSCRIPTDIR='"/usr/local/etc"' -DSMARTMONTOOLS_DRIVEDBDIR='"/usr/local/share/smartmontools"'      -D_FORTIFY_SOURCE=3  -g -O2 -std=gnu++11 -Wall -W -Wformat=2 -Werror=return-type -fstack-protector-strong -MT ataprint.o -MD -MP -MF .deps/ataprint.Tpo -c -o ataprint.o ataprint.cpp

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.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Mar 2, 2024
@EugeneZelenko EugeneZelenko added clang:codegen and removed clang Clang issues not falling into any other category labels Mar 2, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 2, 2024

@llvm/issue-subscribers-clang-codegen

Author: Andy Fiddaman (citrus-it)

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 omniosorg/omnios-extra#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:

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

Here's the context.

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:

The build line for these was:

clang-17 -DHAVE_CONFIG_H -I.  -DBUILD_INFO='"(local build)"' -DSMARTMONTOOLS_SYSCONFDIR='"/usr/local/etc"' -DSMARTMONTOOLS_SMARTDSCRIPTDIR='"/usr/local/etc"' -DSMARTMONTOOLS_DRIVEDBDIR='"/usr/local/share/smartmontools"'      -D_FORTIFY_SOURCE=3  -g -O2 -std=gnu++11 -Wall -W -Wformat=2 -Werror=return-type -fstack-protector-strong -MT ataprint.o -MD -MP -MF .deps/ataprint.Tpo -c -o ataprint.o ataprint.cpp

clang17

   0x000000000000049c &lt;+1068&gt;:    lea    0x6ec7(%rsp),%rbx

clang18

   0x0000000000000496 &lt;+1062&gt;:    lea    0x6ef0(%rsp),%rbx

If there is any more data I can gather on this that would help, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants