blob: dd53e354f630d9251de443d6a9cd6632c3646211 [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
Masami Hiramatsu2c7d6622017-10-06 08:15:17 +0900165#if 0
166static u32 jph_val;
167
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100168static u32 j_kprobe_target(u32 value)
169{
Masami Hiramatsu3539d092017-09-19 18:59:00 +0900170 if (preemptible()) {
171 handler_errors++;
172 pr_err("jprobe-handler is preemptible\n");
173 }
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100174 if (value != rand1) {
175 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700176 pr_err("incorrect value in jprobe handler\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100177 }
178
179 jph_val = rand1;
180 jprobe_return();
181 return 0;
182}
183
184static struct jprobe jp = {
185 .entry = j_kprobe_target,
186 .kp.symbol_name = "kprobe_target"
187};
188
189static int test_jprobe(void)
190{
191 int ret;
192
193 ret = register_jprobe(&jp);
194 if (ret < 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700195 pr_err("register_jprobe returned %d\n", ret);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100196 return ret;
197 }
198
Masami Hiramatsu8e114402009-01-06 14:41:47 -0800199 ret = target(rand1);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100200 unregister_jprobe(&jp);
201 if (jph_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700202 pr_err("jprobe handler not called\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100203 handler_errors++;
204 }
205
206 return 0;
207}
208
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800209static struct jprobe jp2 = {
210 .entry = j_kprobe_target,
211 .kp.symbol_name = "kprobe_target2"
212};
213
214static int test_jprobes(void)
215{
216 int ret;
217 struct jprobe *jps[2] = {&jp, &jp2};
218
Masami Hiramatsufd02e6f2010-10-14 12:10:24 +0900219 /* addr and flags should be cleard for reusing kprobe. */
220 jp.kp.addr = NULL;
221 jp.kp.flags = 0;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800222 ret = register_jprobes(jps, 2);
223 if (ret < 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700224 pr_err("register_jprobes returned %d\n", ret);
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800225 return ret;
226 }
227
228 jph_val = 0;
229 ret = target(rand1);
230 if (jph_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700231 pr_err("jprobe handler not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800232 handler_errors++;
233 }
234
235 jph_val = 0;
236 ret = target2(rand1);
237 if (jph_val == 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700238 pr_err("jprobe handler2 not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800239 handler_errors++;
240 }
241 unregister_jprobes(jps, 2);
242
243 return 0;
244}
Masami Hiramatsu2c7d6622017-10-06 08:15:17 +0900245#else
246#define test_jprobe() (0)
247#define test_jprobes() (0)
248#endif
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100249#ifdef CONFIG_KRETPROBES
250static u32 krph_val;
251
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800252static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
253{
Masami Hiramatsu3539d092017-09-19 18:59:00 +0900254 if (preemptible()) {
255 handler_errors++;
256 pr_err("kretprobe entry handler is preemptible\n");
257 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800258 krph_val = (rand1 / div_factor);
259 return 0;
260}
261
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100262static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
263{
264 unsigned long ret = regs_return_value(regs);
265
Masami Hiramatsu3539d092017-09-19 18:59:00 +0900266 if (preemptible()) {
267 handler_errors++;
268 pr_err("kretprobe return handler is preemptible\n");
269 }
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100270 if (ret != (rand1 / div_factor)) {
271 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700272 pr_err("incorrect value in kretprobe handler\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100273 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800274 if (krph_val == 0) {
275 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700276 pr_err("call to kretprobe entry handler failed\n");
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800277 }
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100278
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800279 krph_val = rand1;
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100280 return 0;
281}
282
283static struct kretprobe rp = {
284 .handler = return_handler,
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800285 .entry_handler = entry_handler,
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100286 .kp.symbol_name = "kprobe_target"
287};
288
289static int test_kretprobe(void)
290{
291 int ret;
292
293 ret = register_kretprobe(&rp);
294 if (ret < 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700295 pr_err("register_kretprobe returned %d\n", ret);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100296 return ret;
297 }
298
Masami Hiramatsu8e114402009-01-06 14:41:47 -0800299 ret = target(rand1);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100300 unregister_kretprobe(&rp);
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800301 if (krph_val != rand1) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700302 pr_err("kretprobe handler not called\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100303 handler_errors++;
304 }
305
306 return 0;
307}
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800308
309static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
310{
311 unsigned long ret = regs_return_value(regs);
312
313 if (ret != (rand1 / div_factor) + 1) {
314 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700315 pr_err("incorrect value in kretprobe handler2\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800316 }
317 if (krph_val == 0) {
318 handler_errors++;
Fabian Frederick4878b142014-08-08 14:19:48 -0700319 pr_err("call to kretprobe entry handler failed\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800320 }
321
322 krph_val = rand1;
323 return 0;
324}
325
326static struct kretprobe rp2 = {
327 .handler = return_handler2,
328 .entry_handler = entry_handler,
329 .kp.symbol_name = "kprobe_target2"
330};
331
332static int test_kretprobes(void)
333{
334 int ret;
335 struct kretprobe *rps[2] = {&rp, &rp2};
336
Masami Hiramatsufd02e6f2010-10-14 12:10:24 +0900337 /* addr and flags should be cleard for reusing kprobe. */
338 rp.kp.addr = NULL;
339 rp.kp.flags = 0;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800340 ret = register_kretprobes(rps, 2);
341 if (ret < 0) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700342 pr_err("register_kretprobe returned %d\n", ret);
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800343 return ret;
344 }
345
346 krph_val = 0;
347 ret = target(rand1);
348 if (krph_val != rand1) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700349 pr_err("kretprobe handler not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800350 handler_errors++;
351 }
352
353 krph_val = 0;
354 ret = target2(rand1);
355 if (krph_val != rand1) {
Fabian Frederick4878b142014-08-08 14:19:48 -0700356 pr_err("kretprobe handler2 not called\n");
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800357 handler_errors++;
358 }
359 unregister_kretprobes(rps, 2);
360 return 0;
361}
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100362#endif /* CONFIG_KRETPROBES */
363
364int init_test_probes(void)
365{
366 int ret;
367
Masami Hiramatsu8e114402009-01-06 14:41:47 -0800368 target = kprobe_target;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800369 target2 = kprobe_target2;
Masami Hiramatsu8e114402009-01-06 14:41:47 -0800370
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100371 do {
Akinobu Mita6d65df32013-04-29 16:21:30 -0700372 rand1 = prandom_u32();
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100373 } while (rand1 <= div_factor);
374
Fabian Frederick4878b142014-08-08 14:19:48 -0700375 pr_info("started\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100376 num_tests++;
377 ret = test_kprobe();
378 if (ret < 0)
379 errors++;
380
381 num_tests++;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800382 ret = test_kprobes();
383 if (ret < 0)
384 errors++;
385
386 num_tests++;
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100387 ret = test_jprobe();
388 if (ret < 0)
389 errors++;
390
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800391 num_tests++;
392 ret = test_jprobes();
393 if (ret < 0)
394 errors++;
395
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100396#ifdef CONFIG_KRETPROBES
397 num_tests++;
398 ret = test_kretprobe();
399 if (ret < 0)
400 errors++;
Masami Hiramatsu12da3b82009-01-06 14:41:48 -0800401
402 num_tests++;
403 ret = test_kretprobes();
404 if (ret < 0)
405 errors++;
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100406#endif /* CONFIG_KRETPROBES */
407
408 if (errors)
Fabian Frederick4878b142014-08-08 14:19:48 -0700409 pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100410 else if (handler_errors)
Fabian Frederick4878b142014-08-08 14:19:48 -0700411 pr_err("BUG: %d error(s) running handlers\n", handler_errors);
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100412 else
Fabian Frederick4878b142014-08-08 14:19:48 -0700413 pr_info("passed successfully\n");
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100414
415 return 0;
416}