https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129134
Bug ID: 129134
Summary: c-c++-common/builtin-bitexpandg-1.c execution failure
on hppa (!FRAME_GROWS_DOWNWARD) due to
assign_stack_temp mode in expand_BITEXPAND
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: danglin at gcc dot gnu.org
Target Milestone: ---
Problem Description
Execution of c-c++-common/builtin-bitexpandg-1.c fails with SIGSEGV on both
hppa-unknown-linux-gnu and hppa64-hp-hpux11.11:
(gdb) r
Starting program:
/home/dave/gnu/gcc/objdir64/gcc/testsuite/gcc/builtin-bitexpandg-1.exe
warning: Private mapping of shared library text was not specified
by the executable; setting a breakpoint in a shared library which
is not privately mapped will not work. See the HP-UX 11i v3 chatr
manpage for methods to privately map shared library text.
Program received signal SIGSEGV, Segmentation fault.
0x400000000000706c in main ()
at
/home/dave/gnu/gcc/gcc/gcc/testsuite/c-c++-common/builtin-bitexpandg-1.c:292
292 if (tests[i].fn (0x123456789abcdefULL, 0) != 0)
(gdb) disass $pc-16,$pc+16
Dump of assembler code from 0x400000000000705c to 0x400000000000707c:
0x400000000000705c <main+240>: ldd 8(ret0),r16
0x4000000000007060 <main+244>: copy r17,r26
0x4000000000007064 <main+248>: copy r16,r25
0x4000000000007068 <main+252>: ldo -30(sp),ret1
=> 0x400000000000706c <main+256>: ldd 10(r3),rp
0x4000000000007070 <main+260>: bve,l (rp),rp
0x4000000000007074 <main+264>: ldd 18(r3),dp
0x4000000000007078 <main+268>: or ret0,ret1,ret0
End of assembler dump.
(gdb) p/x $r3
$1 = 0x0
(gdb) p i
$2 = 67112785
(gdb) p tests[i]
Cannot access memory at address 0x800003fffffffff8
In expand_BITEXPAND (gcc/internal-fn.cc), the fallback loop allocates a
temporary stack array for log2_prec1 elements of mode wmode using:
cpp
rtx array = assign_stack_temp (wmode, log2_prec1 * GET_MODE_SIZE (wmode));
rtx array_addr = force_reg (GET_MODE (XEXP (array, 0)), XEXP (array, 0));
Because wmode is a scalar mode rather than BLKmode while size > GET_MODE_SIZE
(wmode), assign_stack_local_1 on !FRAME_GROWS_DOWNWARD targets (such as
PA-RISC) offsets the returned address by size - GET_MODE_SIZE (wmode) so that a
scalar wmode access targets the top element of the allocated slot.
expand_BITEXPAND then uses XEXP (array, 0) as the base address and indexes
upward (array_addr + i * GET_MODE_SIZE (wmode) for i = 0 .. log2_prec1 - 1),
writing past the end of the allocated stack slot into the function's saved
register area and frame marker.
On hppa64-hp-hpux11.11, in bitexpand128 (TImode, 7 elements = 112 bytes), the
112-byte slot is allocated at [-240(%r30), -128(%r30)), array_addr points to
-128(%r30), and stores for i = 1..6 overwrite [-112(%r30), -17(%r30)],
clobbering saved registers %r3–%r9 (including %r3, which holds tests[i].fn in
main) and the caller's frame marker. Upon returning from bitexpand128 (0, 0),
%r3 is restored as 0x0, causing a SIGSEGV on the next indirect call (ldd
16(%r3),%r2). The same out-of-bounds write occurs on 32-bit
hppa-unknown-linux-gnu in bitexpand64 (DImode).
Proposed Fix
Allocate the stack temporary with BLKmode so XEXP (array, 0) points to the
start (lowest address) of the allocated block regardless of
FRAME_GROWS_DOWNWARD, and set the alignment to GET_MODE_ALIGNMENT (wmode) so
subsequent change_address (array, wmode, ...) accesses retain proper alignment
on STRICT_ALIGNMENT targets: