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