2 * unwind_vdso.c - tests unwind info for AT_SYSINFO in the vDSO
3 * Copyright (c) 2014-2015 Andrew Lutomirski
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.
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.
14 * This tests __kernel_vsyscall's unwind info.
22 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16
26 /* We need getauxval(). */
27 printf("[SKIP]\tGLIBC before 2.16 cannot compile this test\n");
41 #include <sys/ucontext.h>
45 #include <sys/ptrace.h>
52 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
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))
70 static unsigned long get_eflags(void)
73 asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
77 static void set_eflags(unsigned long eflags)
79 asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
80 : : "rm" (eflags) : "flags");
83 #define X86_EFLAGS_TF (1UL << 8)
85 static volatile sig_atomic_t nerrs;
86 static unsigned long sysinfo;
87 static bool got_sysinfo = false;
88 static unsigned long return_address;
91 unsigned long ip; /* trap source */
92 int depth; /* -1 until we hit the trap source */
95 _Unwind_Reason_Code trace_fn(struct _Unwind_Context * ctx, void *opaque)
97 struct unwind_state *state = opaque;
98 unsigned long ip = _Unwind_GetIP(ctx);
100 if (state->depth == -1) {
104 return _URC_NO_REASON; /* Not there yet */
106 printf("\t 0x%lx\n", ip);
108 if (ip == return_address) {
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;
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);
127 return _URC_NORMAL_STOP;
130 return _URC_NO_REASON;
134 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
136 ucontext_t *ctx = (ucontext_t *)ctx_void;
137 struct unwind_state state;
138 unsigned long ip = ctx->uc_mcontext.gregs[REG_EIP];
140 if (!got_sysinfo && ip == sysinfo) {
143 /* Find the return address. */
144 return_address = *(unsigned long *)(unsigned long)ctx->uc_mcontext.gregs[REG_ESP];
146 printf("\tIn vsyscall at 0x%lx, returning to 0x%lx\n",
151 return; /* Not there yet */
153 if (ip == return_address) {
154 ctx->uc_mcontext.gregs[REG_EFL] &= ~X86_EFLAGS_TF;
155 printf("\tVsyscall is done\n");
159 printf("\tSIGTRAP at 0x%lx\n", ip);
163 _Unwind_Backtrace(trace_fn, &state);
168 sysinfo = getauxval(AT_SYSINFO);
169 printf("\tAT_SYSINFO is 0x%lx\n", sysinfo);
172 if (!dladdr((void *)sysinfo, &info)) {
173 printf("[WARN]\tdladdr failed on AT_SYSINFO\n");
175 printf("[OK]\tAT_SYSINFO maps to %s, loaded at 0x%p\n",
176 info.dli_fname, info.dli_fbase);
179 sethandler(SIGTRAP, sigtrap, 0);
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);
186 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
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).
193 printf("[WARN]\tsyscall(2) didn't enter AT_SYSINFO\n");
196 if (get_eflags() & X86_EFLAGS_TF) {
197 printf("[FAIL]\tTF is still set\n");
202 printf("[FAIL]\tThere were errors\n");
205 printf("[OK]\tAll is well\n");
210 #endif /* New enough libc */