1 // SPDX-License-Identifier: GPL-2.0-only
3 * unwind_vdso.c - tests unwind info for AT_SYSINFO in the vDSO
4 * Copyright (c) 2014-2015 Andrew Lutomirski
6 * This tests __kernel_vsyscall's unwind info.
14 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16
18 /* We need getauxval(). */
19 printf("[SKIP]\tGLIBC before 2.16 cannot compile this test\n");
33 #include <sys/ucontext.h>
37 #include <sys/ptrace.h>
44 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
48 memset(&sa, 0, sizeof(sa));
49 sa.sa_sigaction = handler;
50 sa.sa_flags = SA_SIGINFO | flags;
51 sigemptyset(&sa.sa_mask);
52 if (sigaction(sig, &sa, 0))
62 static unsigned long get_eflags(void)
65 asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
69 static void set_eflags(unsigned long eflags)
71 asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
72 : : "rm" (eflags) : "flags");
75 #define X86_EFLAGS_TF (1UL << 8)
77 static volatile sig_atomic_t nerrs;
78 static unsigned long sysinfo;
79 static bool got_sysinfo = false;
80 static unsigned long return_address;
83 unsigned long ip; /* trap source */
84 int depth; /* -1 until we hit the trap source */
87 _Unwind_Reason_Code trace_fn(struct _Unwind_Context * ctx, void *opaque)
89 struct unwind_state *state = opaque;
90 unsigned long ip = _Unwind_GetIP(ctx);
92 if (state->depth == -1) {
96 return _URC_NO_REASON; /* Not there yet */
98 printf("\t 0x%lx\n", ip);
100 if (ip == return_address) {
102 unsigned long eax = _Unwind_GetGR(ctx, 0);
103 unsigned long ecx = _Unwind_GetGR(ctx, 1);
104 unsigned long edx = _Unwind_GetGR(ctx, 2);
105 unsigned long ebx = _Unwind_GetGR(ctx, 3);
106 unsigned long ebp = _Unwind_GetGR(ctx, 5);
107 unsigned long esi = _Unwind_GetGR(ctx, 6);
108 unsigned long edi = _Unwind_GetGR(ctx, 7);
109 bool ok = (eax == SYS_getpid || eax == getpid()) &&
110 ebx == 1 && ecx == 2 && edx == 3 &&
111 esi == 4 && edi == 5 && ebp == 6;
115 printf("[%s]\t NR = %ld, args = %ld, %ld, %ld, %ld, %ld, %ld\n",
116 (ok ? "OK" : "FAIL"),
117 eax, ebx, ecx, edx, esi, edi, ebp);
119 return _URC_NORMAL_STOP;
122 return _URC_NO_REASON;
126 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
128 ucontext_t *ctx = (ucontext_t *)ctx_void;
129 struct unwind_state state;
130 unsigned long ip = ctx->uc_mcontext.gregs[REG_EIP];
132 if (!got_sysinfo && ip == sysinfo) {
135 /* Find the return address. */
136 return_address = *(unsigned long *)(unsigned long)ctx->uc_mcontext.gregs[REG_ESP];
138 printf("\tIn vsyscall at 0x%lx, returning to 0x%lx\n",
143 return; /* Not there yet */
145 if (ip == return_address) {
146 ctx->uc_mcontext.gregs[REG_EFL] &= ~X86_EFLAGS_TF;
147 printf("\tVsyscall is done\n");
151 printf("\tSIGTRAP at 0x%lx\n", ip);
155 _Unwind_Backtrace(trace_fn, &state);
160 sysinfo = getauxval(AT_SYSINFO);
161 printf("\tAT_SYSINFO is 0x%lx\n", sysinfo);
164 if (!dladdr((void *)sysinfo, &info)) {
165 printf("[WARN]\tdladdr failed on AT_SYSINFO\n");
167 printf("[OK]\tAT_SYSINFO maps to %s, loaded at 0x%p\n",
168 info.dli_fname, info.dli_fbase);
171 sethandler(SIGTRAP, sigtrap, 0);
173 syscall(SYS_getpid); /* Force symbol binding without TF set. */
174 printf("[RUN]\tSet TF and check a fast syscall\n");
175 set_eflags(get_eflags() | X86_EFLAGS_TF);
176 syscall(SYS_getpid, 1, 2, 3, 4, 5, 6);
178 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
181 * The most likely cause of this is that you're on Debian or
182 * a Debian-based distro, you're missing libc6-i686, and you're
183 * affected by libc/19006 (https://sourceware.org/PR19006).
185 printf("[WARN]\tsyscall(2) didn't enter AT_SYSINFO\n");
188 if (get_eflags() & X86_EFLAGS_TF) {
189 printf("[FAIL]\tTF is still set\n");
194 printf("[FAIL]\tThere were errors\n");
197 printf("[OK]\tAll is well\n");
202 #endif /* New enough libc */