Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
24int 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 <link.h>
48#include <sys/auxv.h>
49#include <dlfcn.h>
50#include <unwind.h>
51
52static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
53 int flags)
54{
55 struct sigaction sa;
56 memset(&sa, 0, sizeof(sa));
57 sa.sa_sigaction = handler;
58 sa.sa_flags = SA_SIGINFO | flags;
59 sigemptyset(&sa.sa_mask);
60 if (sigaction(sig, &sa, 0))
61 err(1, "sigaction");
62}
63
64#ifdef __x86_64__
65# define WIDTH "q"
66#else
67# define WIDTH "l"
68#endif
69
70static unsigned long get_eflags(void)
71{
72 unsigned long eflags;
73 asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
74 return eflags;
75}
76
77static void set_eflags(unsigned long eflags)
78{
79 asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
80 : : "rm" (eflags) : "flags");
81}
82
83#define X86_EFLAGS_TF (1UL << 8)
84
85static volatile sig_atomic_t nerrs;
86static unsigned long sysinfo;
87static bool got_sysinfo = false;
88static unsigned long return_address;
89
90struct unwind_state {
91 unsigned long ip; /* trap source */
92 int depth; /* -1 until we hit the trap source */
93};
94
95_Unwind_Reason_Code trace_fn(struct _Unwind_Context * ctx, void *opaque)
96{
97 struct unwind_state *state = opaque;
98 unsigned long ip = _Unwind_GetIP(ctx);
99
100 if (state->depth == -1) {
101 if (ip == state->ip)
102 state->depth = 0;
103 else
104 return _URC_NO_REASON; /* Not there yet */
105 }
106 printf("\t 0x%lx\n", ip);
107
108 if (ip == return_address) {
109 /* Here we are. */
110 unsigned long eax = _Unwind_GetGR(ctx, 0);
111 unsigned long ecx = _Unwind_GetGR(ctx, 1);
112 unsigned long edx = _Unwind_GetGR(ctx, 2);
113 unsigned long ebx = _Unwind_GetGR(ctx, 3);
114 unsigned long ebp = _Unwind_GetGR(ctx, 5);
115 unsigned long esi = _Unwind_GetGR(ctx, 6);
116 unsigned long edi = _Unwind_GetGR(ctx, 7);
117 bool ok = (eax == SYS_getpid || eax == getpid()) &&
118 ebx == 1 && ecx == 2 && edx == 3 &&
119 esi == 4 && edi == 5 && ebp == 6;
120
121 if (!ok)
122 nerrs++;
123 printf("[%s]\t NR = %ld, args = %ld, %ld, %ld, %ld, %ld, %ld\n",
124 (ok ? "OK" : "FAIL"),
125 eax, ebx, ecx, edx, esi, edi, ebp);
126
127 return _URC_NORMAL_STOP;
128 } else {
129 state->depth++;
130 return _URC_NO_REASON;
131 }
132}
133
134static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
135{
136 ucontext_t *ctx = (ucontext_t *)ctx_void;
137 struct unwind_state state;
138 unsigned long ip = ctx->uc_mcontext.gregs[REG_EIP];
139
140 if (!got_sysinfo && ip == sysinfo) {
141 got_sysinfo = true;
142
143 /* Find the return address. */
144 return_address = *(unsigned long *)(unsigned long)ctx->uc_mcontext.gregs[REG_ESP];
145
146 printf("\tIn vsyscall at 0x%lx, returning to 0x%lx\n",
147 ip, return_address);
148 }
149
150 if (!got_sysinfo)
151 return; /* Not there yet */
152
153 if (ip == return_address) {
154 ctx->uc_mcontext.gregs[REG_EFL] &= ~X86_EFLAGS_TF;
155 printf("\tVsyscall is done\n");
156 return;
157 }
158
159 printf("\tSIGTRAP at 0x%lx\n", ip);
160
161 state.ip = ip;
162 state.depth = -1;
163 _Unwind_Backtrace(trace_fn, &state);
164}
165
166int main()
167{
168 sysinfo = getauxval(AT_SYSINFO);
169 printf("\tAT_SYSINFO is 0x%lx\n", sysinfo);
170
171 Dl_info info;
172 if (!dladdr((void *)sysinfo, &info)) {
173 printf("[WARN]\tdladdr failed on AT_SYSINFO\n");
174 } else {
175 printf("[OK]\tAT_SYSINFO maps to %s, loaded at 0x%p\n",
176 info.dli_fname, info.dli_fbase);
177 }
178
179 sethandler(SIGTRAP, sigtrap, 0);
180
181 syscall(SYS_getpid); /* Force symbol binding without TF set. */
182 printf("[RUN]\tSet TF and check a fast syscall\n");
183 set_eflags(get_eflags() | X86_EFLAGS_TF);
184 syscall(SYS_getpid, 1, 2, 3, 4, 5, 6);
185 if (!got_sysinfo) {
186 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
187
188 /*
189 * The most likely cause of this is that you're on Debian or
190 * a Debian-based distro, you're missing libc6-i686, and you're
191 * affected by libc/19006 (https://sourceware.org/PR19006).
192 */
193 printf("[WARN]\tsyscall(2) didn't enter AT_SYSINFO\n");
194 }
195
196 if (get_eflags() & X86_EFLAGS_TF) {
197 printf("[FAIL]\tTF is still set\n");
198 nerrs++;
199 }
200
201 if (nerrs) {
202 printf("[FAIL]\tThere were errors\n");
203 return 1;
204 } else {
205 printf("[OK]\tAll is well\n");
206 return 0;
207 }
208}
209
210#endif /* New enough libc */