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

source code of compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp