* include/bits/stl_deque.h (_Deque_impl): Move comment.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_linux_libcdep.cc
blob5b70a69abec1ad294108c920ca48c2952d118d9e
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_flags.h"
18 #include "sanitizer_linux.h"
19 #include "sanitizer_placement_new.h"
20 #include "sanitizer_procmaps.h"
21 #include "sanitizer_stacktrace.h"
23 #include <dlfcn.h>
24 #include <pthread.h>
25 #include <sys/prctl.h>
26 #include <sys/resource.h>
27 #include <unwind.h>
29 #if !SANITIZER_ANDROID
30 #include <elf.h>
31 #include <link.h>
32 #endif
34 // This function is defined elsewhere if we intercepted pthread_attr_getstack.
35 SANITIZER_WEAK_ATTRIBUTE
36 int __sanitizer_pthread_attr_getstack(void *attr, void **addr, size_t *size) {
37 return pthread_attr_getstack((pthread_attr_t*)attr, addr, size);
40 namespace __sanitizer {
42 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
43 uptr *stack_bottom) {
44 static const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
45 CHECK(stack_top);
46 CHECK(stack_bottom);
47 if (at_initialization) {
48 // This is the main thread. Libpthread may not be initialized yet.
49 struct rlimit rl;
50 CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
52 // Find the mapping that contains a stack variable.
53 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
54 uptr start, end, offset;
55 uptr prev_end = 0;
56 while (proc_maps.Next(&start, &end, &offset, 0, 0, /* protection */0)) {
57 if ((uptr)&rl < end)
58 break;
59 prev_end = end;
61 CHECK((uptr)&rl >= start && (uptr)&rl < end);
63 // Get stacksize from rlimit, but clip it so that it does not overlap
64 // with other mappings.
65 uptr stacksize = rl.rlim_cur;
66 if (stacksize > end - prev_end)
67 stacksize = end - prev_end;
68 // When running with unlimited stack size, we still want to set some limit.
69 // The unlimited stack size is caused by 'ulimit -s unlimited'.
70 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
71 if (stacksize > kMaxThreadStackSize)
72 stacksize = kMaxThreadStackSize;
73 *stack_top = end;
74 *stack_bottom = end - stacksize;
75 return;
77 pthread_attr_t attr;
78 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
79 uptr stacksize = 0;
80 void *stackaddr = 0;
81 __sanitizer_pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize);
82 pthread_attr_destroy(&attr);
84 CHECK_LE(stacksize, kMaxThreadStackSize); // Sanity check.
85 *stack_top = (uptr)stackaddr + stacksize;
86 *stack_bottom = (uptr)stackaddr;
89 // Does not compile for Go because dlsym() requires -ldl
90 #ifndef SANITIZER_GO
91 bool SetEnv(const char *name, const char *value) {
92 void *f = dlsym(RTLD_NEXT, "setenv");
93 if (f == 0)
94 return false;
95 typedef int(*setenv_ft)(const char *name, const char *value, int overwrite);
96 setenv_ft setenv_f;
97 CHECK_EQ(sizeof(setenv_f), sizeof(f));
98 internal_memcpy(&setenv_f, &f, sizeof(f));
99 return setenv_f(name, value, 1) == 0;
101 #endif
103 bool SanitizerSetThreadName(const char *name) {
104 #ifdef PR_SET_NAME
105 return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); // NOLINT
106 #else
107 return false;
108 #endif
111 bool SanitizerGetThreadName(char *name, int max_len) {
112 #ifdef PR_GET_NAME
113 char buff[17];
114 if (prctl(PR_GET_NAME, (unsigned long)buff, 0, 0, 0)) // NOLINT
115 return false;
116 internal_strncpy(name, buff, max_len);
117 name[max_len] = 0;
118 return true;
119 #else
120 return false;
121 #endif
124 #ifndef SANITIZER_GO
125 //------------------------- SlowUnwindStack -----------------------------------
126 #ifdef __arm__
127 #define UNWIND_STOP _URC_END_OF_STACK
128 #define UNWIND_CONTINUE _URC_NO_REASON
129 #else
130 #define UNWIND_STOP _URC_NORMAL_STOP
131 #define UNWIND_CONTINUE _URC_NO_REASON
132 #endif
134 uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
135 #ifdef __arm__
136 uptr val;
137 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
138 15 /* r15 = PC */, _UVRSD_UINT32, &val);
139 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
140 // Clear the Thumb bit.
141 return val & ~(uptr)1;
142 #else
143 return _Unwind_GetIP(ctx);
144 #endif
147 struct UnwindTraceArg {
148 StackTrace *stack;
149 uptr max_depth;
152 _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
153 UnwindTraceArg *arg = (UnwindTraceArg*)param;
154 CHECK_LT(arg->stack->size, arg->max_depth);
155 uptr pc = Unwind_GetIP(ctx);
156 arg->stack->trace[arg->stack->size++] = pc;
157 if (arg->stack->size == arg->max_depth) return UNWIND_STOP;
158 return UNWIND_CONTINUE;
161 void StackTrace::SlowUnwindStack(uptr pc, uptr max_depth) {
162 size = 0;
163 if (max_depth == 0)
164 return;
165 UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)};
166 _Unwind_Backtrace(Unwind_Trace, &arg);
167 // We need to pop a few frames so that pc is on top.
168 uptr to_pop = LocatePcInTrace(pc);
169 // trace[0] belongs to the current function so we always pop it.
170 if (to_pop == 0)
171 to_pop = 1;
172 PopStackFrames(to_pop);
173 trace[0] = pc;
176 #endif // !SANITIZER_GO
178 static uptr g_tls_size;
180 #ifdef __i386__
181 # define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
182 #else
183 # define DL_INTERNAL_FUNCTION
184 #endif
186 void InitTlsSize() {
187 #if !defined(SANITIZER_GO) && !SANITIZER_ANDROID
188 typedef void (*get_tls_func)(size_t*, size_t*) DL_INTERNAL_FUNCTION;
189 get_tls_func get_tls;
190 void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info");
191 CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr));
192 internal_memcpy(&get_tls, &get_tls_static_info_ptr,
193 sizeof(get_tls_static_info_ptr));
194 CHECK_NE(get_tls, 0);
195 size_t tls_size = 0;
196 size_t tls_align = 0;
197 get_tls(&tls_size, &tls_align);
198 g_tls_size = tls_size;
199 #endif
202 uptr GetTlsSize() {
203 return g_tls_size;
206 #if defined(__x86_64__) || defined(__i386__)
207 // sizeof(struct thread) from glibc.
208 // There has been a report of this being different on glibc 2.11 and 2.13. We
209 // don't know when this change happened, so 2.14 is a conservative estimate.
210 #if __GLIBC_PREREQ(2, 14)
211 const uptr kThreadDescriptorSize = FIRST_32_SECOND_64(1216, 2304);
212 #else
213 const uptr kThreadDescriptorSize = FIRST_32_SECOND_64(1168, 2304);
214 #endif
216 uptr ThreadDescriptorSize() {
217 return kThreadDescriptorSize;
220 // The offset at which pointer to self is located in the thread descriptor.
221 const uptr kThreadSelfOffset = FIRST_32_SECOND_64(8, 16);
223 uptr ThreadSelfOffset() {
224 return kThreadSelfOffset;
227 uptr ThreadSelf() {
228 uptr descr_addr;
229 #ifdef __i386__
230 asm("mov %%gs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
231 #else
232 asm("mov %%fs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
233 #endif
234 return descr_addr;
236 #endif // defined(__x86_64__) || defined(__i386__)
238 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
239 uptr *tls_addr, uptr *tls_size) {
240 #ifndef SANITIZER_GO
241 #if defined(__x86_64__) || defined(__i386__)
242 *tls_addr = ThreadSelf();
243 *tls_size = GetTlsSize();
244 *tls_addr -= *tls_size;
245 *tls_addr += kThreadDescriptorSize;
246 #else
247 *tls_addr = 0;
248 *tls_size = 0;
249 #endif
251 uptr stack_top, stack_bottom;
252 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
253 *stk_addr = stack_bottom;
254 *stk_size = stack_top - stack_bottom;
256 if (!main) {
257 // If stack and tls intersect, make them non-intersecting.
258 if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) {
259 CHECK_GT(*tls_addr + *tls_size, *stk_addr);
260 CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size);
261 *stk_size -= *tls_size;
262 *tls_addr = *stk_addr + *stk_size;
265 #else // SANITIZER_GO
266 *stk_addr = 0;
267 *stk_size = 0;
268 *tls_addr = 0;
269 *tls_size = 0;
270 #endif // SANITIZER_GO
273 void AdjustStackSizeLinux(void *attr_) {
274 pthread_attr_t *attr = (pthread_attr_t *)attr_;
275 uptr stackaddr = 0;
276 size_t stacksize = 0;
277 __sanitizer_pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize);
278 // GLibC will return (0 - stacksize) as the stack address in the case when
279 // stacksize is set, but stackaddr is not.
280 bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0);
281 // We place a lot of tool data into TLS, account for that.
282 const uptr minstacksize = GetTlsSize() + 128*1024;
283 if (stacksize < minstacksize) {
284 if (!stack_set) {
285 if (common_flags()->verbosity && stacksize != 0)
286 Printf("Sanitizer: increasing stacksize %zu->%zu\n", stacksize,
287 minstacksize);
288 pthread_attr_setstacksize(attr, minstacksize);
289 } else {
290 Printf("Sanitizer: pre-allocated stack size is insufficient: "
291 "%zu < %zu\n", stacksize, minstacksize);
292 Printf("Sanitizer: pthread_create is likely to fail.\n");
297 #if SANITIZER_ANDROID
298 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
299 string_predicate_t filter) {
300 return 0;
302 #else // SANITIZER_ANDROID
303 typedef ElfW(Phdr) Elf_Phdr;
305 struct DlIteratePhdrData {
306 LoadedModule *modules;
307 uptr current_n;
308 bool first;
309 uptr max_n;
310 string_predicate_t filter;
313 static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
314 DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
315 if (data->current_n == data->max_n)
316 return 0;
317 InternalScopedBuffer<char> module_name(kMaxPathLength);
318 module_name.data()[0] = '\0';
319 if (data->first) {
320 data->first = false;
321 // First module is the binary itself.
322 ReadBinaryName(module_name.data(), module_name.size());
323 } else if (info->dlpi_name) {
324 internal_strncpy(module_name.data(), info->dlpi_name, module_name.size());
326 if (module_name.data()[0] == '\0')
327 return 0;
328 if (data->filter && !data->filter(module_name.data()))
329 return 0;
330 void *mem = &data->modules[data->current_n];
331 LoadedModule *cur_module = new(mem) LoadedModule(module_name.data(),
332 info->dlpi_addr);
333 data->current_n++;
334 for (int i = 0; i < info->dlpi_phnum; i++) {
335 const Elf_Phdr *phdr = &info->dlpi_phdr[i];
336 if (phdr->p_type == PT_LOAD) {
337 uptr cur_beg = info->dlpi_addr + phdr->p_vaddr;
338 uptr cur_end = cur_beg + phdr->p_memsz;
339 cur_module->addAddressRange(cur_beg, cur_end);
342 return 0;
345 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
346 string_predicate_t filter) {
347 CHECK(modules);
348 DlIteratePhdrData data = {modules, 0, true, max_modules, filter};
349 dl_iterate_phdr(dl_iterate_phdr_cb, &data);
350 return data.current_n;
352 #endif // SANITIZER_ANDROID
354 } // namespace __sanitizer
356 #endif // SANITIZER_LINUX