tests/tcg/x86_64: Add tests for zero_bss() with RX PT_LOAD segments
PATCH 1/1 tests/tcg/x86_64: Add tests for zero_bss() with RX PT_LOAD segments
Here are the test cases for the zero_bss fix, as Richard requested.
I've included two tests that trigger the bug:
- test-zerobss-1.c: minimal case that just validates the loader handles it
- test-zerobss-2.c: actually reads from .bss to verify it's properly zeroed
I also tested a third case where the .bss is file-backed in the R_X segment, but that one fails with EINVAL at mmap() time (before reaching zero_bss). That's expected behavior (kernel blocks it) so I didn't include it as a test. I mentioned it in the first commit message though.
Thanks,
Razvan
PATCH 1/1 tests/tcg/x86_64: Add tests for zero_bss() with RX PT_LOAD segments
Add 2 test cases to validate the fix for issue #3179, where the linux-user ELF Loader incorrectly rejected binaries with .bss sections in R_X PT_LOAD segments.
test-zerobss-1.c: defines a minimal _start routine that exists immediately without accessing the .bss region. Although a .bss region is present in ELF, it is never referenced at runtime. This test validates that the loader can correctly handle and zero the fractional .bss region in an R_X segment without triggering permission violations.
test-zerobss-2.c: explicitly reads from multiple .bss variables to verify they are zeroed properly. If the .bss was correctly zeroed, the exit code will be 0.
rx_anonymous_bss.ld: tests require a custom linker script to create the specific ELF layout that triggers the bug. The linker script:
- creates a single PT_LOAD segment with FLAGS(5) = PF_R | PF_X (read-execute)
- places .text and .bss in this same segment
- sets .bss to ALIGN(1) to force it into the tail of the R_X segment
- results in p_filesz < p_memsz, where the gap is anonymous .bss
Expected behavior:
- Without the fix: Both tests fail with "PT_LOAD with non-writable bss"
- With the fix: Both tests pass (exit code 0)
--- tests/tcg/x86_64/Makefile.target | 15 ++++++++++++ tests/tcg/x86_64/rx_anonymous_bss.ld | 27 +++++++++++++++++++++ tests/tcg/x86_64/test-zerobss-1.c | 20 ++++++++++++++++ tests/tcg/x86_64/test-zerobss-2.c | 36 ++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 tests/tcg/x86_64/rx_anonymous_bss.ld create mode 100644 tests/tcg/x86_64/test-zerobss-1.c create mode 100644 tests/tcg/x86_64/test-zerobss-2.c
tests/tcg/x86_64/Makefile.target +15 -0
@@ -19,6 +19,9 @@ X86_64_TESTS += test-1648 X86_64_TESTS += test-2175 X86_64_TESTS += cross-modifying-code X86_64_TESTS += fma + +X86_64_TESTS += test-zerobss-1 +X86_64_TESTS += test-zerobss-2 TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64 else TESTS=$(MULTIARCH_TESTS)
@@ -36,5 +39,17 @@ test-x86_64: LDFLAGS+=-lm -lc test-x86_64: test-i386.c test-i386.h test-i386-shift.h test-i386-muldiv.h $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) +test-zerobss-1: $(SRC_PATH)/tests/tcg/x86_64/test-zerobss-1.c + $(CC) -nostdlib -static -fno-stack-protector -fno-pie -no-pie \ + -Wl,-T,$(SRC_PATH)/tests/tcg/x86_64/rx_anonymous_bss.ld \ + -Wl,--build-id=none \ + $< -o $@ + +test-zerobss-2: $(SRC_PATH)/tests/tcg/x86_64/test-zerobss-2.c + $(CC) -nostdlib -static -fno-stack-protector -fno-pie -no-pie \ + -Wl,-T,$(SRC_PATH)/tests/tcg/x86_64/rx_anonymous_bss.ld \ + -Wl,--build-id=none \ + $< -o $@ + %: $(SRC_PATH)/tests/tcg/x86_64/%.c $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
tests/tcg/x86_64/rx_anonymous_bss.ld new file +27 -0
@@ -0,0 +1,27 @@ +/* Linker script to create RX PT_LOAD with anonymous .bss tail + * SPDX-License-Identifier: GPL-2.0-or-later + */ +PHDRS +{ + text PT_LOAD FLAGS(5); +} +SECTIONS +{ + . = 0x400000; + .text : { + *(.text .text.*) + _etext = .; + } :text + .bss ALIGN(1) : { + _bss_start = .; + *(.bss .bss.* COMMON) + _bss_end = .; + } :text + /DISCARD/ : { + *(.note.GNU-stack) + *(.note.gnu.property) + *(.comment) + *(.eh_frame) + } +} +ENTRY(_start)
tests/tcg/x86_64/test-zerobss-1.c new file +20 -0
@@ -0,0 +1,20 @@ +/* +* .bss region exists but unused +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +int x; +void _start (void ) +{ +/* +* Exit immediately & never touched x +*/ + __asm__volatile ( +"mov $60, %%rax\n" +"xor %%rdi, %%rdi\n" +"syscall\n" + :/* no out */ + :/* no in */ + :"rax" ,"rdi" + ); +__builtin_unreachable (); +}
tests/tcg/x86_64/test-zerobss-2.c new file +36 -0
@@ -0,0 +1,36 @@ +/* +* Test case for zero_bss() with anonymous BSS in RX PT_LOAD +* +* This binary has .bss in the same PT_LOAD as .text (R_X permissions), +* but the BSS is anonymous (beyond p_filesz), not file-backed. +* Actual behavior: +* old code: Fails with "PT_LOAD with non-writable bss" +* new code: Succeeds, zeros BSS, exits with code 0 +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +/* These will be included in .bss (uninitialized anonymous memory) */ +int bss_value; +int bss_array[64 ]; + +void _start (void ) +{ +int sum = bss_value; +int i; + +for (i =0 ; i <64 ; i++) { + sum += bss_array[i]; + } +/* If BSS was properly zeroed, sum should be 0 */ +/* Exit with sum as exit code */ + __asm__volatile ( +"movl %0, %%edi\n\t" +"movl $60, %%eax\n\t" +"syscall\n\t" + :/* no out*/ + :"r" (sum) + :"rdi" ,"rax" + ); + +__builtin_unreachable (); +}
Signature · 1 line
2.43.0
Hi,
I'd like to kindly ping this patch. It has been a few weeks since submission and I haven't received any feedback yet.
I'm working on getting familiar with the contribution flux. Any review, even brief comments on the approach would be very helpful! Thanks in advance,
Razvan
În mar., 3 feb. 2026 la 09:39, Razvan Ghiorghe <[email protected]> a scris:
Quoted 181 lines
Add 2 test cases to validate the fix for issue #3179, where the linux-user ELF Loader incorrectly rejected binaries with .bss sections in
R_XPT_LOADsegments.
test-zerobss-1.c: defines a minimal_startroutine that exists immediately without accessing the .bss region. Although a .bss region is present in ELF,
it is never referenced at runtime. This test validates that the loader can correctly handle and zero the fractional .bss region in anR_Xsegment without triggering permission violations.
test-zerobss-2.c: explicitly reads from multiple .bss variables to verify they are zeroed properly. If the .bss was correctly zeroed, the exit code will
be 0.rx_anonymous_bss.ld: tests require a custom linker script to create the specific ELF
layout that triggers the bug. The linker script:
- creates a singlePT_LOADsegment with FLAGS(5) =PF_R|PF_X(read-execute)
- places .text and .bss in this same segment
- sets .bss to ALIGN(1) to force it into the tail of theR_Xsegment
- results inp_filesz<p_memsz, where the gap is anonymous .bssExpected behavior:
- Without the fix: Both tests fail with "PT_LOADwith non-writable bss"
- With the fix: Both tests pass (exit code 0)Signed-off-by Razvan Ghiorghe <[email protected]>--- tests/tcg/x86_64/Makefile.target | 15 ++++++++++++ tests/tcg/x86_64/rx_anonymous_bss.ld | 27 +++++++++++++++++++++ tests/tcg/x86_64/test-zerobss-1.c | 20 ++++++++++++++++ tests/tcg/x86_64/test-zerobss-2.c | 36 ++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 tests/tcg/x86_64/rx_anonymous_bss.ld create mode 100644 tests/tcg/x86_64/test-zerobss-1.c create mode 100644 tests/tcg/x86_64/test-zerobss-2.ctests/tcg/x86_64/Makefile.target +15
-0@@ -19,6 +19,9 @@ X86_64_TESTS += test-1648 X86_64_TESTS += test-2175 X86_64_TESTS += cross-modifying-code X86_64_TESTS += fma + +X86_64_TESTS += test-zerobss-1 +X86_64_TESTS += test-zerobss-2 TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64 else TESTS=$(MULTIARCH_TESTS)@@ -36,5 +39,17 @@ test-x86_64: LDFLAGS+=-lm -lc test-x86_64: test-i386.c test-i386.h test-i386-shift.h test-i386-muldiv.h $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) +test-zerobss-1: $(SRC_PATH)/tests/tcg/x86_64/test-zerobss-1.c + $(CC) -nostdlib -static -fno-stack-protector -fno-pie -no-pie \ + -Wl,-T,$(SRC_PATH)/tests/tcg/x86_64/rx_anonymous_bss.ld \ + -Wl,--build-id=none \ + $< -o $@ + +test-zerobss-2: $(SRC_PATH)/tests/tcg/x86_64/test-zerobss-2.c + $(CC) -nostdlib -static -fno-stack-protector -fno-pie -no-pie \ + -Wl,-T,$(SRC_PATH)/tests/tcg/x86_64/rx_anonymous_bss.ld \ + -Wl,--build-id=none \ + $< -o $@ + %: $(SRC_PATH)/tests/tcg/x86_64/%.c $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)tests/tcg/x86_64/rx_anonymous_bss.ld new file +27
-0@@ -0,0 +1,27 @@ +/* Linker script to create RX PT_LOAD with anonymous .bss tail + * SPDX-License-Identifier: GPL-2.0-or-later + */ +PHDRS +{ + text PT_LOAD FLAGS(5); +} +SECTIONS +{ + . = 0x400000; + .text : { + *(.text .text.*) + _etext = .; + } :text + .bss ALIGN(1) : { + _bss_start = .; + *(.bss .bss.* COMMON) + _bss_end = .; + } :text + /DISCARD/ : { + *(.note.GNU-stack) + *(.note.gnu.property) + *(.comment) + *(.eh_frame) + } +} +ENTRY(_start)tests/tcg/x86_64/test-zerobss-1.c new file +20
-0@@ -0,0 +1,20 @@ +/* +* .bss region exists but unused +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +int x; +void _start (void ) +{ +/* +* Exit immediately & never touched x +*/ + __asm__volatile ( +"mov $60, %%rax\n" +"xor %%rdi, %%rdi\n" +"syscall\n" + :/* no out */ + :/* no in */ + :"rax" ,"rdi" + ); +__builtin_unreachable (); +}tests/tcg/x86_64/test-zerobss-2.c new file +36
-1@@ -0,0 +1,36 @@ +/* +* Test case for zero_bss() with anonymous BSS in RX PT_LOAD +* +* This binary has .bss in the same PT_LOAD as .text (R_X permissions), +* but the BSS is anonymous (beyond p_filesz), not file-backed. +* Actual behavior: +* old code: Fails with "PT_LOAD with non-writable bss" +* new code: Succeeds, zeros BSS, exits with code 0 +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +/* These will be included in .bss (uninitialized anonymous memory) */ +int bss_value; +int bss_array[64 ]; + +void _start (void ) +{ +int sum = bss_value; +int i; + +for (i =0 ; i <64 ; i++) { + sum += bss_array[i]; + } +/* If BSS was properly zeroed, sum should be 0 */ +/* Exit with sum as exit code */ + __asm__volatile ( +"movl %0, %%edi\n\t" +"movl $60, %%eax\n\t" +"syscall\n\t" + :/* no out*/ + :"r" (sum) + :"rdi" ,"rax" + ); + +__builtin_unreachable (); +} --2.43.0