test_kprobes.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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;
  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. if (preemptible()) {
  32. handler_errors++;
  33. pr_err("pre-handler is preemptible\n");
  34. }
  35. preh_val = (rand1 / div_factor);
  36. return 0;
  37. }
  38. static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
  39. unsigned long flags)
  40. {
  41. if (preemptible()) {
  42. handler_errors++;
  43. pr_err("post-handler is preemptible\n");
  44. }
  45. if (preh_val != (rand1 / div_factor)) {
  46. handler_errors++;
  47. pr_err("incorrect value in post_handler\n");
  48. }
  49. posth_val = preh_val + div_factor;
  50. }
  51. static struct kprobe kp = {
  52. .symbol_name = "kprobe_target",
  53. .pre_handler = kp_pre_handler,
  54. .post_handler = kp_post_handler
  55. };
  56. static int test_kprobe(void)
  57. {
  58. int ret;
  59. ret = register_kprobe(&kp);
  60. if (ret < 0) {
  61. pr_err("register_kprobe returned %d\n", ret);
  62. return ret;
  63. }
  64. ret = target(rand1);
  65. unregister_kprobe(&kp);
  66. if (preh_val == 0) {
  67. pr_err("kprobe pre_handler not called\n");
  68. handler_errors++;
  69. }
  70. if (posth_val == 0) {
  71. pr_err("kprobe post_handler not called\n");
  72. handler_errors++;
  73. }
  74. return 0;
  75. }
  76. static noinline u32 kprobe_target2(u32 value)
  77. {
  78. return (value / div_factor) + 1;
  79. }
  80. static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
  81. {
  82. preh_val = (rand1 / div_factor) + 1;
  83. return 0;
  84. }
  85. static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
  86. unsigned long flags)
  87. {
  88. if (preh_val != (rand1 / div_factor) + 1) {
  89. handler_errors++;
  90. pr_err("incorrect value in post_handler2\n");
  91. }
  92. posth_val = preh_val + div_factor;
  93. }
  94. static struct kprobe kp2 = {
  95. .symbol_name = "kprobe_target2",
  96. .pre_handler = kp_pre_handler2,
  97. .post_handler = kp_post_handler2
  98. };
  99. static int test_kprobes(void)
  100. {
  101. int ret;
  102. struct kprobe *kps[2] = {&kp, &kp2};
  103. /* addr and flags should be cleard for reusing kprobe. */
  104. kp.addr = NULL;
  105. kp.flags = 0;
  106. ret = register_kprobes(kps, 2);
  107. if (ret < 0) {
  108. pr_err("register_kprobes returned %d\n", ret);
  109. return ret;
  110. }
  111. preh_val = 0;
  112. posth_val = 0;
  113. ret = target(rand1);
  114. if (preh_val == 0) {
  115. pr_err("kprobe pre_handler not called\n");
  116. handler_errors++;
  117. }
  118. if (posth_val == 0) {
  119. pr_err("kprobe post_handler not called\n");
  120. handler_errors++;
  121. }
  122. preh_val = 0;
  123. posth_val = 0;
  124. ret = target2(rand1);
  125. if (preh_val == 0) {
  126. pr_err("kprobe pre_handler2 not called\n");
  127. handler_errors++;
  128. }
  129. if (posth_val == 0) {
  130. pr_err("kprobe post_handler2 not called\n");
  131. handler_errors++;
  132. }
  133. unregister_kprobes(kps, 2);
  134. return 0;
  135. }
  136. #ifdef CONFIG_KRETPROBES
  137. static u32 krph_val;
  138. static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  139. {
  140. if (preemptible()) {
  141. handler_errors++;
  142. pr_err("kretprobe entry handler is preemptible\n");
  143. }
  144. krph_val = (rand1 / div_factor);
  145. return 0;
  146. }
  147. static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  148. {
  149. unsigned long ret = regs_return_value(regs);
  150. if (preemptible()) {
  151. handler_errors++;
  152. pr_err("kretprobe return handler is preemptible\n");
  153. }
  154. if (ret != (rand1 / div_factor)) {
  155. handler_errors++;
  156. pr_err("incorrect value in kretprobe handler\n");
  157. }
  158. if (krph_val == 0) {
  159. handler_errors++;
  160. pr_err("call to kretprobe entry handler failed\n");
  161. }
  162. krph_val = rand1;
  163. return 0;
  164. }
  165. static struct kretprobe rp = {
  166. .handler = return_handler,
  167. .entry_handler = entry_handler,
  168. .kp.symbol_name = "kprobe_target"
  169. };
  170. static int test_kretprobe(void)
  171. {
  172. int ret;
  173. ret = register_kretprobe(&rp);
  174. if (ret < 0) {
  175. pr_err("register_kretprobe returned %d\n", ret);
  176. return ret;
  177. }
  178. ret = target(rand1);
  179. unregister_kretprobe(&rp);
  180. if (krph_val != rand1) {
  181. pr_err("kretprobe handler not called\n");
  182. handler_errors++;
  183. }
  184. return 0;
  185. }
  186. static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
  187. {
  188. unsigned long ret = regs_return_value(regs);
  189. if (ret != (rand1 / div_factor) + 1) {
  190. handler_errors++;
  191. pr_err("incorrect value in kretprobe handler2\n");
  192. }
  193. if (krph_val == 0) {
  194. handler_errors++;
  195. pr_err("call to kretprobe entry handler failed\n");
  196. }
  197. krph_val = rand1;
  198. return 0;
  199. }
  200. static struct kretprobe rp2 = {
  201. .handler = return_handler2,
  202. .entry_handler = entry_handler,
  203. .kp.symbol_name = "kprobe_target2"
  204. };
  205. static int test_kretprobes(void)
  206. {
  207. int ret;
  208. struct kretprobe *rps[2] = {&rp, &rp2};
  209. /* addr and flags should be cleard for reusing kprobe. */
  210. rp.kp.addr = NULL;
  211. rp.kp.flags = 0;
  212. ret = register_kretprobes(rps, 2);
  213. if (ret < 0) {
  214. pr_err("register_kretprobe returned %d\n", ret);
  215. return ret;
  216. }
  217. krph_val = 0;
  218. ret = target(rand1);
  219. if (krph_val != rand1) {
  220. pr_err("kretprobe handler not called\n");
  221. handler_errors++;
  222. }
  223. krph_val = 0;
  224. ret = target2(rand1);
  225. if (krph_val != rand1) {
  226. pr_err("kretprobe handler2 not called\n");
  227. handler_errors++;
  228. }
  229. unregister_kretprobes(rps, 2);
  230. return 0;
  231. }
  232. #endif /* CONFIG_KRETPROBES */
  233. int init_test_probes(void)
  234. {
  235. int ret;
  236. target = kprobe_target;
  237. target2 = kprobe_target2;
  238. do {
  239. rand1 = prandom_u32();
  240. } while (rand1 <= div_factor);
  241. pr_info("started\n");
  242. num_tests++;
  243. ret = test_kprobe();
  244. if (ret < 0)
  245. errors++;
  246. num_tests++;
  247. ret = test_kprobes();
  248. if (ret < 0)
  249. errors++;
  250. #ifdef CONFIG_KRETPROBES
  251. num_tests++;
  252. ret = test_kretprobe();
  253. if (ret < 0)
  254. errors++;
  255. num_tests++;
  256. ret = test_kretprobes();
  257. if (ret < 0)
  258. errors++;
  259. #endif /* CONFIG_KRETPROBES */
  260. if (errors)
  261. pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
  262. else if (handler_errors)
  263. pr_err("BUG: %d error(s) running handlers\n", handler_errors);
  264. else
  265. pr_info("passed successfully\n");
  266. return 0;
  267. }