sanitizer_linux_libcdep.cc revision 088ea2b46f97172bd0b0663a629e11cf826b84f7
1//===-- 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 22#ifdef __x86_64__ 23#include <asm/prctl.h> 24#endif 25#include <dlfcn.h> 26#include <pthread.h> 27#include <sys/prctl.h> 28#include <sys/resource.h> 29#include <unwind.h> 30 31#ifdef __x86_64__ 32extern "C" int arch_prctl(int code, __sanitizer::uptr *addr); 33#endif 34 35namespace __sanitizer { 36 37void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, 38 uptr *stack_bottom) { 39 static const uptr kMaxThreadStackSize = 256 * (1 << 20); // 256M 40 CHECK(stack_top); 41 CHECK(stack_bottom); 42 if (at_initialization) { 43 // This is the main thread. Libpthread may not be initialized yet. 44 struct rlimit rl; 45 CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0); 46 47 // Find the mapping that contains a stack variable. 48 MemoryMappingLayout proc_maps(/*cache_enabled*/true); 49 uptr start, end, offset; 50 uptr prev_end = 0; 51 while (proc_maps.Next(&start, &end, &offset, 0, 0, /* protection */0)) { 52 if ((uptr)&rl < end) 53 break; 54 prev_end = end; 55 } 56 CHECK((uptr)&rl >= start && (uptr)&rl < end); 57 58 // Get stacksize from rlimit, but clip it so that it does not overlap 59 // with other mappings. 60 uptr stacksize = rl.rlim_cur; 61 if (stacksize > end - prev_end) 62 stacksize = end - prev_end; 63 // When running with unlimited stack size, we still want to set some limit. 64 // The unlimited stack size is caused by 'ulimit -s unlimited'. 65 // Also, for some reason, GNU make spawns subprocesses with unlimited stack. 66 if (stacksize > kMaxThreadStackSize) 67 stacksize = kMaxThreadStackSize; 68 *stack_top = end; 69 *stack_bottom = end - stacksize; 70 return; 71 } 72 pthread_attr_t attr; 73 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0); 74 uptr stacksize = 0; 75 void *stackaddr = 0; 76 pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize); 77 pthread_attr_destroy(&attr); 78 79 *stack_top = (uptr)stackaddr + stacksize; 80 *stack_bottom = (uptr)stackaddr; 81 CHECK(stacksize < kMaxThreadStackSize); // Sanity check. 82} 83 84// Does not compile for Go because dlsym() requires -ldl 85#ifndef SANITIZER_GO 86bool SetEnv(const char *name, const char *value) { 87 void *f = dlsym(RTLD_NEXT, "setenv"); 88 if (f == 0) 89 return false; 90 typedef int(*setenv_ft)(const char *name, const char *value, int overwrite); 91 setenv_ft setenv_f; 92 CHECK_EQ(sizeof(setenv_f), sizeof(f)); 93 internal_memcpy(&setenv_f, &f, sizeof(f)); 94 return setenv_f(name, value, 1) == 0; 95} 96#endif 97 98bool SanitizerSetThreadName(const char *name) { 99#ifdef PR_SET_NAME 100 return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); // NOLINT 101#else 102 return false; 103#endif 104} 105 106bool SanitizerGetThreadName(char *name, int max_len) { 107#ifdef PR_GET_NAME 108 char buff[17]; 109 if (prctl(PR_GET_NAME, (unsigned long)buff, 0, 0, 0)) // NOLINT 110 return false; 111 internal_strncpy(name, buff, max_len); 112 name[max_len] = 0; 113 return true; 114#else 115 return false; 116#endif 117} 118 119#ifndef SANITIZER_GO 120//------------------------- SlowUnwindStack ----------------------------------- 121#ifdef __arm__ 122#define UNWIND_STOP _URC_END_OF_STACK 123#define UNWIND_CONTINUE _URC_NO_REASON 124#else 125#define UNWIND_STOP _URC_NORMAL_STOP 126#define UNWIND_CONTINUE _URC_NO_REASON 127#endif 128 129uptr Unwind_GetIP(struct _Unwind_Context *ctx) { 130#ifdef __arm__ 131 uptr val; 132 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE, 133 15 /* r15 = PC */, _UVRSD_UINT32, &val); 134 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed"); 135 // Clear the Thumb bit. 136 return val & ~(uptr)1; 137#else 138 return _Unwind_GetIP(ctx); 139#endif 140} 141 142_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) { 143 StackTrace *b = (StackTrace*)param; 144 CHECK(b->size < b->max_size); 145 uptr pc = Unwind_GetIP(ctx); 146 b->trace[b->size++] = pc; 147 if (b->size == b->max_size) return UNWIND_STOP; 148 return UNWIND_CONTINUE; 149} 150 151static bool MatchPc(uptr cur_pc, uptr trace_pc) { 152 return cur_pc - trace_pc <= 64 || trace_pc - cur_pc <= 64; 153} 154 155void StackTrace::SlowUnwindStack(uptr pc, uptr max_depth) { 156 this->size = 0; 157 this->max_size = max_depth; 158 if (max_depth > 1) { 159 _Unwind_Backtrace(Unwind_Trace, this); 160 // We need to pop a few frames so that pc is on top. 161 // trace[0] belongs to the current function so we always pop it. 162 int to_pop = 1; 163 /**/ if (size > 1 && MatchPc(pc, trace[1])) to_pop = 1; 164 else if (size > 2 && MatchPc(pc, trace[2])) to_pop = 2; 165 else if (size > 3 && MatchPc(pc, trace[3])) to_pop = 3; 166 else if (size > 4 && MatchPc(pc, trace[4])) to_pop = 4; 167 else if (size > 5 && MatchPc(pc, trace[5])) to_pop = 5; 168 this->PopStackFrames(to_pop); 169 } 170 this->trace[0] = pc; 171} 172 173#endif // !SANITIZER_GO 174 175static uptr g_tls_size; 176 177#ifdef __i386__ 178# define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall)) 179#else 180# define DL_INTERNAL_FUNCTION 181#endif 182 183void InitTlsSize() { 184#if !defined(SANITIZER_GO) && !SANITIZER_ANDROID 185 typedef void (*get_tls_func)(size_t*, size_t*) DL_INTERNAL_FUNCTION; 186 get_tls_func get_tls; 187 void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info"); 188 CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr)); 189 internal_memcpy(&get_tls, &get_tls_static_info_ptr, 190 sizeof(get_tls_static_info_ptr)); 191 CHECK_NE(get_tls, 0); 192 size_t tls_size = 0; 193 size_t tls_align = 0; 194 get_tls(&tls_size, &tls_align); 195 g_tls_size = tls_size; 196#endif 197} 198 199uptr GetTlsSize() { 200 return g_tls_size; 201} 202 203// sizeof(struct thread) from glibc. 204#ifdef __x86_64__ 205const uptr kThreadDescriptorSize = 2304; 206 207uptr ThreadDescriptorSize() { 208 return kThreadDescriptorSize; 209} 210#endif 211 212void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, 213 uptr *tls_addr, uptr *tls_size) { 214#ifndef SANITIZER_GO 215#ifdef __x86_64__ 216 arch_prctl(ARCH_GET_FS, tls_addr); 217 *tls_size = GetTlsSize(); 218 *tls_addr -= *tls_size; 219 *tls_addr += kThreadDescriptorSize; 220#else 221 *tls_addr = 0; 222 *tls_size = 0; 223#endif 224 225 uptr stack_top, stack_bottom; 226 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom); 227 *stk_addr = stack_bottom; 228 *stk_size = stack_top - stack_bottom; 229 230 if (!main) { 231 // If stack and tls intersect, make them non-intersecting. 232 if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) { 233 CHECK_GT(*tls_addr + *tls_size, *stk_addr); 234 CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size); 235 *stk_size -= *tls_size; 236 *tls_addr = *stk_addr + *stk_size; 237 } 238 } 239#else // SANITIZER_GO 240 *stk_addr = 0; 241 *stk_size = 0; 242 *tls_addr = 0; 243 *tls_size = 0; 244#endif // SANITIZER_GO 245} 246 247void AdjustStackSizeLinux(void *attr_, int verbosity) { 248 pthread_attr_t *attr = (pthread_attr_t *)attr_; 249 uptr stackaddr = 0; 250 size_t stacksize = 0; 251 pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize); 252 // GLibC will return (0 - stacksize) as the stack address in the case when 253 // stacksize is set, but stackaddr is not. 254 bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0); 255 // We place a lot of tool data into TLS, account for that. 256 const uptr minstacksize = GetTlsSize() + 128*1024; 257 if (stacksize < minstacksize) { 258 if (!stack_set) { 259 if (verbosity && stacksize != 0) 260 Printf("Sanitizer: increasing stacksize %zu->%zu\n", stacksize, 261 minstacksize); 262 pthread_attr_setstacksize(attr, minstacksize); 263 } else { 264 Printf("Sanitizer: pre-allocated stack size is insufficient: " 265 "%zu < %zu\n", stacksize, minstacksize); 266 Printf("Sanitizer: pthread_create is likely to fail.\n"); 267 } 268 } 269} 270 271} // namespace __sanitizer 272 273#endif // SANITIZER_LINUX 274