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>
47 #include <sys/ucontext.h>
53 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
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))
71 static unsigned long get_eflags(void)
74 asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
78 static void set_eflags(unsigned long eflags)
80 asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
81 : : "rm" (eflags) : "flags");
84 #define X86_EFLAGS_TF (1UL << 8)
86 static volatile sig_atomic_t nerrs;
87 static unsigned long sysinfo;
88 static bool got_sysinfo = false;
89 static unsigned long return_address;
92 unsigned long ip; /* trap source */
93 int depth; /* -1 until we hit the trap source */
96 _Unwind_Reason_Code trace_fn(struct _Unwind_Context * ctx, void *opaque)
98 struct unwind_state *state = opaque;
99 unsigned long ip = _Unwind_GetIP(ctx);
101 if (state->depth == -1) {
105 return _URC_NO_REASON; /* Not there yet */
107 printf("\t 0x%lx\n", ip);
109 if (ip == return_address) {
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;
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);
128 return _URC_NORMAL_STOP;
131 return _URC_NO_REASON;
135 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
137 ucontext_t *ctx = (ucontext_t *)ctx_void;
138 struct unwind_state state;
139 unsigned long ip = ctx->uc_mcontext.gregs[REG_EIP];
141 if (!got_sysinfo && ip == sysinfo) {
144 /* Find the return address. */
145 return_address = *(unsigned long *)(unsigned long)ctx->uc_mcontext.gregs[REG_ESP];
147 printf("\tIn vsyscall at 0x%lx, returning to 0x%lx\n",
152 return; /* Not there yet */
154 if (ip == return_address) {
155 ctx->uc_mcontext.gregs[REG_EFL] &= ~X86_EFLAGS_TF;
156 printf("\tVsyscall is done\n");
160 printf("\tSIGTRAP at 0x%lx\n", ip);
164 _Unwind_Backtrace(trace_fn, &state);
169 sysinfo = getauxval(AT_SYSINFO);
170 printf("\tAT_SYSINFO is 0x%lx\n", sysinfo);
173 if (!dladdr((void *)sysinfo, &info)) {
174 printf("[WARN]\tdladdr failed on AT_SYSINFO\n");
176 printf("[OK]\tAT_SYSINFO maps to %s, loaded at 0x%p\n",
177 info.dli_fname, info.dli_fbase);
180 sethandler(SIGTRAP, sigtrap, 0);
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);
187 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
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).
194 printf("[WARN]\tsyscall(2) didn't enter AT_SYSINFO\n");
197 if (get_eflags() & X86_EFLAGS_TF) {
198 printf("[FAIL]\tTF is still set\n");
203 printf("[FAIL]\tThere were errors\n");
206 printf("[OK]\tAll is well\n");
211 #endif /* New enough libc */