Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 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" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 16 | #if SANITIZER_FREEBSD || SANITIZER_LINUX |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 17 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 18 | #include "sanitizer_atomic.h" |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 19 | #include "sanitizer_common.h" |
Dmitry Vyukov | 06cbed8 | 2013-10-15 14:12:26 +0000 | [diff] [blame] | 20 | #include "sanitizer_flags.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 21 | #include "sanitizer_freebsd.h" |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 22 | #include "sanitizer_linux.h" |
| 23 | #include "sanitizer_placement_new.h" |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 24 | #include "sanitizer_procmaps.h" |
| 25 | #include "sanitizer_stacktrace.h" |
| 26 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 27 | #if SANITIZER_ANDROID || SANITIZER_FREEBSD |
| 28 | #include <dlfcn.h> // for dlsym() |
| 29 | #endif |
| 30 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 31 | #include <link.h> |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 32 | #include <pthread.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 33 | #include <signal.h> |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 34 | #include <sys/resource.h> |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 35 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 36 | #if SANITIZER_FREEBSD |
| 37 | #include <pthread_np.h> |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 38 | #include <osreldate.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 39 | #define pthread_getattr_np pthread_attr_get_np |
| 40 | #endif |
| 41 | |
| 42 | #if SANITIZER_LINUX |
| 43 | #include <sys/prctl.h> |
| 44 | #endif |
| 45 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 46 | #if SANITIZER_ANDROID |
| 47 | #include <android/api-level.h> |
| 48 | #endif |
| 49 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 50 | #if !SANITIZER_ANDROID |
| 51 | #include <elf.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 52 | #include <unistd.h> |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 55 | namespace __sanitizer { |
| 56 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 57 | // This function is defined elsewhere if we intercepted pthread_attr_getstack. |
| 58 | extern "C" { |
| 59 | SANITIZER_WEAK_ATTRIBUTE int |
| 60 | real_pthread_attr_getstack(void *attr, void **addr, size_t *size); |
| 61 | } // extern "C" |
| 62 | |
| 63 | static int my_pthread_attr_getstack(void *attr, void **addr, size_t *size) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 64 | #if !SANITIZER_GO |
| 65 | if (&real_pthread_attr_getstack) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 66 | return real_pthread_attr_getstack((pthread_attr_t *)attr, addr, size); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 67 | #endif |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 68 | return pthread_attr_getstack((pthread_attr_t *)attr, addr, size); |
| 69 | } |
| 70 | |
| 71 | SANITIZER_WEAK_ATTRIBUTE int |
| 72 | real_sigaction(int signum, const void *act, void *oldact); |
| 73 | |
| 74 | int internal_sigaction(int signum, const void *act, void *oldact) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 75 | #if !SANITIZER_GO |
| 76 | if (&real_sigaction) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 77 | return real_sigaction(signum, act, oldact); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 78 | #endif |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 79 | return sigaction(signum, (const struct sigaction *)act, |
| 80 | (struct sigaction *)oldact); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 83 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
| 84 | uptr *stack_bottom) { |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 85 | CHECK(stack_top); |
| 86 | CHECK(stack_bottom); |
| 87 | if (at_initialization) { |
| 88 | // This is the main thread. Libpthread may not be initialized yet. |
| 89 | struct rlimit rl; |
| 90 | CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0); |
| 91 | |
| 92 | // Find the mapping that contains a stack variable. |
| 93 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
| 94 | uptr start, end, offset; |
| 95 | uptr prev_end = 0; |
| 96 | while (proc_maps.Next(&start, &end, &offset, 0, 0, /* protection */0)) { |
| 97 | if ((uptr)&rl < end) |
| 98 | break; |
| 99 | prev_end = end; |
| 100 | } |
| 101 | CHECK((uptr)&rl >= start && (uptr)&rl < end); |
| 102 | |
| 103 | // Get stacksize from rlimit, but clip it so that it does not overlap |
| 104 | // with other mappings. |
| 105 | uptr stacksize = rl.rlim_cur; |
| 106 | if (stacksize > end - prev_end) |
| 107 | stacksize = end - prev_end; |
| 108 | // When running with unlimited stack size, we still want to set some limit. |
| 109 | // The unlimited stack size is caused by 'ulimit -s unlimited'. |
| 110 | // Also, for some reason, GNU make spawns subprocesses with unlimited stack. |
| 111 | if (stacksize > kMaxThreadStackSize) |
| 112 | stacksize = kMaxThreadStackSize; |
| 113 | *stack_top = end; |
| 114 | *stack_bottom = end - stacksize; |
| 115 | return; |
| 116 | } |
| 117 | pthread_attr_t attr; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 118 | pthread_attr_init(&attr); |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 119 | CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0); |
| 120 | uptr stacksize = 0; |
| 121 | void *stackaddr = 0; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 122 | my_pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize); |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 123 | pthread_attr_destroy(&attr); |
| 124 | |
Evgeniy Stepanov | 933a5d2 | 2013-07-30 09:01:18 +0000 | [diff] [blame] | 125 | CHECK_LE(stacksize, kMaxThreadStackSize); // Sanity check. |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 126 | *stack_top = (uptr)stackaddr + stacksize; |
| 127 | *stack_bottom = (uptr)stackaddr; |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 130 | #if !SANITIZER_GO |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 131 | bool SetEnv(const char *name, const char *value) { |
| 132 | void *f = dlsym(RTLD_NEXT, "setenv"); |
| 133 | if (f == 0) |
| 134 | return false; |
| 135 | typedef int(*setenv_ft)(const char *name, const char *value, int overwrite); |
| 136 | setenv_ft setenv_f; |
| 137 | CHECK_EQ(sizeof(setenv_f), sizeof(f)); |
| 138 | internal_memcpy(&setenv_f, &f, sizeof(f)); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 139 | return setenv_f(name, value, 1) == 0; |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 140 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 141 | #endif |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 142 | |
| 143 | bool SanitizerSetThreadName(const char *name) { |
| 144 | #ifdef PR_SET_NAME |
| 145 | return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); // NOLINT |
| 146 | #else |
| 147 | return false; |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | bool SanitizerGetThreadName(char *name, int max_len) { |
| 152 | #ifdef PR_GET_NAME |
| 153 | char buff[17]; |
| 154 | if (prctl(PR_GET_NAME, (unsigned long)buff, 0, 0, 0)) // NOLINT |
| 155 | return false; |
| 156 | internal_strncpy(name, buff, max_len); |
| 157 | name[max_len] = 0; |
| 158 | return true; |
| 159 | #else |
| 160 | return false; |
| 161 | #endif |
| 162 | } |
| 163 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 164 | #if !SANITIZER_FREEBSD |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 165 | static uptr g_tls_size; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 166 | #endif |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 167 | |
| 168 | #ifdef __i386__ |
| 169 | # define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall)) |
| 170 | #else |
| 171 | # define DL_INTERNAL_FUNCTION |
| 172 | #endif |
| 173 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 174 | #if defined(__mips__) |
| 175 | // TlsPreTcbSize includes size of struct pthread_descr and size of tcb |
| 176 | // head structure. It lies before the static tls blocks. |
| 177 | static uptr TlsPreTcbSize() { |
| 178 | const uptr kTcbHead = 16; |
| 179 | const uptr kTlsAlign = 16; |
| 180 | const uptr kTlsPreTcbSize = |
| 181 | (ThreadDescriptorSize() + kTcbHead + kTlsAlign - 1) & ~(kTlsAlign - 1); |
| 182 | InitTlsSize(); |
| 183 | g_tls_size = (g_tls_size + kTlsPreTcbSize + kTlsAlign -1) & ~(kTlsAlign - 1); |
| 184 | return kTlsPreTcbSize; |
| 185 | } |
| 186 | #endif |
| 187 | |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 188 | void InitTlsSize() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 189 | #if !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 190 | typedef void (*get_tls_func)(size_t*, size_t*) DL_INTERNAL_FUNCTION; |
| 191 | get_tls_func get_tls; |
| 192 | void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info"); |
| 193 | CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr)); |
| 194 | internal_memcpy(&get_tls, &get_tls_static_info_ptr, |
| 195 | sizeof(get_tls_static_info_ptr)); |
| 196 | CHECK_NE(get_tls, 0); |
| 197 | size_t tls_size = 0; |
| 198 | size_t tls_align = 0; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 199 | get_tls(&tls_size, &tls_align); |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 200 | g_tls_size = tls_size; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 201 | #endif // !SANITIZER_FREEBSD && !SANITIZER_ANDROID |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 204 | #if (defined(__x86_64__) || defined(__i386__) || defined(__mips__)) \ |
| 205 | && SANITIZER_LINUX |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 206 | // sizeof(struct thread) from glibc. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 207 | static atomic_uintptr_t kThreadDescriptorSize; |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 208 | |
| 209 | uptr ThreadDescriptorSize() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 210 | uptr val = atomic_load(&kThreadDescriptorSize, memory_order_relaxed); |
| 211 | if (val) |
| 212 | return val; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 213 | #if defined(__x86_64__) || defined(__i386__) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 214 | #ifdef _CS_GNU_LIBC_VERSION |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 215 | char buf[64]; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 216 | uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf)); |
| 217 | if (len < sizeof(buf) && internal_strncmp(buf, "glibc 2.", 8) == 0) { |
| 218 | char *end; |
| 219 | int minor = internal_simple_strtoll(buf + 8, &end, 10); |
| 220 | if (end != buf + 8 && (*end == '\0' || *end == '.')) { |
| 221 | /* sizeof(struct thread) values from various glibc versions. */ |
| 222 | if (SANITIZER_X32) |
| 223 | val = 1728; // Assume only one particular version for x32. |
| 224 | else if (minor <= 3) |
| 225 | val = FIRST_32_SECOND_64(1104, 1696); |
| 226 | else if (minor == 4) |
| 227 | val = FIRST_32_SECOND_64(1120, 1728); |
| 228 | else if (minor == 5) |
| 229 | val = FIRST_32_SECOND_64(1136, 1728); |
| 230 | else if (minor <= 9) |
| 231 | val = FIRST_32_SECOND_64(1136, 1712); |
| 232 | else if (minor == 10) |
| 233 | val = FIRST_32_SECOND_64(1168, 1776); |
| 234 | else if (minor <= 12) |
| 235 | val = FIRST_32_SECOND_64(1168, 2288); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 236 | else if (minor == 13) |
| 237 | val = FIRST_32_SECOND_64(1168, 2304); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 238 | else |
| 239 | val = FIRST_32_SECOND_64(1216, 2304); |
| 240 | } |
| 241 | if (val) |
| 242 | atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed); |
| 243 | return val; |
| 244 | } |
| 245 | #endif |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 246 | #elif defined(__mips__) |
| 247 | // TODO(sagarthakur): add more values as per different glibc versions. |
| 248 | val = FIRST_32_SECOND_64(1152, 1776); |
| 249 | if (val) |
| 250 | atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed); |
| 251 | return val; |
| 252 | #endif |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 253 | return 0; |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 254 | } |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 255 | |
| 256 | // The offset at which pointer to self is located in the thread descriptor. |
| 257 | const uptr kThreadSelfOffset = FIRST_32_SECOND_64(8, 16); |
| 258 | |
| 259 | uptr ThreadSelfOffset() { |
| 260 | return kThreadSelfOffset; |
| 261 | } |
| 262 | |
| 263 | uptr ThreadSelf() { |
| 264 | uptr descr_addr; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 265 | # if defined(__i386__) |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 266 | asm("mov %%gs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset)); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 267 | # elif defined(__x86_64__) |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 268 | asm("mov %%fs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset)); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 269 | # elif defined(__mips__) |
| 270 | // MIPS uses TLS variant I. The thread pointer (in hardware register $29) |
| 271 | // points to the end of the TCB + 0x7000. The pthread_descr structure is |
| 272 | // immediately in front of the TCB. TlsPreTcbSize() includes the size of the |
| 273 | // TCB and the size of pthread_descr. |
| 274 | const uptr kTlsTcbOffset = 0x7000; |
| 275 | uptr thread_pointer; |
| 276 | asm volatile(".set push;\ |
| 277 | .set mips64r2;\ |
| 278 | rdhwr %0,$29;\ |
| 279 | .set pop" : "=r" (thread_pointer)); |
| 280 | descr_addr = thread_pointer - kTlsTcbOffset - TlsPreTcbSize(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 281 | # else |
| 282 | # error "unsupported CPU arch" |
| 283 | # endif |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 284 | return descr_addr; |
| 285 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 286 | #endif // (x86_64 || i386 || MIPS) && SANITIZER_LINUX |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 287 | |
| 288 | #if SANITIZER_FREEBSD |
| 289 | static void **ThreadSelfSegbase() { |
| 290 | void **segbase = 0; |
| 291 | # if defined(__i386__) |
| 292 | // sysarch(I386_GET_GSBASE, segbase); |
| 293 | __asm __volatile("mov %%gs:0, %0" : "=r" (segbase)); |
| 294 | # elif defined(__x86_64__) |
| 295 | // sysarch(AMD64_GET_FSBASE, segbase); |
| 296 | __asm __volatile("movq %%fs:0, %0" : "=r" (segbase)); |
| 297 | # else |
| 298 | # error "unsupported CPU arch for FreeBSD platform" |
| 299 | # endif |
| 300 | return segbase; |
| 301 | } |
| 302 | |
| 303 | uptr ThreadSelf() { |
| 304 | return (uptr)ThreadSelfSegbase()[2]; |
| 305 | } |
| 306 | #endif // SANITIZER_FREEBSD |
| 307 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 308 | #if !SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 309 | static void GetTls(uptr *addr, uptr *size) { |
| 310 | #if SANITIZER_LINUX |
| 311 | # if defined(__x86_64__) || defined(__i386__) |
| 312 | *addr = ThreadSelf(); |
| 313 | *size = GetTlsSize(); |
| 314 | *addr -= *size; |
| 315 | *addr += ThreadDescriptorSize(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 316 | # elif defined(__mips__) |
| 317 | *addr = ThreadSelf(); |
| 318 | *size = GetTlsSize(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 319 | # else |
| 320 | *addr = 0; |
| 321 | *size = 0; |
| 322 | # endif |
| 323 | #elif SANITIZER_FREEBSD |
| 324 | void** segbase = ThreadSelfSegbase(); |
| 325 | *addr = 0; |
| 326 | *size = 0; |
| 327 | if (segbase != 0) { |
| 328 | // tcbalign = 16 |
| 329 | // tls_size = round(tls_static_space, tcbalign); |
| 330 | // dtv = segbase[1]; |
| 331 | // dtv[2] = segbase - tls_static_space; |
| 332 | void **dtv = (void**) segbase[1]; |
| 333 | *addr = (uptr) dtv[2]; |
| 334 | *size = (*addr == 0) ? 0 : ((uptr) segbase[0] - (uptr) dtv[2]); |
| 335 | } |
| 336 | #else |
| 337 | # error "Unknown OS" |
| 338 | #endif |
| 339 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 340 | #endif |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 341 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 342 | #if !SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 343 | uptr GetTlsSize() { |
| 344 | #if SANITIZER_FREEBSD |
| 345 | uptr addr, size; |
| 346 | GetTls(&addr, &size); |
| 347 | return size; |
| 348 | #else |
| 349 | return g_tls_size; |
| 350 | #endif |
| 351 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 352 | #endif |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 353 | |
| 354 | void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, |
| 355 | uptr *tls_addr, uptr *tls_size) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 356 | #if SANITIZER_GO |
| 357 | // Stub implementation for Go. |
| 358 | *stk_addr = *stk_size = *tls_addr = *tls_size = 0; |
| 359 | #else |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 360 | GetTls(tls_addr, tls_size); |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 361 | |
| 362 | uptr stack_top, stack_bottom; |
| 363 | GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom); |
| 364 | *stk_addr = stack_bottom; |
| 365 | *stk_size = stack_top - stack_bottom; |
| 366 | |
| 367 | if (!main) { |
| 368 | // If stack and tls intersect, make them non-intersecting. |
| 369 | if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) { |
| 370 | CHECK_GT(*tls_addr + *tls_size, *stk_addr); |
| 371 | CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size); |
| 372 | *stk_size -= *tls_size; |
| 373 | *tls_addr = *stk_addr + *stk_size; |
| 374 | } |
| 375 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 376 | #endif |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 379 | void AdjustStackSize(void *attr_) { |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 380 | pthread_attr_t *attr = (pthread_attr_t *)attr_; |
| 381 | uptr stackaddr = 0; |
| 382 | size_t stacksize = 0; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 383 | my_pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize); |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 384 | // GLibC will return (0 - stacksize) as the stack address in the case when |
| 385 | // stacksize is set, but stackaddr is not. |
| 386 | bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0); |
| 387 | // We place a lot of tool data into TLS, account for that. |
| 388 | const uptr minstacksize = GetTlsSize() + 128*1024; |
| 389 | if (stacksize < minstacksize) { |
| 390 | if (!stack_set) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 391 | if (stacksize != 0) { |
| 392 | VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize, |
| 393 | minstacksize); |
| 394 | pthread_attr_setstacksize(attr, minstacksize); |
| 395 | } |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 396 | } else { |
| 397 | Printf("Sanitizer: pre-allocated stack size is insufficient: " |
| 398 | "%zu < %zu\n", stacksize, minstacksize); |
| 399 | Printf("Sanitizer: pthread_create is likely to fail.\n"); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 404 | # if !SANITIZER_FREEBSD |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 405 | typedef ElfW(Phdr) Elf_Phdr; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 406 | # elif SANITIZER_WORDSIZE == 32 && __FreeBSD_version <= 902001 // v9.2 |
| 407 | # define Elf_Phdr XElf32_Phdr |
| 408 | # define dl_phdr_info xdl_phdr_info |
| 409 | # define dl_iterate_phdr(c, b) xdl_iterate_phdr((c), (b)) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 410 | # endif |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 411 | |
| 412 | struct DlIteratePhdrData { |
| 413 | LoadedModule *modules; |
| 414 | uptr current_n; |
| 415 | bool first; |
| 416 | uptr max_n; |
| 417 | string_predicate_t filter; |
| 418 | }; |
| 419 | |
| 420 | static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) { |
| 421 | DlIteratePhdrData *data = (DlIteratePhdrData*)arg; |
| 422 | if (data->current_n == data->max_n) |
| 423 | return 0; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 424 | InternalScopedString module_name(kMaxPathLength); |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 425 | if (data->first) { |
| 426 | data->first = false; |
| 427 | // First module is the binary itself. |
| 428 | ReadBinaryName(module_name.data(), module_name.size()); |
| 429 | } else if (info->dlpi_name) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 430 | module_name.append("%s", info->dlpi_name); |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 431 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 432 | if (module_name[0] == '\0') |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 433 | return 0; |
| 434 | if (data->filter && !data->filter(module_name.data())) |
| 435 | return 0; |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 436 | LoadedModule *cur_module = &data->modules[data->current_n]; |
| 437 | cur_module->set(module_name.data(), info->dlpi_addr); |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 438 | data->current_n++; |
| 439 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 440 | const Elf_Phdr *phdr = &info->dlpi_phdr[i]; |
| 441 | if (phdr->p_type == PT_LOAD) { |
| 442 | uptr cur_beg = info->dlpi_addr + phdr->p_vaddr; |
| 443 | uptr cur_end = cur_beg + phdr->p_memsz; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 444 | bool executable = phdr->p_flags & PF_X; |
| 445 | cur_module->addAddressRange(cur_beg, cur_end, executable); |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | return 0; |
| 449 | } |
| 450 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 451 | #if SANITIZER_ANDROID && __ANDROID_API__ < 21 |
| 452 | extern "C" __attribute__((weak)) int dl_iterate_phdr( |
| 453 | int (*)(struct dl_phdr_info *, size_t, void *), void *); |
| 454 | #endif |
| 455 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 456 | uptr GetListOfModules(LoadedModule *modules, uptr max_modules, |
| 457 | string_predicate_t filter) { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 458 | #if SANITIZER_ANDROID && __ANDROID_API__ < 21 |
| 459 | u32 api_level = AndroidGetApiLevel(); |
| 460 | // Fall back to /proc/maps if dl_iterate_phdr is unavailable or broken. |
| 461 | // The runtime check allows the same library to work with |
| 462 | // both K and L (and future) Android releases. |
| 463 | if (api_level <= 22) { // L or earlier |
| 464 | MemoryMappingLayout memory_mapping(false); |
| 465 | return memory_mapping.DumpListOfModules(modules, max_modules, filter); |
| 466 | } |
| 467 | #endif |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 468 | CHECK(modules); |
| 469 | DlIteratePhdrData data = {modules, 0, true, max_modules, filter}; |
| 470 | dl_iterate_phdr(dl_iterate_phdr_cb, &data); |
| 471 | return data.current_n; |
| 472 | } |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 473 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 474 | // getrusage does not give us the current RSS, only the max RSS. |
| 475 | // Still, this is better than nothing if /proc/self/statm is not available |
| 476 | // for some reason, e.g. due to a sandbox. |
| 477 | static uptr GetRSSFromGetrusage() { |
| 478 | struct rusage usage; |
| 479 | if (getrusage(RUSAGE_SELF, &usage)) // Failed, probably due to a sandbox. |
| 480 | return 0; |
| 481 | return usage.ru_maxrss << 10; // ru_maxrss is in Kb. |
| 482 | } |
| 483 | |
| 484 | uptr GetRSS() { |
| 485 | if (!common_flags()->can_use_proc_maps_statm) |
| 486 | return GetRSSFromGetrusage(); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 487 | fd_t fd = OpenFile("/proc/self/statm", RdOnly); |
| 488 | if (fd == kInvalidFd) |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 489 | return GetRSSFromGetrusage(); |
| 490 | char buf[64]; |
| 491 | uptr len = internal_read(fd, buf, sizeof(buf) - 1); |
| 492 | internal_close(fd); |
| 493 | if ((sptr)len <= 0) |
| 494 | return 0; |
| 495 | buf[len] = 0; |
| 496 | // The format of the file is: |
| 497 | // 1084 89 69 11 0 79 0 |
| 498 | // We need the second number which is RSS in pages. |
| 499 | char *pos = buf; |
| 500 | // Skip the first number. |
| 501 | while (*pos >= '0' && *pos <= '9') |
| 502 | pos++; |
| 503 | // Skip whitespaces. |
| 504 | while (!(*pos >= '0' && *pos <= '9') && *pos != 0) |
| 505 | pos++; |
| 506 | // Read the number. |
| 507 | uptr rss = 0; |
| 508 | while (*pos >= '0' && *pos <= '9') |
| 509 | rss = rss * 10 + *pos++ - '0'; |
| 510 | return rss * GetPageSizeCached(); |
| 511 | } |
| 512 | |
Peter Collingbourne | 088ea2b | 2013-05-20 15:57:44 +0000 | [diff] [blame] | 513 | } // namespace __sanitizer |
| 514 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 515 | #endif // SANITIZER_FREEBSD || SANITIZER_LINUX |