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.
16 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16
20 /* We need getauxval(). */
21 printf("[SKIP]\tGLIBC before 2.16 cannot compile this test\n");
35 #include <sys/ucontext.h>
39 #include <sys/ptrace.h>
46 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
50 memset(&sa, 0, sizeof(sa));
51 sa.sa_sigaction = handler;
52 sa.sa_flags = SA_SIGINFO | flags;
53 sigemptyset(&sa.sa_mask);
54 if (sigaction(sig, &sa, 0))
58 static volatile sig_atomic_t nerrs;
59 static unsigned long sysinfo;
60 static bool got_sysinfo = false;
61 static unsigned long return_address;
64 unsigned long ip; /* trap source */
65 int depth; /* -1 until we hit the trap source */
68 _Unwind_Reason_Code trace_fn(struct _Unwind_Context * ctx, void *opaque)
70 struct unwind_state *state = opaque;
71 unsigned long ip = _Unwind_GetIP(ctx);
73 if (state->depth == -1) {
77 return _URC_NO_REASON; /* Not there yet */
79 printf("\t 0x%lx\n", ip);
81 if (ip == return_address) {
83 unsigned long eax = _Unwind_GetGR(ctx, 0);
84 unsigned long ecx = _Unwind_GetGR(ctx, 1);
85 unsigned long edx = _Unwind_GetGR(ctx, 2);
86 unsigned long ebx = _Unwind_GetGR(ctx, 3);
87 unsigned long ebp = _Unwind_GetGR(ctx, 5);
88 unsigned long esi = _Unwind_GetGR(ctx, 6);
89 unsigned long edi = _Unwind_GetGR(ctx, 7);
90 bool ok = (eax == SYS_getpid || eax == getpid()) &&
91 ebx == 1 && ecx == 2 && edx == 3 &&
92 esi == 4 && edi == 5 && ebp == 6;
96 printf("[%s]\t NR = %ld, args = %ld, %ld, %ld, %ld, %ld, %ld\n",
98 eax, ebx, ecx, edx, esi, edi, ebp);
100 return _URC_NORMAL_STOP;
103 return _URC_NO_REASON;
107 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
109 ucontext_t *ctx = (ucontext_t *)ctx_void;
110 struct unwind_state state;
111 unsigned long ip = ctx->uc_mcontext.gregs[REG_EIP];
113 if (!got_sysinfo && ip == sysinfo) {
116 /* Find the return address. */
117 return_address = *(unsigned long *)(unsigned long)ctx->uc_mcontext.gregs[REG_ESP];
119 printf("\tIn vsyscall at 0x%lx, returning to 0x%lx\n",
124 return; /* Not there yet */
126 if (ip == return_address) {
127 ctx->uc_mcontext.gregs[REG_EFL] &= ~X86_EFLAGS_TF;
128 printf("\tVsyscall is done\n");
132 printf("\tSIGTRAP at 0x%lx\n", ip);
136 _Unwind_Backtrace(trace_fn, &state);
141 sysinfo = getauxval(AT_SYSINFO);
142 printf("\tAT_SYSINFO is 0x%lx\n", sysinfo);
145 if (!dladdr((void *)sysinfo, &info)) {
146 printf("[WARN]\tdladdr failed on AT_SYSINFO\n");
148 printf("[OK]\tAT_SYSINFO maps to %s, loaded at 0x%p\n",
149 info.dli_fname, info.dli_fbase);
152 sethandler(SIGTRAP, sigtrap, 0);
154 syscall(SYS_getpid); /* Force symbol binding without TF set. */
155 printf("[RUN]\tSet TF and check a fast syscall\n");
156 set_eflags(get_eflags() | X86_EFLAGS_TF);
157 syscall(SYS_getpid, 1, 2, 3, 4, 5, 6);
159 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
162 * The most likely cause of this is that you're on Debian or
163 * a Debian-based distro, you're missing libc6-i686, and you're
164 * affected by libc/19006 (https://sourceware.org/PR19006).
166 printf("[WARN]\tsyscall(2) didn't enter AT_SYSINFO\n");
169 if (get_eflags() & X86_EFLAGS_TF) {
170 printf("[FAIL]\tTF is still set\n");
175 printf("[FAIL]\tThere were errors\n");
178 printf("[OK]\tAll is well\n");
183 #endif /* New enough libc */