unwind_vdso.c 5.0 KB

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