diff options
author | Andy Lutomirski <luto@kernel.org> | 2015-10-05 20:47:51 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2015-10-07 05:34:06 -0400 |
commit | 3b56aae34bc695638b8673fc8459be1837c18730 (patch) | |
tree | dfba89c8a41302038504e46067aae8f60f70d166 /tools/testing | |
parent | 7e0f51cb445be8d3aee80e433ed8da4a33ad0157 (diff) |
selftests/x86: Add a test for vDSO unwinding
While the kernel itself doesn't use DWARF unwinding, user code
expects to be able to unwind the vDSO. The vsyscall
(AT_SYSINFO) entry is manually CFI-annotated, and this tests
that it unwinds correctly.
I tested the test by incorrectly annotating __kernel_vsyscall,
and the test indeed fails if I do that.
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Shuah Khan <shuahkh@osg.samsung.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Link: http://lkml.kernel.org/r/8bf736d1925cdd165c0f980156a4248e55af47a1.1444091584.git.luto@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'tools/testing')
-rw-r--r-- | tools/testing/selftests/x86/Makefile | 2 | ||||
-rw-r--r-- | tools/testing/selftests/x86/unwind_vdso.c | 209 |
2 files changed, 210 insertions, 1 deletions
diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile index fd55bc37fa18..75413529f4a2 100644 --- a/tools/testing/selftests/x86/Makefile +++ b/tools/testing/selftests/x86/Makefile | |||
@@ -5,7 +5,7 @@ include ../lib.mk | |||
5 | .PHONY: all all_32 all_64 warn_32bit_failure clean | 5 | .PHONY: all all_32 all_64 warn_32bit_failure clean |
6 | 6 | ||
7 | TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs ldt_gdt syscall_nt | 7 | TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs ldt_gdt syscall_nt |
8 | TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault sigreturn test_syscall_vdso | 8 | TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault sigreturn test_syscall_vdso unwind_vdso |
9 | 9 | ||
10 | TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY) | 10 | TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY) |
11 | BINARIES_32 := $(TARGETS_C_32BIT_ALL:%=%_32) | 11 | BINARIES_32 := $(TARGETS_C_32BIT_ALL:%=%_32) |
diff --git a/tools/testing/selftests/x86/unwind_vdso.c b/tools/testing/selftests/x86/unwind_vdso.c new file mode 100644 index 000000000000..5992ff24ab83 --- /dev/null +++ b/tools/testing/selftests/x86/unwind_vdso.c | |||
@@ -0,0 +1,209 @@ | |||
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 | { | ||
137 | ucontext_t *ctx = (ucontext_t*)ctx_void; | ||
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"); | ||
195 | } if (get_eflags() & X86_EFLAGS_TF) { | ||
196 | printf("[FAIL]\tTF is still set\n"); | ||
197 | nerrs++; | ||
198 | } | ||
199 | |||
200 | if (nerrs) { | ||
201 | printf("[FAIL]\tThere were errors\n"); | ||
202 | return 1; | ||
203 | } else { | ||
204 | printf("[OK]\tAll is well\n"); | ||
205 | return 0; | ||
206 | } | ||
207 | } | ||
208 | |||
209 | #endif /* New enough libc */ | ||