blob: 7bca480151b02417d114747494e86e4ee2b1eac8 [file] [log] [blame]
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01001/*
2 * test_kprobes.c - simple sanity test for *probes
3 *
4 * Copyright IBM Corp. 2008
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 */
16
Fabian Frederick4878b142014-08-08 14:19:48 -070017#define pr_fmt(fmt) "Kprobe smoke test: " fmt
18
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010019#include <linux/kernel.h>
20#include <linux/kprobes.h>
21#include <linux/random.h>
22
23#define div_factor 3
24
Masami Hiramatsu2c7d6622017-10-06 08:15:17 +090025static u32 rand1, preh_val, posth_val;
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010026static int errors, handler_errors, num_tests;
Masami Hiramatsu8e114402009-01-06 14:41:47 -080027static u32 (*target)(u32 value);
Masami Hiramatsu12da3b82009-01-06 14:41:48 -080028static u32 (*target2)(u32 value);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010029
30static noinline u32 kprobe_target(u32 value)
31{
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010032 return (value / div_factor);
33}
34
35static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
36{
Masami Hiramatsu3539d092017-09-19 18:59:00 +090037 if (preemptible()) {
38 handler_errors++;
39 pr_err("pre-handler is preemptible\n");
40 }
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010041 preh_val = (rand1 / div_factor);
42 return 0;
43}
44
45static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
46 unsigned long flags)
47{
Masami Hiramatsu3539d092017-09-19 18:59:00 +090048 if (preemptible()) {
49 handler_errors++;
50 pr_err("post-handler is preemptible\n");
51 }
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010052 if (preh_val != (rand1 / div_factor)) {
53 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -070054 pr_err("incorrect value in post_handler\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010055 }
56 posth_val = preh_val + div_factor;
57}
58
59static struct kprobe kp = {
60 .symbol_name = "kprobe_target",
61 .pre_handler = kp_pre_handler,
62 .post_handler = kp_post_handler
63};
64
65static int test_kprobe(void)
66{
67 int ret;
68
69 ret = register_kprobe(&kp);
70 if (ret < 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -070071 pr_err("register_kprobe returned %d\n", ret);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010072 return ret;
73 }
74
Masami Hiramatsu8e114402009-01-06 14:41:47 -080075 ret = target(rand1);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010076 unregister_kprobe(&kp);
77
78 if (preh_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -070079 pr_err("kprobe pre_handler not called\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010080 handler_errors++;
81 }
82
83 if (posth_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -070084 pr_err("kprobe post_handler not called\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +010085 handler_errors++;
86 }
87
88 return 0;
89}
90
Masami Hiramatsu12da3b82009-01-06 14:41:48 -080091static noinline u32 kprobe_target2(u32 value)
92{
93 return (value / div_factor) + 1;
94}
95
96static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
97{
98 preh_val = (rand1 / div_factor) + 1;
99 return 0;
100}
101
102static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
103 unsigned long flags)
104{
105 if (preh_val != (rand1 / div_factor) + 1) {
106 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700107 pr_err("incorrect value in post_handler2\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800108 }
109 posth_val = preh_val + div_factor;
110}
111
112static struct kprobe kp2 = {
113 .symbol_name = "kprobe_target2",
114 .pre_handler = kp_pre_handler2,
115 .post_handler = kp_post_handler2
116};
117
118static int test_kprobes(void)
119{
120 int ret;
121 struct kprobe *kps[2] = {&kp, &kp2};
122
Masami Hiramatsufd02e6f2010-10-14 12:10:24 +0900123 /* addr and flags should be cleard for reusing kprobe. */
124 kp.addr = NULL;
125 kp.flags = 0;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800126 ret = register_kprobes(kps, 2);
127 if (ret < 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700128 pr_err("register_kprobes returned %d\n", ret);
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800129 return ret;
130 }
131
132 preh_val = 0;
133 posth_val = 0;
134 ret = target(rand1);
135
136 if (preh_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700137 pr_err("kprobe pre_handler not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800138 handler_errors++;
139 }
140
141 if (posth_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700142 pr_err("kprobe post_handler not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800143 handler_errors++;
144 }
145
146 preh_val = 0;
147 posth_val = 0;
148 ret = target2(rand1);
149
150 if (preh_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700151 pr_err("kprobe pre_handler2 not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800152 handler_errors++;
153 }
154
155 if (posth_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700156 pr_err("kprobe post_handler2 not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800157 handler_errors++;
158 }
159
160 unregister_kprobes(kps, 2);
161 return 0;
162
163}
164
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100165#ifdef CONFIG_KRETPROBES
166static u32 krph_val;
167
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800168static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
169{
Masami Hiramatsu3539d092017-09-19 18:59:00 +0900170 if (preemptible()) {
171 handler_errors++;
172 pr_err("kretprobe entry handler is preemptible\n");
173 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800174 krph_val = (rand1 / div_factor);
175 return 0;
176}
177
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100178static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
179{
180 unsigned long ret = regs_return_value(regs);
181
Masami Hiramatsu3539d092017-09-19 18:59:00 +0900182 if (preemptible()) {
183 handler_errors++;
184 pr_err("kretprobe return handler is preemptible\n");
185 }
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100186 if (ret != (rand1 / div_factor)) {
187 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700188 pr_err("incorrect value in kretprobe handler\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100189 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800190 if (krph_val == 0) {
191 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700192 pr_err("call to kretprobe entry handler failed\n");
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800193 }
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100194
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800195 krph_val = rand1;
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100196 return 0;
197}
198
199static struct kretprobe rp = {
200 .handler = return_handler,
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800201 .entry_handler = entry_handler,
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100202 .kp.symbol_name = "kprobe_target"
203};
204
205static int test_kretprobe(void)
206{
207 int ret;
208
209 ret = register_kretprobe(&rp);
210 if (ret < 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700211 pr_err("register_kretprobe returned %d\n", ret);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100212 return ret;
213 }
214
Masami Hiramatsu8e114402009-01-06 14:41:47 -0800215 ret = target(rand1);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100216 unregister_kretprobe(&rp);
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800217 if (krph_val != rand1) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700218 pr_err("kretprobe handler not called\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100219 handler_errors++;
220 }
221
222 return 0;
223}
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800224
225static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
226{
227 unsigned long ret = regs_return_value(regs);
228
229 if (ret != (rand1 / div_factor) + 1) {
230 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700231 pr_err("incorrect value in kretprobe handler2\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800232 }
233 if (krph_val == 0) {
234 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700235 pr_err("call to kretprobe entry handler failed\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800236 }
237
238 krph_val = rand1;
239 return 0;
240}
241
242static struct kretprobe rp2 = {
243 .handler = return_handler2,
244 .entry_handler = entry_handler,
245 .kp.symbol_name = "kprobe_target2"
246};
247
248static int test_kretprobes(void)
249{
250 int ret;
251 struct kretprobe *rps[2] = {&rp, &rp2};
252
Masami Hiramatsufd02e6f2010-10-14 12:10:24 +0900253 /* addr and flags should be cleard for reusing kprobe. */
254 rp.kp.addr = NULL;
255 rp.kp.flags = 0;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800256 ret = register_kretprobes(rps, 2);
257 if (ret < 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700258 pr_err("register_kretprobe returned %d\n", ret);
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800259 return ret;
260 }
261
262 krph_val = 0;
263 ret = target(rand1);
264 if (krph_val != rand1) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700265 pr_err("kretprobe handler not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800266 handler_errors++;
267 }
268
269 krph_val = 0;
270 ret = target2(rand1);
271 if (krph_val != rand1) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700272 pr_err("kretprobe handler2 not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800273 handler_errors++;
274 }
275 unregister_kretprobes(rps, 2);
276 return 0;
277}
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100278#endif /* CONFIG_KRETPROBES */
279
280int init_test_probes(void)
281{
282 int ret;
283
Masami Hiramatsu8e114402009-01-06 14:41:47 -0800284 target = kprobe_target;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800285 target2 = kprobe_target2;
Masami Hiramatsu8e114402009-01-06 14:41:47 -0800286
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100287 do {
Akinobu Mita6d65df32013-04-29 16:21:30 -0700288 rand1 = prandom_u32();
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100289 } while (rand1 <= div_factor);
290
Fabian Frederick4878b142014-08-08 14:19:48 -0700291 pr_info("started\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100292 num_tests++;
293 ret = test_kprobe();
294 if (ret < 0)
295 errors++;
296
297 num_tests++;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800298 ret = test_kprobes();
299 if (ret < 0)
300 errors++;
301
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100302#ifdef CONFIG_KRETPROBES
303 num_tests++;
304 ret = test_kretprobe();
305 if (ret < 0)
306 errors++;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800307
308 num_tests++;
309 ret = test_kretprobes();
310 if (ret < 0)
311 errors++;
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100312#endif /* CONFIG_KRETPROBES */
313
314 if (errors)
Fabian Frederick4878b142014-08-08 14:19:48 -0700315 pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100316 else if (handler_errors)
Fabian Frederick4878b142014-08-08 14:19:48 -0700317 pr_err("BUG: %d error(s) running handlers\n", handler_errors);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100318 else
Fabian Frederick4878b142014-08-08 14:19:48 -0700319 pr_info("passed successfully\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100320
321 return 0;
322}