Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Inspired by breakpoint overflow test done by
0004  * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests
0005  * (git://github.com/deater/perf_event_tests)
0006  */
0007 
0008 /*
0009  * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
0010  * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
0011  */
0012 #define __SANE_USERSPACE_TYPES__
0013 
0014 #include <stdlib.h>
0015 #include <stdio.h>
0016 #include <unistd.h>
0017 #include <string.h>
0018 #include <sys/ioctl.h>
0019 #include <time.h>
0020 #include <fcntl.h>
0021 #include <signal.h>
0022 #include <sys/mman.h>
0023 #include <linux/compiler.h>
0024 #include <linux/hw_breakpoint.h>
0025 
0026 #include "tests.h"
0027 #include "debug.h"
0028 #include "event.h"
0029 #include "perf-sys.h"
0030 #include "cloexec.h"
0031 
0032 static int fd1;
0033 static int fd2;
0034 static int fd3;
0035 static int overflows;
0036 static int overflows_2;
0037 
0038 volatile long the_var;
0039 
0040 
0041 /*
0042  * Use ASM to ensure watchpoint and breakpoint can be triggered
0043  * at one instruction.
0044  */
0045 #if defined (__x86_64__)
0046 extern void __test_function(volatile long *ptr);
0047 asm (
0048     ".pushsection .text;"
0049     ".globl __test_function\n"
0050     ".type __test_function, @function;"
0051     "__test_function:\n"
0052     "incq (%rdi)\n"
0053     "ret\n"
0054     ".popsection\n");
0055 #else
0056 static void __test_function(volatile long *ptr)
0057 {
0058     *ptr = 0x1234;
0059 }
0060 #endif
0061 
0062 static noinline int test_function(void)
0063 {
0064     __test_function(&the_var);
0065     the_var++;
0066     return time(NULL);
0067 }
0068 
0069 static void sig_handler_2(int signum __maybe_unused,
0070               siginfo_t *oh __maybe_unused,
0071               void *uc __maybe_unused)
0072 {
0073     overflows_2++;
0074     if (overflows_2 > 10) {
0075         ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
0076         ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
0077         ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
0078     }
0079 }
0080 
0081 static void sig_handler(int signum __maybe_unused,
0082             siginfo_t *oh __maybe_unused,
0083             void *uc __maybe_unused)
0084 {
0085     overflows++;
0086 
0087     if (overflows > 10) {
0088         /*
0089          * This should be executed only once during
0090          * this test, if we are here for the 10th
0091          * time, consider this the recursive issue.
0092          *
0093          * We can get out of here by disable events,
0094          * so no new SIGIO is delivered.
0095          */
0096         ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
0097         ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
0098         ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
0099     }
0100 }
0101 
0102 static int __event(bool is_x, void *addr, int sig)
0103 {
0104     struct perf_event_attr pe;
0105     int fd;
0106 
0107     memset(&pe, 0, sizeof(struct perf_event_attr));
0108     pe.type = PERF_TYPE_BREAKPOINT;
0109     pe.size = sizeof(struct perf_event_attr);
0110 
0111     pe.config = 0;
0112     pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
0113     pe.bp_addr = (unsigned long) addr;
0114     pe.bp_len = sizeof(long);
0115 
0116     pe.sample_period = 1;
0117     pe.sample_type = PERF_SAMPLE_IP;
0118     pe.wakeup_events = 1;
0119 
0120     pe.disabled = 1;
0121     pe.exclude_kernel = 1;
0122     pe.exclude_hv = 1;
0123 
0124     fd = sys_perf_event_open(&pe, 0, -1, -1,
0125                  perf_event_open_cloexec_flag());
0126     if (fd < 0) {
0127         pr_debug("failed opening event %llx\n", pe.config);
0128         return TEST_FAIL;
0129     }
0130 
0131     fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
0132     fcntl(fd, F_SETSIG, sig);
0133     fcntl(fd, F_SETOWN, getpid());
0134 
0135     ioctl(fd, PERF_EVENT_IOC_RESET, 0);
0136 
0137     return fd;
0138 }
0139 
0140 static int bp_event(void *addr, int sig)
0141 {
0142     return __event(true, addr, sig);
0143 }
0144 
0145 static int wp_event(void *addr, int sig)
0146 {
0147     return __event(false, addr, sig);
0148 }
0149 
0150 static long long bp_count(int fd)
0151 {
0152     long long count;
0153     int ret;
0154 
0155     ret = read(fd, &count, sizeof(long long));
0156     if (ret != sizeof(long long)) {
0157         pr_debug("failed to read: %d\n", ret);
0158         return TEST_FAIL;
0159     }
0160 
0161     return count;
0162 }
0163 
0164 static int test__bp_signal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0165 {
0166     struct sigaction sa;
0167     long long count1, count2, count3;
0168 
0169     if (!BP_SIGNAL_IS_SUPPORTED) {
0170         pr_debug("Test not supported on this architecture");
0171         return TEST_SKIP;
0172     }
0173 
0174     /* setup SIGIO signal handler */
0175     memset(&sa, 0, sizeof(struct sigaction));
0176     sa.sa_sigaction = (void *) sig_handler;
0177     sa.sa_flags = SA_SIGINFO;
0178 
0179     if (sigaction(SIGIO, &sa, NULL) < 0) {
0180         pr_debug("failed setting up signal handler\n");
0181         return TEST_FAIL;
0182     }
0183 
0184     sa.sa_sigaction = (void *) sig_handler_2;
0185     if (sigaction(SIGUSR1, &sa, NULL) < 0) {
0186         pr_debug("failed setting up signal handler 2\n");
0187         return TEST_FAIL;
0188     }
0189 
0190     /*
0191      * We create following events:
0192      *
0193      * fd1 - breakpoint event on __test_function with SIGIO
0194      *       signal configured. We should get signal
0195      *       notification each time the breakpoint is hit
0196      *
0197      * fd2 - breakpoint event on sig_handler with SIGUSR1
0198      *       configured. We should get SIGUSR1 each time when
0199      *       breakpoint is hit
0200      *
0201      * fd3 - watchpoint event on __test_function with SIGIO
0202      *       configured.
0203      *
0204      * Following processing should happen:
0205      *   Exec:               Action:                       Result:
0206      *   incq (%rdi)       - fd1 event breakpoint hit   -> count1 == 1
0207      *                     - SIGIO is delivered
0208      *   sig_handler       - fd2 event breakpoint hit   -> count2 == 1
0209      *                     - SIGUSR1 is delivered
0210      *   sig_handler_2                                  -> overflows_2 == 1  (nested signal)
0211      *   sys_rt_sigreturn  - return from sig_handler_2
0212      *   overflows++                                    -> overflows = 1
0213      *   sys_rt_sigreturn  - return from sig_handler
0214      *   incq (%rdi)       - fd3 event watchpoint hit   -> count3 == 1       (wp and bp in one insn)
0215      *                     - SIGIO is delivered
0216      *   sig_handler       - fd2 event breakpoint hit   -> count2 == 2
0217      *                     - SIGUSR1 is delivered
0218      *   sig_handler_2                                  -> overflows_2 == 2  (nested signal)
0219      *   sys_rt_sigreturn  - return from sig_handler_2
0220      *   overflows++                                    -> overflows = 2
0221      *   sys_rt_sigreturn  - return from sig_handler
0222      *   the_var++         - fd3 event watchpoint hit   -> count3 == 2       (standalone watchpoint)
0223      *                     - SIGIO is delivered
0224      *   sig_handler       - fd2 event breakpoint hit   -> count2 == 3
0225      *                     - SIGUSR1 is delivered
0226      *   sig_handler_2                                  -> overflows_2 == 3  (nested signal)
0227      *   sys_rt_sigreturn  - return from sig_handler_2
0228      *   overflows++                                    -> overflows == 3
0229      *   sys_rt_sigreturn  - return from sig_handler
0230      *
0231      * The test case check following error conditions:
0232      * - we get stuck in signal handler because of debug
0233      *   exception being triggered recursively due to
0234      *   the wrong RF EFLAG management
0235      *
0236      * - we never trigger the sig_handler breakpoint due
0237      *   to the wrong RF EFLAG management
0238      *
0239      */
0240 
0241     fd1 = bp_event(__test_function, SIGIO);
0242     fd2 = bp_event(sig_handler, SIGUSR1);
0243     fd3 = wp_event((void *)&the_var, SIGIO);
0244 
0245     ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
0246     ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
0247     ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
0248 
0249     /*
0250      * Kick off the test by triggering 'fd1'
0251      * breakpoint.
0252      */
0253     test_function();
0254 
0255     ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
0256     ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
0257     ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
0258 
0259     count1 = bp_count(fd1);
0260     count2 = bp_count(fd2);
0261     count3 = bp_count(fd3);
0262 
0263     close(fd1);
0264     close(fd2);
0265     close(fd3);
0266 
0267     pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
0268          count1, count2, count3, overflows, overflows_2);
0269 
0270     if (count1 != 1) {
0271         if (count1 == 11)
0272             pr_debug("failed: RF EFLAG recursion issue detected\n");
0273         else
0274             pr_debug("failed: wrong count for bp1: %lld, expected 1\n", count1);
0275     }
0276 
0277     if (overflows != 3)
0278         pr_debug("failed: wrong overflow (%d) hit, expected 3\n", overflows);
0279 
0280     if (overflows_2 != 3)
0281         pr_debug("failed: wrong overflow_2 (%d) hit, expected 3\n", overflows_2);
0282 
0283     if (count2 != 3)
0284         pr_debug("failed: wrong count for bp2 (%lld), expected 3\n", count2);
0285 
0286     if (count3 != 2)
0287         pr_debug("failed: wrong count for bp3 (%lld), expected 2\n", count3);
0288 
0289     return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
0290         TEST_OK : TEST_FAIL;
0291 }
0292 
0293 DEFINE_SUITE("Breakpoint overflow signal handler", bp_signal);