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__) || \
16 defined(__aarch64__) || defined(__powerpc64__) || \
19 #include "sanitizer_stoptheworld.h"
21 #include "sanitizer_platform_limits_posix.h"
22 #include "sanitizer_atomic.h"
25 #include <sched.h> // for CLONE_* definitions
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 #include <sys/uio.h> // for iovec
31 #include <elf.h> // for NT_PRSTATUS
32 #if SANITIZER_ANDROID && defined(__arm__)
33 # include <linux/user.h> // for pt_regs
36 // GLIBC 2.20+ sys/user does not include asm/ptrace.h
37 # include <asm/ptrace.h>
39 # include <sys/user.h> // for user_regs_struct
40 # if SANITIZER_ANDROID && SANITIZER_MIPS
41 # include <asm/reg.h> // for mips SP register in sys/user.h
44 #include <sys/wait.h> // for signal-related stuff
54 #include "sanitizer_common.h"
55 #include "sanitizer_flags.h"
56 #include "sanitizer_libc.h"
57 #include "sanitizer_linux.h"
58 #include "sanitizer_mutex.h"
59 #include "sanitizer_placement_new.h"
61 // This module works by spawning a Linux task which then attaches to every
62 // thread in the caller process with ptrace. This suspends the threads, and
63 // PTRACE_GETREGS can then be used to obtain their register state. The callback
64 // supplied to StopTheWorld() is run in the tracer task while the threads are
66 // The tracer task must be placed in a different thread group for ptrace to
67 // work, so it cannot be spawned as a pthread. Instead, we use the low-level
68 // clone() interface (we want to share the address space with the caller
69 // process, so we prefer clone() over fork()).
71 // We don't use any libc functions, relying instead on direct syscalls. There
72 // are two reasons for this:
73 // 1. calling a library function while threads are suspended could cause a
74 // deadlock, if one of the treads happens to be holding a libc lock;
75 // 2. it's generally not safe to call libc functions from the tracer task,
76 // because clone() does not set up a thread-local storage for it. Any
77 // thread-local variables used by libc will be shared between the tracer task
78 // and the thread which spawned it.
80 namespace __sanitizer
{
82 COMPILER_CHECK(sizeof(SuspendedThreadID
) == sizeof(pid_t
));
84 // Structure for passing arguments into the tracer thread.
85 struct TracerThreadArgument
{
86 StopTheWorldCallback callback
;
87 void *callback_argument
;
88 // The tracer thread waits on this mutex while the parent finishes its
91 // Tracer thread signals its completion by setting done.
92 atomic_uintptr_t done
;
96 // This class handles thread suspending/unsuspending in the tracer thread.
97 class ThreadSuspender
{
99 explicit ThreadSuspender(pid_t pid
, TracerThreadArgument
*arg
)
104 bool SuspendAllThreads();
105 void ResumeAllThreads();
106 void KillAllThreads();
107 SuspendedThreadsList
&suspended_threads_list() {
108 return suspended_threads_list_
;
110 TracerThreadArgument
*arg
;
112 SuspendedThreadsList suspended_threads_list_
;
114 bool SuspendThread(SuspendedThreadID thread_id
);
117 bool ThreadSuspender::SuspendThread(SuspendedThreadID tid
) {
118 // Are we already attached to this thread?
119 // Currently this check takes linear time, however the number of threads is
121 if (suspended_threads_list_
.Contains(tid
))
124 if (internal_iserror(internal_ptrace(PTRACE_ATTACH
, tid
, nullptr, nullptr),
126 // Either the thread is dead, or something prevented us from attaching.
127 // Log this event and move on.
128 VReport(1, "Could not attach to thread %d (errno %d).\n", tid
, pterrno
);
131 VReport(2, "Attached to thread %d.\n", tid
);
132 // The thread is not guaranteed to stop before ptrace returns, so we must
133 // wait on it. Note: if the thread receives a signal concurrently,
134 // we can get notification about the signal before notification about stop.
135 // In such case we need to forward the signal to the thread, otherwise
136 // the signal will be missed (as we do PTRACE_DETACH with arg=0) and
137 // any logic relying on signals will break. After forwarding we need to
138 // continue to wait for stopping, because the thread is not stopped yet.
139 // We do ignore delivery of SIGSTOP, because we want to make stop-the-world
140 // as invisible as possible.
144 HANDLE_EINTR(waitpid_status
, internal_waitpid(tid
, &status
, __WALL
));
146 if (internal_iserror(waitpid_status
, &wperrno
)) {
147 // Got a ECHILD error. I don't think this situation is possible, but it
148 // doesn't hurt to report it.
149 VReport(1, "Waiting on thread %d failed, detaching (errno %d).\n",
151 internal_ptrace(PTRACE_DETACH
, tid
, nullptr, nullptr);
154 if (WIFSTOPPED(status
) && WSTOPSIG(status
) != SIGSTOP
) {
155 internal_ptrace(PTRACE_CONT
, tid
, nullptr,
156 (void*)(uptr
)WSTOPSIG(status
));
161 suspended_threads_list_
.Append(tid
);
166 void ThreadSuspender::ResumeAllThreads() {
167 for (uptr i
= 0; i
< suspended_threads_list_
.thread_count(); i
++) {
168 pid_t tid
= suspended_threads_list_
.GetThreadID(i
);
170 if (!internal_iserror(internal_ptrace(PTRACE_DETACH
, tid
, nullptr, nullptr),
172 VReport(2, "Detached from thread %d.\n", tid
);
174 // Either the thread is dead, or we are already detached.
175 // The latter case is possible, for instance, if this function was called
176 // from a signal handler.
177 VReport(1, "Could not detach from thread %d (errno %d).\n", tid
, pterrno
);
182 void ThreadSuspender::KillAllThreads() {
183 for (uptr i
= 0; i
< suspended_threads_list_
.thread_count(); i
++)
184 internal_ptrace(PTRACE_KILL
, suspended_threads_list_
.GetThreadID(i
),
188 bool ThreadSuspender::SuspendAllThreads() {
189 ThreadLister
thread_lister(pid_
);
191 bool first_iteration
= true;
193 // Run through the directory entries once.
194 added_threads
= false;
195 pid_t tid
= thread_lister
.GetNextTID();
197 if (SuspendThread(tid
))
198 added_threads
= true;
199 tid
= thread_lister
.GetNextTID();
201 if (thread_lister
.error() || (first_iteration
&& !added_threads
)) {
202 // Detach threads and fail.
206 thread_lister
.Reset();
207 first_iteration
= false;
208 } while (added_threads
);
212 // Pointer to the ThreadSuspender instance for use in signal handler.
213 static ThreadSuspender
*thread_suspender_instance
= nullptr;
215 // Synchronous signals that should not be blocked.
216 static const int kSyncSignals
[] = { SIGABRT
, SIGILL
, SIGFPE
, SIGSEGV
, SIGBUS
,
219 static void TracerThreadDieCallback() {
220 // Generally a call to Die() in the tracer thread should be fatal to the
221 // parent process as well, because they share the address space.
222 // This really only works correctly if all the threads are suspended at this
223 // point. So we correctly handle calls to Die() from within the callback, but
224 // not those that happen before or after the callback. Hopefully there aren't
225 // a lot of opportunities for that to happen...
226 ThreadSuspender
*inst
= thread_suspender_instance
;
227 if (inst
&& stoptheworld_tracer_pid
== internal_getpid()) {
228 inst
->KillAllThreads();
229 thread_suspender_instance
= nullptr;
233 // Signal handler to wake up suspended threads when the tracer thread dies.
234 static void TracerThreadSignalHandler(int signum
, void *siginfo
, void *uctx
) {
235 SignalContext ctx
= SignalContext::Create(siginfo
, uctx
);
236 Printf("Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n", signum
,
237 ctx
.addr
, ctx
.pc
, ctx
.sp
);
238 ThreadSuspender
*inst
= thread_suspender_instance
;
240 if (signum
== SIGABRT
)
241 inst
->KillAllThreads();
243 inst
->ResumeAllThreads();
244 RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback
));
245 thread_suspender_instance
= nullptr;
246 atomic_store(&inst
->arg
->done
, 1, memory_order_relaxed
);
248 internal__exit((signum
== SIGABRT
) ? 1 : 2);
251 // Size of alternative stack for signal handlers in the tracer thread.
252 static const int kHandlerStackSize
= 4096;
254 // This function will be run as a cloned task.
255 static int TracerThread(void* argument
) {
256 TracerThreadArgument
*tracer_thread_argument
=
257 (TracerThreadArgument
*)argument
;
259 internal_prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0);
260 // Check if parent is already dead.
261 if (internal_getppid() != tracer_thread_argument
->parent_pid
)
264 // Wait for the parent thread to finish preparations.
265 tracer_thread_argument
->mutex
.Lock();
266 tracer_thread_argument
->mutex
.Unlock();
268 RAW_CHECK(AddDieCallback(TracerThreadDieCallback
));
270 ThreadSuspender
thread_suspender(internal_getppid(), tracer_thread_argument
);
271 // Global pointer for the signal handler.
272 thread_suspender_instance
= &thread_suspender
;
274 // Alternate stack for signal handling.
275 InternalScopedBuffer
<char> handler_stack_memory(kHandlerStackSize
);
276 struct sigaltstack handler_stack
;
277 internal_memset(&handler_stack
, 0, sizeof(handler_stack
));
278 handler_stack
.ss_sp
= handler_stack_memory
.data();
279 handler_stack
.ss_size
= kHandlerStackSize
;
280 internal_sigaltstack(&handler_stack
, nullptr);
282 // Install our handler for synchronous signals. Other signals should be
283 // blocked by the mask we inherited from the parent thread.
284 for (uptr i
= 0; i
< ARRAY_SIZE(kSyncSignals
); i
++) {
285 __sanitizer_sigaction act
;
286 internal_memset(&act
, 0, sizeof(act
));
287 act
.sigaction
= TracerThreadSignalHandler
;
288 act
.sa_flags
= SA_ONSTACK
| SA_SIGINFO
;
289 internal_sigaction_norestorer(kSyncSignals
[i
], &act
, 0);
293 if (!thread_suspender
.SuspendAllThreads()) {
294 VReport(1, "Failed suspending threads.\n");
297 tracer_thread_argument
->callback(thread_suspender
.suspended_threads_list(),
298 tracer_thread_argument
->callback_argument
);
299 thread_suspender
.ResumeAllThreads();
302 RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback
));
303 thread_suspender_instance
= nullptr;
304 atomic_store(&tracer_thread_argument
->done
, 1, memory_order_relaxed
);
308 class ScopedStackSpaceWithGuard
{
310 explicit ScopedStackSpaceWithGuard(uptr stack_size
) {
311 stack_size_
= stack_size
;
312 guard_size_
= GetPageSizeCached();
313 // FIXME: Omitting MAP_STACK here works in current kernels but might break
315 guard_start_
= (uptr
)MmapOrDie(stack_size_
+ guard_size_
,
316 "ScopedStackWithGuard");
317 CHECK(MprotectNoAccess((uptr
)guard_start_
, guard_size_
));
319 ~ScopedStackSpaceWithGuard() {
320 UnmapOrDie((void *)guard_start_
, stack_size_
+ guard_size_
);
322 void *Bottom() const {
323 return (void *)(guard_start_
+ stack_size_
+ guard_size_
);
332 // We have a limitation on the stack frame size, so some stuff had to be moved
334 static __sanitizer_sigset_t blocked_sigset
;
335 static __sanitizer_sigset_t old_sigset
;
337 class StopTheWorldScope
{
339 StopTheWorldScope() {
340 // Make this process dumpable. Processes that are not dumpable cannot be
342 process_was_dumpable_
= internal_prctl(PR_GET_DUMPABLE
, 0, 0, 0, 0);
343 if (!process_was_dumpable_
)
344 internal_prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
347 ~StopTheWorldScope() {
348 // Restore the dumpable flag.
349 if (!process_was_dumpable_
)
350 internal_prctl(PR_SET_DUMPABLE
, 0, 0, 0, 0);
354 int process_was_dumpable_
;
357 // When sanitizer output is being redirected to file (i.e. by using log_path),
358 // the tracer should write to the parent's log instead of trying to open a new
359 // file. Alert the logging code to the fact that we have a tracer.
360 struct ScopedSetTracerPID
{
361 explicit ScopedSetTracerPID(uptr tracer_pid
) {
362 stoptheworld_tracer_pid
= tracer_pid
;
363 stoptheworld_tracer_ppid
= internal_getpid();
365 ~ScopedSetTracerPID() {
366 stoptheworld_tracer_pid
= 0;
367 stoptheworld_tracer_ppid
= 0;
371 void StopTheWorld(StopTheWorldCallback callback
, void *argument
) {
372 StopTheWorldScope in_stoptheworld
;
373 // Prepare the arguments for TracerThread.
374 struct TracerThreadArgument tracer_thread_argument
;
375 tracer_thread_argument
.callback
= callback
;
376 tracer_thread_argument
.callback_argument
= argument
;
377 tracer_thread_argument
.parent_pid
= internal_getpid();
378 atomic_store(&tracer_thread_argument
.done
, 0, memory_order_relaxed
);
379 const uptr kTracerStackSize
= 2 * 1024 * 1024;
380 ScopedStackSpaceWithGuard
tracer_stack(kTracerStackSize
);
381 // Block the execution of TracerThread until after we have set ptrace
383 tracer_thread_argument
.mutex
.Lock();
384 // Signal handling story.
385 // We don't want async signals to be delivered to the tracer thread,
386 // so we block all async signals before creating the thread. An async signal
387 // handler can temporary modify errno, which is shared with this thread.
388 // We ought to use pthread_sigmask here, because sigprocmask has undefined
389 // behavior in multithreaded programs. However, on linux sigprocmask is
390 // equivalent to pthread_sigmask with the exception that pthread_sigmask
391 // does not allow to block some signals used internally in pthread
392 // implementation. We are fine with blocking them here, we are really not
393 // going to pthread_cancel the thread.
394 // The tracer thread should not raise any synchronous signals. But in case it
395 // does, we setup a special handler for sync signals that properly kills the
396 // parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers
397 // in the tracer thread won't interfere with user program. Double note: if a
398 // user does something along the lines of 'kill -11 pid', that can kill the
399 // process even if user setup own handler for SEGV.
400 // Thing to watch out for: this code should not change behavior of user code
401 // in any observable way. In particular it should not override user signal
403 internal_sigfillset(&blocked_sigset
);
404 for (uptr i
= 0; i
< ARRAY_SIZE(kSyncSignals
); i
++)
405 internal_sigdelset(&blocked_sigset
, kSyncSignals
[i
]);
406 int rv
= internal_sigprocmask(SIG_BLOCK
, &blocked_sigset
, &old_sigset
);
408 uptr tracer_pid
= internal_clone(
409 TracerThread
, tracer_stack
.Bottom(),
410 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_UNTRACED
,
411 &tracer_thread_argument
, nullptr /* parent_tidptr */,
412 nullptr /* newtls */, nullptr /* child_tidptr */);
413 internal_sigprocmask(SIG_SETMASK
, &old_sigset
, 0);
415 if (internal_iserror(tracer_pid
, &local_errno
)) {
416 VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno
);
417 tracer_thread_argument
.mutex
.Unlock();
419 ScopedSetTracerPID
scoped_set_tracer_pid(tracer_pid
);
420 // On some systems we have to explicitly declare that we want to be traced
421 // by the tracer thread.
422 #ifdef PR_SET_PTRACER
423 internal_prctl(PR_SET_PTRACER
, tracer_pid
, 0, 0, 0);
425 // Allow the tracer thread to start.
426 tracer_thread_argument
.mutex
.Unlock();
427 // NOTE: errno is shared between this thread and the tracer thread.
428 // internal_waitpid() may call syscall() which can access/spoil errno,
429 // so we can't call it now. Instead we for the tracer thread to finish using
430 // the spin loop below. Man page for sched_yield() says "In the Linux
431 // implementation, sched_yield() always succeeds", so let's hope it does not
432 // spoil errno. Note that this spin loop runs only for brief periods before
433 // the tracer thread has suspended us and when it starts unblocking threads.
434 while (atomic_load(&tracer_thread_argument
.done
, memory_order_relaxed
) == 0)
436 // Now the tracer thread is about to exit and does not touch errno,
439 uptr waitpid_status
= internal_waitpid(tracer_pid
, nullptr, __WALL
);
440 if (!internal_iserror(waitpid_status
, &local_errno
))
442 if (local_errno
== EINTR
)
444 VReport(1, "Waiting on the tracer thread failed (errno %d).\n",
451 // Platform-specific methods from SuspendedThreadsList.
452 #if SANITIZER_ANDROID && defined(__arm__)
453 typedef pt_regs regs_struct
;
454 #define REG_SP ARM_sp
456 #elif SANITIZER_LINUX && defined(__arm__)
457 typedef user_regs regs_struct
;
458 #define REG_SP uregs[13]
460 #elif defined(__i386__) || defined(__x86_64__)
461 typedef user_regs_struct regs_struct
;
462 #if defined(__i386__)
468 #elif defined(__powerpc__) || defined(__powerpc64__)
469 typedef pt_regs regs_struct
;
470 #define REG_SP gpr[PT_R1]
472 #elif defined(__mips__)
473 typedef struct user regs_struct
;
474 # if SANITIZER_ANDROID
475 # define REG_SP regs[EF_R29]
477 # define REG_SP regs[EF_REG29]
480 #elif defined(__aarch64__)
481 typedef struct user_pt_regs regs_struct
;
483 #define ARCH_IOVEC_FOR_GETREGSET
485 #elif defined(__s390__)
486 typedef _user_regs_struct regs_struct
;
487 #define REG_SP gprs[15]
488 #define ARCH_IOVEC_FOR_GETREGSET
491 #error "Unsupported architecture"
492 #endif // SANITIZER_ANDROID && defined(__arm__)
494 int SuspendedThreadsList::GetRegistersAndSP(uptr index
,
497 pid_t tid
= GetThreadID(index
);
500 #ifdef ARCH_IOVEC_FOR_GETREGSET
501 struct iovec regset_io
;
502 regset_io
.iov_base
= ®s
;
503 regset_io
.iov_len
= sizeof(regs_struct
);
504 bool isErr
= internal_iserror(internal_ptrace(PTRACE_GETREGSET
, tid
,
505 (void*)NT_PRSTATUS
, (void*)®set_io
),
508 bool isErr
= internal_iserror(internal_ptrace(PTRACE_GETREGS
, tid
, nullptr,
512 VReport(1, "Could not get registers from thread %d (errno %d).\n", tid
,
518 internal_memcpy(buffer
, ®s
, sizeof(regs
));
522 uptr
SuspendedThreadsList::RegisterCount() {
523 return sizeof(regs_struct
) / sizeof(uptr
);
525 } // namespace __sanitizer
527 #endif // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__)
528 // || defined(__aarch64__) || defined(__powerpc64__)
529 // || defined(__s390__)