1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * test_kprobes.c - simple sanity test for *probes
5 * Copyright IBM Corp. 2008
8 #include <linux/kernel.h>
9 #include <linux/kprobes.h>
10 #include <linux/random.h>
11 #include <kunit/test.h>
15 static u32 rand1, preh_val, posth_val;
16 static u32 (*target)(u32 value);
17 static u32 (*target2)(u32 value);
18 static struct kunit *current_test;
20 static unsigned long (*internal_target)(void);
21 static unsigned long (*stacktrace_target)(void);
22 static unsigned long (*stacktrace_driver)(void);
23 static unsigned long target_return_address[2];
25 static noinline u32 kprobe_target(u32 value)
27 return (value / div_factor);
30 static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
32 KUNIT_EXPECT_FALSE(current_test, preemptible());
33 preh_val = (rand1 / div_factor);
37 static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
40 KUNIT_EXPECT_FALSE(current_test, preemptible());
41 KUNIT_EXPECT_EQ(current_test, preh_val, (rand1 / div_factor));
42 posth_val = preh_val + div_factor;
45 static struct kprobe kp = {
46 .symbol_name = "kprobe_target",
47 .pre_handler = kp_pre_handler,
48 .post_handler = kp_post_handler
51 static void test_kprobe(struct kunit *test)
54 KUNIT_EXPECT_EQ(test, 0, register_kprobe(&kp));
56 unregister_kprobe(&kp);
57 KUNIT_EXPECT_NE(test, 0, preh_val);
58 KUNIT_EXPECT_NE(test, 0, posth_val);
61 static noinline u32 kprobe_target2(u32 value)
63 return (value / div_factor) + 1;
66 static noinline unsigned long kprobe_stacktrace_internal_target(void)
68 if (!target_return_address[0])
69 target_return_address[0] = (unsigned long)__builtin_return_address(0);
70 return target_return_address[0];
73 static noinline unsigned long kprobe_stacktrace_target(void)
75 if (!target_return_address[1])
76 target_return_address[1] = (unsigned long)__builtin_return_address(0);
81 return target_return_address[1];
84 static noinline unsigned long kprobe_stacktrace_driver(void)
86 if (stacktrace_target)
89 /* This is for preventing inlining the function */
90 return (unsigned long)__builtin_return_address(0);
93 static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
95 preh_val = (rand1 / div_factor) + 1;
99 static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
102 KUNIT_EXPECT_EQ(current_test, preh_val, (rand1 / div_factor) + 1);
103 posth_val = preh_val + div_factor;
106 static struct kprobe kp2 = {
107 .symbol_name = "kprobe_target2",
108 .pre_handler = kp_pre_handler2,
109 .post_handler = kp_post_handler2
112 static void test_kprobes(struct kunit *test)
114 struct kprobe *kps[2] = {&kp, &kp2};
118 /* addr and flags should be cleard for reusing kprobe. */
122 KUNIT_EXPECT_EQ(test, 0, register_kprobes(kps, 2));
127 KUNIT_EXPECT_NE(test, 0, preh_val);
128 KUNIT_EXPECT_NE(test, 0, posth_val);
134 KUNIT_EXPECT_NE(test, 0, preh_val);
135 KUNIT_EXPECT_NE(test, 0, posth_val);
136 unregister_kprobes(kps, 2);
139 #ifdef CONFIG_KRETPROBES
142 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
144 KUNIT_EXPECT_FALSE(current_test, preemptible());
145 krph_val = (rand1 / div_factor);
149 static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
151 unsigned long ret = regs_return_value(regs);
153 KUNIT_EXPECT_FALSE(current_test, preemptible());
154 KUNIT_EXPECT_EQ(current_test, ret, rand1 / div_factor);
155 KUNIT_EXPECT_NE(current_test, krph_val, 0);
160 static struct kretprobe rp = {
161 .handler = return_handler,
162 .entry_handler = entry_handler,
163 .kp.symbol_name = "kprobe_target"
166 static void test_kretprobe(struct kunit *test)
169 KUNIT_EXPECT_EQ(test, 0, register_kretprobe(&rp));
171 unregister_kretprobe(&rp);
172 KUNIT_EXPECT_EQ(test, krph_val, rand1);
175 static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
177 unsigned long ret = regs_return_value(regs);
179 KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor) + 1);
180 KUNIT_EXPECT_NE(current_test, krph_val, 0);
185 static struct kretprobe rp2 = {
186 .handler = return_handler2,
187 .entry_handler = entry_handler,
188 .kp.symbol_name = "kprobe_target2"
191 static void test_kretprobes(struct kunit *test)
193 struct kretprobe *rps[2] = {&rp, &rp2};
196 /* addr and flags should be cleard for reusing kprobe. */
199 KUNIT_EXPECT_EQ(test, 0, register_kretprobes(rps, 2));
203 KUNIT_EXPECT_EQ(test, krph_val, rand1);
207 KUNIT_EXPECT_EQ(test, krph_val, rand1);
208 unregister_kretprobes(rps, 2);
211 #ifdef CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE
212 #define STACK_BUF_SIZE 16
213 static unsigned long stack_buf[STACK_BUF_SIZE];
215 static int stacktrace_return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
217 unsigned long retval = regs_return_value(regs);
220 KUNIT_EXPECT_FALSE(current_test, preemptible());
221 KUNIT_EXPECT_EQ(current_test, retval, target_return_address[1]);
224 * Test stacktrace inside the kretprobe handler, this will involves
225 * kretprobe trampoline, but must include correct return address
226 * of the target function.
228 ret = stack_trace_save(stack_buf, STACK_BUF_SIZE, 0);
229 KUNIT_EXPECT_NE(current_test, ret, 0);
231 for (i = 0; i < ret; i++) {
232 if (stack_buf[i] == target_return_address[1])
235 KUNIT_EXPECT_NE(current_test, i, ret);
237 #if !IS_MODULE(CONFIG_KPROBES_SANITY_TEST)
239 * Test stacktrace from pt_regs at the return address. Thus the stack
240 * trace must start from the target return address.
242 ret = stack_trace_save_regs(regs, stack_buf, STACK_BUF_SIZE, 0);
243 KUNIT_EXPECT_NE(current_test, ret, 0);
244 KUNIT_EXPECT_EQ(current_test, stack_buf[0], target_return_address[1]);
250 static struct kretprobe rp3 = {
251 .handler = stacktrace_return_handler,
252 .kp.symbol_name = "kprobe_stacktrace_target"
255 static void test_stacktrace_on_kretprobe(struct kunit *test)
257 unsigned long myretaddr = (unsigned long)__builtin_return_address(0);
264 * Run the stacktrace_driver() to record correct return address in
265 * stacktrace_target() and ensure stacktrace_driver() call is not
266 * inlined by checking the return address of stacktrace_driver()
267 * and the return address of this function is different.
269 KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
271 KUNIT_ASSERT_EQ(test, 0, register_kretprobe(&rp3));
272 KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
273 unregister_kretprobe(&rp3);
276 static int stacktrace_internal_return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
278 unsigned long retval = regs_return_value(regs);
281 KUNIT_EXPECT_FALSE(current_test, preemptible());
282 KUNIT_EXPECT_EQ(current_test, retval, target_return_address[0]);
285 * Test stacktrace inside the kretprobe handler for nested case.
286 * The unwinder will find the kretprobe_trampoline address on the
287 * return address, and kretprobe must solve that.
289 ret = stack_trace_save(stack_buf, STACK_BUF_SIZE, 0);
290 KUNIT_EXPECT_NE(current_test, ret, 0);
292 for (i = 0; i < ret - 1; i++) {
293 if (stack_buf[i] == target_return_address[0]) {
294 KUNIT_EXPECT_EQ(current_test, stack_buf[i + 1], target_return_address[1]);
298 KUNIT_EXPECT_NE(current_test, i, ret);
300 #if !IS_MODULE(CONFIG_KPROBES_SANITY_TEST)
301 /* Ditto for the regs version. */
302 ret = stack_trace_save_regs(regs, stack_buf, STACK_BUF_SIZE, 0);
303 KUNIT_EXPECT_NE(current_test, ret, 0);
304 KUNIT_EXPECT_EQ(current_test, stack_buf[0], target_return_address[0]);
305 KUNIT_EXPECT_EQ(current_test, stack_buf[1], target_return_address[1]);
311 static struct kretprobe rp4 = {
312 .handler = stacktrace_internal_return_handler,
313 .kp.symbol_name = "kprobe_stacktrace_internal_target"
316 static void test_stacktrace_on_nested_kretprobe(struct kunit *test)
318 unsigned long myretaddr = (unsigned long)__builtin_return_address(0);
319 struct kretprobe *rps[2] = {&rp3, &rp4};
325 //KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
327 KUNIT_ASSERT_EQ(test, 0, register_kretprobes(rps, 2));
328 KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
329 unregister_kretprobes(rps, 2);
331 #endif /* CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE */
333 #endif /* CONFIG_KRETPROBES */
335 static int kprobes_test_init(struct kunit *test)
337 target = kprobe_target;
338 target2 = kprobe_target2;
339 stacktrace_target = kprobe_stacktrace_target;
340 internal_target = kprobe_stacktrace_internal_target;
341 stacktrace_driver = kprobe_stacktrace_driver;
344 rand1 = prandom_u32();
345 } while (rand1 <= div_factor);
349 static struct kunit_case kprobes_testcases[] = {
350 KUNIT_CASE(test_kprobe),
351 KUNIT_CASE(test_kprobes),
352 #ifdef CONFIG_KRETPROBES
353 KUNIT_CASE(test_kretprobe),
354 KUNIT_CASE(test_kretprobes),
355 #ifdef CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE
356 KUNIT_CASE(test_stacktrace_on_kretprobe),
357 KUNIT_CASE(test_stacktrace_on_nested_kretprobe),
363 static struct kunit_suite kprobes_test_suite = {
364 .name = "kprobes_test",
365 .init = kprobes_test_init,
366 .test_cases = kprobes_testcases,
369 kunit_test_suites(&kprobes_test_suite);
371 MODULE_LICENSE("GPL");