1//===-- sanitizer_posix_libcdep.cpp ---------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is shared between AddressSanitizer and ThreadSanitizer
10// run-time libraries and implements libc-dependent POSIX-specific functions
11// from sanitizer_libc.h.
12//===----------------------------------------------------------------------===//
13
14#include "sanitizer_platform.h"
15
16#if SANITIZER_POSIX
17
18#include "sanitizer_common.h"
19#include "sanitizer_flags.h"
20#include "sanitizer_platform_limits_netbsd.h"
21#include "sanitizer_platform_limits_posix.h"
22#include "sanitizer_platform_limits_solaris.h"
23#include "sanitizer_posix.h"
24#include "sanitizer_procmaps.h"
25
26#include <errno.h>
27#include <fcntl.h>
28#include <pthread.h>
29#include <signal.h>
30#include <stdlib.h>
31#include <sys/mman.h>
32#include <sys/resource.h>
33#include <sys/stat.h>
34#include <sys/time.h>
35#include <sys/types.h>
36#include <sys/wait.h>
37#include <unistd.h>
38
39#if SANITIZER_FREEBSD
40// The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
41// that, it was never implemented. So just define it to zero.
42#undef MAP_NORESERVE
43#define MAP_NORESERVE 0
44#endif
45
46typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
47
48namespace __sanitizer {
49
50[[maybe_unused]] static atomic_uint8_t signal_handler_is_from_sanitizer[64];
51
52u32 GetUid() {
53 return getuid();
54}
55
56uptr GetThreadSelf() {
57 return (uptr)pthread_self();
58}
59
60void ReleaseMemoryPagesToOS(uptr beg, uptr end) {
61 uptr page_size = GetPageSizeCached();
62 uptr beg_aligned = RoundUpTo(size: beg, boundary: page_size);
63 uptr end_aligned = RoundDownTo(x: end, boundary: page_size);
64 if (beg_aligned < end_aligned)
65 internal_madvise(addr: beg_aligned, length: end_aligned - beg_aligned,
66 SANITIZER_MADVISE_DONTNEED);
67}
68
69void SetShadowRegionHugePageMode(uptr addr, uptr size) {
70#ifdef MADV_NOHUGEPAGE // May not be defined on old systems.
71 if (common_flags()->no_huge_pages_for_shadow)
72 internal_madvise(addr, length: size, MADV_NOHUGEPAGE);
73 else
74 internal_madvise(addr, length: size, MADV_HUGEPAGE);
75#endif // MADV_NOHUGEPAGE
76}
77
78bool DontDumpShadowMemory(uptr addr, uptr length) {
79#if defined(MADV_DONTDUMP)
80 return internal_madvise(addr, length, MADV_DONTDUMP) == 0;
81#elif defined(MADV_NOCORE)
82 return internal_madvise(addr, length, MADV_NOCORE) == 0;
83#else
84 return true;
85#endif // MADV_DONTDUMP
86}
87
88static rlim_t getlim(int res) {
89 rlimit rlim;
90 CHECK_EQ(0, getrlimit(res, &rlim));
91 return rlim.rlim_cur;
92}
93
94static void setlim(int res, rlim_t lim) {
95 struct rlimit rlim;
96 if (getrlimit(resource: res, rlimits: &rlim)) {
97 Report(format: "ERROR: %s getrlimit() failed %d\n", SanitizerToolName, errno);
98 Die();
99 }
100 rlim.rlim_cur = lim;
101 if (setrlimit(resource: res, rlimits: &rlim)) {
102 Report(format: "ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
103 Die();
104 }
105}
106
107void DisableCoreDumperIfNecessary() {
108 if (common_flags()->disable_coredump) {
109 rlimit rlim;
110 CHECK_EQ(0, getrlimit(RLIMIT_CORE, &rlim));
111 // On Linux, if the kernel.core_pattern sysctl starts with a '|' (i.e. it
112 // is being piped to a coredump handler such as systemd-coredumpd), the
113 // kernel ignores RLIMIT_CORE (since we aren't creating a file in the file
114 // system) except for the magic value of 1, which disables coredumps when
115 // piping. 1 byte is too small for any kind of valid core dump, so it
116 // also disables coredumps if kernel.core_pattern creates files directly.
117 // While most piped coredump handlers do respect the crashing processes'
118 // RLIMIT_CORE, this is notable not the case for Debian's systemd-coredump
119 // due to a local patch that changes sysctl.d/50-coredump.conf to ignore
120 // the specified limit and instead use RLIM_INFINITY.
121 //
122 // The alternative to using RLIMIT_CORE=1 would be to use prctl() with the
123 // PR_SET_DUMPABLE flag, however that also prevents ptrace(), so makes it
124 // impossible to attach a debugger.
125 //
126 // Note: we use rlim_max in the Min() call here since that is the upper
127 // limit for what can be set without getting an EINVAL error.
128 rlim.rlim_cur = Min<rlim_t>(SANITIZER_LINUX ? 1 : 0, b: rlim.rlim_max);
129 CHECK_EQ(0, setrlimit(RLIMIT_CORE, &rlim));
130 }
131}
132
133bool StackSizeIsUnlimited() {
134 rlim_t stack_size = getlim(RLIMIT_STACK);
135 return (stack_size == RLIM_INFINITY);
136}
137
138void SetStackSizeLimitInBytes(uptr limit) {
139 setlim(RLIMIT_STACK, lim: (rlim_t)limit);
140 CHECK(!StackSizeIsUnlimited());
141}
142
143bool AddressSpaceIsUnlimited() {
144 rlim_t as_size = getlim(RLIMIT_AS);
145 return (as_size == RLIM_INFINITY);
146}
147
148void SetAddressSpaceUnlimited() {
149 setlim(RLIMIT_AS, RLIM_INFINITY);
150 CHECK(AddressSpaceIsUnlimited());
151}
152
153void Abort() {
154#if !SANITIZER_GO
155 // If we are handling SIGABRT, unhandle it first.
156 // TODO(vitalybuka): Check if handler belongs to sanitizer.
157 if (GetHandleSignalMode(SIGABRT) != kHandleSignalNo) {
158 struct sigaction sigact;
159 internal_memset(s: &sigact, c: 0, n: sizeof(sigact));
160 sigact.sa_handler = SIG_DFL;
161 internal_sigaction(SIGABRT, act: &sigact, oldact: nullptr);
162 }
163#endif
164
165 abort();
166}
167
168int Atexit(void (*function)(void)) {
169#if !SANITIZER_GO
170 return atexit(func: function);
171#else
172 return 0;
173#endif
174}
175
176bool CreateDir(const char *pathname) { return mkdir(path: pathname, mode: 0755) == 0; }
177
178bool SupportsColoredOutput(fd_t fd) {
179 return isatty(fd: fd) != 0;
180}
181
182#if !SANITIZER_GO
183// TODO(glider): different tools may require different altstack size.
184static uptr GetAltStackSize() {
185 // Note: since GLIBC_2.31, SIGSTKSZ may be a function call, so this may be
186 // more costly that you think. However GetAltStackSize is only call 2-3 times
187 // per thread so don't cache the evaluation.
188 return SIGSTKSZ * 4;
189}
190
191void* SetAlternateSignalStack() {
192 stack_t altstack, oldstack;
193 CHECK_EQ(0, sigaltstack(nullptr, &oldstack));
194 // If the alternate stack is already in place, do nothing.
195 // Android always sets an alternate stack, but it's too small for us.
196 if (!SANITIZER_ANDROID && !(oldstack.ss_flags & SS_DISABLE))
197 return nullptr;
198 // TODO(glider): the mapped stack should have the MAP_STACK flag in the
199 // future. It is not required by man 2 sigaltstack now (they're using
200 // malloc()).
201 altstack.ss_size = GetAltStackSize();
202 altstack.ss_sp = (char *)MmapOrDie(size: altstack.ss_size, mem_type: __func__);
203 altstack.ss_flags = 0;
204 CHECK_EQ(0, sigaltstack(&altstack, nullptr));
205 return altstack.ss_sp;
206}
207
208void UnsetAlternateSignalStack(void* altstack_base) {
209 stack_t altstack, oldstack;
210 altstack.ss_sp = nullptr;
211 altstack.ss_flags = SS_DISABLE;
212 altstack.ss_size = GetAltStackSize(); // Some sane value required on Darwin.
213 CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
214 if (altstack_base && altstack_base == oldstack.ss_sp) {
215 UnmapOrDie(addr: oldstack.ss_sp, size: oldstack.ss_size);
216 }
217}
218
219bool IsSignalHandlerFromSanitizer(int signum) {
220 return atomic_load(a: &signal_handler_is_from_sanitizer[signum],
221 mo: memory_order_relaxed);
222}
223
224bool SetSignalHandlerFromSanitizer(int signum, bool new_state) {
225 if (signum < 0 || static_cast<unsigned>(signum) >=
226 ARRAY_SIZE(signal_handler_is_from_sanitizer))
227 return false;
228
229 return atomic_exchange(a: &signal_handler_is_from_sanitizer[signum], v: new_state,
230 mo: memory_order_relaxed);
231}
232
233static void MaybeInstallSigaction(int signum,
234 SignalHandlerType handler) {
235 if (GetHandleSignalMode(signum) == kHandleSignalNo) return;
236
237 struct sigaction sigact;
238 internal_memset(s: &sigact, c: 0, n: sizeof(sigact));
239 sigact.sa_sigaction = (sa_sigaction_t)handler;
240 // Do not block the signal from being received in that signal's handler.
241 // Clients are responsible for handling this correctly.
242 sigact.sa_flags = SA_SIGINFO | SA_NODEFER;
243 if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
244 CHECK_EQ(0, internal_sigaction(signum, &sigact, nullptr));
245 VReport(1, "Installed the sigaction for signal %d\n", signum);
246
247 if (common_flags()->cloak_sanitizer_signal_handlers)
248 SetSignalHandlerFromSanitizer(signum, new_state: true);
249}
250
251void InstallDeadlySignalHandlers(SignalHandlerType handler) {
252 // Set the alternate signal stack for the main thread.
253 // This will cause SetAlternateSignalStack to be called twice, but the stack
254 // will be actually set only once.
255 if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
256 MaybeInstallSigaction(SIGSEGV, handler);
257 MaybeInstallSigaction(SIGBUS, handler);
258 MaybeInstallSigaction(SIGABRT, handler);
259 MaybeInstallSigaction(SIGFPE, handler);
260 MaybeInstallSigaction(SIGILL, handler);
261 MaybeInstallSigaction(SIGTRAP, handler);
262}
263
264bool SignalContext::IsStackOverflow() const {
265 // Access at a reasonable offset above SP, or slightly below it (to account
266 // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
267 // probably a stack overflow.
268#ifdef __s390__
269 // On s390, the fault address in siginfo points to start of the page, not
270 // to the precise word that was accessed. Mask off the low bits of sp to
271 // take it into account.
272 bool IsStackAccess = addr >= (sp & ~0xFFF) && addr < sp + 0xFFFF;
273# elif SANITIZER_HPPA
274 // The stack grows up and a frame is written after sp is bumped, so an
275 // overflow faults below sp, by up to the frame size.
276 bool IsStackAccess = addr + 0xFFFF > sp && addr < sp + GetPageSizeCached();
277# else
278 // Let's accept up to a page size away from top of stack. Things like stack
279 // probing can trigger accesses with such large offsets.
280 bool IsStackAccess = addr + GetPageSizeCached() > sp && addr < sp + 0xFFFF;
281# endif
282
283# if __powerpc__
284 // Large stack frames can be allocated with e.g.
285 // lis r0,-10000
286 // stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
287 // If the store faults then sp will not have been updated, so test above
288 // will not work, because the fault address will be more than just "slightly"
289 // below sp.
290 if (!IsStackAccess && IsAccessibleMemoryRange(pc, 4)) {
291 u32 inst = *(unsigned *)pc;
292 u32 ra = (inst >> 16) & 0x1F;
293 u32 opcd = inst >> 26;
294 u32 xo = (inst >> 1) & 0x3FF;
295 // Check for store-with-update to sp. The instructions we accept are:
296 // stbu rs,d(ra) stbux rs,ra,rb
297 // sthu rs,d(ra) sthux rs,ra,rb
298 // stwu rs,d(ra) stwux rs,ra,rb
299 // stdu rs,ds(ra) stdux rs,ra,rb
300 // where ra is r1 (the stack pointer).
301 if (ra == 1 &&
302 (opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 ||
303 (opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181))))
304 IsStackAccess = true;
305 }
306#endif // __powerpc__
307
308 // We also check si_code to filter out SEGV caused by something else other
309 // then hitting the guard page or unmapped memory, like, for example,
310 // unaligned memory access.
311 auto si = static_cast<const siginfo_t *>(siginfo);
312 return IsStackAccess &&
313 (si->si_code == si_SEGV_MAPERR || si->si_code == si_SEGV_ACCERR);
314}
315
316#endif // SANITIZER_GO
317
318static void SetNonBlock(int fd) {
319 int res = fcntl(fd: fd, F_GETFL, 0);
320 CHECK(!internal_iserror(res, nullptr));
321
322 res |= O_NONBLOCK;
323 res = fcntl(fd: fd, F_SETFL, res);
324 CHECK(!internal_iserror(res, nullptr));
325}
326
327bool IsAccessibleMemoryRange(uptr beg, uptr size) {
328 while (size) {
329 // `read` from `fds[0]` into a dummy buffer to free up the pipe buffer for
330 // more `write` is slower than just recreating a pipe.
331 int fds[2];
332 CHECK_EQ(0, pipe(fds));
333
334 auto cleanup = at_scope_exit(fn: [&]() {
335 internal_close(fd: fds[0]);
336 internal_close(fd: fds[1]);
337 });
338
339 SetNonBlock(fds[1]);
340
341 int write_errno;
342 uptr w = internal_write(fd: fds[1], buf: reinterpret_cast<char *>(beg), count: size);
343 if (internal_iserror(retval: w, rverrno: &write_errno)) {
344 if (write_errno == EINTR)
345 continue;
346 CHECK_EQ(EFAULT, write_errno);
347 return false;
348 }
349 size -= w;
350 beg += w;
351 }
352
353 return true;
354}
355
356bool TryMemCpy(void *dest, const void *src, uptr n) {
357 if (!n)
358 return true;
359 int fds[2];
360 CHECK_EQ(0, pipe(fds));
361
362 auto cleanup = at_scope_exit(fn: [&]() {
363 internal_close(fd: fds[0]);
364 internal_close(fd: fds[1]);
365 });
366
367 SetNonBlock(fds[0]);
368 SetNonBlock(fds[1]);
369
370 char *d = static_cast<char *>(dest);
371 const char *s = static_cast<const char *>(src);
372
373 while (n) {
374 int e;
375 uptr w = internal_write(fd: fds[1], buf: s, count: n);
376 if (internal_iserror(retval: w, rverrno: &e)) {
377 if (e == EINTR)
378 continue;
379 CHECK_EQ(EFAULT, e);
380 return false;
381 }
382 s += w;
383 n -= w;
384
385 while (w) {
386 uptr r = internal_read(fd: fds[0], buf: d, count: w);
387 if (internal_iserror(retval: r, rverrno: &e)) {
388 CHECK_EQ(EINTR, e);
389 continue;
390 }
391
392 d += r;
393 w -= r;
394 }
395 }
396
397 return true;
398}
399
400void PlatformPrepareForSandboxing(void *args) {
401 // Some kinds of sandboxes may forbid filesystem access, so we won't be able
402 // to read the file mappings from /proc/self/maps. Luckily, neither the
403 // process will be able to load additional libraries, so it's fine to use the
404 // cached mappings.
405 MemoryMappingLayout::CacheMemoryMappings();
406}
407
408static bool MmapFixed(uptr fixed_addr, uptr size, int additional_flags,
409 const char *name) {
410 size = RoundUpTo(size, boundary: GetPageSizeCached());
411 fixed_addr = RoundDownTo(x: fixed_addr, boundary: GetPageSizeCached());
412 uptr p =
413 MmapNamed(addr: (void *)fixed_addr, length: size, PROT_READ | PROT_WRITE,
414 MAP_PRIVATE | MAP_FIXED | additional_flags | MAP_ANON, name);
415 int reserrno;
416 if (internal_iserror(retval: p, rverrno: &reserrno)) {
417 Report(
418 format: "ERROR: %s failed to "
419 "allocate 0x%zx (%zd) bytes at address %p (errno: %d)\n",
420 SanitizerToolName, size, size, (void *)fixed_addr, reserrno);
421 return false;
422 }
423 IncreaseTotalMmap(size);
424 return true;
425}
426
427bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) {
428 return MmapFixed(fixed_addr, size, MAP_NORESERVE, name);
429}
430
431bool MmapFixedSuperNoReserve(uptr fixed_addr, uptr size, const char *name) {
432#if SANITIZER_FREEBSD
433 if (common_flags()->no_huge_pages_for_shadow)
434 return MmapFixedNoReserve(fixed_addr, size, name);
435 // MAP_NORESERVE is implicit with FreeBSD
436 return MmapFixed(fixed_addr, size, MAP_ALIGNED_SUPER, name);
437#else
438 bool r = MmapFixedNoReserve(fixed_addr, size, name);
439 if (r)
440 SetShadowRegionHugePageMode(addr: fixed_addr, size);
441 return r;
442#endif
443}
444
445uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) {
446 base_ = fixed_addr ? MmapFixedNoAccess(fixed_addr, size, name)
447 : MmapNoAccess(size);
448 size_ = size;
449 name_ = name;
450 (void)os_handle_; // unsupported
451 return reinterpret_cast<uptr>(base_);
452}
453
454// Uses fixed_addr for now.
455// Will use offset instead once we've implemented this function for real.
456uptr ReservedAddressRange::Map(uptr fixed_addr, uptr size, const char *name) {
457 return reinterpret_cast<uptr>(
458 MmapFixedOrDieOnFatalError(fixed_addr, size, name));
459}
460
461uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr size,
462 const char *name) {
463 return reinterpret_cast<uptr>(MmapFixedOrDie(fixed_addr, size, name));
464}
465
466void ReservedAddressRange::Unmap(uptr addr, uptr size) {
467 CHECK_LE(size, size_);
468 if (addr == reinterpret_cast<uptr>(base_))
469 // If we unmap the whole range, just null out the base.
470 base_ = (size == size_) ? nullptr : reinterpret_cast<void*>(addr + size);
471 else
472 CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_);
473 size_ -= size;
474 UnmapOrDie(addr: reinterpret_cast<void*>(addr), size);
475}
476
477void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
478 return (void *)MmapNamed(addr: (void *)fixed_addr, length: size, PROT_NONE,
479 MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE | MAP_ANON,
480 name);
481}
482
483void *MmapNoAccess(uptr size) {
484 unsigned flags = MAP_PRIVATE | MAP_ANON | MAP_NORESERVE;
485 return (void *)internal_mmap(addr: nullptr, length: size, PROT_NONE, flags, fd: -1, offset: 0);
486}
487
488// This function is defined elsewhere if we intercepted pthread_attr_getstack.
489extern "C" {
490SANITIZER_WEAK_ATTRIBUTE int
491real_pthread_attr_getstack(void *attr, void **addr, size_t *size);
492} // extern "C"
493
494int internal_pthread_attr_getstack(void *attr, void **addr, uptr *size) {
495#if !SANITIZER_GO && !SANITIZER_APPLE
496 if (&real_pthread_attr_getstack)
497 return real_pthread_attr_getstack(attr: (pthread_attr_t *)attr, addr,
498 size: (size_t *)size);
499#endif
500 return pthread_attr_getstack(attr: (pthread_attr_t *)attr, stackaddr: addr, stacksize: (size_t *)size);
501}
502
503#if !SANITIZER_GO
504void AdjustStackSize(void *attr_) {
505 pthread_attr_t *attr = (pthread_attr_t *)attr_;
506 uptr stackaddr = 0;
507 uptr stacksize = 0;
508 internal_pthread_attr_getstack(attr, addr: (void **)&stackaddr, size: &stacksize);
509 // GLibC will return (0 - stacksize) as the stack address in the case when
510 // stacksize is set, but stackaddr is not.
511 bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0);
512 // We place a lot of tool data into TLS, account for that.
513 const uptr minstacksize = GetTlsSize() + 128*1024;
514 if (stacksize < minstacksize) {
515 if (!stack_set) {
516 if (stacksize != 0) {
517 VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize,
518 minstacksize);
519 pthread_attr_setstacksize(attr: attr, stacksize: minstacksize);
520 }
521 } else {
522 Printf(format: "Sanitizer: pre-allocated stack size is insufficient: "
523 "%zu < %zu\n", stacksize, minstacksize);
524 Printf(format: "Sanitizer: pthread_create is likely to fail.\n");
525 }
526 }
527}
528#endif // !SANITIZER_GO
529
530pid_t StartSubprocess(const char *program, const char *const argv[],
531 const char *const envp[], fd_t stdin_fd, fd_t stdout_fd,
532 fd_t stderr_fd) {
533 auto file_closer = at_scope_exit(fn: [&] {
534 if (stdin_fd != kInvalidFd) {
535 internal_close(fd: stdin_fd);
536 }
537 if (stdout_fd != kInvalidFd) {
538 internal_close(fd: stdout_fd);
539 }
540 if (stderr_fd != kInvalidFd) {
541 internal_close(fd: stderr_fd);
542 }
543 });
544
545 int pid = internal_fork();
546
547 if (pid < 0) {
548 int rverrno;
549 if (internal_iserror(retval: pid, rverrno: &rverrno)) {
550 Report(format: "WARNING: failed to fork (errno %d)\n", rverrno);
551 }
552 return pid;
553 }
554
555 if (pid == 0) {
556 // Child subprocess
557 if (stdin_fd != kInvalidFd) {
558 internal_close(STDIN_FILENO);
559 internal_dup2(oldfd: stdin_fd, STDIN_FILENO);
560 internal_close(fd: stdin_fd);
561 }
562 if (stdout_fd != kInvalidFd) {
563 internal_close(STDOUT_FILENO);
564 internal_dup2(oldfd: stdout_fd, STDOUT_FILENO);
565 internal_close(fd: stdout_fd);
566 }
567 if (stderr_fd != kInvalidFd) {
568 internal_close(STDERR_FILENO);
569 internal_dup2(oldfd: stderr_fd, STDERR_FILENO);
570 internal_close(fd: stderr_fd);
571 }
572
573 // Close all fds except stdin/stdout/stderr before exec.
574 // Fallback to the loop if close_range is not supported.
575 if (internal_close_range(lowfd: 3, highfd: ~static_cast<fd_t>(0), flags: 0) != 0)
576 for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd);
577
578 internal_execve(filename: program, argv: const_cast<char **>(&argv[0]),
579 envp: const_cast<char *const *>(envp));
580 internal__exit(exitcode: 1);
581 }
582
583 return pid;
584}
585
586bool IsProcessRunning(pid_t pid) {
587 int process_status;
588 uptr waitpid_status = internal_waitpid(pid, status: &process_status, WNOHANG);
589 int local_errno;
590 if (internal_iserror(retval: waitpid_status, rverrno: &local_errno)) {
591 VReport(1, "Waiting on the process failed (errno %d).\n", local_errno);
592 return false;
593 }
594 return waitpid_status == 0;
595}
596
597int WaitForProcess(pid_t pid) {
598 int process_status;
599 uptr waitpid_status = internal_waitpid(pid, status: &process_status, options: 0);
600 int local_errno;
601 if (internal_iserror(retval: waitpid_status, rverrno: &local_errno)) {
602 VReport(1, "Waiting on the process failed (errno %d).\n", local_errno);
603 return -1;
604 }
605 return process_status;
606}
607
608bool IsStateDetached(int state) {
609 return state == PTHREAD_CREATE_DETACHED;
610}
611
612} // namespace __sanitizer
613
614#endif // SANITIZER_POSIX
615