0001
0002
0003
0004
0005
0006
0007
0008 #define _GNU_SOURCE
0009 #include <stdio.h>
0010 #include <string.h>
0011 #include <stdlib.h>
0012 #include <signal.h>
0013 #include <errno.h>
0014 #include <time.h>
0015 #include <sys/time.h>
0016 #include <unistd.h>
0017 #include <sys/sysinfo.h>
0018 #include <sys/prctl.h>
0019 #include <sys/syscall.h>
0020
0021 #ifndef PR_SET_SYSCALL_USER_DISPATCH
0022 # define PR_SET_SYSCALL_USER_DISPATCH 59
0023 # define PR_SYS_DISPATCH_OFF 0
0024 # define PR_SYS_DISPATCH_ON 1
0025 # define SYSCALL_DISPATCH_FILTER_ALLOW 0
0026 # define SYSCALL_DISPATCH_FILTER_BLOCK 1
0027 #endif
0028
0029 #ifdef __NR_syscalls
0030 # define MAGIC_SYSCALL_1 (__NR_syscalls + 1)
0031 #else
0032 # define MAGIC_SYSCALL_1 (0xff00)
0033 #endif
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 #if defined(__x86_64__) || defined(__i386__)
0045 #define TEST_BLOCKED_RETURN
0046 #endif
0047
0048 #ifdef __x86_64__
0049 void* (syscall_dispatcher_start)(void);
0050 void* (syscall_dispatcher_end)(void);
0051 #else
0052 unsigned long syscall_dispatcher_start = 0;
0053 unsigned long syscall_dispatcher_end = 0;
0054 #endif
0055
0056 unsigned long trapped_call_count = 0;
0057 unsigned long native_call_count = 0;
0058
0059 char selector;
0060 #define SYSCALL_BLOCK (selector = SYSCALL_DISPATCH_FILTER_BLOCK)
0061 #define SYSCALL_UNBLOCK (selector = SYSCALL_DISPATCH_FILTER_ALLOW)
0062
0063 #define CALIBRATION_STEP 100000
0064 #define CALIBRATE_TO_SECS 5
0065 int factor;
0066
0067 static double one_sysinfo_step(void)
0068 {
0069 struct timespec t1, t2;
0070 int i;
0071 struct sysinfo info;
0072
0073 clock_gettime(CLOCK_MONOTONIC, &t1);
0074 for (i = 0; i < CALIBRATION_STEP; i++)
0075 sysinfo(&info);
0076 clock_gettime(CLOCK_MONOTONIC, &t2);
0077 return (t2.tv_sec - t1.tv_sec) + 1.0e-9 * (t2.tv_nsec - t1.tv_nsec);
0078 }
0079
0080 static void calibrate_set(void)
0081 {
0082 double elapsed = 0;
0083
0084 printf("Calibrating test set to last ~%d seconds...\n", CALIBRATE_TO_SECS);
0085
0086 while (elapsed < 1) {
0087 elapsed += one_sysinfo_step();
0088 factor += CALIBRATE_TO_SECS;
0089 }
0090
0091 printf("test iterations = %d\n", CALIBRATION_STEP * factor);
0092 }
0093
0094 static double perf_syscall(void)
0095 {
0096 unsigned int i;
0097 double partial = 0;
0098
0099 for (i = 0; i < factor; ++i)
0100 partial += one_sysinfo_step()/(CALIBRATION_STEP*factor);
0101 return partial;
0102 }
0103
0104 static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)
0105 {
0106 char buf[1024];
0107 int len;
0108
0109 SYSCALL_UNBLOCK;
0110
0111
0112 len = snprintf(buf, 1024, "Caught sys_%x\n", info->si_syscall);
0113 write(1, buf, len);
0114
0115 if (info->si_syscall == MAGIC_SYSCALL_1)
0116 trapped_call_count++;
0117 else
0118 native_call_count++;
0119
0120 #ifdef TEST_BLOCKED_RETURN
0121 SYSCALL_BLOCK;
0122 #endif
0123
0124 #ifdef __x86_64__
0125 __asm__ volatile("movq $0xf, %rax");
0126 __asm__ volatile("leaveq");
0127 __asm__ volatile("add $0x8, %rsp");
0128 __asm__ volatile("syscall_dispatcher_start:");
0129 __asm__ volatile("syscall");
0130 __asm__ volatile("nop");
0131 __asm__ volatile("syscall_dispatcher_end:");
0132 #endif
0133
0134 }
0135
0136 int main(void)
0137 {
0138 struct sigaction act;
0139 double time1, time2;
0140 int ret;
0141 sigset_t mask;
0142
0143 memset(&act, 0, sizeof(act));
0144 sigemptyset(&mask);
0145
0146 act.sa_sigaction = handle_sigsys;
0147 act.sa_flags = SA_SIGINFO;
0148 act.sa_mask = mask;
0149
0150 calibrate_set();
0151
0152 time1 = perf_syscall();
0153 printf("Avg syscall time %.0lfns.\n", time1 * 1.0e9);
0154
0155 ret = sigaction(SIGSYS, &act, NULL);
0156 if (ret) {
0157 perror("Error sigaction:");
0158 exit(-1);
0159 }
0160
0161 fprintf(stderr, "Enabling syscall trapping.\n");
0162
0163 if (prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON,
0164 syscall_dispatcher_start,
0165 (syscall_dispatcher_end - syscall_dispatcher_start + 1),
0166 &selector)) {
0167 perror("prctl failed\n");
0168 exit(-1);
0169 }
0170
0171 SYSCALL_BLOCK;
0172 syscall(MAGIC_SYSCALL_1);
0173
0174 #ifdef TEST_BLOCKED_RETURN
0175 if (selector == SYSCALL_DISPATCH_FILTER_ALLOW) {
0176 fprintf(stderr, "Failed to return with selector blocked.\n");
0177 exit(-1);
0178 }
0179 #endif
0180
0181 SYSCALL_UNBLOCK;
0182
0183 if (!trapped_call_count) {
0184 fprintf(stderr, "syscall trapping does not work.\n");
0185 exit(-1);
0186 }
0187
0188 time2 = perf_syscall();
0189
0190 if (native_call_count) {
0191 perror("syscall trapping intercepted more syscalls than expected\n");
0192 exit(-1);
0193 }
0194
0195 printf("trapped_call_count %lu, native_call_count %lu.\n",
0196 trapped_call_count, native_call_count);
0197 printf("Avg syscall time %.0lfns.\n", time2 * 1.0e9);
0198 printf("Interception overhead: %.1lf%% (+%.0lfns).\n",
0199 100.0 * (time2 / time1 - 1.0), 1.0e9 * (time2 - time1));
0200 return 0;
0201
0202 }