Thomas Gleixner | 7170066 | 2019-05-19 15:51:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 2 | /* |
| 3 | * test_kprobes.c - simple sanity test for *probes |
| 4 | * |
| 5 | * Copyright IBM Corp. 2008 |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/kprobes.h> |
| 10 | #include <linux/random.h> |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 11 | #include <kunit/test.h> |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 12 | |
| 13 | #define div_factor 3 |
| 14 | |
Masami Hiramatsu | 2c7d662 | 2017-10-06 08:15:17 +0900 | [diff] [blame] | 15 | static u32 rand1, preh_val, posth_val; |
Masami Hiramatsu | 8e11440 | 2009-01-06 14:41:47 -0800 | [diff] [blame] | 16 | static u32 (*target)(u32 value); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 17 | static u32 (*target2)(u32 value); |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 18 | static struct kunit *current_test; |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 19 | |
| 20 | static noinline u32 kprobe_target(u32 value) |
| 21 | { |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 22 | return (value / div_factor); |
| 23 | } |
| 24 | |
| 25 | static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 26 | { |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 27 | KUNIT_EXPECT_FALSE(current_test, preemptible()); |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 28 | preh_val = (rand1 / div_factor); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static void kp_post_handler(struct kprobe *p, struct pt_regs *regs, |
| 33 | unsigned long flags) |
| 34 | { |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 35 | KUNIT_EXPECT_FALSE(current_test, preemptible()); |
| 36 | KUNIT_EXPECT_EQ(current_test, preh_val, (rand1 / div_factor)); |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 37 | posth_val = preh_val + div_factor; |
| 38 | } |
| 39 | |
| 40 | static struct kprobe kp = { |
| 41 | .symbol_name = "kprobe_target", |
| 42 | .pre_handler = kp_pre_handler, |
| 43 | .post_handler = kp_post_handler |
| 44 | }; |
| 45 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 46 | static void test_kprobe(struct kunit *test) |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 47 | { |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 48 | current_test = test; |
| 49 | KUNIT_EXPECT_EQ(test, 0, register_kprobe(&kp)); |
| 50 | target(rand1); |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 51 | unregister_kprobe(&kp); |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 52 | KUNIT_EXPECT_NE(test, 0, preh_val); |
| 53 | KUNIT_EXPECT_NE(test, 0, posth_val); |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 56 | static noinline u32 kprobe_target2(u32 value) |
| 57 | { |
| 58 | return (value / div_factor) + 1; |
| 59 | } |
| 60 | |
| 61 | static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs) |
| 62 | { |
| 63 | preh_val = (rand1 / div_factor) + 1; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs, |
| 68 | unsigned long flags) |
| 69 | { |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 70 | KUNIT_EXPECT_EQ(current_test, preh_val, (rand1 / div_factor) + 1); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 71 | posth_val = preh_val + div_factor; |
| 72 | } |
| 73 | |
| 74 | static struct kprobe kp2 = { |
| 75 | .symbol_name = "kprobe_target2", |
| 76 | .pre_handler = kp_pre_handler2, |
| 77 | .post_handler = kp_post_handler2 |
| 78 | }; |
| 79 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 80 | static void test_kprobes(struct kunit *test) |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 81 | { |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 82 | struct kprobe *kps[2] = {&kp, &kp2}; |
| 83 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 84 | current_test = test; |
| 85 | |
Masami Hiramatsu | fd02e6f | 2010-10-14 12:10:24 +0900 | [diff] [blame] | 86 | /* addr and flags should be cleard for reusing kprobe. */ |
| 87 | kp.addr = NULL; |
| 88 | kp.flags = 0; |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 89 | |
| 90 | KUNIT_EXPECT_EQ(test, 0, register_kprobes(kps, 2)); |
| 91 | preh_val = 0; |
| 92 | posth_val = 0; |
| 93 | target(rand1); |
| 94 | |
| 95 | KUNIT_EXPECT_NE(test, 0, preh_val); |
| 96 | KUNIT_EXPECT_NE(test, 0, posth_val); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 97 | |
| 98 | preh_val = 0; |
| 99 | posth_val = 0; |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 100 | target2(rand1); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 101 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 102 | KUNIT_EXPECT_NE(test, 0, preh_val); |
| 103 | KUNIT_EXPECT_NE(test, 0, posth_val); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 104 | unregister_kprobes(kps, 2); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 107 | #ifdef CONFIG_KRETPROBES |
| 108 | static u32 krph_val; |
| 109 | |
Abhishek Sagar | f47cd9b | 2008-02-06 01:38:22 -0800 | [diff] [blame] | 110 | static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs) |
| 111 | { |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 112 | KUNIT_EXPECT_FALSE(current_test, preemptible()); |
Abhishek Sagar | f47cd9b | 2008-02-06 01:38:22 -0800 | [diff] [blame] | 113 | krph_val = (rand1 / div_factor); |
| 114 | return 0; |
| 115 | } |
| 116 | |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 117 | static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs) |
| 118 | { |
| 119 | unsigned long ret = regs_return_value(regs); |
| 120 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 121 | KUNIT_EXPECT_FALSE(current_test, preemptible()); |
| 122 | KUNIT_EXPECT_EQ(current_test, ret, rand1 / div_factor); |
| 123 | KUNIT_EXPECT_NE(current_test, krph_val, 0); |
Abhishek Sagar | f47cd9b | 2008-02-06 01:38:22 -0800 | [diff] [blame] | 124 | krph_val = rand1; |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static struct kretprobe rp = { |
| 129 | .handler = return_handler, |
Abhishek Sagar | f47cd9b | 2008-02-06 01:38:22 -0800 | [diff] [blame] | 130 | .entry_handler = entry_handler, |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 131 | .kp.symbol_name = "kprobe_target" |
| 132 | }; |
| 133 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 134 | static void test_kretprobe(struct kunit *test) |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 135 | { |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 136 | current_test = test; |
| 137 | KUNIT_EXPECT_EQ(test, 0, register_kretprobe(&rp)); |
| 138 | target(rand1); |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 139 | unregister_kretprobe(&rp); |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 140 | KUNIT_EXPECT_EQ(test, krph_val, rand1); |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 141 | } |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 142 | |
| 143 | static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs) |
| 144 | { |
| 145 | unsigned long ret = regs_return_value(regs); |
| 146 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 147 | KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor) + 1); |
| 148 | KUNIT_EXPECT_NE(current_test, krph_val, 0); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 149 | krph_val = rand1; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static struct kretprobe rp2 = { |
| 154 | .handler = return_handler2, |
| 155 | .entry_handler = entry_handler, |
| 156 | .kp.symbol_name = "kprobe_target2" |
| 157 | }; |
| 158 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 159 | static void test_kretprobes(struct kunit *test) |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 160 | { |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 161 | struct kretprobe *rps[2] = {&rp, &rp2}; |
| 162 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 163 | current_test = test; |
Masami Hiramatsu | fd02e6f | 2010-10-14 12:10:24 +0900 | [diff] [blame] | 164 | /* addr and flags should be cleard for reusing kprobe. */ |
| 165 | rp.kp.addr = NULL; |
| 166 | rp.kp.flags = 0; |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 167 | KUNIT_EXPECT_EQ(test, 0, register_kretprobes(rps, 2)); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 168 | |
| 169 | krph_val = 0; |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 170 | target(rand1); |
| 171 | KUNIT_EXPECT_EQ(test, krph_val, rand1); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 172 | |
| 173 | krph_val = 0; |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 174 | target2(rand1); |
| 175 | KUNIT_EXPECT_EQ(test, krph_val, rand1); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 176 | unregister_kretprobes(rps, 2); |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 177 | } |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 178 | #endif /* CONFIG_KRETPROBES */ |
| 179 | |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 180 | static int kprobes_test_init(struct kunit *test) |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 181 | { |
Masami Hiramatsu | 8e11440 | 2009-01-06 14:41:47 -0800 | [diff] [blame] | 182 | target = kprobe_target; |
Masami Hiramatsu | 12da3b8 | 2009-01-06 14:41:48 -0800 | [diff] [blame] | 183 | target2 = kprobe_target2; |
Masami Hiramatsu | 8e11440 | 2009-01-06 14:41:47 -0800 | [diff] [blame] | 184 | |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 185 | do { |
Akinobu Mita | 6d65df3 | 2013-04-29 16:21:30 -0700 | [diff] [blame] | 186 | rand1 = prandom_u32(); |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 187 | } while (rand1 <= div_factor); |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 188 | return 0; |
| 189 | } |
Sven Schnelle | e44e81c | 2021-10-21 09:54:24 +0900 | [diff] [blame^] | 190 | |
| 191 | static struct kunit_case kprobes_testcases[] = { |
| 192 | KUNIT_CASE(test_kprobe), |
| 193 | KUNIT_CASE(test_kprobes), |
| 194 | #ifdef CONFIG_KRETPROBES |
| 195 | KUNIT_CASE(test_kretprobe), |
| 196 | KUNIT_CASE(test_kretprobes), |
| 197 | #endif |
| 198 | {} |
| 199 | }; |
| 200 | |
| 201 | static struct kunit_suite kprobes_test_suite = { |
| 202 | .name = "kprobes_test", |
| 203 | .init = kprobes_test_init, |
| 204 | .test_cases = kprobes_testcases, |
| 205 | }; |
| 206 | |
| 207 | kunit_test_suites(&kprobes_test_suite); |
| 208 | |
| 209 | MODULE_LICENSE("GPL"); |