blob: fe8e67d331df1f5bcd2ed3a4727d04374521b2d6 [file] [log] [blame]
Peter Collingbourne088ea2b2013-05-20 15:57:44 +00001//===-- sanitizer_linux_libcdep.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 shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries and implements linux-specific functions from
12// sanitizer_libc.h.
13//===----------------------------------------------------------------------===//
14
15#include "sanitizer_platform.h"
16#if SANITIZER_LINUX
17
18#include "sanitizer_common.h"
19#include "sanitizer_procmaps.h"
20#include "sanitizer_stacktrace.h"
21
Peter Collingbourne088ea2b2013-05-20 15:57:44 +000022#include <dlfcn.h>
23#include <pthread.h>
24#include <sys/prctl.h>
25#include <sys/resource.h>
26#include <unwind.h>
27
Peter Collingbourne088ea2b2013-05-20 15:57:44 +000028namespace __sanitizer {
29
30void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
31 uptr *stack_bottom) {
32 static const uptr kMaxThreadStackSize = 256 * (1 << 20); // 256M
33 CHECK(stack_top);
34 CHECK(stack_bottom);
35 if (at_initialization) {
36 // This is the main thread. Libpthread may not be initialized yet.
37 struct rlimit rl;
38 CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
39
40 // Find the mapping that contains a stack variable.
41 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
42 uptr start, end, offset;
43 uptr prev_end = 0;
44 while (proc_maps.Next(&start, &end, &offset, 0, 0, /* protection */0)) {
45 if ((uptr)&rl < end)
46 break;
47 prev_end = end;
48 }
49 CHECK((uptr)&rl >= start && (uptr)&rl < end);
50
51 // Get stacksize from rlimit, but clip it so that it does not overlap
52 // with other mappings.
53 uptr stacksize = rl.rlim_cur;
54 if (stacksize > end - prev_end)
55 stacksize = end - prev_end;
56 // When running with unlimited stack size, we still want to set some limit.
57 // The unlimited stack size is caused by 'ulimit -s unlimited'.
58 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
59 if (stacksize > kMaxThreadStackSize)
60 stacksize = kMaxThreadStackSize;
61 *stack_top = end;
62 *stack_bottom = end - stacksize;
63 return;
64 }
65 pthread_attr_t attr;
66 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
67 uptr stacksize = 0;
68 void *stackaddr = 0;
69 pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize);
70 pthread_attr_destroy(&attr);
71
72 *stack_top = (uptr)stackaddr + stacksize;
73 *stack_bottom = (uptr)stackaddr;
74 CHECK(stacksize < kMaxThreadStackSize); // Sanity check.
75}
76
77// Does not compile for Go because dlsym() requires -ldl
78#ifndef SANITIZER_GO
79bool SetEnv(const char *name, const char *value) {
80 void *f = dlsym(RTLD_NEXT, "setenv");
81 if (f == 0)
82 return false;
83 typedef int(*setenv_ft)(const char *name, const char *value, int overwrite);
84 setenv_ft setenv_f;
85 CHECK_EQ(sizeof(setenv_f), sizeof(f));
86 internal_memcpy(&setenv_f, &f, sizeof(f));
87 return setenv_f(name, value, 1) == 0;
88}
89#endif
90
91bool SanitizerSetThreadName(const char *name) {
92#ifdef PR_SET_NAME
93 return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); // NOLINT
94#else
95 return false;
96#endif
97}
98
99bool SanitizerGetThreadName(char *name, int max_len) {
100#ifdef PR_GET_NAME
101 char buff[17];
102 if (prctl(PR_GET_NAME, (unsigned long)buff, 0, 0, 0)) // NOLINT
103 return false;
104 internal_strncpy(name, buff, max_len);
105 name[max_len] = 0;
106 return true;
107#else
108 return false;
109#endif
110}
111
112#ifndef SANITIZER_GO
113//------------------------- SlowUnwindStack -----------------------------------
114#ifdef __arm__
115#define UNWIND_STOP _URC_END_OF_STACK
116#define UNWIND_CONTINUE _URC_NO_REASON
117#else
118#define UNWIND_STOP _URC_NORMAL_STOP
119#define UNWIND_CONTINUE _URC_NO_REASON
120#endif
121
122uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
123#ifdef __arm__
124 uptr val;
125 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
126 15 /* r15 = PC */, _UVRSD_UINT32, &val);
127 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
128 // Clear the Thumb bit.
129 return val & ~(uptr)1;
130#else
131 return _Unwind_GetIP(ctx);
132#endif
133}
134
135_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
136 StackTrace *b = (StackTrace*)param;
137 CHECK(b->size < b->max_size);
138 uptr pc = Unwind_GetIP(ctx);
139 b->trace[b->size++] = pc;
140 if (b->size == b->max_size) return UNWIND_STOP;
141 return UNWIND_CONTINUE;
142}
143
144static bool MatchPc(uptr cur_pc, uptr trace_pc) {
145 return cur_pc - trace_pc <= 64 || trace_pc - cur_pc <= 64;
146}
147
148void StackTrace::SlowUnwindStack(uptr pc, uptr max_depth) {
149 this->size = 0;
150 this->max_size = max_depth;
151 if (max_depth > 1) {
152 _Unwind_Backtrace(Unwind_Trace, this);
153 // We need to pop a few frames so that pc is on top.
154 // trace[0] belongs to the current function so we always pop it.
155 int to_pop = 1;
156 /**/ if (size > 1 && MatchPc(pc, trace[1])) to_pop = 1;
157 else if (size > 2 && MatchPc(pc, trace[2])) to_pop = 2;
158 else if (size > 3 && MatchPc(pc, trace[3])) to_pop = 3;
159 else if (size > 4 && MatchPc(pc, trace[4])) to_pop = 4;
160 else if (size > 5 && MatchPc(pc, trace[5])) to_pop = 5;
161 this->PopStackFrames(to_pop);
162 }
163 this->trace[0] = pc;
164}
165
166#endif // !SANITIZER_GO
167
168static uptr g_tls_size;
169
170#ifdef __i386__
171# define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
172#else
173# define DL_INTERNAL_FUNCTION
174#endif
175
176void InitTlsSize() {
177#if !defined(SANITIZER_GO) && !SANITIZER_ANDROID
178 typedef void (*get_tls_func)(size_t*, size_t*) DL_INTERNAL_FUNCTION;
179 get_tls_func get_tls;
180 void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info");
181 CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr));
182 internal_memcpy(&get_tls, &get_tls_static_info_ptr,
183 sizeof(get_tls_static_info_ptr));
184 CHECK_NE(get_tls, 0);
185 size_t tls_size = 0;
186 size_t tls_align = 0;
187 get_tls(&tls_size, &tls_align);
188 g_tls_size = tls_size;
189#endif
190}
191
192uptr GetTlsSize() {
193 return g_tls_size;
194}
195
Alexey Samsonov816bdb72013-05-30 09:16:04 +0000196#if defined(__x86_64__) || defined(__i386__)
Peter Collingbourne088ea2b2013-05-20 15:57:44 +0000197// sizeof(struct thread) from glibc.
Sergey Matveevd4a04152013-06-03 10:20:23 +0000198// There has been a report of this being different on glibc 2.11. We don't know
199// when this change happened, so 2.12 is a conservative estimate.
200#if __GNUC_PREREQ(2, 12)
Sergey Matveev4c086442013-05-29 13:07:42 +0000201const uptr kThreadDescriptorSize = FIRST_32_SECOND_64(1216, 2304);
Sergey Matveevd4a04152013-06-03 10:20:23 +0000202#else
203const uptr kThreadDescriptorSize = FIRST_32_SECOND_64(1168, 2304);
204#endif
Peter Collingbourne088ea2b2013-05-20 15:57:44 +0000205
206uptr ThreadDescriptorSize() {
207 return kThreadDescriptorSize;
208}
Sergey Matveev4c086442013-05-29 13:07:42 +0000209
210// The offset at which pointer to self is located in the thread descriptor.
211const uptr kThreadSelfOffset = FIRST_32_SECOND_64(8, 16);
212
213uptr ThreadSelfOffset() {
214 return kThreadSelfOffset;
215}
216
217uptr ThreadSelf() {
218 uptr descr_addr;
219#ifdef __i386__
220 asm("mov %%gs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
221#else
222 asm("mov %%fs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
Peter Collingbourne088ea2b2013-05-20 15:57:44 +0000223#endif
Sergey Matveev4c086442013-05-29 13:07:42 +0000224 return descr_addr;
225}
Alexey Samsonov816bdb72013-05-30 09:16:04 +0000226#endif // defined(__x86_64__) || defined(__i386__)
Peter Collingbourne088ea2b2013-05-20 15:57:44 +0000227
228void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
229 uptr *tls_addr, uptr *tls_size) {
230#ifndef SANITIZER_GO
Alexey Samsonov816bdb72013-05-30 09:16:04 +0000231#if defined(__x86_64__) || defined(__i386__)
Sergey Matveev4c086442013-05-29 13:07:42 +0000232 *tls_addr = ThreadSelf();
Peter Collingbourne088ea2b2013-05-20 15:57:44 +0000233 *tls_size = GetTlsSize();
234 *tls_addr -= *tls_size;
235 *tls_addr += kThreadDescriptorSize;
236#else
237 *tls_addr = 0;
238 *tls_size = 0;
239#endif
240
241 uptr stack_top, stack_bottom;
242 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
243 *stk_addr = stack_bottom;
244 *stk_size = stack_top - stack_bottom;
245
246 if (!main) {
247 // If stack and tls intersect, make them non-intersecting.
248 if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) {
249 CHECK_GT(*tls_addr + *tls_size, *stk_addr);
250 CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size);
251 *stk_size -= *tls_size;
252 *tls_addr = *stk_addr + *stk_size;
253 }
254 }
255#else // SANITIZER_GO
256 *stk_addr = 0;
257 *stk_size = 0;
258 *tls_addr = 0;
259 *tls_size = 0;
260#endif // SANITIZER_GO
261}
262
263void AdjustStackSizeLinux(void *attr_, int verbosity) {
264 pthread_attr_t *attr = (pthread_attr_t *)attr_;
265 uptr stackaddr = 0;
266 size_t stacksize = 0;
267 pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize);
268 // GLibC will return (0 - stacksize) as the stack address in the case when
269 // stacksize is set, but stackaddr is not.
270 bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0);
271 // We place a lot of tool data into TLS, account for that.
272 const uptr minstacksize = GetTlsSize() + 128*1024;
273 if (stacksize < minstacksize) {
274 if (!stack_set) {
275 if (verbosity && stacksize != 0)
276 Printf("Sanitizer: increasing stacksize %zu->%zu\n", stacksize,
277 minstacksize);
278 pthread_attr_setstacksize(attr, minstacksize);
279 } else {
280 Printf("Sanitizer: pre-allocated stack size is insufficient: "
281 "%zu < %zu\n", stacksize, minstacksize);
282 Printf("Sanitizer: pthread_create is likely to fail.\n");
283 }
284 }
285}
286
287} // namespace __sanitizer
288
289#endif // SANITIZER_LINUX