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 #if !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO && \
152 static uptr g_tls_size
;
155 # define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
157 # define DL_INTERNAL_FUNCTION
161 // all current supported platforms have 16 bytes stack alignment
162 const size_t kStackAlign
= 16;
163 typedef void (*get_tls_func
)(size_t*, size_t*) DL_INTERNAL_FUNCTION
;
164 get_tls_func get_tls
;
165 void *get_tls_static_info_ptr
= dlsym(RTLD_NEXT
, "_dl_get_tls_static_info");
166 CHECK_EQ(sizeof(get_tls
), sizeof(get_tls_static_info_ptr
));
167 internal_memcpy(&get_tls
, &get_tls_static_info_ptr
,
168 sizeof(get_tls_static_info_ptr
));
169 CHECK_NE(get_tls
, 0);
171 size_t tls_align
= 0;
172 get_tls(&tls_size
, &tls_align
);
173 if (tls_align
< kStackAlign
)
174 tls_align
= kStackAlign
;
175 g_tls_size
= RoundUpTo(tls_size
, tls_align
);
178 void InitTlsSize() { }
179 #endif // !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO &&
182 #if (defined(__x86_64__) || defined(__i386__) || defined(__mips__) \
183 || defined(__aarch64__) || defined(__powerpc64__) || defined(__s390__) \
184 || defined(__arm__)) && SANITIZER_LINUX && !SANITIZER_ANDROID
185 // sizeof(struct pthread) from glibc.
186 static atomic_uintptr_t kThreadDescriptorSize
;
188 uptr
ThreadDescriptorSize() {
189 uptr val
= atomic_load(&kThreadDescriptorSize
, memory_order_relaxed
);
192 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
193 #ifdef _CS_GNU_LIBC_VERSION
195 uptr len
= confstr(_CS_GNU_LIBC_VERSION
, buf
, sizeof(buf
));
196 if (len
< sizeof(buf
) && internal_strncmp(buf
, "glibc 2.", 8) == 0) {
198 int minor
= internal_simple_strtoll(buf
+ 8, &end
, 10);
199 if (end
!= buf
+ 8 && (*end
== '\0' || *end
== '.' || *end
== '-')) {
202 // strtoll will return 0 if no valid conversion could be performed
203 patch
= internal_simple_strtoll(end
+ 1, nullptr, 10);
205 /* sizeof(struct pthread) values from various glibc versions. */
207 val
= 1728; // Assume only one particular version for x32.
208 // For ARM sizeof(struct pthread) changed in Glibc 2.23.
209 else if (SANITIZER_ARM
)
210 val
= minor
<= 22 ? 1120 : 1216;
212 val
= FIRST_32_SECOND_64(1104, 1696);
214 val
= FIRST_32_SECOND_64(1120, 1728);
216 val
= FIRST_32_SECOND_64(1136, 1728);
218 val
= FIRST_32_SECOND_64(1136, 1712);
219 else if (minor
== 10)
220 val
= FIRST_32_SECOND_64(1168, 1776);
221 else if (minor
== 11 || (minor
== 12 && patch
== 1))
222 val
= FIRST_32_SECOND_64(1168, 2288);
223 else if (minor
<= 13)
224 val
= FIRST_32_SECOND_64(1168, 2304);
226 val
= FIRST_32_SECOND_64(1216, 2304);
229 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
233 #elif defined(__mips__)
234 // TODO(sagarthakur): add more values as per different glibc versions.
235 val
= FIRST_32_SECOND_64(1152, 1776);
237 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
239 #elif defined(__aarch64__)
240 // The sizeof (struct pthread) is the same from GLIBC 2.17 to 2.22.
242 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
244 #elif defined(__powerpc64__)
245 val
= 1776; // from glibc.ppc64le 2.20-8.fc21
246 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
248 #elif defined(__s390__)
249 val
= FIRST_32_SECOND_64(1152, 1776); // valid for glibc 2.22
250 atomic_store(&kThreadDescriptorSize
, val
, memory_order_relaxed
);
255 // The offset at which pointer to self is located in the thread descriptor.
256 const uptr kThreadSelfOffset
= FIRST_32_SECOND_64(8, 16);
258 uptr
ThreadSelfOffset() {
259 return kThreadSelfOffset
;
262 #if defined(__mips__) || defined(__powerpc64__)
263 // TlsPreTcbSize includes size of struct pthread_descr and size of tcb
264 // head structure. It lies before the static tls blocks.
265 static uptr
TlsPreTcbSize() {
266 # if defined(__mips__)
267 const uptr kTcbHead
= 16; // sizeof (tcbhead_t)
268 # elif defined(__powerpc64__)
269 const uptr kTcbHead
= 88; // sizeof (tcbhead_t)
271 const uptr kTlsAlign
= 16;
272 const uptr kTlsPreTcbSize
=
273 RoundUpTo(ThreadDescriptorSize() + kTcbHead
, kTlsAlign
);
274 return kTlsPreTcbSize
;
280 # if defined(__i386__)
281 asm("mov %%gs:%c1,%0" : "=r"(descr_addr
) : "i"(kThreadSelfOffset
));
282 # elif defined(__x86_64__)
283 asm("mov %%fs:%c1,%0" : "=r"(descr_addr
) : "i"(kThreadSelfOffset
));
284 # elif defined(__mips__)
285 // MIPS uses TLS variant I. The thread pointer (in hardware register $29)
286 // points to the end of the TCB + 0x7000. The pthread_descr structure is
287 // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
288 // TCB and the size of pthread_descr.
289 const uptr kTlsTcbOffset
= 0x7000;
291 asm volatile(".set push;\
294 .set pop" : "=r" (thread_pointer
));
295 descr_addr
= thread_pointer
- kTlsTcbOffset
- TlsPreTcbSize();
296 # elif defined(__aarch64__) || defined(__arm__)
297 descr_addr
= reinterpret_cast<uptr
>(__builtin_thread_pointer()) -
298 ThreadDescriptorSize();
299 # elif defined(__s390__)
300 descr_addr
= reinterpret_cast<uptr
>(__builtin_thread_pointer());
301 # elif defined(__powerpc64__)
302 // PPC64LE uses TLS variant I. The thread pointer (in GPR 13)
303 // points to the end of the TCB + 0x7000. The pthread_descr structure is
304 // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
305 // TCB and the size of pthread_descr.
306 const uptr kTlsTcbOffset
= 0x7000;
308 asm("addi %0,13,%1" : "=r"(thread_pointer
) : "I"(-kTlsTcbOffset
));
309 descr_addr
= thread_pointer
- TlsPreTcbSize();
311 # error "unsupported CPU arch"
315 #endif // (x86_64 || i386 || MIPS) && SANITIZER_LINUX
317 #if SANITIZER_FREEBSD
318 static void **ThreadSelfSegbase() {
320 # if defined(__i386__)
321 // sysarch(I386_GET_GSBASE, segbase);
322 __asm
__volatile("mov %%gs:0, %0" : "=r" (segbase
));
323 # elif defined(__x86_64__)
324 // sysarch(AMD64_GET_FSBASE, segbase);
325 __asm
__volatile("movq %%fs:0, %0" : "=r" (segbase
));
327 # error "unsupported CPU arch for FreeBSD platform"
333 return (uptr
)ThreadSelfSegbase()[2];
335 #elif SANITIZER_NETBSD
336 uptr
ThreadSelf() { return (uptr
)pthread_self(); }
337 #endif // SANITIZER_NETBSD
340 static void GetTls(uptr
*addr
, uptr
*size
) {
341 #if SANITIZER_LINUX && !SANITIZER_ANDROID
342 # if defined(__x86_64__) || defined(__i386__) || defined(__s390__)
343 *addr
= ThreadSelf();
344 *size
= GetTlsSize();
346 *addr
+= ThreadDescriptorSize();
347 # elif defined(__mips__) || defined(__aarch64__) || defined(__powerpc64__) \
349 *addr
= ThreadSelf();
350 *size
= GetTlsSize();
355 #elif SANITIZER_FREEBSD
356 void** segbase
= ThreadSelfSegbase();
361 // tls_size = round(tls_static_space, tcbalign);
363 // dtv[2] = segbase - tls_static_space;
364 void **dtv
= (void**) segbase
[1];
365 *addr
= (uptr
) dtv
[2];
366 *size
= (*addr
== 0) ? 0 : ((uptr
) segbase
[0] - (uptr
) dtv
[2]);
368 #elif SANITIZER_ANDROID || SANITIZER_NETBSD
379 #if SANITIZER_FREEBSD || SANITIZER_ANDROID || SANITIZER_NETBSD
381 GetTls(&addr
, &size
);
383 #elif defined(__mips__) || defined(__powerpc64__)
384 return RoundUpTo(g_tls_size
+ TlsPreTcbSize(), 16);
391 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
392 uptr
*tls_addr
, uptr
*tls_size
) {
394 // Stub implementation for Go.
395 *stk_addr
= *stk_size
= *tls_addr
= *tls_size
= 0;
397 GetTls(tls_addr
, tls_size
);
399 uptr stack_top
, stack_bottom
;
400 GetThreadStackTopAndBottom(main
, &stack_top
, &stack_bottom
);
401 *stk_addr
= stack_bottom
;
402 *stk_size
= stack_top
- stack_bottom
;
405 // If stack and tls intersect, make them non-intersecting.
406 if (*tls_addr
> *stk_addr
&& *tls_addr
< *stk_addr
+ *stk_size
) {
407 CHECK_GT(*tls_addr
+ *tls_size
, *stk_addr
);
408 CHECK_LE(*tls_addr
+ *tls_size
, *stk_addr
+ *stk_size
);
409 *stk_size
-= *tls_size
;
410 *tls_addr
= *stk_addr
+ *stk_size
;
416 # if !SANITIZER_FREEBSD
417 typedef ElfW(Phdr
) Elf_Phdr
;
418 # elif SANITIZER_WORDSIZE == 32 && __FreeBSD_version <= 902001 // v9.2
419 # define Elf_Phdr XElf32_Phdr
420 # define dl_phdr_info xdl_phdr_info
421 # define dl_iterate_phdr(c, b) xdl_iterate_phdr((c), (b))
424 struct DlIteratePhdrData
{
425 InternalMmapVectorNoCtor
<LoadedModule
> *modules
;
429 static int dl_iterate_phdr_cb(dl_phdr_info
*info
, size_t size
, void *arg
) {
430 DlIteratePhdrData
*data
= (DlIteratePhdrData
*)arg
;
431 InternalScopedString
module_name(kMaxPathLength
);
434 // First module is the binary itself.
435 ReadBinaryNameCached(module_name
.data(), module_name
.size());
436 } else if (info
->dlpi_name
) {
437 module_name
.append("%s", info
->dlpi_name
);
439 if (module_name
[0] == '\0')
441 LoadedModule cur_module
;
442 cur_module
.set(module_name
.data(), info
->dlpi_addr
);
443 for (int i
= 0; i
< info
->dlpi_phnum
; i
++) {
444 const Elf_Phdr
*phdr
= &info
->dlpi_phdr
[i
];
445 if (phdr
->p_type
== PT_LOAD
) {
446 uptr cur_beg
= info
->dlpi_addr
+ phdr
->p_vaddr
;
447 uptr cur_end
= cur_beg
+ phdr
->p_memsz
;
448 bool executable
= phdr
->p_flags
& PF_X
;
449 bool writable
= phdr
->p_flags
& PF_W
;
450 cur_module
.addAddressRange(cur_beg
, cur_end
, executable
,
454 data
->modules
->push_back(cur_module
);
458 #if SANITIZER_ANDROID && __ANDROID_API__ < 21
459 extern "C" __attribute__((weak
)) int dl_iterate_phdr(
460 int (*)(struct dl_phdr_info
*, size_t, void *), void *);
463 static bool requiresProcmaps() {
464 #if SANITIZER_ANDROID && __ANDROID_API__ <= 22
465 // Fall back to /proc/maps if dl_iterate_phdr is unavailable or broken.
466 // The runtime check allows the same library to work with
467 // both K and L (and future) Android releases.
468 return AndroidGetApiLevel() <= ANDROID_LOLLIPOP_MR1
;
474 static void procmapsInit(InternalMmapVectorNoCtor
<LoadedModule
> *modules
) {
475 MemoryMappingLayout
memory_mapping(/*cache_enabled*/true);
476 memory_mapping
.DumpListOfModules(modules
);
479 void ListOfModules::init() {
481 if (requiresProcmaps()) {
482 procmapsInit(&modules_
);
484 DlIteratePhdrData data
= {&modules_
, true};
485 dl_iterate_phdr(dl_iterate_phdr_cb
, &data
);
489 // When a custom loader is used, dl_iterate_phdr may not contain the full
490 // list of modules. Allow callers to fall back to using procmaps.
491 void ListOfModules::fallbackInit() {
492 if (!requiresProcmaps()) {
494 procmapsInit(&modules_
);
500 // getrusage does not give us the current RSS, only the max RSS.
501 // Still, this is better than nothing if /proc/self/statm is not available
502 // for some reason, e.g. due to a sandbox.
503 static uptr
GetRSSFromGetrusage() {
505 if (getrusage(RUSAGE_SELF
, &usage
)) // Failed, probably due to a sandbox.
507 return usage
.ru_maxrss
<< 10; // ru_maxrss is in Kb.
511 if (!common_flags()->can_use_proc_maps_statm
)
512 return GetRSSFromGetrusage();
513 fd_t fd
= OpenFile("/proc/self/statm", RdOnly
);
514 if (fd
== kInvalidFd
)
515 return GetRSSFromGetrusage();
517 uptr len
= internal_read(fd
, buf
, sizeof(buf
) - 1);
522 // The format of the file is:
523 // 1084 89 69 11 0 79 0
524 // We need the second number which is RSS in pages.
526 // Skip the first number.
527 while (*pos
>= '0' && *pos
<= '9')
530 while (!(*pos
>= '0' && *pos
<= '9') && *pos
!= 0)
534 while (*pos
>= '0' && *pos
<= '9')
535 rss
= rss
* 10 + *pos
++ - '0';
536 return rss
* GetPageSizeCached();
539 // 64-bit Android targets don't provide the deprecated __android_log_write.
540 // Starting with the L release, syslog() works and is preferable to
541 // __android_log_write.
544 #if SANITIZER_ANDROID
545 static atomic_uint8_t android_log_initialized
;
547 void AndroidLogInit() {
548 openlog(GetProcessName(), 0, LOG_USER
);
549 atomic_store(&android_log_initialized
, 1, memory_order_release
);
552 static bool ShouldLogAfterPrintf() {
553 return atomic_load(&android_log_initialized
, memory_order_acquire
);
556 void AndroidLogInit() {}
558 static bool ShouldLogAfterPrintf() { return true; }
559 #endif // SANITIZER_ANDROID
561 void WriteOneLineToSyslog(const char *s
) {
562 #if SANITIZER_ANDROID &&__ANDROID_API__ < 21
563 __android_log_write(ANDROID_LOG_INFO
, NULL
, s
);
565 syslog(LOG_INFO
, "%s", s
);
569 void LogMessageOnPrintf(const char *str
) {
570 if (common_flags()->log_to_syslog
&& ShouldLogAfterPrintf())
574 #if SANITIZER_ANDROID
575 extern "C" __attribute__((weak
)) void android_set_abort_message(const char *);
576 void SetAbortMessage(const char *str
) {
577 if (&android_set_abort_message
) android_set_abort_message(str
);
580 void SetAbortMessage(const char *str
) {}
583 #endif // SANITIZER_LINUX
585 } // namespace __sanitizer
587 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX