gap icon indicating copy to clipboard operation
gap copied to clipboard

ScanRange: UBSan reports misaligned load of struct OpaqueBag * on the first GC at startup

Open waroad opened this issue 1 month ago • 3 comments

On a GAP built with UBSan, every start reports "load of misaligned address" in ScanRange (src/gasman.c) during the first garbage collection, and stops there if halt_on_error is set.

quit.g contains one line:

QUIT;

Command, on a UBSan build (the check that fires is -fsanitize=alignment). No options are needed; -q -A only shorten the output, and an empty stdin instead of quit.g also triggers it:

UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1:abort_on_error=1 gap -q -A quit.g

Output:

src/gasman.c:1945:17: runtime error: load of misaligned address 0x7ffc92d1e0ac for type 'struct OpaqueBag *', which requires 8 byte alignment
    #0 0x55d8f45f1821 in ScanRange src/gasman.c:1945
    #1 0x55d8f45f1ad4 in GenStackFuncBags src/gasman.c:1968
    #2 0x55d8f45f213d in CollectBags_Mark src/gasman.c:2016
    #3 0x55d8f45f6793 in CollectBags src/gasman.c:2456
    #4 0x55d8f45f8660 in NewBag src/gasman.c:1389

The process then exits with SIGABRT (rc 134).

Found at commit f3761d644aaa83691902dfa32efa8596c19569b8, and it still reproduces at ad75aa76108fab60653d5299d0f58b1a7be045f9. The UBSan build reports and aborts as above; the release build prints nothing and exits 0 (x86-64 tolerates the unaligned loads).

Looks like the conservative stack scan in src/gasman.c:ScanRange dereferences a Bag * at offsets 2, 4 and 6 on purpose (C_STACK_ALIGN is 2 on x86_64), which is undefined behaviour in C. Marking ScanRange with __attribute__((no_sanitize("alignment"))) makes the report go away in my test.

I can send a patch if that helps.

Found with an LLM-based testing tool; I used Claude to reduce it and look for the cause.

waroad avatar Oct 14 '26 01:10 waroad

Can you tell us more about your CPU, operating system, because this isn't 'undefined in C', C in general doesn't say anything how aligned the stack has to be. To fix this we would need to figure out stack alignment on whatever CPU and OS we were on.

ChrisJefferson avatar Oct 14 '26 06:10 ChrisJefferson

Thanks for looking at this. The machine is x86-64 (AMD EPYC 7513), Ubuntu 22.04.5, Linux 5.15, GCC 11.4.0, and GAP was configured with CFLAGS="-g -O1 -fno-omit-frame-pointer -fsanitize=undefined" and LDFLAGS="-fsanitize=undefined" (plus --enable-debug --without-readline --disable-backtrace).

I don't think the OS's stack alignment is involved: cnf/m4/gap.m4 sets C_STACK_ALIGN to 2 for any x86_64-* host, so ScanRange reads a Bag * at offsets 2, 4 and 6 of each word on purpose (for (i = 0; i < sizeof(Bag *); i += C_STACK_ALIGN) with (Bag *)((char *)pA - i)). Those are the loads -fsanitize=alignment reports. C leaves a pointer converted to a type it is not correctly aligned for undefined (C11 6.3.2.3p7), even though x86-64 handles the loads fine in practice.

So as far as I can tell this only shows up in sanitizer builds. On x86 hosts, gap.m4 sets 2 explicitly (for i386-i686 as well as x86_64-*); I haven't checked what other hosts end up with. If that isn't worth a change, feel free to close it. Otherwise, marking ScanRange with __attribute__((no_sanitize("alignment"))) keeps UBSan quiet without changing the scan, and I can send that as a PR.

waroad avatar Oct 14 '26 06:10 waroad

I think a PR would be good by adding the explicit flag we are misaligning on purpose would be good, if there doesn't turn out to be lots of other issues (or it breaks CI).

I wouldn't want to fix it by following the proper alignment, because that would require us being sure no GAP package is doing something stupid and creating unaligned pointers (they probably aren't, but it's very very hard to check, and bugs in the GC missing a pointer can be incredibly hard to track down).

ChrisJefferson avatar Oct 14 '26 07:10 ChrisJefferson