1 //===-- sanitizer_linux_libcdep.cc ----------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements linux-specific functions from
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()
32 #include <sys/resource.h>
36 #include <pthread_np.h>
37 #include <osreldate.h>
38 #define pthread_getattr_np pthread_attr_get_np
42 #include <sys/prctl.h>
46 #include <android/api-level.h>
49 #if SANITIZER_ANDROID && __ANDROID_API__ < 21
50 #include <android/log.h>
53 #if !SANITIZER_ANDROID
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
) {
66 return real_sigaction(signum
, act
, oldact
);
68 return sigaction(signum
, (const struct sigaction
*)act
,
69 (struct sigaction
*)oldact
);
72 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
76 if (at_initialization
) {
77 // This is the main thread. Libpthread may not be initialized yet.
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
;
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
;
105 pthread_attr_init(&attr
);
106 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr
), 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
;
117 bool SetEnv(const char *name
, const char *value
) {
118 void *f
= dlsym(RTLD_NEXT
, "setenv");
121 typedef int(*setenv_ft
)(const char *name
, const char *value
, int overwrite
);
123 CHECK_EQ(sizeof(setenv_f
), sizeof(f
));
124 internal_memcpy(&setenv_f
, &f
, sizeof(f
));
125 return setenv_f(name
, value
, 1) == 0;
129 bool SanitizerSetThreadName(const char *name
) {
131 return 0 == prctl(PR_SET_NAME
, (unsigned long)name
, 0, 0, 0); // NOLINT
137 bool SanitizerGetThreadName(char *name
, int max_len
) {
140 if (prctl(PR_GET_NAME
, (unsigned long)buff
, 0, 0, 0)) // NOLINT
142 internal_strncpy(name
, buff
, max_len
);
150 #ifndef __GLIBC_PREREQ
151 #define __GLIBC_PREREQ(x, y) 0
154 #if !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO && \
156 static uptr g_tls_size
;
159 // all current supported platforms have 16 bytes stack alignment
160 const size_t kStackAlign
= 16;
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
);
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
);
193 void InitTlsSize() { }
194 #endif // !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO &&
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
);
207 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
208 #ifdef _CS_GNU_LIBC_VERSION
210 uptr len
= confstr(_CS_GNU_LIBC_VERSION
, buf
, sizeof(buf
));
211 if (len
< sizeof(buf
) && internal_strncmp(buf
, "glibc 2.", 8) == 0) {
213 int minor
= internal_simple_strtoll(buf
+ 8, &end
, 10);
214 if (end
!= buf
+ 8 && (*end
== '\0' || *end
== '.' || *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. */
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;
227 val
= FIRST_32_SECOND_64(1104, 1696);
229 val
= FIRST_32_SECOND_64(1120, 1728);
231 val
= FIRST_32_SECOND_64(1136, 1728);
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
<= 14)
239 val
= FIRST_32_SECOND_64(1168, 2304);
241 val
= FIRST_32_SECOND_64(1216, 2304);
244 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
248 #elif defined(__mips__)
249 // TODO(sagarthakur): add more values as per different glibc versions.
250 val
= FIRST_32_SECOND_64(1152, 1776);
252 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
254 #elif defined(__aarch64__)
255 // The sizeof (struct pthread) is the same from GLIBC 2.17 to 2.22.
257 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
259 #elif defined(__powerpc64__)
260 val
= 1776; // from glibc.ppc64le 2.20-8.fc21
261 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
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
);
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)
286 const uptr kTlsAlign
= 16;
287 const uptr kTlsPreTcbSize
=
288 RoundUpTo(ThreadDescriptorSize() + kTcbHead
, kTlsAlign
);
289 return kTlsPreTcbSize
;
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;
306 asm volatile(".set push;\
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;
323 asm("addi %0,13,%1" : "=r"(thread_pointer
) : "I"(-kTlsTcbOffset
));
324 descr_addr
= thread_pointer
- TlsPreTcbSize();
326 # error "unsupported CPU arch"
330 #endif // (x86_64 || i386 || MIPS) && SANITIZER_LINUX
332 #if SANITIZER_FREEBSD
333 static void **ThreadSelfSegbase() {
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
));
342 # error "unsupported CPU arch for FreeBSD platform"
348 return (uptr
)ThreadSelfSegbase()[2];
350 #elif SANITIZER_NETBSD
351 uptr
ThreadSelf() { return (uptr
)pthread_self(); }
352 #endif // SANITIZER_NETBSD
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();
361 *addr
+= ThreadDescriptorSize();
362 # elif defined(__mips__) || defined(__aarch64__) || defined(__powerpc64__) \
364 *addr
= ThreadSelf();
365 *size
= GetTlsSize();
370 #elif SANITIZER_FREEBSD
371 void** segbase
= ThreadSelfSegbase();
376 // tls_size = round(tls_static_space, tcbalign);
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
394 #if SANITIZER_FREEBSD || SANITIZER_ANDROID || SANITIZER_NETBSD
396 GetTls(&addr
, &size
);
398 #elif defined(__mips__) || defined(__powerpc64__)
399 return RoundUpTo(g_tls_size
+ TlsPreTcbSize(), 16);
406 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
407 uptr
*tls_addr
, uptr
*tls_size
) {
409 // Stub implementation for Go.
410 *stk_addr
= *stk_size
= *tls_addr
= *tls_size
= 0;
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
;
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
;
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))
439 struct DlIteratePhdrData
{
440 InternalMmapVectorNoCtor
<LoadedModule
> *modules
;
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
);
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')
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
,
469 data
->modules
->push_back(cur_module
);
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 *);
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
;
489 static void procmapsInit(InternalMmapVectorNoCtor
<LoadedModule
> *modules
) {
490 MemoryMappingLayout
memory_mapping(/*cache_enabled*/true);
491 memory_mapping
.DumpListOfModules(modules
);
494 void ListOfModules::init() {
496 if (requiresProcmaps()) {
497 procmapsInit(&modules_
);
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()) {
509 procmapsInit(&modules_
);
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() {
520 if (getrusage(RUSAGE_SELF
, &usage
)) // Failed, probably due to a sandbox.
522 return usage
.ru_maxrss
<< 10; // ru_maxrss is in Kb.
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();
532 uptr len
= internal_read(fd
, buf
, sizeof(buf
) - 1);
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.
541 // Skip the first number.
542 while (*pos
>= '0' && *pos
<= '9')
545 while (!(*pos
>= '0' && *pos
<= '9') && *pos
!= 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.
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
);
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
);
580 syslog(LOG_INFO
, "%s", s
);
584 void LogMessageOnPrintf(const char *str
) {
585 if (common_flags()->log_to_syslog
&& ShouldLogAfterPrintf())
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
);
595 void SetAbortMessage(const char *str
) {}
598 #endif // SANITIZER_LINUX
600 } // namespace __sanitizer
602 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX