Re: [PATCH v12 07/12] KVM: selftests: Report stacktraces SIGBUS, SIGSEGV, SIGILL, and SIGFPE by default

Ackerley Tng
2025-10-09T22:31:17Z

Sean Christopherson <seanjc@google.com> writes:

> Register handlers for signals for all selftests that are likely happen due
> to test (or kernel) bugs, and explicitly fail tests on unexpected signals
> so that users get a stack trace, i.e. don't have to go spelunking to do
> basic triage.
>
> Register the handlers as early as possible, to catch as many unexpected
> signals as possible, and also so that the common code doesn't clobber a
> handler that's installed by test (or arch) code.
>
> Signed-off-by: Sean Christopherson <seanjc@google.com>

I tested this with

diff --git i/tools/testing/selftests/kvm/guest_memfd_test.c w/tools/testing/selftests/kvm/guest_memfd_test.c
index 618c937f3c90f..f6de2a678bf99 100644
--- i/tools/testing/selftests/kvm/guest_memfd_test.c
+++ w/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -182,6 +182,8 @@ static void test_fault_sigbus(int fd, size_t accessible_size, size_t map_size)
TEST_EXPECT_SIGBUS(memset(mem, val, map_size));
TEST_EXPECT_SIGBUS((void)READ_ONCE(mem[accessible_size]));

+ mem[accessible_size] = 0xdd;
+
for (i = 0; i < accessible_size; i++)
TEST_ASSERT_EQ(READ_ONCE(mem[i]), val);

And got

==== Test Assertion Failure ====
lib/kvm_util.c:2299: false
pid=388 tid=388 errno=29 - Illegal seek
1 0x000000000040a253: report_unexpected_signal at kvm_util.c:2299
2 0x000000000042615f: sigaction at ??:?
3 0x000000000040283f: test_fault_sigbus at guest_memfd_test.c:183 (discriminator 4)
4 0x0000000000402c1c: test_fault_private at guest_memfd_test.c:200
5 (inlined by) __test_guest_memfd at guest_memfd_test.c:376
6 0x0000000000401e15: test_guest_memfd at guest_memfd_test.c:401
7 (inlined by) main at guest_memfd_test.c:491
8 0x000000000041ea03: __libc_start_call_main at libc-start.o:?
9 0x0000000000420bac: __libc_start_main_impl at ??:?
10 0x0000000000401fe0: _start at ??:?
Unexpected SIGBUS (7)

I expected the line number to be 185 but the report says 183. Not sure
if this is a compiler issue or something caused by macros, or if it's
because of signals mess with the tracking of instruction execution.

Either way, this is a very useful test feature, thanks!

Tested-by: Ackerley Tng <ackerleytng@google.com>
Reviewed-by: Ackerley Tng <ackerleytng@google.com>

> ---
> tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++++++++++++++++
> 1 file changed, 24 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index 8b60b767224b..0c3a6a40d1a9 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -2290,11 +2290,35 @@ __weak void kvm_selftest_arch_init(void)
> {
> }
>
> +static void report_unexpected_signal(int signum)
> +{
> +#define KVM_CASE_SIGNUM(sig) \
> + case sig: TEST_FAIL("Unexpected " #sig " (%d)\n", signum)
> +
> + switch (signum) {
> + KVM_CASE_SIGNUM(SIGBUS);
> + KVM_CASE_SIGNUM(SIGSEGV);
> + KVM_CASE_SIGNUM(SIGILL);
> + KVM_CASE_SIGNUM(SIGFPE);
> + default:
> + TEST_FAIL("Unexpected signal %d\n", signum);
> + }
> +}
> +
> void __attribute((constructor)) kvm_selftest_init(void)
> {
> + struct sigaction sig_sa = {
> + .sa_handler = report_unexpected_signal,
> + };
> +
> /* Tell stdout not to buffer its content. */
> setbuf(stdout, NULL);
>
> + sigaction(SIGBUS, &sig_sa, NULL);
> + sigaction(SIGSEGV, &sig_sa, NULL);
> + sigaction(SIGILL, &sig_sa, NULL);
> + sigaction(SIGFPE, &sig_sa, NULL);
> +
> guest_random_seed = last_guest_seed = random();
> pr_info("Random seed: 0x%x\n", guest_random_seed);
>
> --
> 2.51.0.710.ga91ca5db03-goog