test_kprobes.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  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. #define pr_fmt(fmt) "Kprobe smoke test: " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/random.h>
  20. #define div_factor 3
  21. static u32 rand1, preh_val, posth_val, jph_val;
  22. static int errors, handler_errors, num_tests;
  23. static u32 (*target)(u32 value);
  24. static u32 (*target2)(u32 value);
  25. static noinline u32 kprobe_target(u32 value)
  26. {
  27. return (value / div_factor);
  28. }
  29. static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  30. {
  31. preh_val = (rand1 / div_factor);
  32. return 0;
  33. }
  34. static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
  35. unsigned long flags)
  36. {
  37. if (preh_val != (rand1 / div_factor)) {
  38. handler_errors++;
  39. pr_err("incorrect value in post_handler\n");
  40. }
  41. posth_val = preh_val + div_factor;
  42. }
  43. static struct kprobe kp = {
  44. .symbol_name = "kprobe_target",
  45. .pre_handler = kp_pre_handler,
  46. .post_handler = kp_post_handler
  47. };
  48. static int test_kprobe(void)
  49. {
  50. int ret;
  51. ret = register_kprobe(&kp);
  52. if (ret < 0) {
  53. pr_err("register_kprobe returned %d\n", ret);
  54. return ret;
  55. }
  56. ret = target(rand1);
  57. unregister_kprobe(&kp);
  58. if (preh_val == 0) {
  59. pr_err("kprobe pre_handler not called\n");
  60. handler_errors++;
  61. }
  62. if (posth_val == 0) {
  63. pr_err("kprobe post_handler not called\n");
  64. handler_errors++;
  65. }
  66. return 0;
  67. }
  68. static noinline u32 kprobe_target2(u32 value)
  69. {
  70. return (value / div_factor) + 1;
  71. }
  72. static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
  73. {
  74. preh_val = (rand1 / div_factor) + 1;
  75. return 0;
  76. }
  77. static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
  78. unsigned long flags)
  79. {
  80. if (preh_val != (rand1 / div_factor) + 1) {
  81. handler_errors++;
  82. pr_err("incorrect value in post_handler2\n");
  83. }
  84. posth_val = preh_val + div_factor;
  85. }
  86. static struct kprobe kp2 = {
  87. .symbol_name = "kprobe_target2",
  88. .pre_handler = kp_pre_handler2,
  89. .post_handler = kp_post_handler2
  90. };
  91. static int test_kprobes(void)
  92. {
  93. int ret;
  94. struct kprobe *kps[2] = {&kp, &kp2};
  95. /* addr and flags should be cleard for reusing kprobe. */
  96. kp.addr = NULL;
  97. kp.flags = 0;
  98. ret = register_kprobes(kps, 2);
  99. if (ret < 0) {
  100. pr_err("register_kprobes returned %d\n", ret);
  101. return ret;
  102. }
  103. preh_val = 0;
  104. posth_val = 0;
  105. ret = target(rand1);
  106. if (preh_val == 0) {
  107. pr_err("kprobe pre_handler not called\n");
  108. handler_errors++;
  109. }
  110. if (posth_val == 0) {
  111. pr_err("kprobe post_handler not called\n");
  112. handler_errors++;
  113. }
  114. preh_val = 0;
  115. posth_val = 0;
  116. ret = target2(rand1);
  117. if (preh_val == 0) {
  118. pr_err("kprobe pre_handler2 not called\n");
  119. handler_errors++;
  120. }
  121. if (posth_val == 0) {
  122. pr_err("kprobe post_handler2 not called\n");
  123. handler_errors++;
  124. }
  125. unregister_kprobes(kps, 2);
  126. return 0;
  127. }
  128. static u32 j_kprobe_target(u32 value)
  129. {
  130. if (value != rand1) {
  131. handler_errors++;
  132. pr_err("incorrect value in jprobe handler\n");
  133. }
  134. jph_val = rand1;
  135. jprobe_return();
  136. return 0;
  137. }
  138. static struct jprobe jp = {
  139. .entry = j_kprobe_target,
  140. .kp.symbol_name = "kprobe_target"
  141. };
  142. static int test_jprobe(void)
  143. {
  144. int ret;
  145. ret = register_jprobe(&jp);
  146. if (ret < 0) {
  147. pr_err("register_jprobe returned %d\n", ret);
  148. return ret;
  149. }
  150. ret = target(rand1);
  151. unregister_jprobe(&jp);
  152. if (jph_val == 0) {
  153. pr_err("jprobe handler not called\n");
  154. handler_errors++;
  155. }
  156. return 0;
  157. }
  158. static struct jprobe jp2 = {
  159. .entry = j_kprobe_target,
  160. .kp.symbol_name = "kprobe_target2"
  161. };
  162. static int test_jprobes(void)
  163. {
  164. int ret;
  165. struct jprobe *jps[2] = {&jp, &jp2};
  166. /* addr and flags should be cleard for reusing kprobe. */
  167. jp.kp.addr = NULL;
  168. jp.kp.flags = 0;
  169. ret = register_jprobes(jps, 2);
  170. if (ret < 0) {
  171. pr_err("register_jprobes returned %d\n", ret);
  172. return ret;
  173. }
  174. jph_val = 0;
  175. ret = target(rand1);
  176. if (jph_val == 0) {
  177. pr_err("jprobe handler not called\n");
  178. handler_errors++;
  179. }
  180. jph_val = 0;
  181. ret = target2(rand1);
  182. if (jph_val == 0) {
  183. pr_err("jprobe handler2 not called\n");
  184. handler_errors++;
  185. }
  186. unregister_jprobes(jps, 2);
  187. return 0;
  188. }
  189. #ifdef CONFIG_KRETPROBES
  190. static u32 krph_val;
  191. static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  192. {
  193. krph_val = (rand1 / div_factor);
  194. return 0;
  195. }
  196. static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  197. {
  198. unsigned long ret = regs_return_value(regs);
  199. if (ret != (rand1 / div_factor)) {
  200. handler_errors++;
  201. pr_err("incorrect value in kretprobe handler\n");
  202. }
  203. if (krph_val == 0) {
  204. handler_errors++;
  205. pr_err("call to kretprobe entry handler failed\n");
  206. }
  207. krph_val = rand1;
  208. return 0;
  209. }
  210. static struct kretprobe rp = {
  211. .handler = return_handler,
  212. .entry_handler = entry_handler,
  213. .kp.symbol_name = "kprobe_target"
  214. };
  215. static int test_kretprobe(void)
  216. {
  217. int ret;
  218. ret = register_kretprobe(&rp);
  219. if (ret < 0) {
  220. pr_err("register_kretprobe returned %d\n", ret);
  221. return ret;
  222. }
  223. ret = target(rand1);
  224. unregister_kretprobe(&rp);
  225. if (krph_val != rand1) {
  226. pr_err("kretprobe handler not called\n");
  227. handler_errors++;
  228. }
  229. return 0;
  230. }
  231. static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
  232. {
  233. unsigned long ret = regs_return_value(regs);
  234. if (ret != (rand1 / div_factor) + 1) {
  235. handler_errors++;
  236. pr_err("incorrect value in kretprobe handler2\n");
  237. }
  238. if (krph_val == 0) {
  239. handler_errors++;
  240. pr_err("call to kretprobe entry handler failed\n");
  241. }
  242. krph_val = rand1;
  243. return 0;
  244. }
  245. static struct kretprobe rp2 = {
  246. .handler = return_handler2,
  247. .entry_handler = entry_handler,
  248. .kp.symbol_name = "kprobe_target2"
  249. };
  250. static int test_kretprobes(void)
  251. {
  252. int ret;
  253. struct kretprobe *rps[2] = {&rp, &rp2};
  254. /* addr and flags should be cleard for reusing kprobe. */
  255. rp.kp.addr = NULL;
  256. rp.kp.flags = 0;
  257. ret = register_kretprobes(rps, 2);
  258. if (ret < 0) {
  259. pr_err("register_kretprobe returned %d\n", ret);
  260. return ret;
  261. }
  262. krph_val = 0;
  263. ret = target(rand1);
  264. if (krph_val != rand1) {
  265. pr_err("kretprobe handler not called\n");
  266. handler_errors++;
  267. }
  268. krph_val = 0;
  269. ret = target2(rand1);
  270. if (krph_val != rand1) {
  271. pr_err("kretprobe handler2 not called\n");
  272. handler_errors++;
  273. }
  274. unregister_kretprobes(rps, 2);
  275. return 0;
  276. }
  277. #endif /* CONFIG_KRETPROBES */
  278. int init_test_probes(void)
  279. {
  280. int ret;
  281. target = kprobe_target;
  282. target2 = kprobe_target2;
  283. do {
  284. rand1 = prandom_u32();
  285. } while (rand1 <= div_factor);
  286. pr_info("started\n");
  287. num_tests++;
  288. ret = test_kprobe();
  289. if (ret < 0)
  290. errors++;
  291. num_tests++;
  292. ret = test_kprobes();
  293. if (ret < 0)
  294. errors++;
  295. num_tests++;
  296. ret = test_jprobe();
  297. if (ret < 0)
  298. errors++;
  299. num_tests++;
  300. ret = test_jprobes();
  301. if (ret < 0)
  302. errors++;
  303. #ifdef CONFIG_KRETPROBES
  304. num_tests++;
  305. ret = test_kretprobe();
  306. if (ret < 0)
  307. errors++;
  308. num_tests++;
  309. ret = test_kretprobes();
  310. if (ret < 0)
  311. errors++;
  312. #endif /* CONFIG_KRETPROBES */
  313. if (errors)
  314. pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
  315. else if (handler_errors)
  316. pr_err("BUG: %d error(s) running handlers\n", handler_errors);
  317. else
  318. pr_info("passed successfully\n");
  319. return 0;
  320. }