]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - kernel/test_kprobes.c
2 * test_kprobes.c - simple sanity test for *probes
4 * Copyright IBM Corp. 2008
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.
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.
17 #define pr_fmt(fmt) "Kprobe smoke test: " fmt
19 #include <linux/kernel.h>
20 #include <linux/kprobes.h>
21 #include <linux/random.h>
25 static u32 rand1
, preh_val
, posth_val
, jph_val
;
26 static int errors
, handler_errors
, num_tests
;
27 static u32 (*target
)(u32 value
);
28 static u32 (*target2
)(u32 value
);
30 static noinline u32
kprobe_target(u32 value
)
32 return (value
/ div_factor
);
35 static int kp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
37 preh_val
= (rand1
/ div_factor
);
41 static void kp_post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
44 if (preh_val
!= (rand1
/ div_factor
)) {
46 pr_err("incorrect value in post_handler\n");
48 posth_val
= preh_val
+ div_factor
;
51 static struct kprobe kp
= {
52 .symbol_name
= "kprobe_target",
53 .pre_handler
= kp_pre_handler
,
54 .post_handler
= kp_post_handler
57 static int test_kprobe(void)
61 ret
= register_kprobe(&kp
);
63 pr_err("register_kprobe returned %d\n", ret
);
68 unregister_kprobe(&kp
);
71 pr_err("kprobe pre_handler not called\n");
76 pr_err("kprobe post_handler not called\n");
83 static noinline u32
kprobe_target2(u32 value
)
85 return (value
/ div_factor
) + 1;
88 static int kp_pre_handler2(struct kprobe
*p
, struct pt_regs
*regs
)
90 preh_val
= (rand1
/ div_factor
) + 1;
94 static void kp_post_handler2(struct kprobe
*p
, struct pt_regs
*regs
,
97 if (preh_val
!= (rand1
/ div_factor
) + 1) {
99 pr_err("incorrect value in post_handler2\n");
101 posth_val
= preh_val
+ div_factor
;
104 static struct kprobe kp2
= {
105 .symbol_name
= "kprobe_target2",
106 .pre_handler
= kp_pre_handler2
,
107 .post_handler
= kp_post_handler2
110 static int test_kprobes(void)
113 struct kprobe
*kps
[2] = {&kp
, &kp2
};
115 /* addr and flags should be cleard for reusing kprobe. */
118 ret
= register_kprobes(kps
, 2);
120 pr_err("register_kprobes returned %d\n", ret
);
129 pr_err("kprobe pre_handler not called\n");
133 if (posth_val
== 0) {
134 pr_err("kprobe post_handler not called\n");
140 ret
= target2(rand1
);
143 pr_err("kprobe pre_handler2 not called\n");
147 if (posth_val
== 0) {
148 pr_err("kprobe post_handler2 not called\n");
152 unregister_kprobes(kps
, 2);
157 static u32
j_kprobe_target(u32 value
)
159 if (value
!= rand1
) {
161 pr_err("incorrect value in jprobe handler\n");
169 static struct jprobe jp
= {
170 .entry
= j_kprobe_target
,
171 .kp
.symbol_name
= "kprobe_target"
174 static int test_jprobe(void)
178 ret
= register_jprobe(&jp
);
180 pr_err("register_jprobe returned %d\n", ret
);
185 unregister_jprobe(&jp
);
187 pr_err("jprobe handler not called\n");
194 static struct jprobe jp2
= {
195 .entry
= j_kprobe_target
,
196 .kp
.symbol_name
= "kprobe_target2"
199 static int test_jprobes(void)
202 struct jprobe
*jps
[2] = {&jp
, &jp2
};
204 /* addr and flags should be cleard for reusing kprobe. */
207 ret
= register_jprobes(jps
, 2);
209 pr_err("register_jprobes returned %d\n", ret
);
216 pr_err("jprobe handler not called\n");
221 ret
= target2(rand1
);
223 pr_err("jprobe handler2 not called\n");
226 unregister_jprobes(jps
, 2);
230 #ifdef CONFIG_KRETPROBES
233 static int entry_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
235 krph_val
= (rand1
/ div_factor
);
239 static int return_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
241 unsigned long ret
= regs_return_value(regs
);
243 if (ret
!= (rand1
/ div_factor
)) {
245 pr_err("incorrect value in kretprobe handler\n");
249 pr_err("call to kretprobe entry handler failed\n");
256 static struct kretprobe rp
= {
257 .handler
= return_handler
,
258 .entry_handler
= entry_handler
,
259 .kp
.symbol_name
= "kprobe_target"
262 static int test_kretprobe(void)
266 ret
= register_kretprobe(&rp
);
268 pr_err("register_kretprobe returned %d\n", ret
);
273 unregister_kretprobe(&rp
);
274 if (krph_val
!= rand1
) {
275 pr_err("kretprobe handler not called\n");
282 static int return_handler2(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
284 unsigned long ret
= regs_return_value(regs
);
286 if (ret
!= (rand1
/ div_factor
) + 1) {
288 pr_err("incorrect value in kretprobe handler2\n");
292 pr_err("call to kretprobe entry handler failed\n");
299 static struct kretprobe rp2
= {
300 .handler
= return_handler2
,
301 .entry_handler
= entry_handler
,
302 .kp
.symbol_name
= "kprobe_target2"
305 static int test_kretprobes(void)
308 struct kretprobe
*rps
[2] = {&rp
, &rp2
};
310 /* addr and flags should be cleard for reusing kprobe. */
313 ret
= register_kretprobes(rps
, 2);
315 pr_err("register_kretprobe returned %d\n", ret
);
321 if (krph_val
!= rand1
) {
322 pr_err("kretprobe handler not called\n");
327 ret
= target2(rand1
);
328 if (krph_val
!= rand1
) {
329 pr_err("kretprobe handler2 not called\n");
332 unregister_kretprobes(rps
, 2);
335 #endif /* CONFIG_KRETPROBES */
337 int init_test_probes(void)
341 target
= kprobe_target
;
342 target2
= kprobe_target2
;
345 rand1
= prandom_u32();
346 } while (rand1
<= div_factor
);
348 pr_info("started\n");
355 ret
= test_kprobes();
365 ret
= test_jprobes();
369 #ifdef CONFIG_KRETPROBES
371 ret
= test_kretprobe();
376 ret
= test_kretprobes();
379 #endif /* CONFIG_KRETPROBES */
382 pr_err("BUG: %d out of %d tests failed\n", errors
, num_tests
);
383 else if (handler_errors
)
384 pr_err("BUG: %d error(s) running handlers\n", handler_errors
);
386 pr_info("passed successfully\n");