104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne//===-- sanitizer_posix_libcdep.cc ----------------------------------------===//
204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne//
304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne//                     The LLVM Compiler Infrastructure
404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne//
504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne// This file is distributed under the University of Illinois Open Source
604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne// License. See LICENSE.TXT for details.
704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne//
804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne//===----------------------------------------------------------------------===//
904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne//
1004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne// This file is shared between AddressSanitizer and ThreadSanitizer
1104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne// run-time libraries and implements libc-dependent POSIX-specific functions
1204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne// from sanitizer_libc.h.
1304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne//===----------------------------------------------------------------------===//
1404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
1504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include "sanitizer_platform.h"
1604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
1704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#if SANITIZER_LINUX || SANITIZER_MAC
1804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include "sanitizer_common.h"
1904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include "sanitizer_stacktrace.h"
2004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
2104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include <errno.h>
2204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include <pthread.h>
2304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include <stdlib.h>
2404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include <sys/mman.h>
2504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include <sys/resource.h>
2604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include <sys/time.h>
2704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include <sys/types.h>
2804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#include <unistd.h>
2904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
3004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournenamespace __sanitizer {
3104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
3204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourneu32 GetUid() {
3304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  return getuid();
3404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
3504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
3604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourneuptr GetThreadSelf() {
3704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  return (uptr)pthread_self();
3804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
3904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
4004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournevoid FlushUnneededShadowMemory(uptr addr, uptr size) {
4104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  madvise((void*)addr, size, MADV_DONTNEED);
4204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
4304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
4404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournevoid DisableCoreDumper() {
4504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  struct rlimit nocore;
4604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  nocore.rlim_cur = 0;
4704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  nocore.rlim_max = 0;
4804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  setrlimit(RLIMIT_CORE, &nocore);
4904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
5004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
5104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournebool StackSizeIsUnlimited() {
5204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  struct rlimit rlim;
5304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  CHECK_EQ(0, getrlimit(RLIMIT_STACK, &rlim));
5404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  return (rlim.rlim_cur == (uptr)-1);
5504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
5604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
5704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournevoid SetStackSizeLimitInBytes(uptr limit) {
5804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  struct rlimit rlim;
5904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  rlim.rlim_cur = limit;
6004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  rlim.rlim_max = limit;
6104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  if (setrlimit(RLIMIT_STACK, &rlim)) {
6204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne    Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
6304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne    Die();
6404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  }
6504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  CHECK(!StackSizeIsUnlimited());
6604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
6704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
6804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournevoid SleepForSeconds(int seconds) {
6904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  sleep(seconds);
7004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
7104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
7204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournevoid SleepForMillis(int millis) {
7304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  usleep(millis * 1000);
7404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
7504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
7604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournevoid Abort() {
7704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  abort();
7804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
7904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
8004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourneint Atexit(void (*function)(void)) {
8104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#ifndef SANITIZER_GO
8204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  return atexit(function);
8304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#else
8404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  return 0;
8504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#endif
8604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
8704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
8804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourneint internal_isatty(fd_t fd) {
8904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  return isatty(fd);
9004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
9104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
9204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#ifndef SANITIZER_GO
9304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbournevoid GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp,
9404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne                   uptr stack_top, uptr stack_bottom, bool fast) {
9504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#if !SANITIZER_CAN_FAST_UNWIND
9604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  fast = false;
9704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#endif
9804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#if SANITIZER_MAC
9904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  // Always unwind fast on Mac.
10004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  (void)fast;
10104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#else
1020550a3f9982ee7af5e16fa333f988d42b4cbf765Sergey Matveev  if (!fast)
10304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne    return stack->SlowUnwindStack(pc, max_s);
10404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#endif  // SANITIZER_MAC
10504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  stack->size = 0;
10604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  stack->trace[0] = pc;
10704a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  if (max_s > 1) {
10804a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne    stack->max_size = max_s;
10904a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne    stack->FastUnwindStack(pc, bp, stack_top, stack_bottom);
11004a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne  }
11104a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}
11204a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#endif  // SANITIZER_GO
11304a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
11404a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne}  // namespace __sanitizer
11504a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne
11604a2281586c2dafaa523d4e8dc5488b52e44194ePeter Collingbourne#endif
117