Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * unwind_vdso.c - tests unwind info for AT_SYSINFO in the vDSO
4 * Copyright (c) 2014-2015 Andrew Lutomirski
5 *
6 * This tests __kernel_vsyscall's unwind info.
7 */
8
9#define _GNU_SOURCE
10
11#include <features.h>
12#include <stdio.h>
13
14#include "helpers.h"
15
16#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16
17
18int main()
19{
20 /* We need getauxval(). */
21 printf("[SKIP]\tGLIBC before 2.16 cannot compile this test\n");
22 return 0;
23}
24
25#else
26
27#include <sys/time.h>
28#include <stdlib.h>
29#include <syscall.h>
30#include <unistd.h>
31#include <string.h>
32#include <inttypes.h>
33#include <sys/mman.h>
34#include <signal.h>
35#include <sys/ucontext.h>
36#include <err.h>
37#include <stddef.h>
38#include <stdbool.h>
39#include <sys/ptrace.h>
40#include <sys/user.h>
41#include <link.h>
42#include <sys/auxv.h>
43#include <dlfcn.h>
44#include <unwind.h>
45
46static volatile sig_atomic_t nerrs;
47static unsigned long sysinfo;
48static bool got_sysinfo = false;
49static unsigned long return_address;
50
51struct unwind_state {
52 unsigned long ip; /* trap source */
53 int depth; /* -1 until we hit the trap source */
54};
55
56_Unwind_Reason_Code trace_fn(struct _Unwind_Context * ctx, void *opaque)
57{
58 struct unwind_state *state = opaque;
59 unsigned long ip = _Unwind_GetIP(ctx);
60
61 if (state->depth == -1) {
62 if (ip == state->ip)
63 state->depth = 0;
64 else
65 return _URC_NO_REASON; /* Not there yet */
66 }
67 printf("\t 0x%lx\n", ip);
68
69 if (ip == return_address) {
70 /* Here we are. */
71 unsigned long eax = _Unwind_GetGR(ctx, 0);
72 unsigned long ecx = _Unwind_GetGR(ctx, 1);
73 unsigned long edx = _Unwind_GetGR(ctx, 2);
74 unsigned long ebx = _Unwind_GetGR(ctx, 3);
75 unsigned long ebp = _Unwind_GetGR(ctx, 5);
76 unsigned long esi = _Unwind_GetGR(ctx, 6);
77 unsigned long edi = _Unwind_GetGR(ctx, 7);
78 bool ok = (eax == SYS_getpid || eax == getpid()) &&
79 ebx == 1 && ecx == 2 && edx == 3 &&
80 esi == 4 && edi == 5 && ebp == 6;
81
82 if (!ok)
83 nerrs++;
84 printf("[%s]\t NR = %ld, args = %ld, %ld, %ld, %ld, %ld, %ld\n",
85 (ok ? "OK" : "FAIL"),
86 eax, ebx, ecx, edx, esi, edi, ebp);
87
88 return _URC_NORMAL_STOP;
89 } else {
90 state->depth++;
91 return _URC_NO_REASON;
92 }
93}
94
95static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
96{
97 ucontext_t *ctx = (ucontext_t *)ctx_void;
98 struct unwind_state state;
99 unsigned long ip = ctx->uc_mcontext.gregs[REG_EIP];
100
101 if (!got_sysinfo && ip == sysinfo) {
102 got_sysinfo = true;
103
104 /* Find the return address. */
105 return_address = *(unsigned long *)(unsigned long)ctx->uc_mcontext.gregs[REG_ESP];
106
107 printf("\tIn vsyscall at 0x%lx, returning to 0x%lx\n",
108 ip, return_address);
109 }
110
111 if (!got_sysinfo)
112 return; /* Not there yet */
113
114 if (ip == return_address) {
115 ctx->uc_mcontext.gregs[REG_EFL] &= ~X86_EFLAGS_TF;
116 printf("\tVsyscall is done\n");
117 return;
118 }
119
120 printf("\tSIGTRAP at 0x%lx\n", ip);
121
122 state.ip = ip;
123 state.depth = -1;
124 _Unwind_Backtrace(trace_fn, &state);
125}
126
127int main()
128{
129 sysinfo = getauxval(AT_SYSINFO);
130 printf("\tAT_SYSINFO is 0x%lx\n", sysinfo);
131
132 Dl_info info;
133 if (!dladdr((void *)sysinfo, &info)) {
134 printf("[WARN]\tdladdr failed on AT_SYSINFO\n");
135 } else {
136 printf("[OK]\tAT_SYSINFO maps to %s, loaded at 0x%p\n",
137 info.dli_fname, info.dli_fbase);
138 }
139
140 sethandler(SIGTRAP, sigtrap, 0);
141
142 syscall(SYS_getpid); /* Force symbol binding without TF set. */
143 printf("[RUN]\tSet TF and check a fast syscall\n");
144 set_eflags(get_eflags() | X86_EFLAGS_TF);
145 syscall(SYS_getpid, 1, 2, 3, 4, 5, 6);
146 if (!got_sysinfo) {
147 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
148
149 /*
150 * The most likely cause of this is that you're on Debian or
151 * a Debian-based distro, you're missing libc6-i686, and you're
152 * affected by libc/19006 (https://sourceware.org/PR19006).
153 */
154 printf("[WARN]\tsyscall(2) didn't enter AT_SYSINFO\n");
155 }
156
157 if (get_eflags() & X86_EFLAGS_TF) {
158 printf("[FAIL]\tTF is still set\n");
159 nerrs++;
160 }
161
162 if (nerrs) {
163 printf("[FAIL]\tThere were errors\n");
164 return 1;
165 } else {
166 printf("[OK]\tAll is well\n");
167 return 0;
168 }
169}
170
171#endif /* New enough libc */