]>
Commit | Line | Data |
---|---|---|
1e80ce41 | 1 | //===-- sanitizer_posix_libcdep.cc ----------------------------------------===// |
2 | // | |
3 | // This file is distributed under the University of Illinois Open Source | |
4 | // License. See LICENSE.TXT for details. | |
5 | // | |
6 | //===----------------------------------------------------------------------===// | |
7 | // | |
8 | // This file is shared between AddressSanitizer and ThreadSanitizer | |
9 | // run-time libraries and implements libc-dependent POSIX-specific functions | |
10 | // from sanitizer_libc.h. | |
11 | //===----------------------------------------------------------------------===// | |
12 | ||
13 | #include "sanitizer_platform.h" | |
14 | ||
7d752f28 | 15 | #if SANITIZER_POSIX |
5645a48f | 16 | |
1e80ce41 | 17 | #include "sanitizer_common.h" |
7d752f28 | 18 | #include "sanitizer_flags.h" |
36093749 | 19 | #include "sanitizer_platform_limits_netbsd.h" |
d2ef4bee | 20 | #include "sanitizer_platform_limits_openbsd.h" |
7d752f28 | 21 | #include "sanitizer_platform_limits_posix.h" |
d2ef4bee | 22 | #include "sanitizer_platform_limits_solaris.h" |
5645a48f | 23 | #include "sanitizer_posix.h" |
24 | #include "sanitizer_procmaps.h" | |
1e80ce41 | 25 | |
26 | #include <errno.h> | |
5645a48f | 27 | #include <fcntl.h> |
1e80ce41 | 28 | #include <pthread.h> |
7d752f28 | 29 | #include <signal.h> |
1e80ce41 | 30 | #include <stdlib.h> |
31 | #include <sys/mman.h> | |
32 | #include <sys/resource.h> | |
5645a48f | 33 | #include <sys/stat.h> |
1e80ce41 | 34 | #include <sys/time.h> |
35 | #include <sys/types.h> | |
23e39437 | 36 | #include <sys/wait.h> |
1e80ce41 | 37 | #include <unistd.h> |
38 | ||
5645a48f | 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. | |
d2ef4bee | 42 | #undef MAP_NORESERVE |
5645a48f | 43 | #define MAP_NORESERVE 0 |
44 | #endif | |
45 | ||
23e39437 | 46 | typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); |
47 | ||
1e80ce41 | 48 | namespace __sanitizer { |
49 | ||
50 | u32 GetUid() { | |
51 | return getuid(); | |
52 | } | |
53 | ||
54 | uptr GetThreadSelf() { | |
55 | return (uptr)pthread_self(); | |
56 | } | |
57 | ||
36093749 | 58 | void ReleaseMemoryPagesToOS(uptr beg, uptr end) { |
59 | uptr page_size = GetPageSizeCached(); | |
60 | uptr beg_aligned = RoundUpTo(beg, page_size); | |
61 | uptr end_aligned = RoundDownTo(end, page_size); | |
62 | if (beg_aligned < end_aligned) | |
d2ef4bee | 63 | // In the default Solaris compilation environment, madvise() is declared |
64 | // to take a caddr_t arg; casting it to void * results in an invalid | |
65 | // conversion error, so use char * instead. | |
66 | madvise((char *)beg_aligned, end_aligned - beg_aligned, | |
67 | SANITIZER_MADVISE_DONTNEED); | |
1e80ce41 | 68 | } |
69 | ||
d2ef4bee | 70 | bool NoHugePagesInRegion(uptr addr, uptr size) { |
5645a48f | 71 | #ifdef MADV_NOHUGEPAGE // May not be defined on old systems. |
d2ef4bee | 72 | return madvise((void *)addr, size, MADV_NOHUGEPAGE) == 0; |
73 | #else | |
74 | return true; | |
5645a48f | 75 | #endif // MADV_NOHUGEPAGE |
76 | } | |
77 | ||
d2ef4bee | 78 | bool DontDumpShadowMemory(uptr addr, uptr length) { |
79 | #if defined(MADV_DONTDUMP) | |
80 | return madvise((void *)addr, length, MADV_DONTDUMP) == 0; | |
81 | #elif defined(MADV_NOCORE) | |
82 | return madvise((void *)addr, length, MADV_NOCORE) == 0; | |
83 | #else | |
84 | return true; | |
85 | #endif // MADV_DONTDUMP | |
5645a48f | 86 | } |
87 | ||
a9586c9c | 88 | static rlim_t getlim(int res) { |
89 | rlimit rlim; | |
90 | CHECK_EQ(0, getrlimit(res, &rlim)); | |
91 | return rlim.rlim_cur; | |
92 | } | |
93 | ||
94 | static void setlim(int res, rlim_t lim) { | |
95 | // The following magic is to prevent clang from replacing it with memset. | |
96 | volatile struct rlimit rlim; | |
97 | rlim.rlim_cur = lim; | |
98 | rlim.rlim_max = lim; | |
5645a48f | 99 | if (setrlimit(res, const_cast<struct rlimit *>(&rlim))) { |
a9586c9c | 100 | Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno); |
101 | Die(); | |
102 | } | |
103 | } | |
104 | ||
105 | void DisableCoreDumperIfNecessary() { | |
106 | if (common_flags()->disable_coredump) { | |
107 | setlim(RLIMIT_CORE, 0); | |
108 | } | |
1e80ce41 | 109 | } |
110 | ||
111 | bool StackSizeIsUnlimited() { | |
a9586c9c | 112 | rlim_t stack_size = getlim(RLIMIT_STACK); |
113 | return (stack_size == RLIM_INFINITY); | |
1e80ce41 | 114 | } |
115 | ||
23e39437 | 116 | uptr GetStackSizeLimitInBytes() { |
117 | return (uptr)getlim(RLIMIT_STACK); | |
118 | } | |
119 | ||
1e80ce41 | 120 | void SetStackSizeLimitInBytes(uptr limit) { |
a9586c9c | 121 | setlim(RLIMIT_STACK, (rlim_t)limit); |
1e80ce41 | 122 | CHECK(!StackSizeIsUnlimited()); |
123 | } | |
124 | ||
a9586c9c | 125 | bool AddressSpaceIsUnlimited() { |
126 | rlim_t as_size = getlim(RLIMIT_AS); | |
127 | return (as_size == RLIM_INFINITY); | |
128 | } | |
129 | ||
130 | void SetAddressSpaceUnlimited() { | |
131 | setlim(RLIMIT_AS, RLIM_INFINITY); | |
132 | CHECK(AddressSpaceIsUnlimited()); | |
133 | } | |
134 | ||
1e80ce41 | 135 | void SleepForSeconds(int seconds) { |
136 | sleep(seconds); | |
137 | } | |
138 | ||
139 | void SleepForMillis(int millis) { | |
140 | usleep(millis * 1000); | |
141 | } | |
142 | ||
143 | void Abort() { | |
23e39437 | 144 | #if !SANITIZER_GO |
145 | // If we are handling SIGABRT, unhandle it first. | |
36093749 | 146 | // TODO(vitalybuka): Check if handler belongs to sanitizer. |
147 | if (GetHandleSignalMode(SIGABRT) != kHandleSignalNo) { | |
23e39437 | 148 | struct sigaction sigact; |
149 | internal_memset(&sigact, 0, sizeof(sigact)); | |
150 | sigact.sa_sigaction = (sa_sigaction_t)SIG_DFL; | |
151 | internal_sigaction(SIGABRT, &sigact, nullptr); | |
152 | } | |
153 | #endif | |
154 | ||
1e80ce41 | 155 | abort(); |
156 | } | |
157 | ||
158 | int Atexit(void (*function)(void)) { | |
23e39437 | 159 | #if !SANITIZER_GO |
1e80ce41 | 160 | return atexit(function); |
161 | #else | |
162 | return 0; | |
163 | #endif | |
164 | } | |
165 | ||
5645a48f | 166 | bool SupportsColoredOutput(fd_t fd) { |
167 | return isatty(fd) != 0; | |
1e80ce41 | 168 | } |
169 | ||
23e39437 | 170 | #if !SANITIZER_GO |
7d752f28 | 171 | // TODO(glider): different tools may require different altstack size. |
172 | static const uptr kAltStackSize = SIGSTKSZ * 4; // SIGSTKSZ is not enough. | |
173 | ||
174 | void SetAlternateSignalStack() { | |
175 | stack_t altstack, oldstack; | |
5645a48f | 176 | CHECK_EQ(0, sigaltstack(nullptr, &oldstack)); |
7d752f28 | 177 | // If the alternate stack is already in place, do nothing. |
178 | // Android always sets an alternate stack, but it's too small for us. | |
179 | if (!SANITIZER_ANDROID && !(oldstack.ss_flags & SS_DISABLE)) return; | |
180 | // TODO(glider): the mapped stack should have the MAP_STACK flag in the | |
181 | // future. It is not required by man 2 sigaltstack now (they're using | |
182 | // malloc()). | |
183 | void* base = MmapOrDie(kAltStackSize, __func__); | |
184 | altstack.ss_sp = (char*) base; | |
185 | altstack.ss_flags = 0; | |
186 | altstack.ss_size = kAltStackSize; | |
5645a48f | 187 | CHECK_EQ(0, sigaltstack(&altstack, nullptr)); |
7d752f28 | 188 | } |
189 | ||
190 | void UnsetAlternateSignalStack() { | |
191 | stack_t altstack, oldstack; | |
5645a48f | 192 | altstack.ss_sp = nullptr; |
7d752f28 | 193 | altstack.ss_flags = SS_DISABLE; |
194 | altstack.ss_size = kAltStackSize; // Some sane value required on Darwin. | |
195 | CHECK_EQ(0, sigaltstack(&altstack, &oldstack)); | |
196 | UnmapOrDie(oldstack.ss_sp, oldstack.ss_size); | |
197 | } | |
198 | ||
7d752f28 | 199 | static void MaybeInstallSigaction(int signum, |
200 | SignalHandlerType handler) { | |
36093749 | 201 | if (GetHandleSignalMode(signum) == kHandleSignalNo) return; |
202 | ||
7d752f28 | 203 | struct sigaction sigact; |
204 | internal_memset(&sigact, 0, sizeof(sigact)); | |
205 | sigact.sa_sigaction = (sa_sigaction_t)handler; | |
a9586c9c | 206 | // Do not block the signal from being received in that signal's handler. |
207 | // Clients are responsible for handling this correctly. | |
208 | sigact.sa_flags = SA_SIGINFO | SA_NODEFER; | |
7d752f28 | 209 | if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK; |
5645a48f | 210 | CHECK_EQ(0, internal_sigaction(signum, &sigact, nullptr)); |
7d752f28 | 211 | VReport(1, "Installed the sigaction for signal %d\n", signum); |
212 | } | |
213 | ||
214 | void InstallDeadlySignalHandlers(SignalHandlerType handler) { | |
215 | // Set the alternate signal stack for the main thread. | |
216 | // This will cause SetAlternateSignalStack to be called twice, but the stack | |
217 | // will be actually set only once. | |
218 | if (common_flags()->use_sigaltstack) SetAlternateSignalStack(); | |
219 | MaybeInstallSigaction(SIGSEGV, handler); | |
220 | MaybeInstallSigaction(SIGBUS, handler); | |
5645a48f | 221 | MaybeInstallSigaction(SIGABRT, handler); |
222 | MaybeInstallSigaction(SIGFPE, handler); | |
23e39437 | 223 | MaybeInstallSigaction(SIGILL, handler); |
d2ef4bee | 224 | MaybeInstallSigaction(SIGTRAP, handler); |
7d752f28 | 225 | } |
36093749 | 226 | |
227 | bool SignalContext::IsStackOverflow() const { | |
228 | // Access at a reasonable offset above SP, or slightly below it (to account | |
229 | // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is | |
230 | // probably a stack overflow. | |
231 | #ifdef __s390__ | |
232 | // On s390, the fault address in siginfo points to start of the page, not | |
233 | // to the precise word that was accessed. Mask off the low bits of sp to | |
234 | // take it into account. | |
235 | bool IsStackAccess = addr >= (sp & ~0xFFF) && addr < sp + 0xFFFF; | |
236 | #else | |
d2ef4bee | 237 | // Let's accept up to a page size away from top of stack. Things like stack |
238 | // probing can trigger accesses with such large offsets. | |
239 | bool IsStackAccess = addr + GetPageSizeCached() > sp && addr < sp + 0xFFFF; | |
36093749 | 240 | #endif |
241 | ||
242 | #if __powerpc__ | |
243 | // Large stack frames can be allocated with e.g. | |
244 | // lis r0,-10000 | |
245 | // stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000 | |
246 | // If the store faults then sp will not have been updated, so test above | |
247 | // will not work, because the fault address will be more than just "slightly" | |
248 | // below sp. | |
249 | if (!IsStackAccess && IsAccessibleMemoryRange(pc, 4)) { | |
250 | u32 inst = *(unsigned *)pc; | |
251 | u32 ra = (inst >> 16) & 0x1F; | |
252 | u32 opcd = inst >> 26; | |
253 | u32 xo = (inst >> 1) & 0x3FF; | |
254 | // Check for store-with-update to sp. The instructions we accept are: | |
255 | // stbu rs,d(ra) stbux rs,ra,rb | |
256 | // sthu rs,d(ra) sthux rs,ra,rb | |
257 | // stwu rs,d(ra) stwux rs,ra,rb | |
258 | // stdu rs,ds(ra) stdux rs,ra,rb | |
259 | // where ra is r1 (the stack pointer). | |
260 | if (ra == 1 && | |
261 | (opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 || | |
262 | (opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181)))) | |
263 | IsStackAccess = true; | |
264 | } | |
265 | #endif // __powerpc__ | |
266 | ||
267 | // We also check si_code to filter out SEGV caused by something else other | |
268 | // then hitting the guard page or unmapped memory, like, for example, | |
269 | // unaligned memory access. | |
270 | auto si = static_cast<const siginfo_t *>(siginfo); | |
271 | return IsStackAccess && | |
272 | (si->si_code == si_SEGV_MAPERR || si->si_code == si_SEGV_ACCERR); | |
273 | } | |
274 | ||
7d752f28 | 275 | #endif // SANITIZER_GO |
276 | ||
a9586c9c | 277 | bool IsAccessibleMemoryRange(uptr beg, uptr size) { |
278 | uptr page_size = GetPageSizeCached(); | |
279 | // Checking too large memory ranges is slow. | |
280 | CHECK_LT(size, page_size * 10); | |
281 | int sock_pair[2]; | |
282 | if (pipe(sock_pair)) | |
283 | return false; | |
284 | uptr bytes_written = | |
285 | internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size); | |
286 | int write_errno; | |
287 | bool result; | |
288 | if (internal_iserror(bytes_written, &write_errno)) { | |
289 | CHECK_EQ(EFAULT, write_errno); | |
290 | result = false; | |
291 | } else { | |
292 | result = (bytes_written == size); | |
293 | } | |
294 | internal_close(sock_pair[0]); | |
295 | internal_close(sock_pair[1]); | |
296 | return result; | |
297 | } | |
298 | ||
d2ef4bee | 299 | void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args) { |
5645a48f | 300 | // Some kinds of sandboxes may forbid filesystem access, so we won't be able |
301 | // to read the file mappings from /proc/self/maps. Luckily, neither the | |
302 | // process will be able to load additional libraries, so it's fine to use the | |
303 | // cached mappings. | |
304 | MemoryMappingLayout::CacheMemoryMappings(); | |
5645a48f | 305 | } |
306 | ||
61b44d84 | 307 | #if SANITIZER_ANDROID || SANITIZER_GO |
5645a48f | 308 | int GetNamedMappingFd(const char *name, uptr size) { |
309 | return -1; | |
310 | } | |
311 | #else | |
312 | int GetNamedMappingFd(const char *name, uptr size) { | |
313 | if (!common_flags()->decorate_proc_maps) | |
314 | return -1; | |
315 | char shmname[200]; | |
316 | CHECK(internal_strlen(name) < sizeof(shmname) - 10); | |
317 | internal_snprintf(shmname, sizeof(shmname), "%zu [%s]", internal_getpid(), | |
318 | name); | |
319 | int fd = shm_open(shmname, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); | |
320 | CHECK_GE(fd, 0); | |
321 | int res = internal_ftruncate(fd, size); | |
322 | CHECK_EQ(0, res); | |
323 | res = shm_unlink(shmname); | |
324 | CHECK_EQ(0, res); | |
325 | return fd; | |
326 | } | |
327 | #endif | |
328 | ||
d2ef4bee | 329 | bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) { |
5645a48f | 330 | int fd = name ? GetNamedMappingFd(name, size) : -1; |
331 | unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE; | |
332 | if (fd == -1) flags |= MAP_ANON; | |
333 | ||
334 | uptr PageSize = GetPageSizeCached(); | |
335 | uptr p = internal_mmap((void *)(fixed_addr & ~(PageSize - 1)), | |
336 | RoundUpTo(size, PageSize), PROT_READ | PROT_WRITE, | |
337 | flags, fd, 0); | |
338 | int reserrno; | |
d2ef4bee | 339 | if (internal_iserror(p, &reserrno)) { |
5645a48f | 340 | Report("ERROR: %s failed to " |
341 | "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n", | |
342 | SanitizerToolName, size, size, fixed_addr, reserrno); | |
d2ef4bee | 343 | return false; |
344 | } | |
5645a48f | 345 | IncreaseTotalMmap(size); |
d2ef4bee | 346 | return true; |
347 | } | |
348 | ||
349 | uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) { | |
350 | // We don't pass `name` along because, when you enable `decorate_proc_maps` | |
351 | // AND actually use a named mapping AND are using a sanitizer intercepting | |
352 | // `open` (e.g. TSAN, ESAN), then you'll get a failure during initialization. | |
353 | // TODO(flowerhack): Fix the implementation of GetNamedMappingFd to solve | |
354 | // this problem. | |
355 | base_ = fixed_addr ? MmapFixedNoAccess(fixed_addr, size) : MmapNoAccess(size); | |
356 | size_ = size; | |
357 | name_ = name; | |
358 | (void)os_handle_; // unsupported | |
359 | return reinterpret_cast<uptr>(base_); | |
360 | } | |
361 | ||
362 | // Uses fixed_addr for now. | |
363 | // Will use offset instead once we've implemented this function for real. | |
364 | uptr ReservedAddressRange::Map(uptr fixed_addr, uptr size) { | |
365 | return reinterpret_cast<uptr>(MmapFixedOrDieOnFatalError(fixed_addr, size)); | |
366 | } | |
367 | ||
368 | uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr size) { | |
369 | return reinterpret_cast<uptr>(MmapFixedOrDie(fixed_addr, size)); | |
370 | } | |
371 | ||
372 | void ReservedAddressRange::Unmap(uptr addr, uptr size) { | |
373 | CHECK_LE(size, size_); | |
374 | if (addr == reinterpret_cast<uptr>(base_)) | |
375 | // If we unmap the whole range, just null out the base. | |
376 | base_ = (size == size_) ? nullptr : reinterpret_cast<void*>(addr + size); | |
377 | else | |
378 | CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_); | |
379 | size_ -= size; | |
380 | UnmapOrDie(reinterpret_cast<void*>(addr), size); | |
5645a48f | 381 | } |
382 | ||
23e39437 | 383 | void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) { |
5645a48f | 384 | int fd = name ? GetNamedMappingFd(name, size) : -1; |
385 | unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE; | |
386 | if (fd == -1) flags |= MAP_ANON; | |
387 | ||
388 | return (void *)internal_mmap((void *)fixed_addr, size, PROT_NONE, flags, fd, | |
389 | 0); | |
390 | } | |
61b44d84 | 391 | |
23e39437 | 392 | void *MmapNoAccess(uptr size) { |
393 | unsigned flags = MAP_PRIVATE | MAP_ANON | MAP_NORESERVE; | |
394 | return (void *)internal_mmap(nullptr, size, PROT_NONE, flags, -1, 0); | |
395 | } | |
396 | ||
61b44d84 | 397 | // This function is defined elsewhere if we intercepted pthread_attr_getstack. |
398 | extern "C" { | |
399 | SANITIZER_WEAK_ATTRIBUTE int | |
400 | real_pthread_attr_getstack(void *attr, void **addr, size_t *size); | |
401 | } // extern "C" | |
402 | ||
403 | int my_pthread_attr_getstack(void *attr, void **addr, uptr *size) { | |
404 | #if !SANITIZER_GO && !SANITIZER_MAC | |
405 | if (&real_pthread_attr_getstack) | |
406 | return real_pthread_attr_getstack((pthread_attr_t *)attr, addr, | |
407 | (size_t *)size); | |
408 | #endif | |
409 | return pthread_attr_getstack((pthread_attr_t *)attr, addr, (size_t *)size); | |
410 | } | |
411 | ||
412 | #if !SANITIZER_GO | |
413 | void AdjustStackSize(void *attr_) { | |
414 | pthread_attr_t *attr = (pthread_attr_t *)attr_; | |
415 | uptr stackaddr = 0; | |
416 | uptr stacksize = 0; | |
417 | my_pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize); | |
418 | // GLibC will return (0 - stacksize) as the stack address in the case when | |
419 | // stacksize is set, but stackaddr is not. | |
420 | bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0); | |
421 | // We place a lot of tool data into TLS, account for that. | |
422 | const uptr minstacksize = GetTlsSize() + 128*1024; | |
423 | if (stacksize < minstacksize) { | |
424 | if (!stack_set) { | |
425 | if (stacksize != 0) { | |
426 | VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize, | |
427 | minstacksize); | |
428 | pthread_attr_setstacksize(attr, minstacksize); | |
429 | } | |
430 | } else { | |
431 | Printf("Sanitizer: pre-allocated stack size is insufficient: " | |
432 | "%zu < %zu\n", stacksize, minstacksize); | |
433 | Printf("Sanitizer: pthread_create is likely to fail.\n"); | |
434 | } | |
435 | } | |
436 | } | |
437 | #endif // !SANITIZER_GO | |
438 | ||
23e39437 | 439 | pid_t StartSubprocess(const char *program, const char *const argv[], |
440 | fd_t stdin_fd, fd_t stdout_fd, fd_t stderr_fd) { | |
441 | auto file_closer = at_scope_exit([&] { | |
442 | if (stdin_fd != kInvalidFd) { | |
443 | internal_close(stdin_fd); | |
444 | } | |
445 | if (stdout_fd != kInvalidFd) { | |
446 | internal_close(stdout_fd); | |
447 | } | |
448 | if (stderr_fd != kInvalidFd) { | |
449 | internal_close(stderr_fd); | |
450 | } | |
451 | }); | |
452 | ||
453 | int pid = internal_fork(); | |
454 | ||
455 | if (pid < 0) { | |
456 | int rverrno; | |
457 | if (internal_iserror(pid, &rverrno)) { | |
458 | Report("WARNING: failed to fork (errno %d)\n", rverrno); | |
459 | } | |
460 | return pid; | |
461 | } | |
462 | ||
463 | if (pid == 0) { | |
464 | // Child subprocess | |
465 | if (stdin_fd != kInvalidFd) { | |
466 | internal_close(STDIN_FILENO); | |
467 | internal_dup2(stdin_fd, STDIN_FILENO); | |
468 | internal_close(stdin_fd); | |
469 | } | |
470 | if (stdout_fd != kInvalidFd) { | |
471 | internal_close(STDOUT_FILENO); | |
472 | internal_dup2(stdout_fd, STDOUT_FILENO); | |
473 | internal_close(stdout_fd); | |
474 | } | |
475 | if (stderr_fd != kInvalidFd) { | |
476 | internal_close(STDERR_FILENO); | |
477 | internal_dup2(stderr_fd, STDERR_FILENO); | |
478 | internal_close(stderr_fd); | |
479 | } | |
480 | ||
481 | for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd); | |
482 | ||
483 | execv(program, const_cast<char **>(&argv[0])); | |
484 | internal__exit(1); | |
485 | } | |
486 | ||
487 | return pid; | |
488 | } | |
489 | ||
490 | bool IsProcessRunning(pid_t pid) { | |
491 | int process_status; | |
492 | uptr waitpid_status = internal_waitpid(pid, &process_status, WNOHANG); | |
493 | int local_errno; | |
494 | if (internal_iserror(waitpid_status, &local_errno)) { | |
495 | VReport(1, "Waiting on the process failed (errno %d).\n", local_errno); | |
496 | return false; | |
497 | } | |
498 | return waitpid_status == 0; | |
499 | } | |
500 | ||
501 | int WaitForProcess(pid_t pid) { | |
502 | int process_status; | |
503 | uptr waitpid_status = internal_waitpid(pid, &process_status, 0); | |
504 | int local_errno; | |
505 | if (internal_iserror(waitpid_status, &local_errno)) { | |
506 | VReport(1, "Waiting on the process failed (errno %d).\n", local_errno); | |
507 | return -1; | |
508 | } | |
509 | return process_status; | |
510 | } | |
511 | ||
36093749 | 512 | bool IsStateDetached(int state) { |
513 | return state == PTHREAD_CREATE_DETACHED; | |
514 | } | |
515 | ||
5645a48f | 516 | } // namespace __sanitizer |
1e80ce41 | 517 | |
5645a48f | 518 | #endif // SANITIZER_POSIX |