kprobes-test.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * arch/arm/kernel/kprobes-test.c
  3. *
  4. * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/kprobes.h>
  13. #include "kprobes.h"
  14. /*
  15. * Test basic API
  16. */
  17. static bool test_regs_ok;
  18. static int test_func_instance;
  19. static int pre_handler_called;
  20. static int post_handler_called;
  21. static int jprobe_func_called;
  22. static int kretprobe_handler_called;
  23. #define FUNC_ARG1 0x12345678
  24. #define FUNC_ARG2 0xabcdef
  25. #ifndef CONFIG_THUMB2_KERNEL
  26. long arm_func(long r0, long r1);
  27. static void __used __naked __arm_kprobes_test_func(void)
  28. {
  29. __asm__ __volatile__ (
  30. ".arm \n\t"
  31. ".type arm_func, %%function \n\t"
  32. "arm_func: \n\t"
  33. "adds r0, r0, r1 \n\t"
  34. "bx lr \n\t"
  35. ".code "NORMAL_ISA /* Back to Thumb if necessary */
  36. : : : "r0", "r1", "cc"
  37. );
  38. }
  39. #else /* CONFIG_THUMB2_KERNEL */
  40. long thumb16_func(long r0, long r1);
  41. long thumb32even_func(long r0, long r1);
  42. long thumb32odd_func(long r0, long r1);
  43. static void __used __naked __thumb_kprobes_test_funcs(void)
  44. {
  45. __asm__ __volatile__ (
  46. ".type thumb16_func, %%function \n\t"
  47. "thumb16_func: \n\t"
  48. "adds.n r0, r0, r1 \n\t"
  49. "bx lr \n\t"
  50. ".align \n\t"
  51. ".type thumb32even_func, %%function \n\t"
  52. "thumb32even_func: \n\t"
  53. "adds.w r0, r0, r1 \n\t"
  54. "bx lr \n\t"
  55. ".align \n\t"
  56. "nop.n \n\t"
  57. ".type thumb32odd_func, %%function \n\t"
  58. "thumb32odd_func: \n\t"
  59. "adds.w r0, r0, r1 \n\t"
  60. "bx lr \n\t"
  61. : : : "r0", "r1", "cc"
  62. );
  63. }
  64. #endif /* CONFIG_THUMB2_KERNEL */
  65. static int call_test_func(long (*func)(long, long), bool check_test_regs)
  66. {
  67. long ret;
  68. ++test_func_instance;
  69. test_regs_ok = false;
  70. ret = (*func)(FUNC_ARG1, FUNC_ARG2);
  71. if (ret != FUNC_ARG1 + FUNC_ARG2) {
  72. pr_err("FAIL: call_test_func: func returned %lx\n", ret);
  73. return false;
  74. }
  75. if (check_test_regs && !test_regs_ok) {
  76. pr_err("FAIL: test regs not OK\n");
  77. return false;
  78. }
  79. return true;
  80. }
  81. static int __kprobes pre_handler(struct kprobe *p, struct pt_regs *regs)
  82. {
  83. pre_handler_called = test_func_instance;
  84. if (regs->ARM_r0 == FUNC_ARG1 && regs->ARM_r1 == FUNC_ARG2)
  85. test_regs_ok = true;
  86. return 0;
  87. }
  88. static void __kprobes post_handler(struct kprobe *p, struct pt_regs *regs,
  89. unsigned long flags)
  90. {
  91. post_handler_called = test_func_instance;
  92. if (regs->ARM_r0 != FUNC_ARG1 + FUNC_ARG2 || regs->ARM_r1 != FUNC_ARG2)
  93. test_regs_ok = false;
  94. }
  95. static struct kprobe the_kprobe = {
  96. .addr = 0,
  97. .pre_handler = pre_handler,
  98. .post_handler = post_handler
  99. };
  100. static int test_kprobe(long (*func)(long, long))
  101. {
  102. int ret;
  103. the_kprobe.addr = (kprobe_opcode_t *)func;
  104. ret = register_kprobe(&the_kprobe);
  105. if (ret < 0) {
  106. pr_err("FAIL: register_kprobe failed with %d\n", ret);
  107. return ret;
  108. }
  109. ret = call_test_func(func, true);
  110. unregister_kprobe(&the_kprobe);
  111. the_kprobe.flags = 0; /* Clear disable flag to allow reuse */
  112. if (!ret)
  113. return -EINVAL;
  114. if (pre_handler_called != test_func_instance) {
  115. pr_err("FAIL: kprobe pre_handler not called\n");
  116. return -EINVAL;
  117. }
  118. if (post_handler_called != test_func_instance) {
  119. pr_err("FAIL: kprobe post_handler not called\n");
  120. return -EINVAL;
  121. }
  122. if (!call_test_func(func, false))
  123. return -EINVAL;
  124. if (pre_handler_called == test_func_instance ||
  125. post_handler_called == test_func_instance) {
  126. pr_err("FAIL: probe called after unregistering\n");
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. static void __kprobes jprobe_func(long r0, long r1)
  132. {
  133. jprobe_func_called = test_func_instance;
  134. if (r0 == FUNC_ARG1 && r1 == FUNC_ARG2)
  135. test_regs_ok = true;
  136. jprobe_return();
  137. }
  138. static struct jprobe the_jprobe = {
  139. .entry = jprobe_func,
  140. };
  141. static int test_jprobe(long (*func)(long, long))
  142. {
  143. int ret;
  144. the_jprobe.kp.addr = (kprobe_opcode_t *)func;
  145. ret = register_jprobe(&the_jprobe);
  146. if (ret < 0) {
  147. pr_err("FAIL: register_jprobe failed with %d\n", ret);
  148. return ret;
  149. }
  150. ret = call_test_func(func, true);
  151. unregister_jprobe(&the_jprobe);
  152. the_jprobe.kp.flags = 0; /* Clear disable flag to allow reuse */
  153. if (!ret)
  154. return -EINVAL;
  155. if (jprobe_func_called != test_func_instance) {
  156. pr_err("FAIL: jprobe handler function not called\n");
  157. return -EINVAL;
  158. }
  159. if (!call_test_func(func, false))
  160. return -EINVAL;
  161. if (jprobe_func_called == test_func_instance) {
  162. pr_err("FAIL: probe called after unregistering\n");
  163. return -EINVAL;
  164. }
  165. return 0;
  166. }
  167. static int __kprobes
  168. kretprobe_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  169. {
  170. kretprobe_handler_called = test_func_instance;
  171. if (regs_return_value(regs) == FUNC_ARG1 + FUNC_ARG2)
  172. test_regs_ok = true;
  173. return 0;
  174. }
  175. static struct kretprobe the_kretprobe = {
  176. .handler = kretprobe_handler,
  177. };
  178. static int test_kretprobe(long (*func)(long, long))
  179. {
  180. int ret;
  181. the_kretprobe.kp.addr = (kprobe_opcode_t *)func;
  182. ret = register_kretprobe(&the_kretprobe);
  183. if (ret < 0) {
  184. pr_err("FAIL: register_kretprobe failed with %d\n", ret);
  185. return ret;
  186. }
  187. ret = call_test_func(func, true);
  188. unregister_kretprobe(&the_kretprobe);
  189. the_kretprobe.kp.flags = 0; /* Clear disable flag to allow reuse */
  190. if (!ret)
  191. return -EINVAL;
  192. if (kretprobe_handler_called != test_func_instance) {
  193. pr_err("FAIL: kretprobe handler not called\n");
  194. return -EINVAL;
  195. }
  196. if (!call_test_func(func, false))
  197. return -EINVAL;
  198. if (jprobe_func_called == test_func_instance) {
  199. pr_err("FAIL: kretprobe called after unregistering\n");
  200. return -EINVAL;
  201. }
  202. return 0;
  203. }
  204. static int run_api_tests(long (*func)(long, long))
  205. {
  206. int ret;
  207. pr_info(" kprobe\n");
  208. ret = test_kprobe(func);
  209. if (ret < 0)
  210. return ret;
  211. pr_info(" jprobe\n");
  212. ret = test_jprobe(func);
  213. if (ret < 0)
  214. return ret;
  215. pr_info(" kretprobe\n");
  216. ret = test_kretprobe(func);
  217. if (ret < 0)
  218. return ret;
  219. return 0;
  220. }
  221. /*
  222. * Top level test functions
  223. */
  224. static int __init run_all_tests(void)
  225. {
  226. int ret = 0;
  227. pr_info("Begining kprobe tests...\n");
  228. #ifndef CONFIG_THUMB2_KERNEL
  229. pr_info("Probe ARM code\n");
  230. ret = run_api_tests(arm_func);
  231. if (ret)
  232. goto out;
  233. #else /* CONFIG_THUMB2_KERNEL */
  234. pr_info("Probe 16-bit Thumb code\n");
  235. ret = run_api_tests(thumb16_func);
  236. if (ret)
  237. goto out;
  238. pr_info("Probe 32-bit Thumb code, even halfword\n");
  239. ret = run_api_tests(thumb32even_func);
  240. if (ret)
  241. goto out;
  242. pr_info("Probe 32-bit Thumb code, odd halfword\n");
  243. ret = run_api_tests(thumb32odd_func);
  244. if (ret)
  245. goto out;
  246. #endif
  247. out:
  248. if (ret == 0)
  249. pr_info("Finished kprobe tests OK\n");
  250. else
  251. pr_err("kprobe tests failed\n");
  252. return ret;
  253. }
  254. /*
  255. * Module setup
  256. */
  257. #ifdef MODULE
  258. static void __exit kprobe_test_exit(void)
  259. {
  260. }
  261. module_init(run_all_tests)
  262. module_exit(kprobe_test_exit)
  263. MODULE_LICENSE("GPL");
  264. #else /* !MODULE */
  265. late_initcall(run_all_tests);
  266. #endif