Back to home page

LXR

 
 

    


0001 /*
0002  * test_kprobes.c - simple sanity test for *probes
0003  *
0004  * Copyright IBM Corp. 2008
0005  *
0006  * This program is free software;  you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it would be useful, but
0012  * WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
0014  * the GNU General Public License for more details.
0015  */
0016 
0017 #define pr_fmt(fmt) "Kprobe smoke test: " fmt
0018 
0019 #include <linux/kernel.h>
0020 #include <linux/kprobes.h>
0021 #include <linux/random.h>
0022 
0023 #define div_factor 3
0024 
0025 static u32 rand1, preh_val, posth_val, jph_val;
0026 static int errors, handler_errors, num_tests;
0027 static u32 (*target)(u32 value);
0028 static u32 (*target2)(u32 value);
0029 
0030 static noinline u32 kprobe_target(u32 value)
0031 {
0032     return (value / div_factor);
0033 }
0034 
0035 static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
0036 {
0037     preh_val = (rand1 / div_factor);
0038     return 0;
0039 }
0040 
0041 static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
0042         unsigned long flags)
0043 {
0044     if (preh_val != (rand1 / div_factor)) {
0045         handler_errors++;
0046         pr_err("incorrect value in post_handler\n");
0047     }
0048     posth_val = preh_val + div_factor;
0049 }
0050 
0051 static struct kprobe kp = {
0052     .symbol_name = "kprobe_target",
0053     .pre_handler = kp_pre_handler,
0054     .post_handler = kp_post_handler
0055 };
0056 
0057 static int test_kprobe(void)
0058 {
0059     int ret;
0060 
0061     ret = register_kprobe(&kp);
0062     if (ret < 0) {
0063         pr_err("register_kprobe returned %d\n", ret);
0064         return ret;
0065     }
0066 
0067     ret = target(rand1);
0068     unregister_kprobe(&kp);
0069 
0070     if (preh_val == 0) {
0071         pr_err("kprobe pre_handler not called\n");
0072         handler_errors++;
0073     }
0074 
0075     if (posth_val == 0) {
0076         pr_err("kprobe post_handler not called\n");
0077         handler_errors++;
0078     }
0079 
0080     return 0;
0081 }
0082 
0083 static noinline u32 kprobe_target2(u32 value)
0084 {
0085     return (value / div_factor) + 1;
0086 }
0087 
0088 static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
0089 {
0090     preh_val = (rand1 / div_factor) + 1;
0091     return 0;
0092 }
0093 
0094 static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
0095         unsigned long flags)
0096 {
0097     if (preh_val != (rand1 / div_factor) + 1) {
0098         handler_errors++;
0099         pr_err("incorrect value in post_handler2\n");
0100     }
0101     posth_val = preh_val + div_factor;
0102 }
0103 
0104 static struct kprobe kp2 = {
0105     .symbol_name = "kprobe_target2",
0106     .pre_handler = kp_pre_handler2,
0107     .post_handler = kp_post_handler2
0108 };
0109 
0110 static int test_kprobes(void)
0111 {
0112     int ret;
0113     struct kprobe *kps[2] = {&kp, &kp2};
0114 
0115     /* addr and flags should be cleard for reusing kprobe. */
0116     kp.addr = NULL;
0117     kp.flags = 0;
0118     ret = register_kprobes(kps, 2);
0119     if (ret < 0) {
0120         pr_err("register_kprobes returned %d\n", ret);
0121         return ret;
0122     }
0123 
0124     preh_val = 0;
0125     posth_val = 0;
0126     ret = target(rand1);
0127 
0128     if (preh_val == 0) {
0129         pr_err("kprobe pre_handler not called\n");
0130         handler_errors++;
0131     }
0132 
0133     if (posth_val == 0) {
0134         pr_err("kprobe post_handler not called\n");
0135         handler_errors++;
0136     }
0137 
0138     preh_val = 0;
0139     posth_val = 0;
0140     ret = target2(rand1);
0141 
0142     if (preh_val == 0) {
0143         pr_err("kprobe pre_handler2 not called\n");
0144         handler_errors++;
0145     }
0146 
0147     if (posth_val == 0) {
0148         pr_err("kprobe post_handler2 not called\n");
0149         handler_errors++;
0150     }
0151 
0152     unregister_kprobes(kps, 2);
0153     return 0;
0154 
0155 }
0156 
0157 static u32 j_kprobe_target(u32 value)
0158 {
0159     if (value != rand1) {
0160         handler_errors++;
0161         pr_err("incorrect value in jprobe handler\n");
0162     }
0163 
0164     jph_val = rand1;
0165     jprobe_return();
0166     return 0;
0167 }
0168 
0169 static struct jprobe jp = {
0170     .entry      = j_kprobe_target,
0171     .kp.symbol_name = "kprobe_target"
0172 };
0173 
0174 static int test_jprobe(void)
0175 {
0176     int ret;
0177 
0178     ret = register_jprobe(&jp);
0179     if (ret < 0) {
0180         pr_err("register_jprobe returned %d\n", ret);
0181         return ret;
0182     }
0183 
0184     ret = target(rand1);
0185     unregister_jprobe(&jp);
0186     if (jph_val == 0) {
0187         pr_err("jprobe handler not called\n");
0188         handler_errors++;
0189     }
0190 
0191     return 0;
0192 }
0193 
0194 static struct jprobe jp2 = {
0195     .entry          = j_kprobe_target,
0196     .kp.symbol_name = "kprobe_target2"
0197 };
0198 
0199 static int test_jprobes(void)
0200 {
0201     int ret;
0202     struct jprobe *jps[2] = {&jp, &jp2};
0203 
0204     /* addr and flags should be cleard for reusing kprobe. */
0205     jp.kp.addr = NULL;
0206     jp.kp.flags = 0;
0207     ret = register_jprobes(jps, 2);
0208     if (ret < 0) {
0209         pr_err("register_jprobes returned %d\n", ret);
0210         return ret;
0211     }
0212 
0213     jph_val = 0;
0214     ret = target(rand1);
0215     if (jph_val == 0) {
0216         pr_err("jprobe handler not called\n");
0217         handler_errors++;
0218     }
0219 
0220     jph_val = 0;
0221     ret = target2(rand1);
0222     if (jph_val == 0) {
0223         pr_err("jprobe handler2 not called\n");
0224         handler_errors++;
0225     }
0226     unregister_jprobes(jps, 2);
0227 
0228     return 0;
0229 }
0230 #ifdef CONFIG_KRETPROBES
0231 static u32 krph_val;
0232 
0233 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
0234 {
0235     krph_val = (rand1 / div_factor);
0236     return 0;
0237 }
0238 
0239 static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
0240 {
0241     unsigned long ret = regs_return_value(regs);
0242 
0243     if (ret != (rand1 / div_factor)) {
0244         handler_errors++;
0245         pr_err("incorrect value in kretprobe handler\n");
0246     }
0247     if (krph_val == 0) {
0248         handler_errors++;
0249         pr_err("call to kretprobe entry handler failed\n");
0250     }
0251 
0252     krph_val = rand1;
0253     return 0;
0254 }
0255 
0256 static struct kretprobe rp = {
0257     .handler    = return_handler,
0258     .entry_handler  = entry_handler,
0259     .kp.symbol_name = "kprobe_target"
0260 };
0261 
0262 static int test_kretprobe(void)
0263 {
0264     int ret;
0265 
0266     ret = register_kretprobe(&rp);
0267     if (ret < 0) {
0268         pr_err("register_kretprobe returned %d\n", ret);
0269         return ret;
0270     }
0271 
0272     ret = target(rand1);
0273     unregister_kretprobe(&rp);
0274     if (krph_val != rand1) {
0275         pr_err("kretprobe handler not called\n");
0276         handler_errors++;
0277     }
0278 
0279     return 0;
0280 }
0281 
0282 static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
0283 {
0284     unsigned long ret = regs_return_value(regs);
0285 
0286     if (ret != (rand1 / div_factor) + 1) {
0287         handler_errors++;
0288         pr_err("incorrect value in kretprobe handler2\n");
0289     }
0290     if (krph_val == 0) {
0291         handler_errors++;
0292         pr_err("call to kretprobe entry handler failed\n");
0293     }
0294 
0295     krph_val = rand1;
0296     return 0;
0297 }
0298 
0299 static struct kretprobe rp2 = {
0300     .handler    = return_handler2,
0301     .entry_handler  = entry_handler,
0302     .kp.symbol_name = "kprobe_target2"
0303 };
0304 
0305 static int test_kretprobes(void)
0306 {
0307     int ret;
0308     struct kretprobe *rps[2] = {&rp, &rp2};
0309 
0310     /* addr and flags should be cleard for reusing kprobe. */
0311     rp.kp.addr = NULL;
0312     rp.kp.flags = 0;
0313     ret = register_kretprobes(rps, 2);
0314     if (ret < 0) {
0315         pr_err("register_kretprobe returned %d\n", ret);
0316         return ret;
0317     }
0318 
0319     krph_val = 0;
0320     ret = target(rand1);
0321     if (krph_val != rand1) {
0322         pr_err("kretprobe handler not called\n");
0323         handler_errors++;
0324     }
0325 
0326     krph_val = 0;
0327     ret = target2(rand1);
0328     if (krph_val != rand1) {
0329         pr_err("kretprobe handler2 not called\n");
0330         handler_errors++;
0331     }
0332     unregister_kretprobes(rps, 2);
0333     return 0;
0334 }
0335 #endif /* CONFIG_KRETPROBES */
0336 
0337 int init_test_probes(void)
0338 {
0339     int ret;
0340 
0341     target = kprobe_target;
0342     target2 = kprobe_target2;
0343 
0344     do {
0345         rand1 = prandom_u32();
0346     } while (rand1 <= div_factor);
0347 
0348     pr_info("started\n");
0349     num_tests++;
0350     ret = test_kprobe();
0351     if (ret < 0)
0352         errors++;
0353 
0354     num_tests++;
0355     ret = test_kprobes();
0356     if (ret < 0)
0357         errors++;
0358 
0359     num_tests++;
0360     ret = test_jprobe();
0361     if (ret < 0)
0362         errors++;
0363 
0364     num_tests++;
0365     ret = test_jprobes();
0366     if (ret < 0)
0367         errors++;
0368 
0369 #ifdef CONFIG_KRETPROBES
0370     num_tests++;
0371     ret = test_kretprobe();
0372     if (ret < 0)
0373         errors++;
0374 
0375     num_tests++;
0376     ret = test_kretprobes();
0377     if (ret < 0)
0378         errors++;
0379 #endif /* CONFIG_KRETPROBES */
0380 
0381     if (errors)
0382         pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
0383     else if (handler_errors)
0384         pr_err("BUG: %d error(s) running handlers\n", handler_errors);
0385     else
0386         pr_info("passed successfully\n");
0387 
0388     return 0;
0389 }