sanitizer_stoptheworld_linux_libcdep.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. //===-- sanitizer_stoptheworld_linux_libcdep.cc ---------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // See sanitizer_stoptheworld.h for details.
  9. // This implementation was inspired by Markus Gutschke's linuxthreads.cc.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_platform.h"
  13. #if SANITIZER_LINUX && defined(__x86_64__)
  14. #include "sanitizer_stoptheworld.h"
  15. #include "sanitizer_platform_limits_posix.h"
  16. #include <errno.h>
  17. #include <sched.h> // for CLONE_* definitions
  18. #include <stddef.h>
  19. #include <sys/prctl.h> // for PR_* definitions
  20. #include <sys/ptrace.h> // for PTRACE_* definitions
  21. #include <sys/types.h> // for pid_t
  22. #if SANITIZER_ANDROID && defined(__arm__)
  23. # include <linux/user.h> // for pt_regs
  24. #else
  25. # include <sys/user.h> // for user_regs_struct
  26. #endif
  27. #include <sys/wait.h> // for signal-related stuff
  28. #ifdef sa_handler
  29. # undef sa_handler
  30. #endif
  31. #ifdef sa_sigaction
  32. # undef sa_sigaction
  33. #endif
  34. #include "sanitizer_common.h"
  35. #include "sanitizer_flags.h"
  36. #include "sanitizer_libc.h"
  37. #include "sanitizer_linux.h"
  38. #include "sanitizer_mutex.h"
  39. #include "sanitizer_placement_new.h"
  40. // This module works by spawning a Linux task which then attaches to every
  41. // thread in the caller process with ptrace. This suspends the threads, and
  42. // PTRACE_GETREGS can then be used to obtain their register state. The callback
  43. // supplied to StopTheWorld() is run in the tracer task while the threads are
  44. // suspended.
  45. // The tracer task must be placed in a different thread group for ptrace to
  46. // work, so it cannot be spawned as a pthread. Instead, we use the low-level
  47. // clone() interface (we want to share the address space with the caller
  48. // process, so we prefer clone() over fork()).
  49. //
  50. // We don't use any libc functions, relying instead on direct syscalls. There
  51. // are two reasons for this:
  52. // 1. calling a library function while threads are suspended could cause a
  53. // deadlock, if one of the treads happens to be holding a libc lock;
  54. // 2. it's generally not safe to call libc functions from the tracer task,
  55. // because clone() does not set up a thread-local storage for it. Any
  56. // thread-local variables used by libc will be shared between the tracer task
  57. // and the thread which spawned it.
  58. COMPILER_CHECK(sizeof(SuspendedThreadID) == sizeof(pid_t));
  59. namespace __sanitizer {
  60. // This class handles thread suspending/unsuspending in the tracer thread.
  61. class ThreadSuspender {
  62. public:
  63. explicit ThreadSuspender(pid_t pid)
  64. : pid_(pid) {
  65. CHECK_GE(pid, 0);
  66. }
  67. bool SuspendAllThreads();
  68. void ResumeAllThreads();
  69. void KillAllThreads();
  70. SuspendedThreadsList &suspended_threads_list() {
  71. return suspended_threads_list_;
  72. }
  73. private:
  74. SuspendedThreadsList suspended_threads_list_;
  75. pid_t pid_;
  76. bool SuspendThread(SuspendedThreadID thread_id);
  77. };
  78. bool ThreadSuspender::SuspendThread(SuspendedThreadID thread_id) {
  79. // Are we already attached to this thread?
  80. // Currently this check takes linear time, however the number of threads is
  81. // usually small.
  82. if (suspended_threads_list_.Contains(thread_id))
  83. return false;
  84. int pterrno;
  85. if (internal_iserror(internal_ptrace(PTRACE_ATTACH, thread_id, NULL, NULL),
  86. &pterrno)) {
  87. // Either the thread is dead, or something prevented us from attaching.
  88. // Log this event and move on.
  89. VReport(1, "Could not attach to thread %d (errno %d).\n", thread_id,
  90. pterrno);
  91. return false;
  92. } else {
  93. VReport(1, "Attached to thread %d.\n", thread_id);
  94. // The thread is not guaranteed to stop before ptrace returns, so we must
  95. // wait on it.
  96. uptr waitpid_status;
  97. HANDLE_EINTR(waitpid_status, internal_waitpid(thread_id, NULL, __WALL));
  98. int wperrno;
  99. if (internal_iserror(waitpid_status, &wperrno)) {
  100. // Got a ECHILD error. I don't think this situation is possible, but it
  101. // doesn't hurt to report it.
  102. VReport(1, "Waiting on thread %d failed, detaching (errno %d).\n",
  103. thread_id, wperrno);
  104. internal_ptrace(PTRACE_DETACH, thread_id, NULL, NULL);
  105. return false;
  106. }
  107. suspended_threads_list_.Append(thread_id);
  108. return true;
  109. }
  110. }
  111. void ThreadSuspender::ResumeAllThreads() {
  112. for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++) {
  113. pid_t tid = suspended_threads_list_.GetThreadID(i);
  114. int pterrno;
  115. if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, NULL, NULL),
  116. &pterrno)) {
  117. VReport(1, "Detached from thread %d.\n", tid);
  118. } else {
  119. // Either the thread is dead, or we are already detached.
  120. // The latter case is possible, for instance, if this function was called
  121. // from a signal handler.
  122. VReport(1, "Could not detach from thread %d (errno %d).\n", tid, pterrno);
  123. }
  124. }
  125. }
  126. void ThreadSuspender::KillAllThreads() {
  127. for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++)
  128. internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i),
  129. NULL, NULL);
  130. }
  131. bool ThreadSuspender::SuspendAllThreads() {
  132. ThreadLister thread_lister(pid_);
  133. bool added_threads;
  134. do {
  135. // Run through the directory entries once.
  136. added_threads = false;
  137. pid_t tid = thread_lister.GetNextTID();
  138. while (tid >= 0) {
  139. if (SuspendThread(tid))
  140. added_threads = true;
  141. tid = thread_lister.GetNextTID();
  142. }
  143. if (thread_lister.error()) {
  144. // Detach threads and fail.
  145. ResumeAllThreads();
  146. return false;
  147. }
  148. thread_lister.Reset();
  149. } while (added_threads);
  150. return true;
  151. }
  152. // Pointer to the ThreadSuspender instance for use in signal handler.
  153. static ThreadSuspender *thread_suspender_instance = NULL;
  154. // Signals that should not be blocked (this is used in the parent thread as well
  155. // as the tracer thread).
  156. static const int kUnblockedSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV,
  157. SIGBUS, SIGXCPU, SIGXFSZ };
  158. // Structure for passing arguments into the tracer thread.
  159. struct TracerThreadArgument {
  160. StopTheWorldCallback callback;
  161. void *callback_argument;
  162. // The tracer thread waits on this mutex while the parent finishes its
  163. // preparations.
  164. BlockingMutex mutex;
  165. uptr parent_pid;
  166. };
  167. static DieCallbackType old_die_callback;
  168. // Signal handler to wake up suspended threads when the tracer thread dies.
  169. void TracerThreadSignalHandler(int signum, void *siginfo, void *) {
  170. if (thread_suspender_instance != NULL) {
  171. if (signum == SIGABRT)
  172. thread_suspender_instance->KillAllThreads();
  173. else
  174. thread_suspender_instance->ResumeAllThreads();
  175. }
  176. internal__exit((signum == SIGABRT) ? 1 : 2);
  177. }
  178. static void TracerThreadDieCallback() {
  179. // Generally a call to Die() in the tracer thread should be fatal to the
  180. // parent process as well, because they share the address space.
  181. // This really only works correctly if all the threads are suspended at this
  182. // point. So we correctly handle calls to Die() from within the callback, but
  183. // not those that happen before or after the callback. Hopefully there aren't
  184. // a lot of opportunities for that to happen...
  185. if (thread_suspender_instance)
  186. thread_suspender_instance->KillAllThreads();
  187. if (old_die_callback)
  188. old_die_callback();
  189. }
  190. // Size of alternative stack for signal handlers in the tracer thread.
  191. static const int kHandlerStackSize = 4096;
  192. // This function will be run as a cloned task.
  193. static int TracerThread(void* argument) {
  194. TracerThreadArgument *tracer_thread_argument =
  195. (TracerThreadArgument *)argument;
  196. internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  197. // Check if parent is already dead.
  198. if (internal_getppid() != tracer_thread_argument->parent_pid)
  199. internal__exit(4);
  200. // Wait for the parent thread to finish preparations.
  201. tracer_thread_argument->mutex.Lock();
  202. tracer_thread_argument->mutex.Unlock();
  203. SetDieCallback(TracerThreadDieCallback);
  204. ThreadSuspender thread_suspender(internal_getppid());
  205. // Global pointer for the signal handler.
  206. thread_suspender_instance = &thread_suspender;
  207. // Alternate stack for signal handling.
  208. InternalScopedBuffer<char> handler_stack_memory(kHandlerStackSize);
  209. struct sigaltstack handler_stack;
  210. internal_memset(&handler_stack, 0, sizeof(handler_stack));
  211. handler_stack.ss_sp = handler_stack_memory.data();
  212. handler_stack.ss_size = kHandlerStackSize;
  213. internal_sigaltstack(&handler_stack, NULL);
  214. // Install our handler for fatal signals. Other signals should be blocked by
  215. // the mask we inherited from the caller thread.
  216. for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
  217. signal_index++) {
  218. __sanitizer_sigaction new_sigaction;
  219. internal_memset(&new_sigaction, 0, sizeof(new_sigaction));
  220. new_sigaction.sigaction = TracerThreadSignalHandler;
  221. new_sigaction.sa_flags = SA_ONSTACK | SA_SIGINFO;
  222. internal_sigfillset(&new_sigaction.sa_mask);
  223. internal_sigaction_norestorer(kUnblockedSignals[signal_index],
  224. &new_sigaction, NULL);
  225. }
  226. int exit_code = 0;
  227. if (!thread_suspender.SuspendAllThreads()) {
  228. VReport(1, "Failed suspending threads.\n");
  229. exit_code = 3;
  230. } else {
  231. tracer_thread_argument->callback(thread_suspender.suspended_threads_list(),
  232. tracer_thread_argument->callback_argument);
  233. thread_suspender.ResumeAllThreads();
  234. exit_code = 0;
  235. }
  236. thread_suspender_instance = NULL;
  237. handler_stack.ss_flags = SS_DISABLE;
  238. internal_sigaltstack(&handler_stack, NULL);
  239. return exit_code;
  240. }
  241. class ScopedStackSpaceWithGuard {
  242. public:
  243. explicit ScopedStackSpaceWithGuard(uptr stack_size) {
  244. stack_size_ = stack_size;
  245. guard_size_ = GetPageSizeCached();
  246. // FIXME: Omitting MAP_STACK here works in current kernels but might break
  247. // in the future.
  248. guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_,
  249. "ScopedStackWithGuard");
  250. CHECK_EQ(guard_start_, (uptr)Mprotect((uptr)guard_start_, guard_size_));
  251. }
  252. ~ScopedStackSpaceWithGuard() {
  253. UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_);
  254. }
  255. void *Bottom() const {
  256. return (void *)(guard_start_ + stack_size_ + guard_size_);
  257. }
  258. private:
  259. uptr stack_size_;
  260. uptr guard_size_;
  261. uptr guard_start_;
  262. };
  263. // We have a limitation on the stack frame size, so some stuff had to be moved
  264. // into globals.
  265. static __sanitizer_sigset_t blocked_sigset;
  266. static __sanitizer_sigset_t old_sigset;
  267. static __sanitizer_sigaction old_sigactions
  268. [ARRAY_SIZE(kUnblockedSignals)];
  269. class StopTheWorldScope {
  270. public:
  271. StopTheWorldScope() {
  272. // Block all signals that can be blocked safely, and install
  273. // default handlers for the remaining signals.
  274. // We cannot allow user-defined handlers to run while the ThreadSuspender
  275. // thread is active, because they could conceivably call some libc functions
  276. // which modify errno (which is shared between the two threads).
  277. internal_sigfillset(&blocked_sigset);
  278. for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
  279. signal_index++) {
  280. // Remove the signal from the set of blocked signals.
  281. internal_sigdelset(&blocked_sigset, kUnblockedSignals[signal_index]);
  282. // Install the default handler.
  283. __sanitizer_sigaction new_sigaction;
  284. internal_memset(&new_sigaction, 0, sizeof(new_sigaction));
  285. new_sigaction.handler = SIG_DFL;
  286. internal_sigfillset(&new_sigaction.sa_mask);
  287. internal_sigaction_norestorer(kUnblockedSignals[signal_index],
  288. &new_sigaction, &old_sigactions[signal_index]);
  289. }
  290. int sigprocmask_status =
  291. internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset);
  292. CHECK_EQ(sigprocmask_status, 0); // sigprocmask should never fail
  293. // Make this process dumpable. Processes that are not dumpable cannot be
  294. // attached to.
  295. process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
  296. if (!process_was_dumpable_)
  297. internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
  298. old_die_callback = GetDieCallback();
  299. }
  300. ~StopTheWorldScope() {
  301. SetDieCallback(old_die_callback);
  302. // Restore the dumpable flag.
  303. if (!process_was_dumpable_)
  304. internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
  305. // Restore the signal handlers.
  306. for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
  307. signal_index++) {
  308. internal_sigaction_norestorer(kUnblockedSignals[signal_index],
  309. &old_sigactions[signal_index], NULL);
  310. }
  311. internal_sigprocmask(SIG_SETMASK, &old_sigset, &old_sigset);
  312. }
  313. private:
  314. int process_was_dumpable_;
  315. };
  316. // When sanitizer output is being redirected to file (i.e. by using log_path),
  317. // the tracer should write to the parent's log instead of trying to open a new
  318. // file. Alert the logging code to the fact that we have a tracer.
  319. struct ScopedSetTracerPID {
  320. explicit ScopedSetTracerPID(uptr tracer_pid) {
  321. stoptheworld_tracer_pid = tracer_pid;
  322. stoptheworld_tracer_ppid = internal_getpid();
  323. }
  324. ~ScopedSetTracerPID() {
  325. stoptheworld_tracer_pid = 0;
  326. stoptheworld_tracer_ppid = 0;
  327. }
  328. };
  329. void StopTheWorld(StopTheWorldCallback callback, void *argument) {
  330. StopTheWorldScope in_stoptheworld;
  331. // Prepare the arguments for TracerThread.
  332. struct TracerThreadArgument tracer_thread_argument;
  333. tracer_thread_argument.callback = callback;
  334. tracer_thread_argument.callback_argument = argument;
  335. tracer_thread_argument.parent_pid = internal_getpid();
  336. const uptr kTracerStackSize = 2 * 1024 * 1024;
  337. ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize);
  338. // Block the execution of TracerThread until after we have set ptrace
  339. // permissions.
  340. tracer_thread_argument.mutex.Lock();
  341. uptr tracer_pid = internal_clone(
  342. TracerThread, tracer_stack.Bottom(),
  343. CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,
  344. &tracer_thread_argument, 0 /* parent_tidptr */, 0 /* newtls */, 0
  345. /* child_tidptr */);
  346. int local_errno = 0;
  347. if (internal_iserror(tracer_pid, &local_errno)) {
  348. VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno);
  349. tracer_thread_argument.mutex.Unlock();
  350. } else {
  351. ScopedSetTracerPID scoped_set_tracer_pid(tracer_pid);
  352. // On some systems we have to explicitly declare that we want to be traced
  353. // by the tracer thread.
  354. #ifdef PR_SET_PTRACER
  355. internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);
  356. #endif
  357. // Allow the tracer thread to start.
  358. tracer_thread_argument.mutex.Unlock();
  359. // Since errno is shared between this thread and the tracer thread, we
  360. // must avoid using errno while the tracer thread is running.
  361. // At this point, any signal will either be blocked or kill us, so waitpid
  362. // should never return (and set errno) while the tracer thread is alive.
  363. uptr waitpid_status = internal_waitpid(tracer_pid, NULL, __WALL);
  364. if (internal_iserror(waitpid_status, &local_errno))
  365. VReport(1, "Waiting on the tracer thread failed (errno %d).\n",
  366. local_errno);
  367. }
  368. }
  369. // Platform-specific methods from SuspendedThreadsList.
  370. #if SANITIZER_ANDROID && defined(__arm__)
  371. typedef pt_regs regs_struct;
  372. #define REG_SP ARM_sp
  373. #elif SANITIZER_LINUX && defined(__arm__)
  374. typedef user_regs regs_struct;
  375. #define REG_SP uregs[13]
  376. #elif defined(__i386__) || defined(__x86_64__)
  377. typedef user_regs_struct regs_struct;
  378. #if defined(__i386__)
  379. #define REG_SP esp
  380. #else
  381. #define REG_SP rsp
  382. #endif
  383. #elif defined(__powerpc__) || defined(__powerpc64__)
  384. typedef pt_regs regs_struct;
  385. #define REG_SP gpr[PT_R1]
  386. #elif defined(__mips__)
  387. typedef struct user regs_struct;
  388. #define REG_SP regs[EF_REG29]
  389. #else
  390. #error "Unsupported architecture"
  391. #endif // SANITIZER_ANDROID && defined(__arm__)
  392. int SuspendedThreadsList::GetRegistersAndSP(uptr index,
  393. uptr *buffer,
  394. uptr *sp) const {
  395. pid_t tid = GetThreadID(index);
  396. regs_struct regs;
  397. int pterrno;
  398. if (internal_iserror(internal_ptrace(PTRACE_GETREGS, tid, NULL, &regs),
  399. &pterrno)) {
  400. VReport(1, "Could not get registers from thread %d (errno %d).\n", tid,
  401. pterrno);
  402. return -1;
  403. }
  404. *sp = regs.REG_SP;
  405. internal_memcpy(buffer, &regs, sizeof(regs));
  406. return 0;
  407. }
  408. uptr SuspendedThreadsList::RegisterCount() {
  409. return sizeof(regs_struct) / sizeof(uptr);
  410. }
  411. } // namespace __sanitizer
  412. #endif // SANITIZER_LINUX && defined(__x86_64__)