Skip to content

led: keep the structs in a module aligned for the host - #385

Merged
davidgiven merged 2 commits into
davidgiven:defaultfrom
maarten-boot:upstream/led-align
Nov 1, 2026
Merged

davidgiven merged 2 commits into
davidgiven:defaultfrom
maarten-boot:upstream/led-align

Conversation

@maarten-boot

Copy link
Copy Markdown
Contributor

led keeps each input module in one memory piece: header, section
table, name table, strings, then the offsets of the section contents.
With USEMALLOC each piece is its own malloc block, but the offsets
inside it were only 4-aligned (the archive magic took sizeof(int),
int_align() rounded to sizeof(int), the name table followed the
20-byte header and section entries). On a 64-bit host struct outname,
struct ar_hdr and ind_t need 8, so linking with an archive used
them at misaligned addresses: undefined behaviour, reported by
-fsanitize=alignment on every such link, and a fault on a
strict-alignment host.

int_align() now rounds to _Alignof(max_align_t), each table starts
aligned, and modulsize() is computed from that layout.

Tested: make with PLATS = all, all tests pass; every linked program
is byte-identical to before (the hilo_bas examples differ between any
two builds because the BASIC compiler emits a heap address, a separate
bug). With -fsanitize=alignment led has no reports left (the
remaining ones are in util/ass).

🤖 Generated with Claude Code

With USEMALLOC every piece is its own malloc block, but offsets inside
ALLOMODL were only 4-aligned: the archive magic took sizeof(int),
int_align() rounded to sizeof(int), and the name table followed the
20-byte header and section entries. struct outname, struct ar_hdr and
ind_t (8-byte aligned on a 64-bit host) were then used at 4 (mod 8)
addresses, which is undefined behaviour (UBSan -fsanitize=alignment
reports it on every link with an archive) and would fault on a
strict-alignment host.

int_align() now rounds to _Alignof(max_align_t), the magic takes
int_align(sizeof(int)), the section, name and offset tables start
aligned (IND_SECT, IND_NAME, IND_EMIT), and modulsize() is computed from
that layout so the parts direct_alloc() and read_modul() allocate add up
to it. Linked programs are byte-identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@davidgiven

Copy link
Copy Markdown
Owner

Nice, thanks; we can probably dispense with the !USEMALLOC case entirely and remove the #ifdef. It would simplify the code.

USEMALLOC was always defined, so the !USEMALLOC memory manager (sbreak(),
the brk() halves of init_core(), move_up() and compact(), copy_down()
and copy_up()) was never compiled. Removed with the #ifdefs, the
#define and <unistd.h>; the comments now describe the malloc() pieces.

Every linked program is byte-identical to before (cpm, linux68k,
linuxppc, pc86; hilo_bas differs between any two builds anyway), all
tests pass, and -fsanitize=alignment reports nothing for led.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
maarten-boot added a commit to maarten-boot/ack that referenced this pull request Nov 1, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
maarten-boot added a commit to maarten-boot/ack that referenced this pull request Nov 1, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@maarten-boot

Copy link
Copy Markdown
Contributor Author

Done: USEMALLOC was always defined, so the brk() allocator was never compiled. 38656bd removes it with the #ifdefs (memory.c goes from 810 to 469 lines). On cpm, linux68k, linuxppc and pc86 every linked program is byte-identical to before and all tests pass; with
▎ -fsanitize=alignment, led has no reports.

@davidgiven

Copy link
Copy Markdown
Owner

Excellent, thanks. Merging.

@davidgiven
davidgiven merged commit ea30997 into davidgiven:default Nov 1, 2026
3 checks passed
maarten-boot added a commit to maarten-boot/ack that referenced this pull request Nov 1, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
maarten-boot added a commit to maarten-boot/ack that referenced this pull request Nov 1, 2026
…checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants