Reverting merge from trunk
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_linux_libcdep.cc
blob7d6c63918de4c5d80e1989744a01dba2933d9f1f
1 //===-- sanitizer_linux_libcdep.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 shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements linux-specific functions from
10 // sanitizer_libc.h.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
14 #if SANITIZER_LINUX
16 #include "sanitizer_common.h"
17 #include "sanitizer_linux.h"
18 #include "sanitizer_placement_new.h"
19 #include "sanitizer_procmaps.h"
20 #include "sanitizer_stacktrace.h"
22 #include <dlfcn.h>
23 #include <pthread.h>
24 #include <sys/prctl.h>
25 #include <sys/resource.h>
26 #include <unwind.h>
28 #if !SANITIZER_ANDROID
29 #include <elf.h>
30 #include <link.h>
31 #endif
33 namespace __sanitizer {
35 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
36 uptr *stack_bottom) {
37 static const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
38 CHECK(stack_top);
39 CHECK(stack_bottom);
40 if (at_initialization) {
41 // This is the main thread. Libpthread may not be initialized yet.
42 struct rlimit rl;
43 CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
45 // Find the mapping that contains a stack variable.
46 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
47 uptr start, end, offset;
48 uptr prev_end = 0;
49 while (proc_maps.Next(&start, &end, &offset, 0, 0, /* protection */0)) {
50 if ((uptr)&rl < end)
51 break;
52 prev_end = end;
54 CHECK((uptr)&rl >= start && (uptr)&rl < end);
56 // Get stacksize from rlimit, but clip it so that it does not overlap
57 // with other mappings.
58 uptr stacksize = rl.rlim_cur;
59 if (stacksize > end - prev_end)
60 stacksize = end - prev_end;
61 // When running with unlimited stack size, we still want to set some limit.
62 // The unlimited stack size is caused by 'ulimit -s unlimited'.
63 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
64 if (stacksize > kMaxThreadStackSize)
65 stacksize = kMaxThreadStackSize;
66 *stack_top = end;
67 *stack_bottom = end - stacksize;
68 return;
70 pthread_attr_t attr;
71 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
72 uptr stacksize = 0;
73 void *stackaddr = 0;
74 pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize);
75 pthread_attr_destroy(&attr);
77 CHECK_LE(stacksize, kMaxThreadStackSize); // Sanity check.
78 *stack_top = (uptr)stackaddr + stacksize;
79 *stack_bottom = (uptr)stackaddr;
82 // Does not compile for Go because dlsym() requires -ldl
83 #ifndef SANITIZER_GO
84 bool SetEnv(const char *name, const char *value) {
85 void *f = dlsym(RTLD_NEXT, "setenv");
86 if (f == 0)
87 return false;
88 typedef int(*setenv_ft)(const char *name, const char *value, int overwrite);
89 setenv_ft setenv_f;
90 CHECK_EQ(sizeof(setenv_f), sizeof(f));
91 internal_memcpy(&setenv_f, &f, sizeof(f));
92 return setenv_f(name, value, 1) == 0;
94 #endif
96 bool SanitizerSetThreadName(const char *name) {
97 #ifdef PR_SET_NAME
98 return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); // NOLINT
99 #else
100 return false;
101 #endif
104 bool SanitizerGetThreadName(char *name, int max_len) {
105 #ifdef PR_GET_NAME
106 char buff[17];
107 if (prctl(PR_GET_NAME, (unsigned long)buff, 0, 0, 0)) // NOLINT
108 return false;
109 internal_strncpy(name, buff, max_len);
110 name[max_len] = 0;
111 return true;
112 #else
113 return false;
114 #endif
117 #ifndef SANITIZER_GO
118 //------------------------- SlowUnwindStack -----------------------------------
119 #ifdef __arm__
120 #define UNWIND_STOP _URC_END_OF_STACK
121 #define UNWIND_CONTINUE _URC_NO_REASON
122 #else
123 #define UNWIND_STOP _URC_NORMAL_STOP
124 #define UNWIND_CONTINUE _URC_NO_REASON
125 #endif
127 uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
128 #ifdef __arm__
129 uptr val;
130 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
131 15 /* r15 = PC */, _UVRSD_UINT32, &val);
132 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
133 // Clear the Thumb bit.
134 return val & ~(uptr)1;
135 #else
136 return _Unwind_GetIP(ctx);
137 #endif
140 _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
141 StackTrace *b = (StackTrace*)param;
142 CHECK(b->size < b->max_size);
143 uptr pc = Unwind_GetIP(ctx);
144 b->trace[b->size++] = pc;
145 if (b->size == b->max_size) return UNWIND_STOP;
146 return UNWIND_CONTINUE;
149 static bool MatchPc(uptr cur_pc, uptr trace_pc) {
150 return cur_pc - trace_pc <= 64 || trace_pc - cur_pc <= 64;
153 void StackTrace::SlowUnwindStack(uptr pc, uptr max_depth) {
154 this->size = 0;
155 this->max_size = max_depth;
156 if (max_depth > 1) {
157 _Unwind_Backtrace(Unwind_Trace, this);
158 // We need to pop a few frames so that pc is on top.
159 // trace[0] belongs to the current function so we always pop it.
160 int to_pop = 1;
161 /**/ if (size > 1 && MatchPc(pc, trace[1])) to_pop = 1;
162 else if (size > 2 && MatchPc(pc, trace[2])) to_pop = 2;
163 else if (size > 3 && MatchPc(pc, trace[3])) to_pop = 3;
164 else if (size > 4 && MatchPc(pc, trace[4])) to_pop = 4;
165 else if (size > 5 && MatchPc(pc, trace[5])) to_pop = 5;
166 this->PopStackFrames(to_pop);
168 this->trace[0] = pc;
171 #endif // !SANITIZER_GO
173 static uptr g_tls_size;
175 #ifdef __i386__
176 # define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
177 #else
178 # define DL_INTERNAL_FUNCTION
179 #endif
181 void InitTlsSize() {
182 #if !defined(SANITIZER_GO) && !SANITIZER_ANDROID
183 typedef void (*get_tls_func)(size_t*, size_t*) DL_INTERNAL_FUNCTION;
184 get_tls_func get_tls;
185 void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info");
186 CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr));
187 internal_memcpy(&get_tls, &get_tls_static_info_ptr,
188 sizeof(get_tls_static_info_ptr));
189 CHECK_NE(get_tls, 0);
190 size_t tls_size = 0;
191 size_t tls_align = 0;
192 get_tls(&tls_size, &tls_align);
193 g_tls_size = tls_size;
194 #endif
197 uptr GetTlsSize() {
198 return g_tls_size;
201 #if defined(__x86_64__) || defined(__i386__)
202 // sizeof(struct thread) from glibc.
203 // There has been a report of this being different on glibc 2.11 and 2.13. We
204 // don't know when this change happened, so 2.14 is a conservative estimate.
205 #if __GLIBC_PREREQ(2, 14)
206 const uptr kThreadDescriptorSize = FIRST_32_SECOND_64(1216, 2304);
207 #else
208 const uptr kThreadDescriptorSize = FIRST_32_SECOND_64(1168, 2304);
209 #endif
211 uptr ThreadDescriptorSize() {
212 return kThreadDescriptorSize;
215 // The offset at which pointer to self is located in the thread descriptor.
216 const uptr kThreadSelfOffset = FIRST_32_SECOND_64(8, 16);
218 uptr ThreadSelfOffset() {
219 return kThreadSelfOffset;
222 uptr ThreadSelf() {
223 uptr descr_addr;
224 #ifdef __i386__
225 asm("mov %%gs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
226 #else
227 asm("mov %%fs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
228 #endif
229 return descr_addr;
231 #endif // defined(__x86_64__) || defined(__i386__)
233 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
234 uptr *tls_addr, uptr *tls_size) {
235 #ifndef SANITIZER_GO
236 #if defined(__x86_64__) || defined(__i386__)
237 *tls_addr = ThreadSelf();
238 *tls_size = GetTlsSize();
239 *tls_addr -= *tls_size;
240 *tls_addr += kThreadDescriptorSize;
241 #else
242 *tls_addr = 0;
243 *tls_size = 0;
244 #endif
246 uptr stack_top, stack_bottom;
247 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
248 *stk_addr = stack_bottom;
249 *stk_size = stack_top - stack_bottom;
251 if (!main) {
252 // If stack and tls intersect, make them non-intersecting.
253 if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) {
254 CHECK_GT(*tls_addr + *tls_size, *stk_addr);
255 CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size);
256 *stk_size -= *tls_size;
257 *tls_addr = *stk_addr + *stk_size;
260 #else // SANITIZER_GO
261 *stk_addr = 0;
262 *stk_size = 0;
263 *tls_addr = 0;
264 *tls_size = 0;
265 #endif // SANITIZER_GO
268 void AdjustStackSizeLinux(void *attr_, int verbosity) {
269 pthread_attr_t *attr = (pthread_attr_t *)attr_;
270 uptr stackaddr = 0;
271 size_t stacksize = 0;
272 pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize);
273 // GLibC will return (0 - stacksize) as the stack address in the case when
274 // stacksize is set, but stackaddr is not.
275 bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0);
276 // We place a lot of tool data into TLS, account for that.
277 const uptr minstacksize = GetTlsSize() + 128*1024;
278 if (stacksize < minstacksize) {
279 if (!stack_set) {
280 if (verbosity && stacksize != 0)
281 Printf("Sanitizer: increasing stacksize %zu->%zu\n", stacksize,
282 minstacksize);
283 pthread_attr_setstacksize(attr, minstacksize);
284 } else {
285 Printf("Sanitizer: pre-allocated stack size is insufficient: "
286 "%zu < %zu\n", stacksize, minstacksize);
287 Printf("Sanitizer: pthread_create is likely to fail.\n");
292 #if SANITIZER_ANDROID
293 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
294 string_predicate_t filter) {
295 return 0;
297 #else // SANITIZER_ANDROID
298 typedef ElfW(Phdr) Elf_Phdr;
300 struct DlIteratePhdrData {
301 LoadedModule *modules;
302 uptr current_n;
303 bool first;
304 uptr max_n;
305 string_predicate_t filter;
308 static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
309 DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
310 if (data->current_n == data->max_n)
311 return 0;
312 InternalScopedBuffer<char> module_name(kMaxPathLength);
313 module_name.data()[0] = '\0';
314 if (data->first) {
315 data->first = false;
316 // First module is the binary itself.
317 ReadBinaryName(module_name.data(), module_name.size());
318 } else if (info->dlpi_name) {
319 internal_strncpy(module_name.data(), info->dlpi_name, module_name.size());
321 if (module_name.data()[0] == '\0')
322 return 0;
323 if (data->filter && !data->filter(module_name.data()))
324 return 0;
325 void *mem = &data->modules[data->current_n];
326 LoadedModule *cur_module = new(mem) LoadedModule(module_name.data(),
327 info->dlpi_addr);
328 data->current_n++;
329 for (int i = 0; i < info->dlpi_phnum; i++) {
330 const Elf_Phdr *phdr = &info->dlpi_phdr[i];
331 if (phdr->p_type == PT_LOAD) {
332 uptr cur_beg = info->dlpi_addr + phdr->p_vaddr;
333 uptr cur_end = cur_beg + phdr->p_memsz;
334 cur_module->addAddressRange(cur_beg, cur_end);
337 return 0;
340 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
341 string_predicate_t filter) {
342 CHECK(modules);
343 DlIteratePhdrData data = {modules, 0, true, max_modules, filter};
344 dl_iterate_phdr(dl_iterate_phdr_cb, &data);
345 return data.current_n;
347 #endif // SANITIZER_ANDROID
349 } // namespace __sanitizer
351 #endif // SANITIZER_LINUX