Commit | Line | Data |
---|---|---|
3b56aae3 AL |
1 | /* |
2 | * unwind_vdso.c - tests unwind info for AT_SYSINFO in the vDSO | |
3 | * Copyright (c) 2014-2015 Andrew Lutomirski | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * This tests __kernel_vsyscall's unwind info. | |
15 | */ | |
16 | ||
17 | #define _GNU_SOURCE | |
18 | ||
19 | #include <features.h> | |
20 | #include <stdio.h> | |
21 | ||
22 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16 | |
23 | ||
24 | int main() | |
25 | { | |
26 | /* We need getauxval(). */ | |
27 | printf("[SKIP]\tGLIBC before 2.16 cannot compile this test\n"); | |
28 | return 0; | |
29 | } | |
30 | ||
31 | #else | |
32 | ||
33 | #include <sys/time.h> | |
34 | #include <stdlib.h> | |
35 | #include <syscall.h> | |
36 | #include <unistd.h> | |
37 | #include <string.h> | |
38 | #include <inttypes.h> | |
39 | #include <sys/mman.h> | |
40 | #include <signal.h> | |
41 | #include <sys/ucontext.h> | |
42 | #include <err.h> | |
43 | #include <stddef.h> | |
44 | #include <stdbool.h> | |
45 | #include <sys/ptrace.h> | |
46 | #include <sys/user.h> | |
47 | #include <sys/ucontext.h> | |
48 | #include <link.h> | |
49 | #include <sys/auxv.h> | |
50 | #include <dlfcn.h> | |
51 | #include <unwind.h> | |
52 | ||
53 | static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), | |
54 | int flags) | |
55 | { | |
56 | struct sigaction sa; | |
57 | memset(&sa, 0, sizeof(sa)); | |
58 | sa.sa_sigaction = handler; | |
59 | sa.sa_flags = SA_SIGINFO | flags; | |
60 | sigemptyset(&sa.sa_mask); | |
61 | if (sigaction(sig, &sa, 0)) | |
62 | err(1, "sigaction"); | |
63 | } | |
64 | ||
65 | #ifdef __x86_64__ | |
66 | # define WIDTH "q" | |
67 | #else | |
68 | # define WIDTH "l" | |
69 | #endif | |
70 | ||
71 | static unsigned long get_eflags(void) | |
72 | { | |
73 | unsigned long eflags; | |
74 | asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags)); | |
75 | return eflags; | |
76 | } | |
77 | ||
78 | static void set_eflags(unsigned long eflags) | |
79 | { | |
80 | asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH | |
81 | : : "rm" (eflags) : "flags"); | |
82 | } | |
83 | ||
84 | #define X86_EFLAGS_TF (1UL << 8) | |
85 | ||
86 | static volatile sig_atomic_t nerrs; | |
87 | static unsigned long sysinfo; | |
88 | static bool got_sysinfo = false; | |
89 | static unsigned long return_address; | |
90 | ||
91 | struct unwind_state { | |
92 | unsigned long ip; /* trap source */ | |
93 | int depth; /* -1 until we hit the trap source */ | |
94 | }; | |
95 | ||
96 | _Unwind_Reason_Code trace_fn(struct _Unwind_Context * ctx, void *opaque) | |
97 | { | |
98 | struct unwind_state *state = opaque; | |
99 | unsigned long ip = _Unwind_GetIP(ctx); | |
100 | ||
101 | if (state->depth == -1) { | |
102 | if (ip == state->ip) | |
103 | state->depth = 0; | |
104 | else | |
105 | return _URC_NO_REASON; /* Not there yet */ | |
106 | } | |
107 | printf("\t 0x%lx\n", ip); | |
108 | ||
109 | if (ip == return_address) { | |
110 | /* Here we are. */ | |
111 | unsigned long eax = _Unwind_GetGR(ctx, 0); | |
112 | unsigned long ecx = _Unwind_GetGR(ctx, 1); | |
113 | unsigned long edx = _Unwind_GetGR(ctx, 2); | |
114 | unsigned long ebx = _Unwind_GetGR(ctx, 3); | |
115 | unsigned long ebp = _Unwind_GetGR(ctx, 5); | |
116 | unsigned long esi = _Unwind_GetGR(ctx, 6); | |
117 | unsigned long edi = _Unwind_GetGR(ctx, 7); | |
118 | bool ok = (eax == SYS_getpid || eax == getpid()) && | |
119 | ebx == 1 && ecx == 2 && edx == 3 && | |
120 | esi == 4 && edi == 5 && ebp == 6; | |
121 | ||
122 | if (!ok) | |
123 | nerrs++; | |
124 | printf("[%s]\t NR = %ld, args = %ld, %ld, %ld, %ld, %ld, %ld\n", | |
125 | (ok ? "OK" : "FAIL"), | |
126 | eax, ebx, ecx, edx, esi, edi, ebp); | |
127 | ||
128 | return _URC_NORMAL_STOP; | |
129 | } else { | |
130 | state->depth++; | |
131 | return _URC_NO_REASON; | |
132 | } | |
133 | } | |
134 | ||
135 | static void sigtrap(int sig, siginfo_t *info, void *ctx_void) | |
136 | { | |
893a3ec2 | 137 | ucontext_t *ctx = (ucontext_t *)ctx_void; |
3b56aae3 AL |
138 | struct unwind_state state; |
139 | unsigned long ip = ctx->uc_mcontext.gregs[REG_EIP]; | |
140 | ||
141 | if (!got_sysinfo && ip == sysinfo) { | |
142 | got_sysinfo = true; | |
143 | ||
144 | /* Find the return address. */ | |
145 | return_address = *(unsigned long *)(unsigned long)ctx->uc_mcontext.gregs[REG_ESP]; | |
146 | ||
147 | printf("\tIn vsyscall at 0x%lx, returning to 0x%lx\n", | |
148 | ip, return_address); | |
149 | } | |
150 | ||
151 | if (!got_sysinfo) | |
152 | return; /* Not there yet */ | |
153 | ||
154 | if (ip == return_address) { | |
155 | ctx->uc_mcontext.gregs[REG_EFL] &= ~X86_EFLAGS_TF; | |
156 | printf("\tVsyscall is done\n"); | |
157 | return; | |
158 | } | |
159 | ||
160 | printf("\tSIGTRAP at 0x%lx\n", ip); | |
161 | ||
162 | state.ip = ip; | |
163 | state.depth = -1; | |
164 | _Unwind_Backtrace(trace_fn, &state); | |
165 | } | |
166 | ||
167 | int main() | |
168 | { | |
169 | sysinfo = getauxval(AT_SYSINFO); | |
170 | printf("\tAT_SYSINFO is 0x%lx\n", sysinfo); | |
171 | ||
172 | Dl_info info; | |
173 | if (!dladdr((void *)sysinfo, &info)) { | |
174 | printf("[WARN]\tdladdr failed on AT_SYSINFO\n"); | |
175 | } else { | |
176 | printf("[OK]\tAT_SYSINFO maps to %s, loaded at 0x%p\n", | |
177 | info.dli_fname, info.dli_fbase); | |
178 | } | |
179 | ||
180 | sethandler(SIGTRAP, sigtrap, 0); | |
181 | ||
182 | syscall(SYS_getpid); /* Force symbol binding without TF set. */ | |
183 | printf("[RUN]\tSet TF and check a fast syscall\n"); | |
184 | set_eflags(get_eflags() | X86_EFLAGS_TF); | |
185 | syscall(SYS_getpid, 1, 2, 3, 4, 5, 6); | |
186 | if (!got_sysinfo) { | |
187 | set_eflags(get_eflags() & ~X86_EFLAGS_TF); | |
188 | ||
189 | /* | |
190 | * The most likely cause of this is that you're on Debian or | |
191 | * a Debian-based distro, you're missing libc6-i686, and you're | |
192 | * affected by libc/19006 (https://sourceware.org/PR19006). | |
193 | */ | |
194 | printf("[WARN]\tsyscall(2) didn't enter AT_SYSINFO\n"); | |
893a3ec2 AL |
195 | } |
196 | ||
197 | if (get_eflags() & X86_EFLAGS_TF) { | |
3b56aae3 AL |
198 | printf("[FAIL]\tTF is still set\n"); |
199 | nerrs++; | |
200 | } | |
201 | ||
202 | if (nerrs) { | |
203 | printf("[FAIL]\tThere were errors\n"); | |
204 | return 1; | |
205 | } else { | |
206 | printf("[OK]\tAll is well\n"); | |
207 | return 0; | |
208 | } | |
209 | } | |
210 | ||
211 | #endif /* New enough libc */ |