| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
/* Copyright (c) 2005-2007, Google Inc. |
| 3 |
* Copyright (c) 2023, gperftools Contributors |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions are |
| 8 |
* met: |
| 9 |
* |
| 10 |
* * Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* * Redistributions in binary form must reproduce the above |
| 13 |
* copyright notice, this list of conditions and the following disclaimer |
| 14 |
* in the documentation and/or other materials provided with the |
| 15 |
* distribution. |
| 16 |
* * Neither the name of Google Inc. nor the names of its |
| 17 |
* contributors may be used to endorse or promote products derived from |
| 18 |
* this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
* |
| 32 |
* --- |
| 33 |
* Author: Markus Gutschke |
| 34 |
* |
| 35 |
* Substantial upgrades by Aliaksey Kandratsenka. All bugs are mine. |
| 36 |
*/ |
| 37 |
#ifndef _GNU_SOURCE |
| 38 |
#define _GNU_SOURCE |
| 39 |
#endif |
| 40 |
|
| 41 |
#include "base/linuxthreads.h" |
| 42 |
|
| 43 |
#include <errno.h> |
| 44 |
#include <fcntl.h> |
| 45 |
#include <limits.h> |
| 46 |
#include <sched.h> |
| 47 |
#include <signal.h> |
| 48 |
#include <stdio.h> |
| 49 |
#include <stdlib.h> |
| 50 |
#include <string.h> |
| 51 |
#include <sys/prctl.h> |
| 52 |
#include <sys/ptrace.h> |
| 53 |
#include <sys/socket.h> |
| 54 |
#include <sys/stat.h> |
| 55 |
#include <sys/syscall.h> |
| 56 |
#include <sys/wait.h> |
| 57 |
#include <unistd.h> |
| 58 |
|
| 59 |
#include <atomic> |
| 60 |
|
| 61 |
#include "base/basictypes.h" |
| 62 |
#include "base/logging.h" |
| 63 |
|
| 64 |
#ifndef CLONE_UNTRACED |
| 65 |
#define CLONE_UNTRACED 0x00800000 |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifndef PR_SET_PTRACER |
| 69 |
#define PR_SET_PTRACER 0x59616d61 |
| 70 |
#endif |
| 71 |
|
| 72 |
namespace { |
| 73 |
|
| 74 |
class SetPTracerSetup { |
| 75 |
public: |
| 76 |
~SetPTracerSetup() { |
| 77 |
if (need_cleanup_) { |
| 78 |
prctl(PR_SET_PTRACER, 0, 0, 0, 0); |
| 79 |
} |
| 80 |
} |
| 81 |
void Prepare(int clone_pid) { |
| 82 |
if (prctl(PR_SET_PTRACER, clone_pid, 0, 0, 0) == 0) { |
| 83 |
need_cleanup_ = true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private: |
| 88 |
bool need_cleanup_ = false; |
| 89 |
}; |
| 90 |
|
| 91 |
class UniqueFD { |
| 92 |
public: |
| 93 |
explicit UniqueFD(int fd) : fd_(fd) {} |
| 94 |
|
| 95 |
int ReleaseFD() { |
| 96 |
int retval = fd_; |
| 97 |
fd_ = -1; |
| 98 |
return retval; |
| 99 |
} |
| 100 |
|
| 101 |
~UniqueFD() { |
| 102 |
if (fd_ < 0) { |
| 103 |
return; |
| 104 |
} |
| 105 |
(void)close(fd_); |
| 106 |
} |
| 107 |
private: |
| 108 |
int fd_; |
| 109 |
}; |
| 110 |
|
| 111 |
template <typename Body> |
| 112 |
struct SimpleCleanup { |
| 113 |
const Body body; |
| 114 |
|
| 115 |
explicit SimpleCleanup(const Body& body) : body(body) {} |
| 116 |
|
| 117 |
~SimpleCleanup() { |
| 118 |
body(); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
template <typename Body> |
| 123 |
SimpleCleanup<Body> MakeSimpleCleanup(const Body& body) { |
| 124 |
return SimpleCleanup<Body>{body}; |
| 125 |
}; |
| 126 |
|
| 127 |
} // namespace |
| 128 |
|
| 129 |
/* Synchronous signals that should not be blocked while in the lister thread. |
| 130 |
*/ |
| 131 |
static const int sync_signals[] = { |
| 132 |
SIGABRT, SIGILL, |
| 133 |
SIGFPE, SIGSEGV, SIGBUS, |
| 134 |
#ifdef SIGEMT |
| 135 |
SIGEMT, |
| 136 |
#endif |
| 137 |
SIGSYS, SIGTRAP, |
| 138 |
SIGXCPU, SIGXFSZ }; |
| 139 |
|
| 140 |
ATTRIBUTE_NOINLINE |
| 141 |
static int local_clone (int (*fn)(void *), void *arg) { |
| 142 |
#ifdef __PPC64__ |
| 143 |
/* To avoid the gap cross page boundaries, increase by the large parge |
| 144 |
* size mostly PowerPC system uses. */ |
| 145 |
|
| 146 |
// FIXME(alk): I don't really understand why ppc needs this and why |
| 147 |
// 64k pages matter. I.e. some other architectures have 64k pages, |
| 148 |
// so should we do the same there? |
| 149 |
uintptr_t clone_stack_size = 64 << 10; |
| 150 |
#else |
| 151 |
uintptr_t clone_stack_size = 4 << 10; |
| 152 |
#endif |
| 153 |
|
| 154 |
bool grows_to_low = (&arg < arg); |
| 155 |
if (grows_to_low) { |
| 156 |
// Negate clone_stack_size if stack grows to lower addresses |
| 157 |
// (common for arch-es that matter). |
| 158 |
clone_stack_size = ~clone_stack_size + 1; |
| 159 |
} |
| 160 |
|
| 161 |
#if defined(__i386__) || defined(__x86_64__) || defined(__riscv) || defined(__arm__) || defined(__aarch64__) |
| 162 |
// Sanity check code above. We know that those arch-es grow stack to |
| 163 |
// lower addresses. |
| 164 |
CHECK(grows_to_low); |
| 165 |
#endif |
| 166 |
|
| 167 |
/* Leave 4kB of gap between the callers stack and the new clone. This |
| 168 |
* should be more than sufficient for the caller to call waitpid() until |
| 169 |
* the cloned thread terminates. |
| 170 |
* |
| 171 |
* It is important that we set the CLONE_UNTRACED flag, because newer |
| 172 |
* versions of "gdb" otherwise attempt to attach to our thread, and will |
| 173 |
* attempt to reap its status codes. This subsequently results in the |
| 174 |
* caller hanging indefinitely in waitpid(), waiting for a change in |
| 175 |
* status that will never happen. By setting the CLONE_UNTRACED flag, we |
| 176 |
* prevent "gdb" from stealing events, but we still expect the thread |
| 177 |
* lister to fail, because it cannot PTRACE_ATTACH to the process that |
| 178 |
* is being debugged. This is OK and the error code will be reported |
| 179 |
* correctly. |
| 180 |
*/ |
| 181 |
uintptr_t stack_addr = reinterpret_cast<uintptr_t>(&arg) + clone_stack_size; |
| 182 |
stack_addr &= ~63; // align stack address on 64 bytes (x86 needs 16, but lets be generous) |
| 183 |
return clone(fn, reinterpret_cast<void*>(stack_addr), |
| 184 |
CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_UNTRACED, |
| 185 |
arg, 0, 0, 0); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
/* Local substitute for the atoi() function, which is not necessarily safe |
| 190 |
* to call once threads are suspended (depending on whether libc looks up |
| 191 |
* locale information, when executing atoi()). |
| 192 |
*/ |
| 193 |
static int local_atoi(const char *s) { |
| 194 |
int n = 0; |
| 195 |
int neg = *s == '-'; |
| 196 |
if (neg) |
| 197 |
s++; |
| 198 |
while (*s >= '0' && *s <= '9') |
| 199 |
n = 10*n + (*s++ - '0'); |
| 200 |
return neg ? -n : n; |
| 201 |
} |
| 202 |
|
| 203 |
static int ptrace_detach(pid_t pid) { |
| 204 |
return ptrace(PTRACE_DETACH, pid, nullptr, nullptr); |
| 205 |
} |
| 206 |
|
| 207 |
/* Re-runs fn until it doesn't cause EINTR |
| 208 |
*/ |
| 209 |
#define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR) |
| 210 |
|
| 211 |
/* abort() is not safely reentrant, and changes it's behavior each time |
| 212 |
* it is called. This means, if the main application ever called abort() |
| 213 |
* we cannot safely call it again. This would happen if we were called |
| 214 |
* from a SIGABRT signal handler in the main application. So, document |
| 215 |
* that calling SIGABRT from the thread lister makes it not signal safe |
| 216 |
* (and vice-versa). |
| 217 |
* Also, since we share address space with the main application, we |
| 218 |
* cannot call abort() from the callback and expect the main application |
| 219 |
* to behave correctly afterwards. In fact, the only thing we can do, is |
| 220 |
* to terminate the main application with extreme prejudice (aka |
| 221 |
* PTRACE_KILL). |
| 222 |
* We set up our own SIGABRT handler to do this. |
| 223 |
* In order to find the main application from the signal handler, we |
| 224 |
* need to store information about it in global variables. This is |
| 225 |
* safe, because the main application should be suspended at this |
| 226 |
* time. If the callback ever called TCMalloc_ResumeAllProcessThreads(), then |
| 227 |
* we are running a higher risk, though. So, try to avoid calling |
| 228 |
* abort() after calling TCMalloc_ResumeAllProcessThreads. |
| 229 |
*/ |
| 230 |
static volatile int *sig_pids, sig_num_threads; |
| 231 |
|
| 232 |
|
| 233 |
/* Signal handler to help us recover from dying while we are attached to |
| 234 |
* other threads. |
| 235 |
*/ |
| 236 |
static void SignalHandler(int signum, siginfo_t *si, void *data) { |
| 237 |
RAW_LOG(ERROR, "Got fatal signal %d inside ListerThread", signum); |
| 238 |
|
| 239 |
if (sig_pids != NULL) { |
| 240 |
if (signum == SIGABRT) { |
| 241 |
prctl(PR_SET_PDEATHSIG, 0); |
| 242 |
while (sig_num_threads-- > 0) { |
| 243 |
/* Not sure if sched_yield is really necessary here, but it does not */ |
| 244 |
/* hurt, and it might be necessary for the same reasons that we have */ |
| 245 |
/* to do so in ptrace_detach(). */ |
| 246 |
sched_yield(); |
| 247 |
ptrace(PTRACE_KILL, sig_pids[sig_num_threads], 0, 0); |
| 248 |
} |
| 249 |
} else if (sig_num_threads > 0) { |
| 250 |
TCMalloc_ResumeAllProcessThreads(sig_num_threads, (int *)sig_pids); |
| 251 |
} |
| 252 |
} |
| 253 |
sig_pids = NULL; |
| 254 |
|
| 255 |
syscall(SYS_exit, signum == SIGABRT ? 1 : 2); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
/* Try to dirty the stack, and hope that the compiler is not smart enough |
| 260 |
* to optimize this function away. Or worse, the compiler could inline the |
| 261 |
* function and permanently allocate the data on the stack. |
| 262 |
*/ |
| 263 |
static void DirtyStack(size_t amount) { |
| 264 |
char buf[amount]; |
| 265 |
memset(buf, 0, amount); |
| 266 |
read(-1, buf, amount); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
/* Data structure for passing arguments to the lister thread. |
| 271 |
*/ |
| 272 |
#define ALT_STACKSIZE (MINSIGSTKSZ + 4096) |
| 273 |
|
| 274 |
struct ListerParams { |
| 275 |
int result, err; |
| 276 |
pid_t ppid; |
| 277 |
int start_pipe_rd; |
| 278 |
int start_pipe_wr; |
| 279 |
char *altstack_mem; |
| 280 |
ListAllProcessThreadsCallBack callback; |
| 281 |
void *parameter; |
| 282 |
va_list ap; |
| 283 |
int proc_fd; |
| 284 |
}; |
| 285 |
|
| 286 |
struct kernel_dirent64 { // see man 2 getdents |
| 287 |
int64_t d_ino; /* 64-bit inode number */ |
| 288 |
int64_t d_off; /* 64-bit offset to next structure */ |
| 289 |
unsigned short d_reclen; /* Size of this dirent */ |
| 290 |
unsigned char d_type; /* File type */ |
| 291 |
char d_name[]; /* Filename (null-terminated) */ |
| 292 |
}; |
| 293 |
|
| 294 |
static const kernel_dirent64 *BumpDirentPtr(const kernel_dirent64 *ptr, uintptr_t by_bytes) { |
| 295 |
return reinterpret_cast<kernel_dirent64*>(reinterpret_cast<uintptr_t>(ptr) + by_bytes); |
| 296 |
} |
| 297 |
|
| 298 |
static int ListerThread(struct ListerParams *args) { |
| 299 |
int found_parent = 0; |
| 300 |
pid_t clone_pid = syscall(SYS_gettid); |
| 301 |
int proc = args->proc_fd, num_threads = 0; |
| 302 |
int max_threads = 0, sig; |
| 303 |
struct stat proc_sb; |
| 304 |
stack_t altstack; |
| 305 |
|
| 306 |
/* Wait for parent thread to set appropriate permissions to allow |
| 307 |
* ptrace activity. Note we using pipe pair, so which ensures we |
| 308 |
* don't sleep past parent's death. |
| 309 |
*/ |
| 310 |
(void)close(args->start_pipe_wr); |
| 311 |
{ |
| 312 |
char tmp; |
| 313 |
read(args->start_pipe_rd, &tmp, sizeof(tmp)); |
| 314 |
} |
| 315 |
|
| 316 |
// No point in continuing if parent dies before/during ptracing. |
| 317 |
prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 318 |
|
| 319 |
/* Catch signals on an alternate pre-allocated stack. This way, we can |
| 320 |
* safely execute the signal handler even if we ran out of memory. |
| 321 |
*/ |
| 322 |
memset(&altstack, 0, sizeof(altstack)); |
| 323 |
altstack.ss_sp = args->altstack_mem; |
| 324 |
altstack.ss_flags = 0; |
| 325 |
altstack.ss_size = ALT_STACKSIZE; |
| 326 |
sigaltstack(&altstack, nullptr); |
| 327 |
|
| 328 |
/* Some kernels forget to wake up traced processes, when the |
| 329 |
* tracer dies. So, intercept synchronous signals and make sure |
| 330 |
* that we wake up our tracees before dying. It is the caller's |
| 331 |
* responsibility to ensure that asynchronous signals do not |
| 332 |
* interfere with this function. |
| 333 |
*/ |
| 334 |
for (sig = 0; sig < sizeof(sync_signals)/sizeof(*sync_signals); sig++) { |
| 335 |
struct sigaction sa; |
| 336 |
memset(&sa, 0, sizeof(sa)); |
| 337 |
sa.sa_sigaction = SignalHandler; |
| 338 |
sigfillset(&sa.sa_mask); |
| 339 |
sa.sa_flags = SA_ONSTACK|SA_SIGINFO|SA_RESETHAND; |
| 340 |
sigaction(sync_signals[sig], &sa, nullptr); |
| 341 |
} |
| 342 |
|
| 343 |
/* Read process directories in /proc/... */ |
| 344 |
for (;;) { |
| 345 |
if (lseek(proc, 0, SEEK_SET) < 0) { |
| 346 |
goto failure; |
| 347 |
} |
| 348 |
if (fstat(proc, &proc_sb) < 0) { |
| 349 |
goto failure; |
| 350 |
} |
| 351 |
|
| 352 |
/* Since we are suspending threads, we cannot call any libc |
| 353 |
* functions that might acquire locks. Most notably, we cannot |
| 354 |
* call malloc(). So, we have to allocate memory on the stack, |
| 355 |
* instead. Since we do not know how much memory we need, we |
| 356 |
* make a best guess. And if we guessed incorrectly we retry on |
| 357 |
* a second iteration (by jumping to "detach_threads"). |
| 358 |
* |
| 359 |
* Unless the number of threads is increasing very rapidly, we |
| 360 |
* should never need to do so, though, as our guestimate is very |
| 361 |
* conservative. |
| 362 |
*/ |
| 363 |
if (max_threads < proc_sb.st_nlink + 100) { |
| 364 |
max_threads = proc_sb.st_nlink + 100; |
| 365 |
} |
| 366 |
|
| 367 |
/* scope */ { |
| 368 |
pid_t pids[max_threads]; |
| 369 |
int added_entries = 0; |
| 370 |
sig_num_threads = num_threads; |
| 371 |
sig_pids = pids; |
| 372 |
for (;;) { |
| 373 |
// lets make sure to align buf to store kernel_dirent64-s properly. |
| 374 |
int64_t buf[4096 / sizeof(int64_t)]; |
| 375 |
|
| 376 |
ssize_t nbytes = syscall(SYS_getdents64, proc, buf, sizeof(buf)); |
| 377 |
// fprintf(stderr, "nbytes = %zd\n", nbytes); |
| 378 |
|
| 379 |
if (nbytes < 0) { |
| 380 |
goto failure; |
| 381 |
} |
| 382 |
|
| 383 |
if (nbytes == 0) { |
| 384 |
if (added_entries) { |
| 385 |
/* Need to keep iterating over "/proc" in multiple |
| 386 |
* passes until we no longer find any more threads. This |
| 387 |
* algorithm eventually completes, when all threads have |
| 388 |
* been suspended. |
| 389 |
*/ |
| 390 |
added_entries = 0; |
| 391 |
lseek(proc, 0, SEEK_SET); |
| 392 |
continue; |
| 393 |
} |
| 394 |
break; |
| 395 |
} |
| 396 |
|
| 397 |
const kernel_dirent64 *entry = reinterpret_cast<kernel_dirent64*>(buf); |
| 398 |
const kernel_dirent64 *end = BumpDirentPtr(entry, nbytes); |
| 399 |
|
| 400 |
for (;entry < end; entry = BumpDirentPtr(entry, entry->d_reclen)) { |
| 401 |
if (entry->d_ino == 0) { |
| 402 |
continue; |
| 403 |
} |
| 404 |
|
| 405 |
const char *ptr = entry->d_name; |
| 406 |
// fprintf(stderr, "name: %s\n", ptr); |
| 407 |
pid_t pid; |
| 408 |
|
| 409 |
/* Some kernels hide threads by preceding the pid with a '.' */ |
| 410 |
if (*ptr == '.') |
| 411 |
ptr++; |
| 412 |
|
| 413 |
/* If the directory is not numeric, it cannot be a |
| 414 |
* process/thread |
| 415 |
*/ |
| 416 |
if (*ptr < '0' || *ptr > '9') |
| 417 |
continue; |
| 418 |
pid = local_atoi(ptr); |
| 419 |
// fprintf(stderr, "pid = %d (%d)\n", pid, getpid()); |
| 420 |
|
| 421 |
if (!pid || pid == clone_pid) { |
| 422 |
continue; |
| 423 |
} |
| 424 |
|
| 425 |
/* Attach (and suspend) all threads */ |
| 426 |
long i, j; |
| 427 |
|
| 428 |
/* Found one of our threads, make sure it is no duplicate */ |
| 429 |
for (i = 0; i < num_threads; i++) { |
| 430 |
/* Linear search is slow, but should not matter much for |
| 431 |
* the typically small number of threads. |
| 432 |
*/ |
| 433 |
if (pids[i] == pid) { |
| 434 |
/* Found a duplicate; most likely on second pass */ |
| 435 |
goto next_entry; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
/* Check whether data structure needs growing */ |
| 440 |
if (num_threads >= max_threads) { |
| 441 |
/* Back to square one, this time with more memory */ |
| 442 |
goto detach_threads; |
| 443 |
} |
| 444 |
|
| 445 |
/* Attaching to thread suspends it */ |
| 446 |
pids[num_threads++] = pid; |
| 447 |
sig_num_threads = num_threads; |
| 448 |
|
| 449 |
if (ptrace(PTRACE_ATTACH, pid, (void *)0, |
| 450 |
(void *)0) < 0) { |
| 451 |
/* If operation failed, ignore thread. Maybe it |
| 452 |
* just died? There might also be a race |
| 453 |
* condition with a concurrent core dumper or |
| 454 |
* with a debugger. In that case, we will just |
| 455 |
* make a best effort, rather than failing |
| 456 |
* entirely. |
| 457 |
*/ |
| 458 |
num_threads--; |
| 459 |
sig_num_threads = num_threads; |
| 460 |
goto next_entry; |
| 461 |
} |
| 462 |
while (waitpid(pid, (int *)0, __WALL) < 0) { |
| 463 |
if (errno != EINTR) { |
| 464 |
ptrace_detach(pid); |
| 465 |
num_threads--; |
| 466 |
sig_num_threads = num_threads; |
| 467 |
goto next_entry; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
if (syscall(SYS_ptrace, PTRACE_PEEKDATA, pid, &i, &j) || i++ != j || |
| 472 |
syscall(SYS_ptrace, PTRACE_PEEKDATA, pid, &i, &j) || i != j) { |
| 473 |
/* Address spaces are distinct. This is probably |
| 474 |
* a forked child process rather than a thread. |
| 475 |
*/ |
| 476 |
ptrace_detach(pid); |
| 477 |
num_threads--; |
| 478 |
sig_num_threads = num_threads; |
| 479 |
goto next_entry; |
| 480 |
} |
| 481 |
|
| 482 |
found_parent |= pid == args->ppid; |
| 483 |
added_entries++; |
| 484 |
|
| 485 |
next_entry:; |
| 486 |
} // entries iterations loop |
| 487 |
} // getdents loop |
| 488 |
|
| 489 |
/* If we never found the parent process, something is very wrong. |
| 490 |
* Most likely, we are running in debugger. Any attempt to operate |
| 491 |
* on the threads would be very incomplete. Let's just report an |
| 492 |
* error to the caller. |
| 493 |
*/ |
| 494 |
if (!found_parent) { |
| 495 |
TCMalloc_ResumeAllProcessThreads(num_threads, pids); |
| 496 |
return 3; |
| 497 |
} |
| 498 |
|
| 499 |
/* Now we are ready to call the callback, |
| 500 |
* which takes care of resuming the threads for us. |
| 501 |
*/ |
| 502 |
args->result = args->callback(args->parameter, num_threads, |
| 503 |
pids, args->ap); |
| 504 |
args->err = errno; |
| 505 |
|
| 506 |
/* Callback should have resumed threads, but better safe than sorry */ |
| 507 |
if (TCMalloc_ResumeAllProcessThreads(num_threads, pids)) { |
| 508 |
/* Callback forgot to resume at least one thread, report error */ |
| 509 |
args->err = EINVAL; |
| 510 |
args->result = -1; |
| 511 |
} |
| 512 |
|
| 513 |
return 0; |
| 514 |
|
| 515 |
detach_threads: |
| 516 |
/* Resume all threads prior to retrying the operation */ |
| 517 |
TCMalloc_ResumeAllProcessThreads(num_threads, pids); |
| 518 |
sig_pids = NULL; |
| 519 |
num_threads = 0; |
| 520 |
sig_num_threads = num_threads; |
| 521 |
max_threads += 100; |
| 522 |
} // pids[max_threads] scope |
| 523 |
} // for (;;) |
| 524 |
|
| 525 |
failure: |
| 526 |
args->result = -1; |
| 527 |
args->err = errno; |
| 528 |
return 1; |
| 529 |
} |
| 530 |
|
| 531 |
/* This function gets the list of all linux threads of the current process |
| 532 |
* passes them to the 'callback' along with the 'parameter' pointer; at the |
| 533 |
* call back call time all the threads are paused via |
| 534 |
* PTRACE_ATTACH. |
| 535 |
* The callback is executed from a separate thread which shares only the |
| 536 |
* address space, the filesystem, and the filehandles with the caller. Most |
| 537 |
* notably, it does not share the same pid and ppid; and if it terminates, |
| 538 |
* the rest of the application is still there. 'callback' is supposed to do |
| 539 |
* or arrange for TCMalloc_ResumeAllProcessThreads. This happens automatically, if |
| 540 |
* the thread raises a synchronous signal (e.g. SIGSEGV); asynchronous |
| 541 |
* signals are blocked. If the 'callback' decides to unblock them, it must |
| 542 |
* ensure that they cannot terminate the application, or that |
| 543 |
* TCMalloc_ResumeAllProcessThreads will get called. |
| 544 |
* It is an error for the 'callback' to make any library calls that could |
| 545 |
* acquire locks. Most notably, this means that most system calls have to |
| 546 |
* avoid going through libc. Also, this means that it is not legal to call |
| 547 |
* exit() or abort(). |
| 548 |
* We return -1 on error and the return value of 'callback' on success. |
| 549 |
*/ |
| 550 |
int TCMalloc_ListAllProcessThreads(void *parameter, |
| 551 |
ListAllProcessThreadsCallBack callback, ...) { |
| 552 |
char altstack_mem[ALT_STACKSIZE]; |
| 553 |
struct ListerParams args; |
| 554 |
pid_t clone_pid; |
| 555 |
int dumpable = 1; |
| 556 |
int need_sigprocmask = 0; |
| 557 |
sigset_t sig_blocked, sig_old; |
| 558 |
int status, rc; |
| 559 |
|
| 560 |
SetPTracerSetup ptracer_setup; |
| 561 |
|
| 562 |
auto cleanup = MakeSimpleCleanup([&] () { |
| 563 |
int old_errno = errno; |
| 564 |
|
| 565 |
if (need_sigprocmask) { |
| 566 |
sigprocmask(SIG_SETMASK, &sig_old, nullptr); |
| 567 |
} |
| 568 |
|
| 569 |
if (!dumpable) { |
| 570 |
prctl(PR_SET_DUMPABLE, dumpable); |
| 571 |
} |
| 572 |
|
| 573 |
errno = old_errno; |
| 574 |
}); |
| 575 |
|
| 576 |
va_start(args.ap, callback); |
| 577 |
|
| 578 |
/* If we are short on virtual memory, initializing the alternate stack |
| 579 |
* might trigger a SIGSEGV. Let's do this early, before it could get us |
| 580 |
* into more trouble (i.e. before signal handlers try to use the alternate |
| 581 |
* stack, and before we attach to other threads). |
| 582 |
*/ |
| 583 |
memset(altstack_mem, 0, sizeof(altstack_mem)); |
| 584 |
|
| 585 |
/* Some of our cleanup functions could conceivable use more stack space. |
| 586 |
* Try to touch the stack right now. This could be defeated by the compiler |
| 587 |
* being too smart for it's own good, so try really hard. |
| 588 |
*/ |
| 589 |
DirtyStack(32768); |
| 590 |
|
| 591 |
/* Make this process "dumpable". This is necessary in order to ptrace() |
| 592 |
* after having called setuid(). |
| 593 |
*/ |
| 594 |
dumpable = prctl(PR_GET_DUMPABLE, 0); |
| 595 |
if (!dumpable) { |
| 596 |
prctl(PR_SET_DUMPABLE, 1); |
| 597 |
} |
| 598 |
|
| 599 |
/* Fill in argument block for dumper thread */ |
| 600 |
args.result = -1; |
| 601 |
args.err = 0; |
| 602 |
args.ppid = getpid(); |
| 603 |
args.altstack_mem = altstack_mem; |
| 604 |
args.parameter = parameter; |
| 605 |
args.callback = callback; |
| 606 |
|
| 607 |
NO_INTR(args.proc_fd = open("/proc/self/task/", O_RDONLY|O_DIRECTORY|O_CLOEXEC)); |
| 608 |
UniqueFD proc_closer{args.proc_fd}; |
| 609 |
|
| 610 |
if (args.proc_fd < 0) { |
| 611 |
return -1; |
| 612 |
} |
| 613 |
|
| 614 |
int pipefds[2]; |
| 615 |
if (pipe2(pipefds, O_CLOEXEC)) { |
| 616 |
return -1; |
| 617 |
} |
| 618 |
|
| 619 |
UniqueFD pipe_rd_closer{pipefds[0]}; |
| 620 |
UniqueFD pipe_wr_closer{pipefds[1]}; |
| 621 |
|
| 622 |
args.start_pipe_rd = pipefds[0]; |
| 623 |
args.start_pipe_wr = pipefds[1]; |
| 624 |
|
| 625 |
/* Before cloning the thread lister, block all asynchronous signals, as we */ |
| 626 |
/* are not prepared to handle them. */ |
| 627 |
sigfillset(&sig_blocked); |
| 628 |
for (int sig = 0; sig < sizeof(sync_signals)/sizeof(*sync_signals); sig++) { |
| 629 |
sigdelset(&sig_blocked, sync_signals[sig]); |
| 630 |
} |
| 631 |
if (sigprocmask(SIG_BLOCK, &sig_blocked, &sig_old)) { |
| 632 |
return -1; |
| 633 |
} |
| 634 |
need_sigprocmask = 1; |
| 635 |
|
| 636 |
// make sure all functions used by parent from local_clone to after |
| 637 |
// waitpid have plt entries fully initialized. We cannot afford |
| 638 |
// dynamic linker running relocations and messing with errno (see |
| 639 |
// comment just below) |
| 640 |
(void)prctl(PR_GET_PDEATHSIG, 0); |
| 641 |
(void)close(-1); |
| 642 |
(void)waitpid(INT_MIN, nullptr, 0); |
| 643 |
|
| 644 |
/* After cloning, both the parent and the child share the same |
| 645 |
* instance of errno. We deal with this by being very |
| 646 |
* careful. Specifically, child immediately calls into sem_wait |
| 647 |
* which never fails (cannot even EINTR), so doesn't touch errno. |
| 648 |
* |
| 649 |
* Parent sets up PR_SET_PTRACER prctl (if it fails, which usually |
| 650 |
* doesn't happen, we ignore that failure). Then parent does close |
| 651 |
* on write side of start pipe. After that child runs complex code, |
| 652 |
* including arbitrary callback. So parent avoids screwing with |
| 653 |
* errno by immediately calling waitpid with async signals disabled. |
| 654 |
* |
| 655 |
* I.e. errno is parent's up until close below. Then errno belongs |
| 656 |
* to child up until it exits. |
| 657 |
*/ |
| 658 |
clone_pid = local_clone((int (*)(void *))ListerThread, &args); |
| 659 |
if (clone_pid < 0) { |
| 660 |
return -1; |
| 661 |
} |
| 662 |
|
| 663 |
/* Most Linux kernels in the wild have Yama LSM enabled, so |
| 664 |
* requires us to explicitly give permission for child to ptrace |
| 665 |
* us. See man 2 ptrace for details. This then requires us to |
| 666 |
* synchronize with the child (see close on start pipe |
| 667 |
* below). I.e. so that child doesn't start ptracing before we've |
| 668 |
* completed this prctl call. |
| 669 |
*/ |
| 670 |
ptracer_setup.Prepare(clone_pid); |
| 671 |
|
| 672 |
/* Closing write side of pipe works like releasing the lock. It |
| 673 |
* allows the ListerThread to run past read() call on read side of |
| 674 |
* pipe and ptrace us. |
| 675 |
*/ |
| 676 |
close(pipe_wr_closer.ReleaseFD()); |
| 677 |
|
| 678 |
/* So here child runs (see ListerThread), it finds and ptraces all |
| 679 |
* threads, runs whatever callback is setup and then |
| 680 |
* detaches/resumes everything. In any case we wait for child's |
| 681 |
* completion to gather status and synchronize everything. */ |
| 682 |
|
| 683 |
rc = waitpid(clone_pid, &status, __WALL); |
| 684 |
|
| 685 |
if (rc < 0) { |
| 686 |
if (errno == EINTR) { |
| 687 |
RAW_LOG(FATAL, "BUG: EINTR from waitpid shouldn't be possible!"); |
| 688 |
} |
| 689 |
// Any error waiting for child is sign of some bug, so abort |
| 690 |
// asap. Continuing is unsafe anyways with child potentially writing to our |
| 691 |
// stack. |
| 692 |
RAW_LOG(FATAL, "BUG: waitpid inside TCMalloc_ListAllProcessThreads cannot fail, but it did. Raw errno: %d\n", errno); |
| 693 |
} else if (WIFEXITED(status)) { |
| 694 |
errno = args.err; |
| 695 |
switch (WEXITSTATUS(status)) { |
| 696 |
case 0: break; /* Normal process termination */ |
| 697 |
case 2: args.err = EFAULT; /* Some fault (e.g. SIGSEGV) detected */ |
| 698 |
args.result = -1; |
| 699 |
break; |
| 700 |
case 3: args.err = EPERM; /* Process is already being traced */ |
| 701 |
args.result = -1; |
| 702 |
break; |
| 703 |
default:args.err = ECHILD; /* Child died unexpectedly */ |
| 704 |
args.result = -1; |
| 705 |
break; |
| 706 |
} |
| 707 |
} else if (!WIFEXITED(status)) { |
| 708 |
args.err = EFAULT; /* Terminated due to an unhandled signal*/ |
| 709 |
args.result = -1; |
| 710 |
} |
| 711 |
|
| 712 |
errno = args.err; |
| 713 |
return args.result; |
| 714 |
} |
| 715 |
|
| 716 |
/* This function resumes the list of all linux threads that |
| 717 |
* TCMalloc_ListAllProcessThreads pauses before giving to its callback. |
| 718 |
* The function returns non-zero if at least one thread was |
| 719 |
* suspended and has now been resumed. |
| 720 |
*/ |
| 721 |
int TCMalloc_ResumeAllProcessThreads(int num_threads, pid_t *thread_pids) { |
| 722 |
int detached_at_least_one = 0; |
| 723 |
while (num_threads-- > 0) { |
| 724 |
detached_at_least_one |= (ptrace_detach(thread_pids[num_threads]) >= 0); |
| 725 |
} |
| 726 |
return detached_at_least_one; |
| 727 |
} |