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