aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests
diff options
context:
space:
mode:
authorShuah Khan <shuahkh@osg.samsung.com>2016-09-13 14:06:20 -0400
committerShuah Khan <shuahkh@osg.samsung.com>2016-09-20 11:58:04 -0400
commitf9b6b0ef60349cf1747d8f366f23900671f888c5 (patch)
treeb99a15e9b1eb73b160f6495a0371c538733608f3 /tools/testing/selftests
parent8dbbf854202610a033a0788c33d8feb1548d3eeb (diff)
selftests: move vDSO tests from Documentation/vDSO
Remove vDSO from Makefile to move the to selftests. Update vDSO Makefile to work under selftests. vDSO will not be run as part of selftests suite and will not be included in install targets. They can be built separately for now. Acked-by: Jonathan Corbet <corbet@lwn.net> Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
Diffstat (limited to 'tools/testing/selftests')
-rw-r--r--tools/testing/selftests/vDSO/.gitignore2
-rw-r--r--tools/testing/selftests/vDSO/Makefile20
-rw-r--r--tools/testing/selftests/vDSO/parse_vdso.c269
-rw-r--r--tools/testing/selftests/vDSO/vdso_standalone_test_x86.c128
-rw-r--r--tools/testing/selftests/vDSO/vdso_test.c52
5 files changed, 471 insertions, 0 deletions
diff --git a/tools/testing/selftests/vDSO/.gitignore b/tools/testing/selftests/vDSO/.gitignore
new file mode 100644
index 000000000000..133bf9ee986c
--- /dev/null
+++ b/tools/testing/selftests/vDSO/.gitignore
@@ -0,0 +1,2 @@
1vdso_test
2vdso_standalone_test_x86
diff --git a/tools/testing/selftests/vDSO/Makefile b/tools/testing/selftests/vDSO/Makefile
new file mode 100644
index 000000000000..706b68b1c372
--- /dev/null
+++ b/tools/testing/selftests/vDSO/Makefile
@@ -0,0 +1,20 @@
1ifndef CROSS_COMPILE
2CFLAGS := -std=gnu99
3CFLAGS_vdso_standalone_test_x86 := -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector
4ifeq ($(CONFIG_X86_32),y)
5LDLIBS += -lgcc_s
6endif
7
8TEST_PROGS := vdso_test vdso_standalone_test_x86
9
10all: $(TEST_PROGS)
11vdso_test: parse_vdso.c vdso_test.c
12vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c
13 $(CC) $(CFLAGS) $(CFLAGS_vdso_standalone_test_x86) \
14 vdso_standalone_test_x86.c parse_vdso.c \
15 -o vdso_standalone_test_x86
16
17include ../lib.mk
18clean:
19 rm -fr $(TEST_PROGS)
20endif
diff --git a/tools/testing/selftests/vDSO/parse_vdso.c b/tools/testing/selftests/vDSO/parse_vdso.c
new file mode 100644
index 000000000000..1dbb4b87268f
--- /dev/null
+++ b/tools/testing/selftests/vDSO/parse_vdso.c
@@ -0,0 +1,269 @@
1/*
2 * parse_vdso.c: Linux reference vDSO parser
3 * Written by Andrew Lutomirski, 2011-2014.
4 *
5 * This code is meant to be linked in to various programs that run on Linux.
6 * As such, it is available with as few restrictions as possible. This file
7 * is licensed under the Creative Commons Zero License, version 1.0,
8 * available at http://creativecommons.org/publicdomain/zero/1.0/legalcode
9 *
10 * The vDSO is a regular ELF DSO that the kernel maps into user space when
11 * it starts a program. It works equally well in statically and dynamically
12 * linked binaries.
13 *
14 * This code is tested on x86. In principle it should work on any
15 * architecture that has a vDSO.
16 */
17
18#include <stdbool.h>
19#include <stdint.h>
20#include <string.h>
21#include <limits.h>
22#include <elf.h>
23
24/*
25 * To use this vDSO parser, first call one of the vdso_init_* functions.
26 * If you've already parsed auxv, then pass the value of AT_SYSINFO_EHDR
27 * to vdso_init_from_sysinfo_ehdr. Otherwise pass auxv to vdso_init_from_auxv.
28 * Then call vdso_sym for each symbol you want. For example, to look up
29 * gettimeofday on x86_64, use:
30 *
31 * <some pointer> = vdso_sym("LINUX_2.6", "gettimeofday");
32 * or
33 * <some pointer> = vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
34 *
35 * vdso_sym will return 0 if the symbol doesn't exist or if the init function
36 * failed or was not called. vdso_sym is a little slow, so its return value
37 * should be cached.
38 *
39 * vdso_sym is threadsafe; the init functions are not.
40 *
41 * These are the prototypes:
42 */
43extern void vdso_init_from_auxv(void *auxv);
44extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
45extern void *vdso_sym(const char *version, const char *name);
46
47
48/* And here's the code. */
49#ifndef ELF_BITS
50# if ULONG_MAX > 0xffffffffUL
51# define ELF_BITS 64
52# else
53# define ELF_BITS 32
54# endif
55#endif
56
57#define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
58#define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
59#define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
60
61static struct vdso_info
62{
63 bool valid;
64
65 /* Load information */
66 uintptr_t load_addr;
67 uintptr_t load_offset; /* load_addr - recorded vaddr */
68
69 /* Symbol table */
70 ELF(Sym) *symtab;
71 const char *symstrings;
72 ELF(Word) *bucket, *chain;
73 ELF(Word) nbucket, nchain;
74
75 /* Version table */
76 ELF(Versym) *versym;
77 ELF(Verdef) *verdef;
78} vdso_info;
79
80/* Straight from the ELF specification. */
81static unsigned long elf_hash(const unsigned char *name)
82{
83 unsigned long h = 0, g;
84 while (*name)
85 {
86 h = (h << 4) + *name++;
87 if (g = h & 0xf0000000)
88 h ^= g >> 24;
89 h &= ~g;
90 }
91 return h;
92}
93
94void vdso_init_from_sysinfo_ehdr(uintptr_t base)
95{
96 size_t i;
97 bool found_vaddr = false;
98
99 vdso_info.valid = false;
100
101 vdso_info.load_addr = base;
102
103 ELF(Ehdr) *hdr = (ELF(Ehdr)*)base;
104 if (hdr->e_ident[EI_CLASS] !=
105 (ELF_BITS == 32 ? ELFCLASS32 : ELFCLASS64)) {
106 return; /* Wrong ELF class -- check ELF_BITS */
107 }
108
109 ELF(Phdr) *pt = (ELF(Phdr)*)(vdso_info.load_addr + hdr->e_phoff);
110 ELF(Dyn) *dyn = 0;
111
112 /*
113 * We need two things from the segment table: the load offset
114 * and the dynamic table.
115 */
116 for (i = 0; i < hdr->e_phnum; i++)
117 {
118 if (pt[i].p_type == PT_LOAD && !found_vaddr) {
119 found_vaddr = true;
120 vdso_info.load_offset = base
121 + (uintptr_t)pt[i].p_offset
122 - (uintptr_t)pt[i].p_vaddr;
123 } else if (pt[i].p_type == PT_DYNAMIC) {
124 dyn = (ELF(Dyn)*)(base + pt[i].p_offset);
125 }
126 }
127
128 if (!found_vaddr || !dyn)
129 return; /* Failed */
130
131 /*
132 * Fish out the useful bits of the dynamic table.
133 */
134 ELF(Word) *hash = 0;
135 vdso_info.symstrings = 0;
136 vdso_info.symtab = 0;
137 vdso_info.versym = 0;
138 vdso_info.verdef = 0;
139 for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
140 switch (dyn[i].d_tag) {
141 case DT_STRTAB:
142 vdso_info.symstrings = (const char *)
143 ((uintptr_t)dyn[i].d_un.d_ptr
144 + vdso_info.load_offset);
145 break;
146 case DT_SYMTAB:
147 vdso_info.symtab = (ELF(Sym) *)
148 ((uintptr_t)dyn[i].d_un.d_ptr
149 + vdso_info.load_offset);
150 break;
151 case DT_HASH:
152 hash = (ELF(Word) *)
153 ((uintptr_t)dyn[i].d_un.d_ptr
154 + vdso_info.load_offset);
155 break;
156 case DT_VERSYM:
157 vdso_info.versym = (ELF(Versym) *)
158 ((uintptr_t)dyn[i].d_un.d_ptr
159 + vdso_info.load_offset);
160 break;
161 case DT_VERDEF:
162 vdso_info.verdef = (ELF(Verdef) *)
163 ((uintptr_t)dyn[i].d_un.d_ptr
164 + vdso_info.load_offset);
165 break;
166 }
167 }
168 if (!vdso_info.symstrings || !vdso_info.symtab || !hash)
169 return; /* Failed */
170
171 if (!vdso_info.verdef)
172 vdso_info.versym = 0;
173
174 /* Parse the hash table header. */
175 vdso_info.nbucket = hash[0];
176 vdso_info.nchain = hash[1];
177 vdso_info.bucket = &hash[2];
178 vdso_info.chain = &hash[vdso_info.nbucket + 2];
179
180 /* That's all we need. */
181 vdso_info.valid = true;
182}
183
184static bool vdso_match_version(ELF(Versym) ver,
185 const char *name, ELF(Word) hash)
186{
187 /*
188 * This is a helper function to check if the version indexed by
189 * ver matches name (which hashes to hash).
190 *
191 * The version definition table is a mess, and I don't know how
192 * to do this in better than linear time without allocating memory
193 * to build an index. I also don't know why the table has
194 * variable size entries in the first place.
195 *
196 * For added fun, I can't find a comprehensible specification of how
197 * to parse all the weird flags in the table.
198 *
199 * So I just parse the whole table every time.
200 */
201
202 /* First step: find the version definition */
203 ver &= 0x7fff; /* Apparently bit 15 means "hidden" */
204 ELF(Verdef) *def = vdso_info.verdef;
205 while(true) {
206 if ((def->vd_flags & VER_FLG_BASE) == 0
207 && (def->vd_ndx & 0x7fff) == ver)
208 break;
209
210 if (def->vd_next == 0)
211 return false; /* No definition. */
212
213 def = (ELF(Verdef) *)((char *)def + def->vd_next);
214 }
215
216 /* Now figure out whether it matches. */
217 ELF(Verdaux) *aux = (ELF(Verdaux)*)((char *)def + def->vd_aux);
218 return def->vd_hash == hash
219 && !strcmp(name, vdso_info.symstrings + aux->vda_name);
220}
221
222void *vdso_sym(const char *version, const char *name)
223{
224 unsigned long ver_hash;
225 if (!vdso_info.valid)
226 return 0;
227
228 ver_hash = elf_hash(version);
229 ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
230
231 for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
232 ELF(Sym) *sym = &vdso_info.symtab[chain];
233
234 /* Check for a defined global or weak function w/ right name. */
235 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
236 continue;
237 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
238 ELF64_ST_BIND(sym->st_info) != STB_WEAK)
239 continue;
240 if (sym->st_shndx == SHN_UNDEF)
241 continue;
242 if (strcmp(name, vdso_info.symstrings + sym->st_name))
243 continue;
244
245 /* Check symbol version. */
246 if (vdso_info.versym
247 && !vdso_match_version(vdso_info.versym[chain],
248 version, ver_hash))
249 continue;
250
251 return (void *)(vdso_info.load_offset + sym->st_value);
252 }
253
254 return 0;
255}
256
257void vdso_init_from_auxv(void *auxv)
258{
259 ELF(auxv_t) *elf_auxv = auxv;
260 for (int i = 0; elf_auxv[i].a_type != AT_NULL; i++)
261 {
262 if (elf_auxv[i].a_type == AT_SYSINFO_EHDR) {
263 vdso_init_from_sysinfo_ehdr(elf_auxv[i].a_un.a_val);
264 return;
265 }
266 }
267
268 vdso_info.valid = false;
269}
diff --git a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
new file mode 100644
index 000000000000..93b0ebf8cc38
--- /dev/null
+++ b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
@@ -0,0 +1,128 @@
1/*
2 * vdso_test.c: Sample code to test parse_vdso.c on x86
3 * Copyright (c) 2011-2014 Andy Lutomirski
4 * Subject to the GNU General Public License, version 2
5 *
6 * You can amuse yourself by compiling with:
7 * gcc -std=gnu99 -nostdlib
8 * -Os -fno-asynchronous-unwind-tables -flto -lgcc_s
9 * vdso_standalone_test_x86.c parse_vdso.c
10 * to generate a small binary. On x86_64, you can omit -lgcc_s
11 * if you want the binary to be completely standalone.
12 */
13
14#include <sys/syscall.h>
15#include <sys/time.h>
16#include <unistd.h>
17#include <stdint.h>
18
19extern void *vdso_sym(const char *version, const char *name);
20extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
21extern void vdso_init_from_auxv(void *auxv);
22
23/* We need a libc functions... */
24int strcmp(const char *a, const char *b)
25{
26 /* This implementation is buggy: it never returns -1. */
27 while (*a || *b) {
28 if (*a != *b)
29 return 1;
30 if (*a == 0 || *b == 0)
31 return 1;
32 a++;
33 b++;
34 }
35
36 return 0;
37}
38
39/* ...and two syscalls. This is x86-specific. */
40static inline long x86_syscall3(long nr, long a0, long a1, long a2)
41{
42 long ret;
43#ifdef __x86_64__
44 asm volatile ("syscall" : "=a" (ret) : "a" (nr),
45 "D" (a0), "S" (a1), "d" (a2) :
46 "cc", "memory", "rcx",
47 "r8", "r9", "r10", "r11" );
48#else
49 asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
50 "b" (a0), "c" (a1), "d" (a2) :
51 "cc", "memory" );
52#endif
53 return ret;
54}
55
56static inline long linux_write(int fd, const void *data, size_t len)
57{
58 return x86_syscall3(__NR_write, fd, (long)data, (long)len);
59}
60
61static inline void linux_exit(int code)
62{
63 x86_syscall3(__NR_exit, code, 0, 0);
64}
65
66void to_base10(char *lastdig, time_t n)
67{
68 while (n) {
69 *lastdig = (n % 10) + '0';
70 n /= 10;
71 lastdig--;
72 }
73}
74
75__attribute__((externally_visible)) void c_main(void **stack)
76{
77 /* Parse the stack */
78 long argc = (long)*stack;
79 stack += argc + 2;
80
81 /* Now we're pointing at the environment. Skip it. */
82 while(*stack)
83 stack++;
84 stack++;
85
86 /* Now we're pointing at auxv. Initialize the vDSO parser. */
87 vdso_init_from_auxv((void *)stack);
88
89 /* Find gettimeofday. */
90 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
91 gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
92
93 if (!gtod)
94 linux_exit(1);
95
96 struct timeval tv;
97 long ret = gtod(&tv, 0);
98
99 if (ret == 0) {
100 char buf[] = "The time is .000000\n";
101 to_base10(buf + 31, tv.tv_sec);
102 to_base10(buf + 38, tv.tv_usec);
103 linux_write(1, buf, sizeof(buf) - 1);
104 } else {
105 linux_exit(ret);
106 }
107
108 linux_exit(0);
109}
110
111/*
112 * This is the real entry point. It passes the initial stack into
113 * the C entry point.
114 */
115asm (
116 ".text\n"
117 ".global _start\n"
118 ".type _start,@function\n"
119 "_start:\n\t"
120#ifdef __x86_64__
121 "mov %rsp,%rdi\n\t"
122 "jmp c_main"
123#else
124 "push %esp\n\t"
125 "call c_main\n\t"
126 "int $3"
127#endif
128 );
diff --git a/tools/testing/selftests/vDSO/vdso_test.c b/tools/testing/selftests/vDSO/vdso_test.c
new file mode 100644
index 000000000000..8daeb7d7032c
--- /dev/null
+++ b/tools/testing/selftests/vDSO/vdso_test.c
@@ -0,0 +1,52 @@
1/*
2 * vdso_test.c: Sample code to test parse_vdso.c
3 * Copyright (c) 2014 Andy Lutomirski
4 * Subject to the GNU General Public License, version 2
5 *
6 * Compile with:
7 * gcc -std=gnu99 vdso_test.c parse_vdso.c
8 *
9 * Tested on x86, 32-bit and 64-bit. It may work on other architectures, too.
10 */
11
12#include <stdint.h>
13#include <elf.h>
14#include <stdio.h>
15#include <sys/auxv.h>
16#include <sys/time.h>
17
18extern void *vdso_sym(const char *version, const char *name);
19extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
20extern void vdso_init_from_auxv(void *auxv);
21
22int main(int argc, char **argv)
23{
24 unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
25 if (!sysinfo_ehdr) {
26 printf("AT_SYSINFO_EHDR is not present!\n");
27 return 0;
28 }
29
30 vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
31
32 /* Find gettimeofday. */
33 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
34 gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
35
36 if (!gtod) {
37 printf("Could not find __vdso_gettimeofday\n");
38 return 1;
39 }
40
41 struct timeval tv;
42 long ret = gtod(&tv, 0);
43
44 if (ret == 0) {
45 printf("The time is %lld.%06lld\n",
46 (long long)tv.tv_sec, (long long)tv.tv_usec);
47 } else {
48 printf("__vdso_gettimeofday failed\n");
49 }
50
51 return 0;
52}