Skip to content

Commit f91187c

Browse files
committed
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>
1 parent 505c953 commit f91187c

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <linux/bitmap.h>
1515
#include <linux/falloc.h>
1616
#include <linux/sizes.h>
17-
#include <setjmp.h>
18-
#include <signal.h>
1917
#include <sys/mman.h>
2018
#include <sys/types.h>
2119
#include <sys/stat.h>
@@ -77,30 +75,16 @@ static void test_mmap_supported(int fd, size_t total_size)
7775
kvm_munmap(mem, total_size);
7876
}
7977

80-
static sigjmp_buf jmpbuf;
81-
void fault_sigbus_handler(int signum)
82-
{
83-
siglongjmp(jmpbuf, 1);
84-
}
85-
8678
static void test_fault_overflow(int fd, size_t total_size)
8779
{
88-
struct sigaction sa_old, sa_new = {
89-
.sa_handler = fault_sigbus_handler,
90-
};
9180
size_t map_size = total_size * 4;
9281
const char val = 0xaa;
9382
char *mem;
9483
size_t i;
9584

9685
mem = kvm_mmap(map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd);
9786

98-
sigaction(SIGBUS, &sa_new, &sa_old);
99-
if (sigsetjmp(jmpbuf, 1) == 0) {
100-
memset(mem, 0xaa, map_size);
101-
TEST_ASSERT(false, "memset() should have triggered SIGBUS.");
102-
}
103-
sigaction(SIGBUS, &sa_old, NULL);
87+
TEST_EXPECT_SIGBUS(memset(mem, val, map_size));
10488

10589
for (i = 0; i < total_size; i++)
10690
TEST_ASSERT_EQ(READ_ONCE(mem[i]), val);

tools/testing/selftests/kvm/include/test_util.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef SELFTEST_KVM_TEST_UTIL_H
99
#define SELFTEST_KVM_TEST_UTIL_H
1010

11+
#include <setjmp.h>
12+
#include <signal.h>
1113
#include <stdlib.h>
1214
#include <stdarg.h>
1315
#include <stdbool.h>
@@ -78,6 +80,23 @@ do { \
7880
__builtin_unreachable(); \
7981
} while (0)
8082

83+
extern sigjmp_buf expect_sigbus_jmpbuf;
84+
void expect_sigbus_handler(int signum);
85+
86+
#define TEST_EXPECT_SIGBUS(action) \
87+
do { \
88+
struct sigaction sa_old, sa_new = { \
89+
.sa_handler = expect_sigbus_handler, \
90+
}; \
91+
\
92+
sigaction(SIGBUS, &sa_new, &sa_old); \
93+
if (sigsetjmp(expect_sigbus_jmpbuf, 1) == 0) { \
94+
action; \
95+
TEST_FAIL("'%s' should have triggered SIGBUS", #action); \
96+
} \
97+
sigaction(SIGBUS, &sa_old, NULL); \
98+
} while (0)
99+
81100
size_t parse_size(const char *size);
82101

83102
int64_t timespec_to_ns(struct timespec ts);

tools/testing/selftests/kvm/lib/test_util.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
#include "test_util.h"
2020

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+
2128
/*
2229
* Random number generator that is usable from guest code. This is the
2330
* Park-Miller LCG using standard constants.

0 commit comments

Comments
 (0)