Bug 278868 - [regression] llvm18/clang18 produces crashing 32 bit code with -malign-double
Summary: [regression] llvm18/clang18 produces crashing 32 bit code with -malign-double
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 15.0-CURRENT
Hardware: i386 Any
: --- Affects Some People
Assignee: Dimitry Andric
URL:
Keywords: regression
Depends on:
Blocks:
 
Reported: 2024-05-09 11:29 UTC by Eugene Grosbein
Modified: 2024-05-09 21:33 UTC (History)
1 user (show)

See Also:


Attachments
sample code (322 bytes, text/plain)
2024-05-09 11:29 UTC, Eugene Grosbein
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Eugene Grosbein freebsd_committer freebsd_triage 2024-05-09 11:29:03 UTC
Created attachment 250544 [details]
sample code

32 bit C++ code compiles and works but crashes with SIGSEGV at exit if compiled with -malign-double.

Compile attached show.cc:

$ c++ -malign-double -o show show.cc

Run it:
$ ktrace -i ./show /etc/motd; echo $?
FreeBSD 15.0-CURRENT (GENERIC) #0 main-n269695-78101d437a92: Thu Apr 25 03:38:32 UTC 2024

Welcome to FreeBSD!

Release Notes, Errata: https://www.FreeBSD.org/releases/
Security Advisories:   https://www.FreeBSD.org/security/
FreeBSD Handbook:      https://www.FreeBSD.org/handbook/
FreeBSD FAQ:           https://www.FreeBSD.org/faq/
Questions List:        https://www.FreeBSD.org/lists/questions/
FreeBSD Forums:        https://forums.FreeBSD.org/

Documents installed with the system are in the /usr/local/share/doc/freebsd/
directory, or can be installed later with:  pkg install en-freebsd-doc
For other languages, replace "en" with a language code like de or fr.

Show the version of FreeBSD installed:  freebsd-version ; uname -a
Please include that output and any error messages when posting questions.
Introduction to manual pages:  man man
FreeBSD directory layout:      man hier

To change this login announcement, see motd(5).
Segmentation fault (core dumped)
139
Comment 1 Eugene Grosbein freebsd_committer freebsd_triage 2024-05-09 11:30:40 UTC
# kdump | tail -15
 28660 show     CALL  write(0x1,0x20a09000,0x30)
 28660 show     GIO   fd 1 wrote 48 bytes
       "To change this login announcement, see motd(5).
       "
 28660 show     RET   write 48/0x30
 28660 show     CALL  lseek(0x3,0,SEEK_CUR)
 28660 show     RET   lseek 939/0x3ab
 28660 show     CALL  lseek(0x3,0,SEEK_SET)
 28660 show     RET   lseek 0
 28660 show     CALL  lseek(0x3,0x3ab,SEEK_SET)
 28660 show     RET   lseek 939/0x3ab
 28660 show     CALL  close(0x3)
 28660 show     RET   close 0
 28660 show     PSIG  SIGSEGV SIG_DFL code=SEGV_MAPERR
 28660 show     NAMI  "show.core"
Comment 2 Dimitry Andric freebsd_committer freebsd_triage 2024-05-09 11:57:43 UTC
Yes, that works as advertised. If you want to use -malign-double, you will have to compile the whole system with the same alignment convention. In particular, you need to recompile libc++, at the least. But preferably the whole world.
Comment 3 Dimitry Andric freebsd_committer freebsd_triage 2024-05-09 11:59:35 UTC
See for example bug 277896, for which I committed https://cgit.freebsd.org/ports/commit/?id=b49518c5c7c16418d7da0a5ce786da139b905bb1, in particular the stern warning:

    If it is not the architecture default, as it is on amd64, -malign-double
    should not be used without recompiling basically the entire userspace
    runtime. Quoting the gcc docs:

    > Warning: if you use the -malign-double switch, structures containing
    > the above types are aligned differently than the published application
    > binary interface specifications for the x86-32 and are not binary
    > compatible with structures in code compiled without that switch.
Comment 4 Eugene Grosbein freebsd_committer freebsd_triage 2024-05-09 12:12:08 UTC
That's sad: the port databases/xtrabackup80 (formerly xtrabackup8) with previous llvm/clang versions successfully used -malign-double to build and run auxiliary binaries used in build process, as well as its main binary. Now build process fails early because auxiliary binaries work but crash at exit.
Comment 5 Eugene Grosbein freebsd_committer freebsd_triage 2024-05-09 14:31:53 UTC
We have other ports in our tree that use -malign-double, too:

audio/penguinsap
biology/garlic
games/quantumminigolf
math/blacs
math/fftw3
science/tinker

Not to mention databases/xtrabackup8[01].
Comment 6 Dimitry Andric freebsd_committer freebsd_triage 2024-05-09 17:37:11 UTC
(In reply to Eugene Grosbein from comment #5)
I wonder what the reason for these programs using -malign-double is. Maybe somebody thought that would improve performance? I guess it could, in some cases, but if someone cares about performance, they'd run amd64, not i386. :)

The big problem is that with -malign-double, you subtly modify the ABI of every header you include. In lots of cases you can be lucky and not notice anything wrong, but the ABI has been violated anyway: struct members can be at wrong offsets, alignments are off, etc.

The only 'safe' way to use it would be to compile *only* the programs themselves using that option, but somehow avoid applying those incompatible settings to all system (and third-party!) includes and libraries that are pulled in.

This is also the reason that a global compiler option is a very bad solution for 'improving' the alignment of your own structures: it is much better to selectively add __aligned__ attributes to specific types, of which you *know* (by measuring) that aligning them in a non-default way makes the program go faster...
Comment 7 Eugene Grosbein freebsd_committer freebsd_triage 2024-05-09 21:33:15 UTC
(In reply to Dimitry Andric from comment #6)

> I wonder what the reason for these programs using -malign-double is.

In case of xtrabackup80, -malign-double helped to deal with mysql80-server codebase braindamage, as xtrabackup contains parts of mysql server code embedded. It cannot be compiled by simply removing -malign-double, as in a commint I just found:

https://cgit.freebsd.org/ports/commit/databases/mysql80-server/Makefile?id=b49518c5c7c16418d7da0a5ce786da139b905bb1