1 //===-- sanitizer_stoptheworld_linux_libcdep.cc ---------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // See sanitizer_stoptheworld.h for details.
9 // This implementation was inspired by Markus Gutschke's linuxthreads.cc.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
15 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__) || \
18 #include "sanitizer_stoptheworld.h"
20 #include "sanitizer_platform_limits_posix.h"
21 #include "sanitizer_atomic.h"
24 #include <sched.h> // for CLONE_* definitions
26 #include <sys/prctl.h> // for PR_* definitions
27 #include <sys/ptrace.h> // for PTRACE_* definitions
28 #include <sys/types.h> // for pid_t
29 #include <sys/uio.h> // for iovec
30 #include <elf.h> // for NT_PRSTATUS
31 #if SANITIZER_ANDROID && defined(__arm__)
32 # include <linux/user.h> // for pt_regs
35 // GLIBC 2.20+ sys/user does not include asm/ptrace.h
36 # include <asm/ptrace.h>
38 # include <sys/user.h> // for user_regs_struct
40 #include <sys/wait.h> // for signal-related stuff
50 #include "sanitizer_common.h"
51 #include "sanitizer_flags.h"
52 #include "sanitizer_libc.h"
53 #include "sanitizer_linux.h"
54 #include "sanitizer_mutex.h"
55 #include "sanitizer_placement_new.h"
57 // This module works by spawning a Linux task which then attaches to every
58 // thread in the caller process with ptrace. This suspends the threads, and
59 // PTRACE_GETREGS can then be used to obtain their register state. The callback
60 // supplied to StopTheWorld() is run in the tracer task while the threads are
62 // The tracer task must be placed in a different thread group for ptrace to
63 // work, so it cannot be spawned as a pthread. Instead, we use the low-level
64 // clone() interface (we want to share the address space with the caller
65 // process, so we prefer clone() over fork()).
67 // We don't use any libc functions, relying instead on direct syscalls. There
68 // are two reasons for this:
69 // 1. calling a library function while threads are suspended could cause a
70 // deadlock, if one of the treads happens to be holding a libc lock;
71 // 2. it's generally not safe to call libc functions from the tracer task,
72 // because clone() does not set up a thread-local storage for it. Any
73 // thread-local variables used by libc will be shared between the tracer task
74 // and the thread which spawned it.
76 COMPILER_CHECK(sizeof(SuspendedThreadID
) == sizeof(pid_t
));
78 namespace __sanitizer
{
80 // Structure for passing arguments into the tracer thread.
81 struct TracerThreadArgument
{
82 StopTheWorldCallback callback
;
83 void *callback_argument
;
84 // The tracer thread waits on this mutex while the parent finishes its
87 // Tracer thread signals its completion by setting done.
88 atomic_uintptr_t done
;
92 // This class handles thread suspending/unsuspending in the tracer thread.
93 class ThreadSuspender
{
95 explicit ThreadSuspender(pid_t pid
, TracerThreadArgument
*arg
)
100 bool SuspendAllThreads();
101 void ResumeAllThreads();
102 void KillAllThreads();
103 SuspendedThreadsList
&suspended_threads_list() {
104 return suspended_threads_list_
;
106 TracerThreadArgument
*arg
;
108 SuspendedThreadsList suspended_threads_list_
;
110 bool SuspendThread(SuspendedThreadID thread_id
);
113 bool ThreadSuspender::SuspendThread(SuspendedThreadID tid
) {
114 // Are we already attached to this thread?
115 // Currently this check takes linear time, however the number of threads is
117 if (suspended_threads_list_
.Contains(tid
))
120 if (internal_iserror(internal_ptrace(PTRACE_ATTACH
, tid
, nullptr, nullptr),
122 // Either the thread is dead, or something prevented us from attaching.
123 // Log this event and move on.
124 VReport(1, "Could not attach to thread %d (errno %d).\n", tid
, pterrno
);
127 VReport(2, "Attached to thread %d.\n", tid
);
128 // The thread is not guaranteed to stop before ptrace returns, so we must
129 // wait on it. Note: if the thread receives a signal concurrently,
130 // we can get notification about the signal before notification about stop.
131 // In such case we need to forward the signal to the thread, otherwise
132 // the signal will be missed (as we do PTRACE_DETACH with arg=0) and
133 // any logic relying on signals will break. After forwarding we need to
134 // continue to wait for stopping, because the thread is not stopped yet.
135 // We do ignore delivery of SIGSTOP, because we want to make stop-the-world
136 // as invisible as possible.
140 HANDLE_EINTR(waitpid_status
, internal_waitpid(tid
, &status
, __WALL
));
142 if (internal_iserror(waitpid_status
, &wperrno
)) {
143 // Got a ECHILD error. I don't think this situation is possible, but it
144 // doesn't hurt to report it.
145 VReport(1, "Waiting on thread %d failed, detaching (errno %d).\n",
147 internal_ptrace(PTRACE_DETACH
, tid
, nullptr, nullptr);
150 if (WIFSTOPPED(status
) && WSTOPSIG(status
) != SIGSTOP
) {
151 internal_ptrace(PTRACE_CONT
, tid
, nullptr,
152 (void*)(uptr
)WSTOPSIG(status
));
157 suspended_threads_list_
.Append(tid
);
162 void ThreadSuspender::ResumeAllThreads() {
163 for (uptr i
= 0; i
< suspended_threads_list_
.thread_count(); i
++) {
164 pid_t tid
= suspended_threads_list_
.GetThreadID(i
);
166 if (!internal_iserror(internal_ptrace(PTRACE_DETACH
, tid
, nullptr, nullptr),
168 VReport(2, "Detached from thread %d.\n", tid
);
170 // Either the thread is dead, or we are already detached.
171 // The latter case is possible, for instance, if this function was called
172 // from a signal handler.
173 VReport(1, "Could not detach from thread %d (errno %d).\n", tid
, pterrno
);
178 void ThreadSuspender::KillAllThreads() {
179 for (uptr i
= 0; i
< suspended_threads_list_
.thread_count(); i
++)
180 internal_ptrace(PTRACE_KILL
, suspended_threads_list_
.GetThreadID(i
),
184 bool ThreadSuspender::SuspendAllThreads() {
185 ThreadLister
thread_lister(pid_
);
188 // Run through the directory entries once.
189 added_threads
= false;
190 pid_t tid
= thread_lister
.GetNextTID();
192 if (SuspendThread(tid
))
193 added_threads
= true;
194 tid
= thread_lister
.GetNextTID();
196 if (thread_lister
.error()) {
197 // Detach threads and fail.
201 thread_lister
.Reset();
202 } while (added_threads
);
206 // Pointer to the ThreadSuspender instance for use in signal handler.
207 static ThreadSuspender
*thread_suspender_instance
= nullptr;
209 // Synchronous signals that should not be blocked.
210 static const int kSyncSignals
[] = { SIGABRT
, SIGILL
, SIGFPE
, SIGSEGV
, SIGBUS
,
213 static void TracerThreadDieCallback() {
214 // Generally a call to Die() in the tracer thread should be fatal to the
215 // parent process as well, because they share the address space.
216 // This really only works correctly if all the threads are suspended at this
217 // point. So we correctly handle calls to Die() from within the callback, but
218 // not those that happen before or after the callback. Hopefully there aren't
219 // a lot of opportunities for that to happen...
220 ThreadSuspender
*inst
= thread_suspender_instance
;
221 if (inst
&& stoptheworld_tracer_pid
== internal_getpid()) {
222 inst
->KillAllThreads();
223 thread_suspender_instance
= nullptr;
227 // Signal handler to wake up suspended threads when the tracer thread dies.
228 static void TracerThreadSignalHandler(int signum
, void *siginfo
, void *uctx
) {
229 SignalContext ctx
= SignalContext::Create(siginfo
, uctx
);
230 VPrintf(1, "Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n",
231 signum
, ctx
.addr
, ctx
.pc
, ctx
.sp
);
232 ThreadSuspender
*inst
= thread_suspender_instance
;
234 if (signum
== SIGABRT
)
235 inst
->KillAllThreads();
237 inst
->ResumeAllThreads();
238 RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback
));
239 thread_suspender_instance
= nullptr;
240 atomic_store(&inst
->arg
->done
, 1, memory_order_relaxed
);
242 internal__exit((signum
== SIGABRT
) ? 1 : 2);
245 // Size of alternative stack for signal handlers in the tracer thread.
246 static const int kHandlerStackSize
= 4096;
248 // This function will be run as a cloned task.
249 static int TracerThread(void* argument
) {
250 TracerThreadArgument
*tracer_thread_argument
=
251 (TracerThreadArgument
*)argument
;
253 internal_prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0);
254 // Check if parent is already dead.
255 if (internal_getppid() != tracer_thread_argument
->parent_pid
)
258 // Wait for the parent thread to finish preparations.
259 tracer_thread_argument
->mutex
.Lock();
260 tracer_thread_argument
->mutex
.Unlock();
262 RAW_CHECK(AddDieCallback(TracerThreadDieCallback
));
264 ThreadSuspender
thread_suspender(internal_getppid(), tracer_thread_argument
);
265 // Global pointer for the signal handler.
266 thread_suspender_instance
= &thread_suspender
;
268 // Alternate stack for signal handling.
269 InternalScopedBuffer
<char> handler_stack_memory(kHandlerStackSize
);
270 struct sigaltstack handler_stack
;
271 internal_memset(&handler_stack
, 0, sizeof(handler_stack
));
272 handler_stack
.ss_sp
= handler_stack_memory
.data();
273 handler_stack
.ss_size
= kHandlerStackSize
;
274 internal_sigaltstack(&handler_stack
, nullptr);
276 // Install our handler for synchronous signals. Other signals should be
277 // blocked by the mask we inherited from the parent thread.
278 for (uptr i
= 0; i
< ARRAY_SIZE(kSyncSignals
); i
++) {
279 __sanitizer_sigaction act
;
280 internal_memset(&act
, 0, sizeof(act
));
281 act
.sigaction
= TracerThreadSignalHandler
;
282 act
.sa_flags
= SA_ONSTACK
| SA_SIGINFO
;
283 internal_sigaction_norestorer(kSyncSignals
[i
], &act
, 0);
287 if (!thread_suspender
.SuspendAllThreads()) {
288 VReport(1, "Failed suspending threads.\n");
291 tracer_thread_argument
->callback(thread_suspender
.suspended_threads_list(),
292 tracer_thread_argument
->callback_argument
);
293 thread_suspender
.ResumeAllThreads();
296 RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback
));
297 thread_suspender_instance
= nullptr;
298 atomic_store(&tracer_thread_argument
->done
, 1, memory_order_relaxed
);
302 class ScopedStackSpaceWithGuard
{
304 explicit ScopedStackSpaceWithGuard(uptr stack_size
) {
305 stack_size_
= stack_size
;
306 guard_size_
= GetPageSizeCached();
307 // FIXME: Omitting MAP_STACK here works in current kernels but might break
309 guard_start_
= (uptr
)MmapOrDie(stack_size_
+ guard_size_
,
310 "ScopedStackWithGuard");
311 CHECK(MprotectNoAccess((uptr
)guard_start_
, guard_size_
));
313 ~ScopedStackSpaceWithGuard() {
314 UnmapOrDie((void *)guard_start_
, stack_size_
+ guard_size_
);
316 void *Bottom() const {
317 return (void *)(guard_start_
+ stack_size_
+ guard_size_
);
326 // We have a limitation on the stack frame size, so some stuff had to be moved
328 static __sanitizer_sigset_t blocked_sigset
;
329 static __sanitizer_sigset_t old_sigset
;
331 class StopTheWorldScope
{
333 StopTheWorldScope() {
334 // Make this process dumpable. Processes that are not dumpable cannot be
336 process_was_dumpable_
= internal_prctl(PR_GET_DUMPABLE
, 0, 0, 0, 0);
337 if (!process_was_dumpable_
)
338 internal_prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
341 ~StopTheWorldScope() {
342 // Restore the dumpable flag.
343 if (!process_was_dumpable_
)
344 internal_prctl(PR_SET_DUMPABLE
, 0, 0, 0, 0);
348 int process_was_dumpable_
;
351 // When sanitizer output is being redirected to file (i.e. by using log_path),
352 // the tracer should write to the parent's log instead of trying to open a new
353 // file. Alert the logging code to the fact that we have a tracer.
354 struct ScopedSetTracerPID
{
355 explicit ScopedSetTracerPID(uptr tracer_pid
) {
356 stoptheworld_tracer_pid
= tracer_pid
;
357 stoptheworld_tracer_ppid
= internal_getpid();
359 ~ScopedSetTracerPID() {
360 stoptheworld_tracer_pid
= 0;
361 stoptheworld_tracer_ppid
= 0;
365 void StopTheWorld(StopTheWorldCallback callback
, void *argument
) {
366 StopTheWorldScope in_stoptheworld
;
367 // Prepare the arguments for TracerThread.
368 struct TracerThreadArgument tracer_thread_argument
;
369 tracer_thread_argument
.callback
= callback
;
370 tracer_thread_argument
.callback_argument
= argument
;
371 tracer_thread_argument
.parent_pid
= internal_getpid();
372 atomic_store(&tracer_thread_argument
.done
, 0, memory_order_relaxed
);
373 const uptr kTracerStackSize
= 2 * 1024 * 1024;
374 ScopedStackSpaceWithGuard
tracer_stack(kTracerStackSize
);
375 // Block the execution of TracerThread until after we have set ptrace
377 tracer_thread_argument
.mutex
.Lock();
378 // Signal handling story.
379 // We don't want async signals to be delivered to the tracer thread,
380 // so we block all async signals before creating the thread. An async signal
381 // handler can temporary modify errno, which is shared with this thread.
382 // We ought to use pthread_sigmask here, because sigprocmask has undefined
383 // behavior in multithreaded programs. However, on linux sigprocmask is
384 // equivalent to pthread_sigmask with the exception that pthread_sigmask
385 // does not allow to block some signals used internally in pthread
386 // implementation. We are fine with blocking them here, we are really not
387 // going to pthread_cancel the thread.
388 // The tracer thread should not raise any synchronous signals. But in case it
389 // does, we setup a special handler for sync signals that properly kills the
390 // parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers
391 // in the tracer thread won't interfere with user program. Double note: if a
392 // user does something along the lines of 'kill -11 pid', that can kill the
393 // process even if user setup own handler for SEGV.
394 // Thing to watch out for: this code should not change behavior of user code
395 // in any observable way. In particular it should not override user signal
397 internal_sigfillset(&blocked_sigset
);
398 for (uptr i
= 0; i
< ARRAY_SIZE(kSyncSignals
); i
++)
399 internal_sigdelset(&blocked_sigset
, kSyncSignals
[i
]);
400 int rv
= internal_sigprocmask(SIG_BLOCK
, &blocked_sigset
, &old_sigset
);
402 uptr tracer_pid
= internal_clone(
403 TracerThread
, tracer_stack
.Bottom(),
404 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_UNTRACED
,
405 &tracer_thread_argument
, nullptr /* parent_tidptr */,
406 nullptr /* newtls */, nullptr /* child_tidptr */);
407 internal_sigprocmask(SIG_SETMASK
, &old_sigset
, 0);
409 if (internal_iserror(tracer_pid
, &local_errno
)) {
410 VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno
);
411 tracer_thread_argument
.mutex
.Unlock();
413 ScopedSetTracerPID
scoped_set_tracer_pid(tracer_pid
);
414 // On some systems we have to explicitly declare that we want to be traced
415 // by the tracer thread.
416 #ifdef PR_SET_PTRACER
417 internal_prctl(PR_SET_PTRACER
, tracer_pid
, 0, 0, 0);
419 // Allow the tracer thread to start.
420 tracer_thread_argument
.mutex
.Unlock();
421 // NOTE: errno is shared between this thread and the tracer thread.
422 // internal_waitpid() may call syscall() which can access/spoil errno,
423 // so we can't call it now. Instead we for the tracer thread to finish using
424 // the spin loop below. Man page for sched_yield() says "In the Linux
425 // implementation, sched_yield() always succeeds", so let's hope it does not
426 // spoil errno. Note that this spin loop runs only for brief periods before
427 // the tracer thread has suspended us and when it starts unblocking threads.
428 while (atomic_load(&tracer_thread_argument
.done
, memory_order_relaxed
) == 0)
430 // Now the tracer thread is about to exit and does not touch errno,
433 uptr waitpid_status
= internal_waitpid(tracer_pid
, nullptr, __WALL
);
434 if (!internal_iserror(waitpid_status
, &local_errno
))
436 if (local_errno
== EINTR
)
438 VReport(1, "Waiting on the tracer thread failed (errno %d).\n",
445 // Platform-specific methods from SuspendedThreadsList.
446 #if SANITIZER_ANDROID && defined(__arm__)
447 typedef pt_regs regs_struct
;
448 #define REG_SP ARM_sp
450 #elif SANITIZER_LINUX && defined(__arm__)
451 typedef user_regs regs_struct
;
452 #define REG_SP uregs[13]
454 #elif defined(__i386__) || defined(__x86_64__)
455 typedef user_regs_struct regs_struct
;
456 #if defined(__i386__)
462 #elif defined(__powerpc__) || defined(__powerpc64__)
463 typedef pt_regs regs_struct
;
464 #define REG_SP gpr[PT_R1]
466 #elif defined(__mips__)
467 typedef struct user regs_struct
;
468 #define REG_SP regs[EF_REG29]
470 #elif defined(__aarch64__)
471 typedef struct user_pt_regs regs_struct
;
473 #define ARCH_IOVEC_FOR_GETREGSET
476 #error "Unsupported architecture"
477 #endif // SANITIZER_ANDROID && defined(__arm__)
479 int SuspendedThreadsList::GetRegistersAndSP(uptr index
,
482 pid_t tid
= GetThreadID(index
);
485 #ifdef ARCH_IOVEC_FOR_GETREGSET
486 struct iovec regset_io
;
487 regset_io
.iov_base
= ®s
;
488 regset_io
.iov_len
= sizeof(regs_struct
);
489 bool isErr
= internal_iserror(internal_ptrace(PTRACE_GETREGSET
, tid
,
490 (void*)NT_PRSTATUS
, (void*)®set_io
),
493 bool isErr
= internal_iserror(internal_ptrace(PTRACE_GETREGS
, tid
, nullptr,
497 VReport(1, "Could not get registers from thread %d (errno %d).\n", tid
,
503 internal_memcpy(buffer
, ®s
, sizeof(regs
));
507 uptr
SuspendedThreadsList::RegisterCount() {
508 return sizeof(regs_struct
) / sizeof(uptr
);
510 } // namespace __sanitizer
512 #endif // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__)
513 // || defined(__aarch64__)