bp_signal.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Inspired by breakpoint overflow test done by
  3. * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests
  4. * (git://github.com/deater/perf_event_tests)
  5. */
  6. /*
  7. * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
  8. * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
  9. */
  10. #define __SANE_USERSPACE_TYPES__
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <sys/ioctl.h>
  16. #include <time.h>
  17. #include <fcntl.h>
  18. #include <signal.h>
  19. #include <sys/mman.h>
  20. #include <linux/compiler.h>
  21. #include <linux/hw_breakpoint.h>
  22. #include "tests.h"
  23. #include "debug.h"
  24. #include "perf.h"
  25. #include "cloexec.h"
  26. static int fd1;
  27. static int fd2;
  28. static int fd3;
  29. static int overflows;
  30. static int overflows_2;
  31. volatile long the_var;
  32. /*
  33. * Use ASM to ensure watchpoint and breakpoint can be triggered
  34. * at one instruction.
  35. */
  36. #if defined (__x86_64__)
  37. extern void __test_function(volatile long *ptr);
  38. asm (
  39. ".globl __test_function\n"
  40. "__test_function:\n"
  41. "incq (%rdi)\n"
  42. "ret\n");
  43. #elif defined (__aarch64__)
  44. extern void __test_function(volatile long *ptr);
  45. asm (
  46. ".globl __test_function\n"
  47. "__test_function:\n"
  48. "str x30, [x0]\n"
  49. "ret\n");
  50. #else
  51. static void __test_function(volatile long *ptr)
  52. {
  53. *ptr = 0x1234;
  54. }
  55. #endif
  56. __attribute__ ((noinline))
  57. static int test_function(void)
  58. {
  59. __test_function(&the_var);
  60. the_var++;
  61. return time(NULL);
  62. }
  63. static void sig_handler_2(int signum __maybe_unused,
  64. siginfo_t *oh __maybe_unused,
  65. void *uc __maybe_unused)
  66. {
  67. overflows_2++;
  68. if (overflows_2 > 10) {
  69. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  70. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  71. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  72. }
  73. }
  74. static void sig_handler(int signum __maybe_unused,
  75. siginfo_t *oh __maybe_unused,
  76. void *uc __maybe_unused)
  77. {
  78. overflows++;
  79. if (overflows > 10) {
  80. /*
  81. * This should be executed only once during
  82. * this test, if we are here for the 10th
  83. * time, consider this the recursive issue.
  84. *
  85. * We can get out of here by disable events,
  86. * so no new SIGIO is delivered.
  87. */
  88. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  89. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  90. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  91. }
  92. }
  93. static int __event(bool is_x, void *addr, int sig)
  94. {
  95. struct perf_event_attr pe;
  96. int fd;
  97. memset(&pe, 0, sizeof(struct perf_event_attr));
  98. pe.type = PERF_TYPE_BREAKPOINT;
  99. pe.size = sizeof(struct perf_event_attr);
  100. pe.config = 0;
  101. pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
  102. pe.bp_addr = (unsigned long) addr;
  103. pe.bp_len = sizeof(long);
  104. pe.sample_period = 1;
  105. pe.sample_type = PERF_SAMPLE_IP;
  106. pe.wakeup_events = 1;
  107. pe.disabled = 1;
  108. pe.exclude_kernel = 1;
  109. pe.exclude_hv = 1;
  110. fd = sys_perf_event_open(&pe, 0, -1, -1,
  111. perf_event_open_cloexec_flag());
  112. if (fd < 0) {
  113. pr_debug("failed opening event %llx\n", pe.config);
  114. return TEST_FAIL;
  115. }
  116. fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
  117. fcntl(fd, F_SETSIG, sig);
  118. fcntl(fd, F_SETOWN, getpid());
  119. ioctl(fd, PERF_EVENT_IOC_RESET, 0);
  120. return fd;
  121. }
  122. static int bp_event(void *addr, int sig)
  123. {
  124. return __event(true, addr, sig);
  125. }
  126. static int wp_event(void *addr, int sig)
  127. {
  128. return __event(false, addr, sig);
  129. }
  130. static long long bp_count(int fd)
  131. {
  132. long long count;
  133. int ret;
  134. ret = read(fd, &count, sizeof(long long));
  135. if (ret != sizeof(long long)) {
  136. pr_debug("failed to read: %d\n", ret);
  137. return TEST_FAIL;
  138. }
  139. return count;
  140. }
  141. int test__bp_signal(int subtest __maybe_unused)
  142. {
  143. struct sigaction sa;
  144. long long count1, count2, count3;
  145. /* setup SIGIO signal handler */
  146. memset(&sa, 0, sizeof(struct sigaction));
  147. sa.sa_sigaction = (void *) sig_handler;
  148. sa.sa_flags = SA_SIGINFO;
  149. if (sigaction(SIGIO, &sa, NULL) < 0) {
  150. pr_debug("failed setting up signal handler\n");
  151. return TEST_FAIL;
  152. }
  153. sa.sa_sigaction = (void *) sig_handler_2;
  154. if (sigaction(SIGUSR1, &sa, NULL) < 0) {
  155. pr_debug("failed setting up signal handler 2\n");
  156. return TEST_FAIL;
  157. }
  158. /*
  159. * We create following events:
  160. *
  161. * fd1 - breakpoint event on __test_function with SIGIO
  162. * signal configured. We should get signal
  163. * notification each time the breakpoint is hit
  164. *
  165. * fd2 - breakpoint event on sig_handler with SIGUSR1
  166. * configured. We should get SIGUSR1 each time when
  167. * breakpoint is hit
  168. *
  169. * fd3 - watchpoint event on __test_function with SIGIO
  170. * configured.
  171. *
  172. * Following processing should happen:
  173. * Exec: Action: Result:
  174. * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
  175. * - SIGIO is delivered
  176. * sig_handler - fd2 event breakpoint hit -> count2 == 1
  177. * - SIGUSR1 is delivered
  178. * sig_handler_2 -> overflows_2 == 1 (nested signal)
  179. * sys_rt_sigreturn - return from sig_handler_2
  180. * overflows++ -> overflows = 1
  181. * sys_rt_sigreturn - return from sig_handler
  182. * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
  183. * - SIGIO is delivered
  184. * sig_handler - fd2 event breakpoint hit -> count2 == 2
  185. * - SIGUSR1 is delivered
  186. * sig_handler_2 -> overflows_2 == 2 (nested signal)
  187. * sys_rt_sigreturn - return from sig_handler_2
  188. * overflows++ -> overflows = 2
  189. * sys_rt_sigreturn - return from sig_handler
  190. * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
  191. * - SIGIO is delivered
  192. * sig_handler - fd2 event breakpoint hit -> count2 == 3
  193. * - SIGUSR1 is delivered
  194. * sig_handler_2 -> overflows_2 == 3 (nested signal)
  195. * sys_rt_sigreturn - return from sig_handler_2
  196. * overflows++ -> overflows == 3
  197. * sys_rt_sigreturn - return from sig_handler
  198. *
  199. * The test case check following error conditions:
  200. * - we get stuck in signal handler because of debug
  201. * exception being triggered receursively due to
  202. * the wrong RF EFLAG management
  203. *
  204. * - we never trigger the sig_handler breakpoint due
  205. * to the rong RF EFLAG management
  206. *
  207. */
  208. fd1 = bp_event(__test_function, SIGIO);
  209. fd2 = bp_event(sig_handler, SIGUSR1);
  210. fd3 = wp_event((void *)&the_var, SIGIO);
  211. ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
  212. ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
  213. ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
  214. /*
  215. * Kick off the test by trigering 'fd1'
  216. * breakpoint.
  217. */
  218. test_function();
  219. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  220. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  221. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  222. count1 = bp_count(fd1);
  223. count2 = bp_count(fd2);
  224. count3 = bp_count(fd3);
  225. close(fd1);
  226. close(fd2);
  227. close(fd3);
  228. pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
  229. count1, count2, count3, overflows, overflows_2);
  230. if (count1 != 1) {
  231. if (count1 == 11)
  232. pr_debug("failed: RF EFLAG recursion issue detected\n");
  233. else
  234. pr_debug("failed: wrong count for bp1%lld\n", count1);
  235. }
  236. if (overflows != 3)
  237. pr_debug("failed: wrong overflow hit\n");
  238. if (overflows_2 != 3)
  239. pr_debug("failed: wrong overflow_2 hit\n");
  240. if (count2 != 3)
  241. pr_debug("failed: wrong count for bp2\n");
  242. if (count3 != 2)
  243. pr_debug("failed: wrong count for bp3\n");
  244. return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
  245. TEST_OK : TEST_FAIL;
  246. }