Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Add wrapper macro to handle and assert on expected SIGBUS

Extract the guest_memfd test's SIGBUS handling functionality into a common
TEST_EXPECT_SIGBUS() macro in anticipation of adding more SIGBUS testcases.
Eating a SIGBUS isn't terrible difficult, but it requires a non-trivial
amount of boilerplate code, and using a macro allows selftests to print
out the exact action that failed to generate a SIGBUS without the developer
needing to remember to add a useful error message.

Explicitly mark the SIGBUS handler as "used", as gcc-14 at least likes to
discard the function before linking.

Opportunistically use TEST_FAIL(...) instead of TEST_ASSERT(false, ...),
and fix the write path of the guest_memfd test to use the local "val"
instead of hardcoding the literal value a second time.

Suggested-by: Ackerley Tng <ackerleytng@google.com>
Reviewed-by: Ackerley Tng <ackerleytng@google.com>
Tested-by: Ackerley Tng <ackerleytng@google.com>
Reviewed-by: Lisa Wang <wyihan@google.com>
Tested-by: Lisa Wang <wyihan@google.com>
Link: https://lore.kernel.org/r/20251003232606.4070510-12-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

+27 -17
+1 -17
tools/testing/selftests/kvm/guest_memfd_test.c
··· 14 14 #include <linux/bitmap.h> 15 15 #include <linux/falloc.h> 16 16 #include <linux/sizes.h> 17 - #include <setjmp.h> 18 - #include <signal.h> 19 17 #include <sys/mman.h> 20 18 #include <sys/types.h> 21 19 #include <sys/stat.h> ··· 75 77 kvm_munmap(mem, total_size); 76 78 } 77 79 78 - static sigjmp_buf jmpbuf; 79 - void fault_sigbus_handler(int signum) 80 - { 81 - siglongjmp(jmpbuf, 1); 82 - } 83 - 84 80 static void test_fault_overflow(int fd, size_t total_size) 85 81 { 86 - struct sigaction sa_old, sa_new = { 87 - .sa_handler = fault_sigbus_handler, 88 - }; 89 82 size_t map_size = total_size * 4; 90 83 const char val = 0xaa; 91 84 char *mem; ··· 84 95 85 96 mem = kvm_mmap(map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd); 86 97 87 - sigaction(SIGBUS, &sa_new, &sa_old); 88 - if (sigsetjmp(jmpbuf, 1) == 0) { 89 - memset(mem, 0xaa, map_size); 90 - TEST_ASSERT(false, "memset() should have triggered SIGBUS."); 91 - } 92 - sigaction(SIGBUS, &sa_old, NULL); 98 + TEST_EXPECT_SIGBUS(memset(mem, val, map_size)); 93 99 94 100 for (i = 0; i < total_size; i++) 95 101 TEST_ASSERT_EQ(READ_ONCE(mem[i]), val);
+19
tools/testing/selftests/kvm/include/test_util.h
··· 8 8 #ifndef SELFTEST_KVM_TEST_UTIL_H 9 9 #define SELFTEST_KVM_TEST_UTIL_H 10 10 11 + #include <setjmp.h> 12 + #include <signal.h> 11 13 #include <stdlib.h> 12 14 #include <stdarg.h> 13 15 #include <stdbool.h> ··· 78 76 #define TEST_FAIL(fmt, ...) do { \ 79 77 TEST_ASSERT(false, fmt, ##__VA_ARGS__); \ 80 78 __builtin_unreachable(); \ 79 + } while (0) 80 + 81 + extern sigjmp_buf expect_sigbus_jmpbuf; 82 + void expect_sigbus_handler(int signum); 83 + 84 + #define TEST_EXPECT_SIGBUS(action) \ 85 + do { \ 86 + struct sigaction sa_old, sa_new = { \ 87 + .sa_handler = expect_sigbus_handler, \ 88 + }; \ 89 + \ 90 + sigaction(SIGBUS, &sa_new, &sa_old); \ 91 + if (sigsetjmp(expect_sigbus_jmpbuf, 1) == 0) { \ 92 + action; \ 93 + TEST_FAIL("'%s' should have triggered SIGBUS", #action); \ 94 + } \ 95 + sigaction(SIGBUS, &sa_old, NULL); \ 81 96 } while (0) 82 97 83 98 size_t parse_size(const char *size);
+7
tools/testing/selftests/kvm/lib/test_util.c
··· 18 18 19 19 #include "test_util.h" 20 20 21 + sigjmp_buf expect_sigbus_jmpbuf; 22 + 23 + void __attribute__((used)) expect_sigbus_handler(int signum) 24 + { 25 + siglongjmp(expect_sigbus_jmpbuf, 1); 26 + } 27 + 21 28 /* 22 29 * Random number generator that is usable from guest code. This is the 23 30 * Park-Miller LCG using standard constants.