libsanitizer merge from upstream r175733
[official-gcc.git] / libsanitizer / tsan / tsan_platform_linux.cc
blobf7b05f2bf8ffb88e9b369281aff0f7eeef812b65
1 //===-- tsan_platform_linux.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 a part of ThreadSanitizer (TSan), a race detector.
9 //
10 // Linux-specific code.
11 //===----------------------------------------------------------------------===//
13 #ifdef __linux__
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "sanitizer_common/sanitizer_libc.h"
17 #include "sanitizer_common/sanitizer_procmaps.h"
18 #include "tsan_platform.h"
19 #include "tsan_rtl.h"
20 #include "tsan_flags.h"
22 #include <asm/prctl.h>
23 #include <fcntl.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <sys/mman.h>
31 #include <sys/prctl.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 #include <dlfcn.h>
41 #define __need_res_state
42 #include <resolv.h>
44 extern "C" int arch_prctl(int code, __sanitizer::uptr *addr);
46 namespace __tsan {
48 #ifndef TSAN_GO
49 ScopedInRtl::ScopedInRtl()
50 : thr_(cur_thread()) {
51 in_rtl_ = thr_->in_rtl;
52 thr_->in_rtl++;
53 errno_ = errno;
56 ScopedInRtl::~ScopedInRtl() {
57 thr_->in_rtl--;
58 errno = errno_;
59 CHECK_EQ(in_rtl_, thr_->in_rtl);
61 #else
62 ScopedInRtl::ScopedInRtl() {
65 ScopedInRtl::~ScopedInRtl() {
67 #endif
69 uptr GetShadowMemoryConsumption() {
70 return 0;
73 void FlushShadowMemory() {
74 FlushUnneededShadowMemory(kLinuxShadowBeg, kLinuxShadowEnd - kLinuxShadowBeg);
77 #ifndef TSAN_GO
78 static void ProtectRange(uptr beg, uptr end) {
79 ScopedInRtl in_rtl;
80 CHECK_LE(beg, end);
81 if (beg == end)
82 return;
83 if (beg != (uptr)Mprotect(beg, end - beg)) {
84 Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
85 Printf("FATAL: Make sure you are not using unlimited stack\n");
86 Die();
89 #endif
91 #ifndef TSAN_GO
92 void InitializeShadowMemory() {
93 uptr shadow = (uptr)MmapFixedNoReserve(kLinuxShadowBeg,
94 kLinuxShadowEnd - kLinuxShadowBeg);
95 if (shadow != kLinuxShadowBeg) {
96 Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
97 Printf("FATAL: Make sure to compile with -fPIE and "
98 "to link with -pie (%p, %p).\n", shadow, kLinuxShadowBeg);
99 Die();
101 const uptr kClosedLowBeg = 0x200000;
102 const uptr kClosedLowEnd = kLinuxShadowBeg - 1;
103 const uptr kClosedMidBeg = kLinuxShadowEnd + 1;
104 const uptr kClosedMidEnd = min(kLinuxAppMemBeg, kTraceMemBegin);
105 ProtectRange(kClosedLowBeg, kClosedLowEnd);
106 ProtectRange(kClosedMidBeg, kClosedMidEnd);
107 DPrintf("kClosedLow %zx-%zx (%zuGB)\n",
108 kClosedLowBeg, kClosedLowEnd, (kClosedLowEnd - kClosedLowBeg) >> 30);
109 DPrintf("kLinuxShadow %zx-%zx (%zuGB)\n",
110 kLinuxShadowBeg, kLinuxShadowEnd,
111 (kLinuxShadowEnd - kLinuxShadowBeg) >> 30);
112 DPrintf("kClosedMid %zx-%zx (%zuGB)\n",
113 kClosedMidBeg, kClosedMidEnd, (kClosedMidEnd - kClosedMidBeg) >> 30);
114 DPrintf("kLinuxAppMem %zx-%zx (%zuGB)\n",
115 kLinuxAppMemBeg, kLinuxAppMemEnd,
116 (kLinuxAppMemEnd - kLinuxAppMemBeg) >> 30);
117 DPrintf("stack %zx\n", (uptr)&shadow);
119 #endif
121 static uptr g_data_start;
122 static uptr g_data_end;
124 #ifndef TSAN_GO
125 static void CheckPIE() {
126 // Ensure that the binary is indeed compiled with -pie.
127 MemoryMappingLayout proc_maps;
128 uptr start, end;
129 if (proc_maps.Next(&start, &end,
130 /*offset*/0, /*filename*/0, /*filename_size*/0)) {
131 if ((u64)start < kLinuxAppMemBeg) {
132 Printf("FATAL: ThreadSanitizer can not mmap the shadow memory ("
133 "something is mapped at 0x%zx < 0x%zx)\n",
134 start, kLinuxAppMemBeg);
135 Printf("FATAL: Make sure to compile with -fPIE"
136 " and to link with -pie.\n");
137 Die();
142 static void InitDataSeg() {
143 MemoryMappingLayout proc_maps;
144 uptr start, end, offset;
145 char name[128];
146 bool prev_is_data = false;
147 while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name))) {
148 DPrintf("%p-%p %p %s\n", start, end, offset, name);
149 bool is_data = offset != 0 && name[0] != 0;
150 // BSS may get merged with [heap] in /proc/self/maps. This is not very
151 // reliable.
152 bool is_bss = offset == 0 &&
153 (name[0] == 0 || internal_strcmp(name, "[heap]") == 0) && prev_is_data;
154 if (g_data_start == 0 && is_data)
155 g_data_start = start;
156 if (is_bss)
157 g_data_end = end;
158 prev_is_data = is_data;
160 DPrintf("guessed data_start=%p data_end=%p\n", g_data_start, g_data_end);
161 CHECK_LT(g_data_start, g_data_end);
162 CHECK_GE((uptr)&g_data_start, g_data_start);
163 CHECK_LT((uptr)&g_data_start, g_data_end);
166 static uptr g_tls_size;
168 #ifdef __i386__
169 # define INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
170 #else
171 # define INTERNAL_FUNCTION
172 #endif
174 static int InitTlsSize() {
175 typedef void (*get_tls_func)(size_t*, size_t*) INTERNAL_FUNCTION;
176 get_tls_func get_tls;
177 void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info");
178 CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr));
179 internal_memcpy(&get_tls, &get_tls_static_info_ptr,
180 sizeof(get_tls_static_info_ptr));
181 CHECK_NE(get_tls, 0);
182 size_t tls_size = 0;
183 size_t tls_align = 0;
184 get_tls(&tls_size, &tls_align);
185 return tls_size;
187 #endif // #ifndef TSAN_GO
189 static rlim_t getlim(int res) {
190 rlimit rlim;
191 CHECK_EQ(0, getrlimit(res, &rlim));
192 return rlim.rlim_cur;
195 static void setlim(int res, rlim_t lim) {
196 // The following magic is to prevent clang from replacing it with memset.
197 volatile rlimit rlim;
198 rlim.rlim_cur = lim;
199 rlim.rlim_max = lim;
200 setrlimit(res, (rlimit*)&rlim);
203 const char *InitializePlatform() {
204 void *p = 0;
205 if (sizeof(p) == 8) {
206 // Disable core dumps, dumping of 16TB usually takes a bit long.
207 setlim(RLIMIT_CORE, 0);
210 // Go maps shadow memory lazily and works fine with limited address space.
211 // Unlimited stack is not a problem as well, because the executable
212 // is not compiled with -pie.
213 if (kCppMode) {
214 bool reexec = false;
215 // TSan doesn't play well with unlimited stack size (as stack
216 // overlaps with shadow memory). If we detect unlimited stack size,
217 // we re-exec the program with limited stack size as a best effort.
218 if (getlim(RLIMIT_STACK) == (rlim_t)-1) {
219 const uptr kMaxStackSize = 32 * 1024 * 1024;
220 Report("WARNING: Program is run with unlimited stack size, which "
221 "wouldn't work with ThreadSanitizer.\n");
222 Report("Re-execing with stack size limited to %zd bytes.\n",
223 kMaxStackSize);
224 SetStackSizeLimitInBytes(kMaxStackSize);
225 reexec = true;
228 if (getlim(RLIMIT_AS) != (rlim_t)-1) {
229 Report("WARNING: Program is run with limited virtual address space,"
230 " which wouldn't work with ThreadSanitizer.\n");
231 Report("Re-execing with unlimited virtual address space.\n");
232 setlim(RLIMIT_AS, -1);
233 reexec = true;
235 if (reexec)
236 ReExec();
239 #ifndef TSAN_GO
240 CheckPIE();
241 g_tls_size = (uptr)InitTlsSize();
242 InitDataSeg();
243 #endif
244 return GetEnv(kTsanOptionsEnv);
247 void FinalizePlatform() {
248 fflush(0);
251 uptr GetTlsSize() {
252 #ifndef TSAN_GO
253 return g_tls_size;
254 #else
255 return 0;
256 #endif
259 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
260 uptr *tls_addr, uptr *tls_size) {
261 #ifndef TSAN_GO
262 arch_prctl(ARCH_GET_FS, tls_addr);
263 *tls_addr -= g_tls_size;
264 *tls_size = g_tls_size;
266 uptr stack_top, stack_bottom;
267 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
268 *stk_addr = stack_bottom;
269 *stk_size = stack_top - stack_bottom;
271 if (!main) {
272 // If stack and tls intersect, make them non-intersecting.
273 if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) {
274 CHECK_GT(*tls_addr + *tls_size, *stk_addr);
275 CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size);
276 *stk_size -= *tls_size;
277 *tls_addr = *stk_addr + *stk_size;
280 #else
281 *stk_addr = 0;
282 *stk_size = 0;
283 *tls_addr = 0;
284 *tls_size = 0;
285 #endif
288 bool IsGlobalVar(uptr addr) {
289 return g_data_start && addr >= g_data_start && addr < g_data_end;
292 #ifndef TSAN_GO
293 int ExtractResolvFDs(void *state, int *fds, int nfd) {
294 int cnt = 0;
295 __res_state *statp = (__res_state*)state;
296 for (int i = 0; i < MAXNS && cnt < nfd; i++) {
297 if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1)
298 fds[cnt++] = statp->_u._ext.nssocks[i];
300 return cnt;
302 #endif
305 } // namespace __tsan
307 #endif // #ifdef __linux__