* lto.c (do_stream_out): Add PART parameter; open dump file.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_linux_libcdep.cc
blobd27a84358024c120ea24a2f6535fea939fbaec95
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"
15 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
17 #include "sanitizer_allocator_internal.h"
18 #include "sanitizer_atomic.h"
19 #include "sanitizer_common.h"
20 #include "sanitizer_file.h"
21 #include "sanitizer_flags.h"
22 #include "sanitizer_freebsd.h"
23 #include "sanitizer_linux.h"
24 #include "sanitizer_placement_new.h"
25 #include "sanitizer_procmaps.h"
26 #include "sanitizer_stacktrace.h"
28 #include <dlfcn.h> // for dlsym()
29 #include <link.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <sys/resource.h>
33 #include <syslog.h>
35 #if SANITIZER_FREEBSD
36 #include <pthread_np.h>
37 #include <osreldate.h>
38 #define pthread_getattr_np pthread_attr_get_np
39 #endif
41 #if SANITIZER_LINUX
42 #include <sys/prctl.h>
43 #endif
45 #if SANITIZER_ANDROID
46 #include <android/api-level.h>
47 #endif
49 #if SANITIZER_ANDROID && __ANDROID_API__ < 21
50 #include <android/log.h>
51 #endif
53 #if !SANITIZER_ANDROID
54 #include <elf.h>
55 #include <unistd.h>
56 #endif
58 namespace __sanitizer {
60 SANITIZER_WEAK_ATTRIBUTE int
61 real_sigaction(int signum, const void *act, void *oldact);
63 int internal_sigaction(int signum, const void *act, void *oldact) {
64 #if !SANITIZER_GO
65 if (&real_sigaction)
66 return real_sigaction(signum, act, oldact);
67 #endif
68 return sigaction(signum, (const struct sigaction *)act,
69 (struct sigaction *)oldact);
72 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
73 uptr *stack_bottom) {
74 CHECK(stack_top);
75 CHECK(stack_bottom);
76 if (at_initialization) {
77 // This is the main thread. Libpthread may not be initialized yet.
78 struct rlimit rl;
79 CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
81 // Find the mapping that contains a stack variable.
82 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
83 MemoryMappedSegment segment;
84 uptr prev_end = 0;
85 while (proc_maps.Next(&segment)) {
86 if ((uptr)&rl < segment.end) break;
87 prev_end = segment.end;
89 CHECK((uptr)&rl >= segment.start && (uptr)&rl < segment.end);
91 // Get stacksize from rlimit, but clip it so that it does not overlap
92 // with other mappings.
93 uptr stacksize = rl.rlim_cur;
94 if (stacksize > segment.end - prev_end) stacksize = segment.end - prev_end;
95 // When running with unlimited stack size, we still want to set some limit.
96 // The unlimited stack size is caused by 'ulimit -s unlimited'.
97 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
98 if (stacksize > kMaxThreadStackSize)
99 stacksize = kMaxThreadStackSize;
100 *stack_top = segment.end;
101 *stack_bottom = segment.end - stacksize;
102 return;
104 pthread_attr_t attr;
105 pthread_attr_init(&attr);
106 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
107 uptr stacksize = 0;
108 void *stackaddr = nullptr;
109 my_pthread_attr_getstack(&attr, &stackaddr, &stacksize);
110 pthread_attr_destroy(&attr);
112 *stack_top = (uptr)stackaddr + stacksize;
113 *stack_bottom = (uptr)stackaddr;
116 #if !SANITIZER_GO
117 bool SetEnv(const char *name, const char *value) {
118 void *f = dlsym(RTLD_NEXT, "setenv");
119 if (!f)
120 return false;
121 typedef int(*setenv_ft)(const char *name, const char *value, int overwrite);
122 setenv_ft setenv_f;
123 CHECK_EQ(sizeof(setenv_f), sizeof(f));
124 internal_memcpy(&setenv_f, &f, sizeof(f));
125 return setenv_f(name, value, 1) == 0;
127 #endif
129 bool SanitizerSetThreadName(const char *name) {
130 #ifdef PR_SET_NAME
131 return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); // NOLINT
132 #else
133 return false;
134 #endif
137 bool SanitizerGetThreadName(char *name, int max_len) {
138 #ifdef PR_GET_NAME
139 char buff[17];
140 if (prctl(PR_GET_NAME, (unsigned long)buff, 0, 0, 0)) // NOLINT
141 return false;
142 internal_strncpy(name, buff, max_len);
143 name[max_len] = 0;
144 return true;
145 #else
146 return false;
147 #endif
150 #ifndef __GLIBC_PREREQ
151 #define __GLIBC_PREREQ(x, y) 0
152 #endif
154 #if !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO && \
155 !SANITIZER_NETBSD
156 static uptr g_tls_size;
158 void InitTlsSize() {
159 // all current supported platforms have 16 bytes stack alignment
160 const size_t kStackAlign = 16;
161 size_t tls_size = 0;
162 size_t tls_align = 0;
163 void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info");
164 #if defined(__i386__) && !__GLIBC_PREREQ(2, 27)
165 /* On i?86, _dl_get_tls_static_info used to be internal_function, i.e.
166 __attribute__((regparm(3), stdcall)) before glibc 2.27 and is normal
167 function in 2.27 and later. */
168 if (!dlvsym(RTLD_NEXT, "glob", "GLIBC_2.27")) {
169 typedef void (*get_tls_func)(size_t*, size_t*)
170 __attribute__((regparm(3), stdcall));
171 get_tls_func get_tls;
172 CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr));
173 internal_memcpy(&get_tls, &get_tls_static_info_ptr,
174 sizeof(get_tls_static_info_ptr));
175 CHECK_NE(get_tls, 0);
176 get_tls(&tls_size, &tls_align);
177 } else
178 #endif
180 typedef void (*get_tls_func)(size_t*, size_t*);
181 get_tls_func get_tls;
182 CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr));
183 internal_memcpy(&get_tls, &get_tls_static_info_ptr,
184 sizeof(get_tls_static_info_ptr));
185 CHECK_NE(get_tls, 0);
186 get_tls(&tls_size, &tls_align);
188 if (tls_align < kStackAlign)
189 tls_align = kStackAlign;
190 g_tls_size = RoundUpTo(tls_size, tls_align);
192 #else
193 void InitTlsSize() { }
194 #endif // !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO &&
195 // !SANITIZER_NETBSD
197 #if (defined(__x86_64__) || defined(__i386__) || defined(__mips__) \
198 || defined(__aarch64__) || defined(__powerpc64__) || defined(__s390__) \
199 || defined(__arm__)) && SANITIZER_LINUX && !SANITIZER_ANDROID
200 // sizeof(struct pthread) from glibc.
201 static atomic_uintptr_t kThreadDescriptorSize;
203 uptr ThreadDescriptorSize() {
204 uptr val = atomic_load(&kThreadDescriptorSize, memory_order_relaxed);
205 if (val)
206 return val;
207 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
208 #ifdef _CS_GNU_LIBC_VERSION
209 char buf[64];
210 uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
211 if (len < sizeof(buf) && internal_strncmp(buf, "glibc 2.", 8) == 0) {
212 char *end;
213 int minor = internal_simple_strtoll(buf + 8, &end, 10);
214 if (end != buf + 8 && (*end == '\0' || *end == '.' || *end == '-')) {
215 int patch = 0;
216 if (*end == '.')
217 // strtoll will return 0 if no valid conversion could be performed
218 patch = internal_simple_strtoll(end + 1, nullptr, 10);
220 /* sizeof(struct pthread) values from various glibc versions. */
221 if (SANITIZER_X32)
222 val = 1728; // Assume only one particular version for x32.
223 // For ARM sizeof(struct pthread) changed in Glibc 2.23.
224 else if (SANITIZER_ARM)
225 val = minor <= 22 ? 1120 : 1216;
226 else if (minor <= 3)
227 val = FIRST_32_SECOND_64(1104, 1696);
228 else if (minor == 4)
229 val = FIRST_32_SECOND_64(1120, 1728);
230 else if (minor == 5)
231 val = FIRST_32_SECOND_64(1136, 1728);
232 else if (minor <= 9)
233 val = FIRST_32_SECOND_64(1136, 1712);
234 else if (minor == 10)
235 val = FIRST_32_SECOND_64(1168, 1776);
236 else if (minor == 11 || (minor == 12 && patch == 1))
237 val = FIRST_32_SECOND_64(1168, 2288);
238 else if (minor <= 13)
239 val = FIRST_32_SECOND_64(1168, 2304);
240 else
241 val = FIRST_32_SECOND_64(1216, 2304);
243 if (val)
244 atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
245 return val;
247 #endif
248 #elif defined(__mips__)
249 // TODO(sagarthakur): add more values as per different glibc versions.
250 val = FIRST_32_SECOND_64(1152, 1776);
251 if (val)
252 atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
253 return val;
254 #elif defined(__aarch64__)
255 // The sizeof (struct pthread) is the same from GLIBC 2.17 to 2.22.
256 val = 1776;
257 atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
258 return val;
259 #elif defined(__powerpc64__)
260 val = 1776; // from glibc.ppc64le 2.20-8.fc21
261 atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
262 return val;
263 #elif defined(__s390__)
264 val = FIRST_32_SECOND_64(1152, 1776); // valid for glibc 2.22
265 atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
266 #endif
267 return 0;
270 // The offset at which pointer to self is located in the thread descriptor.
271 const uptr kThreadSelfOffset = FIRST_32_SECOND_64(8, 16);
273 uptr ThreadSelfOffset() {
274 return kThreadSelfOffset;
277 #if defined(__mips__) || defined(__powerpc64__)
278 // TlsPreTcbSize includes size of struct pthread_descr and size of tcb
279 // head structure. It lies before the static tls blocks.
280 static uptr TlsPreTcbSize() {
281 # if defined(__mips__)
282 const uptr kTcbHead = 16; // sizeof (tcbhead_t)
283 # elif defined(__powerpc64__)
284 const uptr kTcbHead = 88; // sizeof (tcbhead_t)
285 # endif
286 const uptr kTlsAlign = 16;
287 const uptr kTlsPreTcbSize =
288 RoundUpTo(ThreadDescriptorSize() + kTcbHead, kTlsAlign);
289 return kTlsPreTcbSize;
291 #endif
293 uptr ThreadSelf() {
294 uptr descr_addr;
295 # if defined(__i386__)
296 asm("mov %%gs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
297 # elif defined(__x86_64__)
298 asm("mov %%fs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
299 # elif defined(__mips__)
300 // MIPS uses TLS variant I. The thread pointer (in hardware register $29)
301 // points to the end of the TCB + 0x7000. The pthread_descr structure is
302 // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
303 // TCB and the size of pthread_descr.
304 const uptr kTlsTcbOffset = 0x7000;
305 uptr thread_pointer;
306 asm volatile(".set push;\
307 .set mips64r2;\
308 rdhwr %0,$29;\
309 .set pop" : "=r" (thread_pointer));
310 descr_addr = thread_pointer - kTlsTcbOffset - TlsPreTcbSize();
311 # elif defined(__aarch64__) || defined(__arm__)
312 descr_addr = reinterpret_cast<uptr>(__builtin_thread_pointer()) -
313 ThreadDescriptorSize();
314 # elif defined(__s390__)
315 descr_addr = reinterpret_cast<uptr>(__builtin_thread_pointer());
316 # elif defined(__powerpc64__)
317 // PPC64LE uses TLS variant I. The thread pointer (in GPR 13)
318 // points to the end of the TCB + 0x7000. The pthread_descr structure is
319 // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
320 // TCB and the size of pthread_descr.
321 const uptr kTlsTcbOffset = 0x7000;
322 uptr thread_pointer;
323 asm("addi %0,13,%1" : "=r"(thread_pointer) : "I"(-kTlsTcbOffset));
324 descr_addr = thread_pointer - TlsPreTcbSize();
325 # else
326 # error "unsupported CPU arch"
327 # endif
328 return descr_addr;
330 #endif // (x86_64 || i386 || MIPS) && SANITIZER_LINUX
332 #if SANITIZER_FREEBSD
333 static void **ThreadSelfSegbase() {
334 void **segbase = 0;
335 # if defined(__i386__)
336 // sysarch(I386_GET_GSBASE, segbase);
337 __asm __volatile("mov %%gs:0, %0" : "=r" (segbase));
338 # elif defined(__x86_64__)
339 // sysarch(AMD64_GET_FSBASE, segbase);
340 __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
341 # else
342 # error "unsupported CPU arch for FreeBSD platform"
343 # endif
344 return segbase;
347 uptr ThreadSelf() {
348 return (uptr)ThreadSelfSegbase()[2];
350 #elif SANITIZER_NETBSD
351 uptr ThreadSelf() { return (uptr)pthread_self(); }
352 #endif // SANITIZER_NETBSD
354 #if !SANITIZER_GO
355 static void GetTls(uptr *addr, uptr *size) {
356 #if SANITIZER_LINUX && !SANITIZER_ANDROID
357 # if defined(__x86_64__) || defined(__i386__) || defined(__s390__)
358 *addr = ThreadSelf();
359 *size = GetTlsSize();
360 *addr -= *size;
361 *addr += ThreadDescriptorSize();
362 # elif defined(__mips__) || defined(__aarch64__) || defined(__powerpc64__) \
363 || defined(__arm__)
364 *addr = ThreadSelf();
365 *size = GetTlsSize();
366 # else
367 *addr = 0;
368 *size = 0;
369 # endif
370 #elif SANITIZER_FREEBSD
371 void** segbase = ThreadSelfSegbase();
372 *addr = 0;
373 *size = 0;
374 if (segbase != 0) {
375 // tcbalign = 16
376 // tls_size = round(tls_static_space, tcbalign);
377 // dtv = segbase[1];
378 // dtv[2] = segbase - tls_static_space;
379 void **dtv = (void**) segbase[1];
380 *addr = (uptr) dtv[2];
381 *size = (*addr == 0) ? 0 : ((uptr) segbase[0] - (uptr) dtv[2]);
383 #elif SANITIZER_ANDROID || SANITIZER_NETBSD
384 *addr = 0;
385 *size = 0;
386 #else
387 # error "Unknown OS"
388 #endif
390 #endif
392 #if !SANITIZER_GO
393 uptr GetTlsSize() {
394 #if SANITIZER_FREEBSD || SANITIZER_ANDROID || SANITIZER_NETBSD
395 uptr addr, size;
396 GetTls(&addr, &size);
397 return size;
398 #elif defined(__mips__) || defined(__powerpc64__)
399 return RoundUpTo(g_tls_size + TlsPreTcbSize(), 16);
400 #else
401 return g_tls_size;
402 #endif
404 #endif
406 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
407 uptr *tls_addr, uptr *tls_size) {
408 #if SANITIZER_GO
409 // Stub implementation for Go.
410 *stk_addr = *stk_size = *tls_addr = *tls_size = 0;
411 #else
412 GetTls(tls_addr, tls_size);
414 uptr stack_top, stack_bottom;
415 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
416 *stk_addr = stack_bottom;
417 *stk_size = stack_top - stack_bottom;
419 if (!main) {
420 // If stack and tls intersect, make them non-intersecting.
421 if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) {
422 CHECK_GT(*tls_addr + *tls_size, *stk_addr);
423 CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size);
424 *stk_size -= *tls_size;
425 *tls_addr = *stk_addr + *stk_size;
428 #endif
431 # if !SANITIZER_FREEBSD
432 typedef ElfW(Phdr) Elf_Phdr;
433 # elif SANITIZER_WORDSIZE == 32 && __FreeBSD_version <= 902001 // v9.2
434 # define Elf_Phdr XElf32_Phdr
435 # define dl_phdr_info xdl_phdr_info
436 # define dl_iterate_phdr(c, b) xdl_iterate_phdr((c), (b))
437 # endif
439 struct DlIteratePhdrData {
440 InternalMmapVectorNoCtor<LoadedModule> *modules;
441 bool first;
444 static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
445 DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
446 InternalScopedString module_name(kMaxPathLength);
447 if (data->first) {
448 data->first = false;
449 // First module is the binary itself.
450 ReadBinaryNameCached(module_name.data(), module_name.size());
451 } else if (info->dlpi_name) {
452 module_name.append("%s", info->dlpi_name);
454 if (module_name[0] == '\0')
455 return 0;
456 LoadedModule cur_module;
457 cur_module.set(module_name.data(), info->dlpi_addr);
458 for (int i = 0; i < info->dlpi_phnum; i++) {
459 const Elf_Phdr *phdr = &info->dlpi_phdr[i];
460 if (phdr->p_type == PT_LOAD) {
461 uptr cur_beg = info->dlpi_addr + phdr->p_vaddr;
462 uptr cur_end = cur_beg + phdr->p_memsz;
463 bool executable = phdr->p_flags & PF_X;
464 bool writable = phdr->p_flags & PF_W;
465 cur_module.addAddressRange(cur_beg, cur_end, executable,
466 writable);
469 data->modules->push_back(cur_module);
470 return 0;
473 #if SANITIZER_ANDROID && __ANDROID_API__ < 21
474 extern "C" __attribute__((weak)) int dl_iterate_phdr(
475 int (*)(struct dl_phdr_info *, size_t, void *), void *);
476 #endif
478 static bool requiresProcmaps() {
479 #if SANITIZER_ANDROID && __ANDROID_API__ <= 22
480 // Fall back to /proc/maps if dl_iterate_phdr is unavailable or broken.
481 // The runtime check allows the same library to work with
482 // both K and L (and future) Android releases.
483 return AndroidGetApiLevel() <= ANDROID_LOLLIPOP_MR1;
484 #else
485 return false;
486 #endif
489 static void procmapsInit(InternalMmapVectorNoCtor<LoadedModule> *modules) {
490 MemoryMappingLayout memory_mapping(/*cache_enabled*/true);
491 memory_mapping.DumpListOfModules(modules);
494 void ListOfModules::init() {
495 clearOrInit();
496 if (requiresProcmaps()) {
497 procmapsInit(&modules_);
498 } else {
499 DlIteratePhdrData data = {&modules_, true};
500 dl_iterate_phdr(dl_iterate_phdr_cb, &data);
504 // When a custom loader is used, dl_iterate_phdr may not contain the full
505 // list of modules. Allow callers to fall back to using procmaps.
506 void ListOfModules::fallbackInit() {
507 if (!requiresProcmaps()) {
508 clearOrInit();
509 procmapsInit(&modules_);
510 } else {
511 clear();
515 // getrusage does not give us the current RSS, only the max RSS.
516 // Still, this is better than nothing if /proc/self/statm is not available
517 // for some reason, e.g. due to a sandbox.
518 static uptr GetRSSFromGetrusage() {
519 struct rusage usage;
520 if (getrusage(RUSAGE_SELF, &usage)) // Failed, probably due to a sandbox.
521 return 0;
522 return usage.ru_maxrss << 10; // ru_maxrss is in Kb.
525 uptr GetRSS() {
526 if (!common_flags()->can_use_proc_maps_statm)
527 return GetRSSFromGetrusage();
528 fd_t fd = OpenFile("/proc/self/statm", RdOnly);
529 if (fd == kInvalidFd)
530 return GetRSSFromGetrusage();
531 char buf[64];
532 uptr len = internal_read(fd, buf, sizeof(buf) - 1);
533 internal_close(fd);
534 if ((sptr)len <= 0)
535 return 0;
536 buf[len] = 0;
537 // The format of the file is:
538 // 1084 89 69 11 0 79 0
539 // We need the second number which is RSS in pages.
540 char *pos = buf;
541 // Skip the first number.
542 while (*pos >= '0' && *pos <= '9')
543 pos++;
544 // Skip whitespaces.
545 while (!(*pos >= '0' && *pos <= '9') && *pos != 0)
546 pos++;
547 // Read the number.
548 uptr rss = 0;
549 while (*pos >= '0' && *pos <= '9')
550 rss = rss * 10 + *pos++ - '0';
551 return rss * GetPageSizeCached();
554 // 64-bit Android targets don't provide the deprecated __android_log_write.
555 // Starting with the L release, syslog() works and is preferable to
556 // __android_log_write.
557 #if SANITIZER_LINUX
559 #if SANITIZER_ANDROID
560 static atomic_uint8_t android_log_initialized;
562 void AndroidLogInit() {
563 openlog(GetProcessName(), 0, LOG_USER);
564 atomic_store(&android_log_initialized, 1, memory_order_release);
567 static bool ShouldLogAfterPrintf() {
568 return atomic_load(&android_log_initialized, memory_order_acquire);
570 #else
571 void AndroidLogInit() {}
573 static bool ShouldLogAfterPrintf() { return true; }
574 #endif // SANITIZER_ANDROID
576 void WriteOneLineToSyslog(const char *s) {
577 #if SANITIZER_ANDROID &&__ANDROID_API__ < 21
578 __android_log_write(ANDROID_LOG_INFO, NULL, s);
579 #else
580 syslog(LOG_INFO, "%s", s);
581 #endif
584 void LogMessageOnPrintf(const char *str) {
585 if (common_flags()->log_to_syslog && ShouldLogAfterPrintf())
586 WriteToSyslog(str);
589 #if SANITIZER_ANDROID
590 extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
591 void SetAbortMessage(const char *str) {
592 if (&android_set_abort_message) android_set_abort_message(str);
594 #else
595 void SetAbortMessage(const char *str) {}
596 #endif
598 #endif // SANITIZER_LINUX
600 } // namespace __sanitizer
602 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX