1 //===-- sanitizer_stoptheworld_linux_libcdep.cc ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // See sanitizer_stoptheworld.h for details.
11 // This implementation was inspired by Markus Gutschke's linuxthreads.cc.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 
16 #include "sanitizer_platform.h"
17 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__))
18 
19 #include "sanitizer_stoptheworld.h"
20 
21 #include "sanitizer_platform_limits_posix.h"
22 #include "sanitizer_atomic.h"
23 
24 #include <errno.h>
25 #include <sched.h> // for CLONE_* definitions
26 #include <stddef.h>
27 #include <sys/prctl.h> // for PR_* definitions
28 #include <sys/ptrace.h> // for PTRACE_* definitions
29 #include <sys/types.h> // for pid_t
30 #if SANITIZER_ANDROID && defined(__arm__)
31 # include <linux/user.h>  // for pt_regs
32 #else
33 # include <sys/user.h>  // for user_regs_struct
34 #endif
35 #include <sys/wait.h> // for signal-related stuff
36 
37 #ifdef sa_handler
38 # undef sa_handler
39 #endif
40 
41 #ifdef sa_sigaction
42 # undef sa_sigaction
43 #endif
44 
45 #include "sanitizer_common.h"
46 #include "sanitizer_flags.h"
47 #include "sanitizer_libc.h"
48 #include "sanitizer_linux.h"
49 #include "sanitizer_mutex.h"
50 #include "sanitizer_placement_new.h"
51 
52 // This module works by spawning a Linux task which then attaches to every
53 // thread in the caller process with ptrace. This suspends the threads, and
54 // PTRACE_GETREGS can then be used to obtain their register state. The callback
55 // supplied to StopTheWorld() is run in the tracer task while the threads are
56 // suspended.
57 // The tracer task must be placed in a different thread group for ptrace to
58 // work, so it cannot be spawned as a pthread. Instead, we use the low-level
59 // clone() interface (we want to share the address space with the caller
60 // process, so we prefer clone() over fork()).
61 //
62 // We don't use any libc functions, relying instead on direct syscalls. There
63 // are two reasons for this:
64 // 1. calling a library function while threads are suspended could cause a
65 // deadlock, if one of the treads happens to be holding a libc lock;
66 // 2. it's generally not safe to call libc functions from the tracer task,
67 // because clone() does not set up a thread-local storage for it. Any
68 // thread-local variables used by libc will be shared between the tracer task
69 // and the thread which spawned it.
70 
71 COMPILER_CHECK(sizeof(SuspendedThreadID) == sizeof(pid_t));
72 
73 namespace __sanitizer {
74 
75 // Structure for passing arguments into the tracer thread.
76 struct TracerThreadArgument {
77   StopTheWorldCallback callback;
78   void *callback_argument;
79   // The tracer thread waits on this mutex while the parent finishes its
80   // preparations.
81   BlockingMutex mutex;
82   // Tracer thread signals its completion by setting done.
83   atomic_uintptr_t done;
84   uptr parent_pid;
85 };
86 
87 // This class handles thread suspending/unsuspending in the tracer thread.
88 class ThreadSuspender {
89  public:
ThreadSuspender(pid_t pid,TracerThreadArgument * arg)90   explicit ThreadSuspender(pid_t pid, TracerThreadArgument *arg)
91     : arg(arg)
92     , pid_(pid) {
93       CHECK_GE(pid, 0);
94     }
95   bool SuspendAllThreads();
96   void ResumeAllThreads();
97   void KillAllThreads();
suspended_threads_list()98   SuspendedThreadsList &suspended_threads_list() {
99     return suspended_threads_list_;
100   }
101   TracerThreadArgument *arg;
102  private:
103   SuspendedThreadsList suspended_threads_list_;
104   pid_t pid_;
105   bool SuspendThread(SuspendedThreadID thread_id);
106 };
107 
SuspendThread(SuspendedThreadID tid)108 bool ThreadSuspender::SuspendThread(SuspendedThreadID tid) {
109   // Are we already attached to this thread?
110   // Currently this check takes linear time, however the number of threads is
111   // usually small.
112   if (suspended_threads_list_.Contains(tid))
113     return false;
114   int pterrno;
115   if (internal_iserror(internal_ptrace(PTRACE_ATTACH, tid, NULL, NULL),
116                        &pterrno)) {
117     // Either the thread is dead, or something prevented us from attaching.
118     // Log this event and move on.
119     VReport(1, "Could not attach to thread %d (errno %d).\n", tid, pterrno);
120     return false;
121   } else {
122     VReport(2, "Attached to thread %d.\n", tid);
123     // The thread is not guaranteed to stop before ptrace returns, so we must
124     // wait on it. Note: if the thread receives a signal concurrently,
125     // we can get notification about the signal before notification about stop.
126     // In such case we need to forward the signal to the thread, otherwise
127     // the signal will be missed (as we do PTRACE_DETACH with arg=0) and
128     // any logic relying on signals will break. After forwarding we need to
129     // continue to wait for stopping, because the thread is not stopped yet.
130     // We do ignore delivery of SIGSTOP, because we want to make stop-the-world
131     // as invisible as possible.
132     for (;;) {
133       int status;
134       uptr waitpid_status;
135       HANDLE_EINTR(waitpid_status, internal_waitpid(tid, &status, __WALL));
136       int wperrno;
137       if (internal_iserror(waitpid_status, &wperrno)) {
138         // Got a ECHILD error. I don't think this situation is possible, but it
139         // doesn't hurt to report it.
140         VReport(1, "Waiting on thread %d failed, detaching (errno %d).\n",
141                 tid, wperrno);
142         internal_ptrace(PTRACE_DETACH, tid, NULL, NULL);
143         return false;
144       }
145       if (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) {
146         internal_ptrace(PTRACE_CONT, tid, 0, (void*)(uptr)WSTOPSIG(status));
147         continue;
148       }
149       break;
150     }
151     suspended_threads_list_.Append(tid);
152     return true;
153   }
154 }
155 
ResumeAllThreads()156 void ThreadSuspender::ResumeAllThreads() {
157   for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++) {
158     pid_t tid = suspended_threads_list_.GetThreadID(i);
159     int pterrno;
160     if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, NULL, NULL),
161                           &pterrno)) {
162       VReport(2, "Detached from thread %d.\n", tid);
163     } else {
164       // Either the thread is dead, or we are already detached.
165       // The latter case is possible, for instance, if this function was called
166       // from a signal handler.
167       VReport(1, "Could not detach from thread %d (errno %d).\n", tid, pterrno);
168     }
169   }
170 }
171 
KillAllThreads()172 void ThreadSuspender::KillAllThreads() {
173   for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++)
174     internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i),
175                     NULL, NULL);
176 }
177 
SuspendAllThreads()178 bool ThreadSuspender::SuspendAllThreads() {
179   ThreadLister thread_lister(pid_);
180   bool added_threads;
181   do {
182     // Run through the directory entries once.
183     added_threads = false;
184     pid_t tid = thread_lister.GetNextTID();
185     while (tid >= 0) {
186       if (SuspendThread(tid))
187         added_threads = true;
188       tid = thread_lister.GetNextTID();
189     }
190     if (thread_lister.error()) {
191       // Detach threads and fail.
192       ResumeAllThreads();
193       return false;
194     }
195     thread_lister.Reset();
196   } while (added_threads);
197   return true;
198 }
199 
200 // Pointer to the ThreadSuspender instance for use in signal handler.
201 static ThreadSuspender *thread_suspender_instance = NULL;
202 
203 // Synchronous signals that should not be blocked.
204 static const int kSyncSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV, SIGBUS,
205                                     SIGXCPU, SIGXFSZ };
206 
207 static DieCallbackType old_die_callback;
208 
209 // Signal handler to wake up suspended threads when the tracer thread dies.
TracerThreadSignalHandler(int signum,void * siginfo,void * uctx)210 static void TracerThreadSignalHandler(int signum, void *siginfo, void *uctx) {
211   SignalContext ctx = SignalContext::Create(siginfo, uctx);
212   VPrintf(1, "Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n",
213       signum, ctx.addr, ctx.pc, ctx.sp);
214   ThreadSuspender *inst = thread_suspender_instance;
215   if (inst != NULL) {
216     if (signum == SIGABRT)
217       inst->KillAllThreads();
218     else
219       inst->ResumeAllThreads();
220     SetDieCallback(old_die_callback);
221     old_die_callback = NULL;
222     thread_suspender_instance = NULL;
223     atomic_store(&inst->arg->done, 1, memory_order_relaxed);
224   }
225   internal__exit((signum == SIGABRT) ? 1 : 2);
226 }
227 
TracerThreadDieCallback()228 static void TracerThreadDieCallback() {
229   // Generally a call to Die() in the tracer thread should be fatal to the
230   // parent process as well, because they share the address space.
231   // This really only works correctly if all the threads are suspended at this
232   // point. So we correctly handle calls to Die() from within the callback, but
233   // not those that happen before or after the callback. Hopefully there aren't
234   // a lot of opportunities for that to happen...
235   ThreadSuspender *inst = thread_suspender_instance;
236   if (inst != NULL && stoptheworld_tracer_pid == internal_getpid()) {
237     inst->KillAllThreads();
238     thread_suspender_instance = NULL;
239   }
240   if (old_die_callback)
241     old_die_callback();
242   SetDieCallback(old_die_callback);
243   old_die_callback = NULL;
244 }
245 
246 // Size of alternative stack for signal handlers in the tracer thread.
247 static const int kHandlerStackSize = 4096;
248 
249 // This function will be run as a cloned task.
TracerThread(void * argument)250 static int TracerThread(void* argument) {
251   TracerThreadArgument *tracer_thread_argument =
252       (TracerThreadArgument *)argument;
253 
254   internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
255   // Check if parent is already dead.
256   if (internal_getppid() != tracer_thread_argument->parent_pid)
257     internal__exit(4);
258 
259   // Wait for the parent thread to finish preparations.
260   tracer_thread_argument->mutex.Lock();
261   tracer_thread_argument->mutex.Unlock();
262 
263   old_die_callback = GetDieCallback();
264   SetDieCallback(TracerThreadDieCallback);
265 
266   ThreadSuspender thread_suspender(internal_getppid(), tracer_thread_argument);
267   // Global pointer for the signal handler.
268   thread_suspender_instance = &thread_suspender;
269 
270   // Alternate stack for signal handling.
271   InternalScopedBuffer<char> handler_stack_memory(kHandlerStackSize);
272   struct sigaltstack handler_stack;
273   internal_memset(&handler_stack, 0, sizeof(handler_stack));
274   handler_stack.ss_sp = handler_stack_memory.data();
275   handler_stack.ss_size = kHandlerStackSize;
276   internal_sigaltstack(&handler_stack, NULL);
277 
278   // Install our handler for synchronous signals. Other signals should be
279   // blocked by the mask we inherited from the parent thread.
280   for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++) {
281     __sanitizer_sigaction act;
282     internal_memset(&act, 0, sizeof(act));
283     act.sigaction = TracerThreadSignalHandler;
284     act.sa_flags = SA_ONSTACK | SA_SIGINFO;
285     internal_sigaction_norestorer(kSyncSignals[i], &act, 0);
286   }
287 
288   int exit_code = 0;
289   if (!thread_suspender.SuspendAllThreads()) {
290     VReport(1, "Failed suspending threads.\n");
291     exit_code = 3;
292   } else {
293     tracer_thread_argument->callback(thread_suspender.suspended_threads_list(),
294                                      tracer_thread_argument->callback_argument);
295     thread_suspender.ResumeAllThreads();
296     exit_code = 0;
297   }
298   SetDieCallback(old_die_callback);
299   thread_suspender_instance = NULL;
300   atomic_store(&tracer_thread_argument->done, 1, memory_order_relaxed);
301   return exit_code;
302 }
303 
304 class ScopedStackSpaceWithGuard {
305  public:
ScopedStackSpaceWithGuard(uptr stack_size)306   explicit ScopedStackSpaceWithGuard(uptr stack_size) {
307     stack_size_ = stack_size;
308     guard_size_ = GetPageSizeCached();
309     // FIXME: Omitting MAP_STACK here works in current kernels but might break
310     // in the future.
311     guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_,
312                                    "ScopedStackWithGuard");
313     CHECK(MprotectNoAccess((uptr)guard_start_, guard_size_));
314   }
~ScopedStackSpaceWithGuard()315   ~ScopedStackSpaceWithGuard() {
316     UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_);
317   }
Bottom() const318   void *Bottom() const {
319     return (void *)(guard_start_ + stack_size_ + guard_size_);
320   }
321 
322  private:
323   uptr stack_size_;
324   uptr guard_size_;
325   uptr guard_start_;
326 };
327 
328 // We have a limitation on the stack frame size, so some stuff had to be moved
329 // into globals.
330 static __sanitizer_sigset_t blocked_sigset;
331 static __sanitizer_sigset_t old_sigset;
332 
333 class StopTheWorldScope {
334  public:
StopTheWorldScope()335   StopTheWorldScope() {
336     // Make this process dumpable. Processes that are not dumpable cannot be
337     // attached to.
338     process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
339     if (!process_was_dumpable_)
340       internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
341   }
342 
~StopTheWorldScope()343   ~StopTheWorldScope() {
344     // Restore the dumpable flag.
345     if (!process_was_dumpable_)
346       internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
347   }
348 
349  private:
350   int process_was_dumpable_;
351 };
352 
353 // When sanitizer output is being redirected to file (i.e. by using log_path),
354 // the tracer should write to the parent's log instead of trying to open a new
355 // file. Alert the logging code to the fact that we have a tracer.
356 struct ScopedSetTracerPID {
ScopedSetTracerPID__sanitizer::ScopedSetTracerPID357   explicit ScopedSetTracerPID(uptr tracer_pid) {
358     stoptheworld_tracer_pid = tracer_pid;
359     stoptheworld_tracer_ppid = internal_getpid();
360   }
~ScopedSetTracerPID__sanitizer::ScopedSetTracerPID361   ~ScopedSetTracerPID() {
362     stoptheworld_tracer_pid = 0;
363     stoptheworld_tracer_ppid = 0;
364   }
365 };
366 
StopTheWorld(StopTheWorldCallback callback,void * argument)367 void StopTheWorld(StopTheWorldCallback callback, void *argument) {
368   StopTheWorldScope in_stoptheworld;
369   // Prepare the arguments for TracerThread.
370   struct TracerThreadArgument tracer_thread_argument;
371   tracer_thread_argument.callback = callback;
372   tracer_thread_argument.callback_argument = argument;
373   tracer_thread_argument.parent_pid = internal_getpid();
374   atomic_store(&tracer_thread_argument.done, 0, memory_order_relaxed);
375   const uptr kTracerStackSize = 2 * 1024 * 1024;
376   ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize);
377   // Block the execution of TracerThread until after we have set ptrace
378   // permissions.
379   tracer_thread_argument.mutex.Lock();
380   // Signal handling story.
381   // We don't want async signals to be delivered to the tracer thread,
382   // so we block all async signals before creating the thread. An async signal
383   // handler can temporary modify errno, which is shared with this thread.
384   // We ought to use pthread_sigmask here, because sigprocmask has undefined
385   // behavior in multithreaded programs. However, on linux sigprocmask is
386   // equivalent to pthread_sigmask with the exception that pthread_sigmask
387   // does not allow to block some signals used internally in pthread
388   // implementation. We are fine with blocking them here, we are really not
389   // going to pthread_cancel the thread.
390   // The tracer thread should not raise any synchronous signals. But in case it
391   // does, we setup a special handler for sync signals that properly kills the
392   // parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers
393   // in the tracer thread won't interfere with user program. Double note: if a
394   // user does something along the lines of 'kill -11 pid', that can kill the
395   // process even if user setup own handler for SEGV.
396   // Thing to watch out for: this code should not change behavior of user code
397   // in any observable way. In particular it should not override user signal
398   // handlers.
399   internal_sigfillset(&blocked_sigset);
400   for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++)
401     internal_sigdelset(&blocked_sigset, kSyncSignals[i]);
402   int rv = internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset);
403   CHECK_EQ(rv, 0);
404   uptr tracer_pid = internal_clone(
405       TracerThread, tracer_stack.Bottom(),
406       CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,
407       &tracer_thread_argument, 0 /* parent_tidptr */, 0 /* newtls */, 0
408       /* child_tidptr */);
409   internal_sigprocmask(SIG_SETMASK, &old_sigset, 0);
410   int local_errno = 0;
411   if (internal_iserror(tracer_pid, &local_errno)) {
412     VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno);
413     tracer_thread_argument.mutex.Unlock();
414   } else {
415     ScopedSetTracerPID scoped_set_tracer_pid(tracer_pid);
416     // On some systems we have to explicitly declare that we want to be traced
417     // by the tracer thread.
418 #ifdef PR_SET_PTRACER
419     internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);
420 #endif
421     // Allow the tracer thread to start.
422     tracer_thread_argument.mutex.Unlock();
423     // NOTE: errno is shared between this thread and the tracer thread.
424     // internal_waitpid() may call syscall() which can access/spoil errno,
425     // so we can't call it now. Instead we for the tracer thread to finish using
426     // the spin loop below. Man page for sched_yield() says "In the Linux
427     // implementation, sched_yield() always succeeds", so let's hope it does not
428     // spoil errno. Note that this spin loop runs only for brief periods before
429     // the tracer thread has suspended us and when it starts unblocking threads.
430     while (atomic_load(&tracer_thread_argument.done, memory_order_relaxed) == 0)
431       sched_yield();
432     // Now the tracer thread is about to exit and does not touch errno,
433     // wait for it.
434     for (;;) {
435       uptr waitpid_status = internal_waitpid(tracer_pid, NULL, __WALL);
436       if (!internal_iserror(waitpid_status, &local_errno))
437         break;
438       if (local_errno == EINTR)
439         continue;
440       VReport(1, "Waiting on the tracer thread failed (errno %d).\n",
441               local_errno);
442       break;
443     }
444   }
445 }
446 
447 // Platform-specific methods from SuspendedThreadsList.
448 #if SANITIZER_ANDROID && defined(__arm__)
449 typedef pt_regs regs_struct;
450 #define REG_SP ARM_sp
451 
452 #elif SANITIZER_LINUX && defined(__arm__)
453 typedef user_regs regs_struct;
454 #define REG_SP uregs[13]
455 
456 #elif defined(__i386__) || defined(__x86_64__)
457 typedef user_regs_struct regs_struct;
458 #if defined(__i386__)
459 #define REG_SP esp
460 #else
461 #define REG_SP rsp
462 #endif
463 
464 #elif defined(__powerpc__) || defined(__powerpc64__)
465 typedef pt_regs regs_struct;
466 #define REG_SP gpr[PT_R1]
467 
468 #elif defined(__mips__)
469 typedef struct user regs_struct;
470 #define REG_SP regs[EF_REG29]
471 
472 #else
473 #error "Unsupported architecture"
474 #endif // SANITIZER_ANDROID && defined(__arm__)
475 
GetRegistersAndSP(uptr index,uptr * buffer,uptr * sp) const476 int SuspendedThreadsList::GetRegistersAndSP(uptr index,
477                                             uptr *buffer,
478                                             uptr *sp) const {
479   pid_t tid = GetThreadID(index);
480   regs_struct regs;
481   int pterrno;
482   if (internal_iserror(internal_ptrace(PTRACE_GETREGS, tid, NULL, &regs),
483                        &pterrno)) {
484     VReport(1, "Could not get registers from thread %d (errno %d).\n", tid,
485             pterrno);
486     return -1;
487   }
488 
489   *sp = regs.REG_SP;
490   internal_memcpy(buffer, &regs, sizeof(regs));
491   return 0;
492 }
493 
RegisterCount()494 uptr SuspendedThreadsList::RegisterCount() {
495   return sizeof(regs_struct) / sizeof(uptr);
496 }
497 }  // namespace __sanitizer
498 
499 #endif  // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__))
500