1//===-- tsan_platform_mac.cc ----------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12// Mac-specific code.
13//===----------------------------------------------------------------------===//
14
15#include "sanitizer_common/sanitizer_platform.h"
16#if SANITIZER_MAC
17
18#include "sanitizer_common/sanitizer_common.h"
19#include "sanitizer_common/sanitizer_libc.h"
20#include "sanitizer_common/sanitizer_procmaps.h"
21#include "tsan_platform.h"
22#include "tsan_rtl.h"
23#include "tsan_flags.h"
24
25#include <pthread.h>
26#include <signal.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdarg.h>
31#include <sys/mman.h>
32#include <sys/syscall.h>
33#include <sys/time.h>
34#include <sys/types.h>
35#include <sys/resource.h>
36#include <sys/stat.h>
37#include <unistd.h>
38#include <errno.h>
39#include <sched.h>
40
41namespace __tsan {
42
43uptr GetShadowMemoryConsumption() {
44  return 0;
45}
46
47void FlushShadowMemory() {
48}
49
50void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) {
51}
52
53uptr GetRSS() {
54  return 0;
55}
56
57#ifndef TSAN_GO
58void InitializeShadowMemory() {
59  uptr shadow = (uptr)MmapFixedNoReserve(kLinuxShadowBeg,
60    kLinuxShadowEnd - kLinuxShadowBeg);
61  if (shadow != kLinuxShadowBeg) {
62    Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
63    Printf("FATAL: Make sure to compile with -fPIE and "
64           "to link with -pie.\n");
65    Die();
66  }
67  DPrintf("kLinuxShadow %zx-%zx (%zuGB)\n",
68      kLinuxShadowBeg, kLinuxShadowEnd,
69      (kLinuxShadowEnd - kLinuxShadowBeg) >> 30);
70  DPrintf("kLinuxAppMem %zx-%zx (%zuGB)\n",
71      kLinuxAppMemBeg, kLinuxAppMemEnd,
72      (kLinuxAppMemEnd - kLinuxAppMemBeg) >> 30);
73}
74#endif
75
76const char *InitializePlatform() {
77  void *p = 0;
78  if (sizeof(p) == 8) {
79    // Disable core dumps, dumping of 16TB usually takes a bit long.
80    // The following magic is to prevent clang from replacing it with memset.
81    volatile rlimit lim;
82    lim.rlim_cur = 0;
83    lim.rlim_max = 0;
84    setrlimit(RLIMIT_CORE, (rlimit*)&lim);
85  }
86
87  return GetEnv(kTsanOptionsEnv);
88}
89
90void FinalizePlatform() {
91  fflush(0);
92}
93
94#ifndef TSAN_GO
95int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
96    void *abstime), void *c, void *m, void *abstime,
97    void(*cleanup)(void *arg), void *arg) {
98  // pthread_cleanup_push/pop are hardcore macros mess.
99  // We can't intercept nor call them w/o including pthread.h.
100  int res;
101  pthread_cleanup_push(cleanup, arg);
102  res = fn(c, m, abstime);
103  pthread_cleanup_pop(0);
104  return res;
105}
106#endif
107
108}  // namespace __tsan
109
110#endif  // SANITIZER_MAC
111