1 //===-- sanitizer_linux_libcdep.cpp ---------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements linux-specific functions from
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
19 #include "sanitizer_allocator_internal.h"
20 #include "sanitizer_atomic.h"
21 #include "sanitizer_common.h"
22 #include "sanitizer_file.h"
23 #include "sanitizer_flags.h"
24 #include "sanitizer_freebsd.h"
25 #include "sanitizer_getauxval.h"
26 #include "sanitizer_glibc_version.h"
27 #include "sanitizer_linux.h"
28 #include "sanitizer_placement_new.h"
29 #include "sanitizer_procmaps.h"
32 #define _RTLD_SOURCE // for __lwp_gettcb_fast() / __lwp_getprivate_fast()
35 #include <dlfcn.h> // for dlsym()
39 #include <sys/resource.h>
43 #define ElfW(type) Elf_##type
47 #include <pthread_np.h>
48 #include <osreldate.h>
49 #include <sys/sysctl.h>
50 #define pthread_getattr_np pthread_attr_get_np
54 #include <sys/sysctl.h>
65 #include <android/api-level.h>
66 #if !defined(CPU_COUNT) && !defined(__aarch64__)
69 struct __sanitizer::linux_dirent
{
72 unsigned short d_reclen
;
78 #if !SANITIZER_ANDROID
83 namespace __sanitizer
{
85 SANITIZER_WEAK_ATTRIBUTE
int
86 real_sigaction(int signum
, const void *act
, void *oldact
);
88 int internal_sigaction(int signum
, const void *act
, void *oldact
) {
91 return real_sigaction(signum
, act
, oldact
);
93 return sigaction(signum
, (const struct sigaction
*)act
,
94 (struct sigaction
*)oldact
);
97 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
101 if (at_initialization
) {
102 // This is the main thread. Libpthread may not be initialized yet.
104 CHECK_EQ(getrlimit(RLIMIT_STACK
, &rl
), 0);
106 // Find the mapping that contains a stack variable.
107 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
108 if (proc_maps
.Error()) {
109 *stack_top
= *stack_bottom
= 0;
112 MemoryMappedSegment segment
;
114 while (proc_maps
.Next(&segment
)) {
115 if ((uptr
)&rl
< segment
.end
) break;
116 prev_end
= segment
.end
;
118 CHECK((uptr
)&rl
>= segment
.start
&& (uptr
)&rl
< segment
.end
);
120 // Get stacksize from rlimit, but clip it so that it does not overlap
121 // with other mappings.
122 uptr stacksize
= rl
.rlim_cur
;
123 if (stacksize
> segment
.end
- prev_end
) stacksize
= segment
.end
- prev_end
;
124 // When running with unlimited stack size, we still want to set some limit.
125 // The unlimited stack size is caused by 'ulimit -s unlimited'.
126 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
127 if (stacksize
> kMaxThreadStackSize
)
128 stacksize
= kMaxThreadStackSize
;
129 *stack_top
= segment
.end
;
130 *stack_bottom
= segment
.end
- stacksize
;
134 void *stackaddr
= nullptr;
135 #if SANITIZER_SOLARIS
137 CHECK_EQ(thr_stksegment(&ss
), 0);
138 stacksize
= ss
.ss_size
;
139 stackaddr
= (char *)ss
.ss_sp
- stacksize
;
140 #else // !SANITIZER_SOLARIS
142 pthread_attr_init(&attr
);
143 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr
), 0);
144 my_pthread_attr_getstack(&attr
, &stackaddr
, &stacksize
);
145 pthread_attr_destroy(&attr
);
146 #endif // SANITIZER_SOLARIS
148 *stack_top
= (uptr
)stackaddr
+ stacksize
;
149 *stack_bottom
= (uptr
)stackaddr
;
153 bool SetEnv(const char *name
, const char *value
) {
154 void *f
= dlsym(RTLD_NEXT
, "setenv");
157 typedef int(*setenv_ft
)(const char *name
, const char *value
, int overwrite
);
159 CHECK_EQ(sizeof(setenv_f
), sizeof(f
));
160 internal_memcpy(&setenv_f
, &f
, sizeof(f
));
161 return setenv_f(name
, value
, 1) == 0;
165 __attribute__((unused
)) static bool GetLibcVersion(int *major
, int *minor
,
167 #ifdef _CS_GNU_LIBC_VERSION
169 uptr len
= confstr(_CS_GNU_LIBC_VERSION
, buf
, sizeof(buf
));
170 if (len
>= sizeof(buf
))
173 static const char kGLibC
[] = "glibc ";
174 if (internal_strncmp(buf
, kGLibC
, sizeof(kGLibC
) - 1) != 0)
176 const char *p
= buf
+ sizeof(kGLibC
) - 1;
177 *major
= internal_simple_strtoll(p
, &p
, 10);
178 *minor
= (*p
== '.') ? internal_simple_strtoll(p
+ 1, &p
, 10) : 0;
179 *patch
= (*p
== '.') ? internal_simple_strtoll(p
+ 1, &p
, 10) : 0;
186 #if !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO && \
187 !SANITIZER_NETBSD && !SANITIZER_SOLARIS
188 static uptr g_tls_size
;
191 #define CHECK_GET_TLS_STATIC_INFO_VERSION (!__GLIBC_PREREQ(2, 27))
193 #define CHECK_GET_TLS_STATIC_INFO_VERSION 0
196 #if CHECK_GET_TLS_STATIC_INFO_VERSION
197 #define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
199 #define DL_INTERNAL_FUNCTION
203 struct GetTlsStaticInfoCall
{
204 typedef void (*get_tls_func
)(size_t*, size_t*);
206 struct GetTlsStaticInfoRegparmCall
{
207 typedef void (*get_tls_func
)(size_t*, size_t*) DL_INTERNAL_FUNCTION
;
210 template <typename T
>
211 void CallGetTls(void* ptr
, size_t* size
, size_t* align
) {
212 typename
T::get_tls_func get_tls
;
213 CHECK_EQ(sizeof(get_tls
), sizeof(ptr
));
214 internal_memcpy(&get_tls
, &ptr
, sizeof(ptr
));
215 CHECK_NE(get_tls
, 0);
216 get_tls(size
, align
);
219 bool CmpLibcVersion(int major
, int minor
, int patch
) {
223 if (!GetLibcVersion(&ma
, &mi
, &pa
))
239 // all current supported platforms have 16 bytes stack alignment
240 const size_t kStackAlign
= 16;
241 void *get_tls_static_info_ptr
= dlsym(RTLD_NEXT
, "_dl_get_tls_static_info");
243 size_t tls_align
= 0;
244 // On i?86, _dl_get_tls_static_info used to be internal_function, i.e.
245 // __attribute__((regparm(3), stdcall)) before glibc 2.27 and is normal
246 // function in 2.27 and later.
247 if (CHECK_GET_TLS_STATIC_INFO_VERSION
&& !CmpLibcVersion(2, 27, 0))
248 CallGetTls
<GetTlsStaticInfoRegparmCall
>(get_tls_static_info_ptr
,
249 &tls_size
, &tls_align
);
251 CallGetTls
<GetTlsStaticInfoCall
>(get_tls_static_info_ptr
,
252 &tls_size
, &tls_align
);
253 if (tls_align
< kStackAlign
)
254 tls_align
= kStackAlign
;
255 g_tls_size
= RoundUpTo(tls_size
, tls_align
);
258 void InitTlsSize() { }
261 #if (defined(__x86_64__) || defined(__i386__) || defined(__mips__) || \
262 defined(__aarch64__) || defined(__powerpc64__) || defined(__s390__) || \
263 defined(__arm__) || SANITIZER_RISCV64) && \
264 SANITIZER_LINUX && !SANITIZER_ANDROID
265 // sizeof(struct pthread) from glibc.
266 static atomic_uintptr_t thread_descriptor_size
;
268 uptr
ThreadDescriptorSize() {
269 uptr val
= atomic_load_relaxed(&thread_descriptor_size
);
272 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
276 if (GetLibcVersion(&major
, &minor
, &patch
) && major
== 2) {
277 /* sizeof(struct pthread) values from various glibc versions. */
279 val
= 1728; // Assume only one particular version for x32.
280 // For ARM sizeof(struct pthread) changed in Glibc 2.23.
281 else if (SANITIZER_ARM
)
282 val
= minor
<= 22 ? 1120 : 1216;
284 val
= FIRST_32_SECOND_64(1104, 1696);
286 val
= FIRST_32_SECOND_64(1120, 1728);
288 val
= FIRST_32_SECOND_64(1136, 1728);
290 val
= FIRST_32_SECOND_64(1136, 1712);
291 else if (minor
== 10)
292 val
= FIRST_32_SECOND_64(1168, 1776);
293 else if (minor
== 11 || (minor
== 12 && patch
== 1))
294 val
= FIRST_32_SECOND_64(1168, 2288);
295 else if (minor
<= 14)
296 val
= FIRST_32_SECOND_64(1168, 2304);
298 val
= FIRST_32_SECOND_64(1216, 2304);
300 #elif defined(__mips__)
301 // TODO(sagarthakur): add more values as per different glibc versions.
302 val
= FIRST_32_SECOND_64(1152, 1776);
303 #elif SANITIZER_RISCV64
307 if (GetLibcVersion(&major
, &minor
, &patch
) && major
== 2) {
308 // TODO: consider adding an optional runtime check for an unknown (untested)
310 if (minor
<= 28) // WARNING: the highest tested version is 2.29
311 val
= 1772; // no guarantees for this one
312 else if (minor
<= 31)
313 val
= 1772; // tested against glibc 2.29, 2.31
315 val
= 1936; // tested against glibc 2.32
318 #elif defined(__aarch64__)
319 // The sizeof (struct pthread) is the same from GLIBC 2.17 to 2.22.
321 #elif defined(__powerpc64__)
322 val
= 1776; // from glibc.ppc64le 2.20-8.fc21
323 #elif defined(__s390__)
324 val
= FIRST_32_SECOND_64(1152, 1776); // valid for glibc 2.22
327 atomic_store_relaxed(&thread_descriptor_size
, val
);
331 // The offset at which pointer to self is located in the thread descriptor.
332 const uptr kThreadSelfOffset
= FIRST_32_SECOND_64(8, 16);
334 uptr
ThreadSelfOffset() {
335 return kThreadSelfOffset
;
338 #if defined(__mips__) || defined(__powerpc64__) || SANITIZER_RISCV64
339 // TlsPreTcbSize includes size of struct pthread_descr and size of tcb
340 // head structure. It lies before the static tls blocks.
341 static uptr
TlsPreTcbSize() {
342 #if defined(__mips__)
343 const uptr kTcbHead
= 16; // sizeof (tcbhead_t)
344 #elif defined(__powerpc64__)
345 const uptr kTcbHead
= 88; // sizeof (tcbhead_t)
346 #elif SANITIZER_RISCV64
347 const uptr kTcbHead
= 16; // sizeof (tcbhead_t)
349 const uptr kTlsAlign
= 16;
350 const uptr kTlsPreTcbSize
=
351 RoundUpTo(ThreadDescriptorSize() + kTcbHead
, kTlsAlign
);
352 return kTlsPreTcbSize
;
358 #if defined(__i386__)
359 asm("mov %%gs:%c1,%0" : "=r"(descr_addr
) : "i"(kThreadSelfOffset
));
360 #elif defined(__x86_64__)
361 asm("mov %%fs:%c1,%0" : "=r"(descr_addr
) : "i"(kThreadSelfOffset
));
362 #elif defined(__mips__)
363 // MIPS uses TLS variant I. The thread pointer (in hardware register $29)
364 // points to the end of the TCB + 0x7000. The pthread_descr structure is
365 // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
366 // TCB and the size of pthread_descr.
367 const uptr kTlsTcbOffset
= 0x7000;
369 asm volatile(".set push;\
372 .set pop" : "=r" (thread_pointer
));
373 descr_addr
= thread_pointer
- kTlsTcbOffset
- TlsPreTcbSize();
374 #elif defined(__aarch64__) || defined(__arm__)
375 descr_addr
= reinterpret_cast<uptr
>(__builtin_thread_pointer()) -
376 ThreadDescriptorSize();
377 #elif SANITIZER_RISCV64
378 // https://github.com/riscv/riscv-elf-psabi-doc/issues/53
379 uptr thread_pointer
= reinterpret_cast<uptr
>(__builtin_thread_pointer());
380 descr_addr
= thread_pointer
- TlsPreTcbSize();
381 #elif defined(__s390__)
382 descr_addr
= reinterpret_cast<uptr
>(__builtin_thread_pointer());
383 #elif defined(__powerpc64__)
384 // PPC64LE uses TLS variant I. The thread pointer (in GPR 13)
385 // points to the end of the TCB + 0x7000. The pthread_descr structure is
386 // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
387 // TCB and the size of pthread_descr.
388 const uptr kTlsTcbOffset
= 0x7000;
390 asm("addi %0,13,%1" : "=r"(thread_pointer
) : "I"(-kTlsTcbOffset
));
391 descr_addr
= thread_pointer
- TlsPreTcbSize();
393 #error "unsupported CPU arch"
397 #endif // (x86_64 || i386 || MIPS) && SANITIZER_LINUX
399 #if SANITIZER_FREEBSD
400 static void **ThreadSelfSegbase() {
402 #if defined(__i386__)
403 // sysarch(I386_GET_GSBASE, segbase);
404 __asm
__volatile("mov %%gs:0, %0" : "=r" (segbase
));
405 #elif defined(__x86_64__)
406 // sysarch(AMD64_GET_FSBASE, segbase);
407 __asm
__volatile("movq %%fs:0, %0" : "=r" (segbase
));
409 #error "unsupported CPU arch"
415 return (uptr
)ThreadSelfSegbase()[2];
417 #endif // SANITIZER_FREEBSD
420 static struct tls_tcb
* ThreadSelfTlsTcb() {
421 struct tls_tcb
*tcb
= nullptr;
422 #ifdef __HAVE___LWP_GETTCB_FAST
423 tcb
= (struct tls_tcb
*)__lwp_gettcb_fast();
424 #elif defined(__HAVE___LWP_GETPRIVATE_FAST)
425 tcb
= (struct tls_tcb
*)__lwp_getprivate_fast();
431 return (uptr
)ThreadSelfTlsTcb()->tcb_pthread
;
434 int GetSizeFromHdr(struct dl_phdr_info
*info
, size_t size
, void *data
) {
435 const Elf_Phdr
*hdr
= info
->dlpi_phdr
;
436 const Elf_Phdr
*last_hdr
= hdr
+ info
->dlpi_phnum
;
438 for (; hdr
!= last_hdr
; ++hdr
) {
439 if (hdr
->p_type
== PT_TLS
&& info
->dlpi_tls_modid
== 1) {
440 *(uptr
*)data
= hdr
->p_memsz
;
446 #endif // SANITIZER_NETBSD
448 #if SANITIZER_ANDROID
449 // Bionic provides this API since S.
450 extern "C" SANITIZER_WEAK_ATTRIBUTE
void __libc_get_static_tls_bounds(void **,
455 static void GetTls(uptr
*addr
, uptr
*size
) {
456 #if SANITIZER_ANDROID
457 if (&__libc_get_static_tls_bounds
) {
460 __libc_get_static_tls_bounds(&start_addr
, &end_addr
);
461 *addr
= reinterpret_cast<uptr
>(start_addr
);
463 reinterpret_cast<uptr
>(end_addr
) - reinterpret_cast<uptr
>(start_addr
);
468 #elif SANITIZER_LINUX
469 #if defined(__x86_64__) || defined(__i386__) || defined(__s390__)
470 *addr
= ThreadSelf();
471 *size
= GetTlsSize();
473 *addr
+= ThreadDescriptorSize();
474 #elif defined(__mips__) || defined(__aarch64__) || defined(__powerpc64__) || \
475 defined(__arm__) || SANITIZER_RISCV64
476 *addr
= ThreadSelf();
477 *size
= GetTlsSize();
482 #elif SANITIZER_FREEBSD
483 void** segbase
= ThreadSelfSegbase();
488 // tls_size = round(tls_static_space, tcbalign);
490 // dtv[2] = segbase - tls_static_space;
491 void **dtv
= (void**) segbase
[1];
492 *addr
= (uptr
) dtv
[2];
493 *size
= (*addr
== 0) ? 0 : ((uptr
) segbase
[0] - (uptr
) dtv
[2]);
495 #elif SANITIZER_NETBSD
496 struct tls_tcb
* const tcb
= ThreadSelfTlsTcb();
500 // Find size (p_memsz) of dlpi_tls_modid 1 (TLS block of the main program).
501 // ld.elf_so hardcodes the index 1.
502 dl_iterate_phdr(GetSizeFromHdr
, size
);
505 // The block has been found and tcb_dtv[1] contains the base address
506 *addr
= (uptr
)tcb
->tcb_dtv
[1];
509 #elif SANITIZER_SOLARIS
521 #if SANITIZER_FREEBSD || SANITIZER_ANDROID || SANITIZER_NETBSD || \
524 GetTls(&addr
, &size
);
526 #elif defined(__mips__) || defined(__powerpc64__) || SANITIZER_RISCV64
527 return RoundUpTo(g_tls_size
+ TlsPreTcbSize(), 16);
534 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
535 uptr
*tls_addr
, uptr
*tls_size
) {
537 // Stub implementation for Go.
538 *stk_addr
= *stk_size
= *tls_addr
= *tls_size
= 0;
540 GetTls(tls_addr
, tls_size
);
542 uptr stack_top
, stack_bottom
;
543 GetThreadStackTopAndBottom(main
, &stack_top
, &stack_bottom
);
544 *stk_addr
= stack_bottom
;
545 *stk_size
= stack_top
- stack_bottom
;
548 // If stack and tls intersect, make them non-intersecting.
549 if (*tls_addr
> *stk_addr
&& *tls_addr
< *stk_addr
+ *stk_size
) {
550 CHECK_GT(*tls_addr
+ *tls_size
, *stk_addr
);
551 CHECK_LE(*tls_addr
+ *tls_size
, *stk_addr
+ *stk_size
);
552 *stk_size
-= *tls_size
;
553 *tls_addr
= *stk_addr
+ *stk_size
;
559 #if !SANITIZER_FREEBSD
560 typedef ElfW(Phdr
) Elf_Phdr
;
561 #elif SANITIZER_WORDSIZE == 32 && __FreeBSD_version <= 902001 // v9.2
562 #define Elf_Phdr XElf32_Phdr
563 #define dl_phdr_info xdl_phdr_info
564 #define dl_iterate_phdr(c, b) xdl_iterate_phdr((c), (b))
565 #endif // !SANITIZER_FREEBSD
567 struct DlIteratePhdrData
{
568 InternalMmapVectorNoCtor
<LoadedModule
> *modules
;
572 static int dl_iterate_phdr_cb(dl_phdr_info
*info
, size_t size
, void *arg
) {
573 DlIteratePhdrData
*data
= (DlIteratePhdrData
*)arg
;
574 InternalScopedString
module_name(kMaxPathLength
);
577 // First module is the binary itself.
578 ReadBinaryNameCached(module_name
.data(), module_name
.size());
579 } else if (info
->dlpi_name
) {
580 module_name
.append("%s", info
->dlpi_name
);
582 if (module_name
[0] == '\0')
584 LoadedModule cur_module
;
585 cur_module
.set(module_name
.data(), info
->dlpi_addr
);
586 for (int i
= 0; i
< (int)info
->dlpi_phnum
; i
++) {
587 const Elf_Phdr
*phdr
= &info
->dlpi_phdr
[i
];
588 if (phdr
->p_type
== PT_LOAD
) {
589 uptr cur_beg
= info
->dlpi_addr
+ phdr
->p_vaddr
;
590 uptr cur_end
= cur_beg
+ phdr
->p_memsz
;
591 bool executable
= phdr
->p_flags
& PF_X
;
592 bool writable
= phdr
->p_flags
& PF_W
;
593 cur_module
.addAddressRange(cur_beg
, cur_end
, executable
,
597 data
->modules
->push_back(cur_module
);
601 #if SANITIZER_ANDROID && __ANDROID_API__ < 21
602 extern "C" __attribute__((weak
)) int dl_iterate_phdr(
603 int (*)(struct dl_phdr_info
*, size_t, void *), void *);
606 static bool requiresProcmaps() {
607 #if SANITIZER_ANDROID && __ANDROID_API__ <= 22
608 // Fall back to /proc/maps if dl_iterate_phdr is unavailable or broken.
609 // The runtime check allows the same library to work with
610 // both K and L (and future) Android releases.
611 return AndroidGetApiLevel() <= ANDROID_LOLLIPOP_MR1
;
617 static void procmapsInit(InternalMmapVectorNoCtor
<LoadedModule
> *modules
) {
618 MemoryMappingLayout
memory_mapping(/*cache_enabled*/true);
619 memory_mapping
.DumpListOfModules(modules
);
622 void ListOfModules::init() {
624 if (requiresProcmaps()) {
625 procmapsInit(&modules_
);
627 DlIteratePhdrData data
= {&modules_
, true};
628 dl_iterate_phdr(dl_iterate_phdr_cb
, &data
);
632 // When a custom loader is used, dl_iterate_phdr may not contain the full
633 // list of modules. Allow callers to fall back to using procmaps.
634 void ListOfModules::fallbackInit() {
635 if (!requiresProcmaps()) {
637 procmapsInit(&modules_
);
643 // getrusage does not give us the current RSS, only the max RSS.
644 // Still, this is better than nothing if /proc/self/statm is not available
645 // for some reason, e.g. due to a sandbox.
646 static uptr
GetRSSFromGetrusage() {
648 if (getrusage(RUSAGE_SELF
, &usage
)) // Failed, probably due to a sandbox.
650 return usage
.ru_maxrss
<< 10; // ru_maxrss is in Kb.
654 if (!common_flags()->can_use_proc_maps_statm
)
655 return GetRSSFromGetrusage();
656 fd_t fd
= OpenFile("/proc/self/statm", RdOnly
);
657 if (fd
== kInvalidFd
)
658 return GetRSSFromGetrusage();
660 uptr len
= internal_read(fd
, buf
, sizeof(buf
) - 1);
665 // The format of the file is:
666 // 1084 89 69 11 0 79 0
667 // We need the second number which is RSS in pages.
669 // Skip the first number.
670 while (*pos
>= '0' && *pos
<= '9')
673 while (!(*pos
>= '0' && *pos
<= '9') && *pos
!= 0)
677 while (*pos
>= '0' && *pos
<= '9')
678 rss
= rss
* 10 + *pos
++ - '0';
679 return rss
* GetPageSizeCached();
682 // sysconf(_SC_NPROCESSORS_{CONF,ONLN}) cannot be used on most platforms as
683 // they allocate memory.
684 u32
GetNumberOfCPUs() {
685 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
688 uptr len
= sizeof(ncpu
);
691 CHECK_EQ(internal_sysctl(req
, 2, &ncpu
, &len
, NULL
, 0), 0);
693 #elif SANITIZER_ANDROID && !defined(CPU_COUNT) && !defined(__aarch64__)
694 // Fall back to /sys/devices/system/cpu on Android when cpu_set_t doesn't
695 // exist in sched.h. That is the case for toolchains generated with older
697 // This code doesn't work on AArch64 because internal_getdents makes use of
698 // the 64bit getdents syscall, but cpu_set_t seems to always exist on AArch64.
699 uptr fd
= internal_open("/sys/devices/system/cpu", O_RDONLY
| O_DIRECTORY
);
700 if (internal_iserror(fd
))
702 InternalMmapVector
<u8
> buffer(4096);
703 uptr bytes_read
= buffer
.size();
706 struct linux_dirent
*entry
= (struct linux_dirent
*)&buffer
[bytes_read
];
708 if ((u8
*)entry
>= &buffer
[bytes_read
]) {
709 bytes_read
= internal_getdents(fd
, (struct linux_dirent
*)buffer
.data(),
711 if (internal_iserror(bytes_read
) || !bytes_read
)
713 entry
= (struct linux_dirent
*)buffer
.data();
715 d_type
= (u8
*)entry
+ entry
->d_reclen
- 1;
716 if (d_type
>= &buffer
[bytes_read
] ||
717 (u8
*)&entry
->d_name
[3] >= &buffer
[bytes_read
])
719 if (entry
->d_ino
!= 0 && *d_type
== DT_DIR
) {
720 if (entry
->d_name
[0] == 'c' && entry
->d_name
[1] == 'p' &&
721 entry
->d_name
[2] == 'u' &&
722 entry
->d_name
[3] >= '0' && entry
->d_name
[3] <= '9')
725 entry
= (struct linux_dirent
*)(((u8
*)entry
) + entry
->d_reclen
);
729 #elif SANITIZER_SOLARIS
730 return sysconf(_SC_NPROCESSORS_ONLN
);
732 #if defined(CPU_COUNT)
734 CHECK_EQ(sched_getaffinity(0, sizeof(cpu_set_t
), &CPUs
), 0);
735 return CPU_COUNT(&CPUs
);
744 #if SANITIZER_ANDROID
745 static atomic_uint8_t android_log_initialized
;
747 void AndroidLogInit() {
748 openlog(GetProcessName(), 0, LOG_USER
);
749 atomic_store(&android_log_initialized
, 1, memory_order_release
);
752 static bool ShouldLogAfterPrintf() {
753 return atomic_load(&android_log_initialized
, memory_order_acquire
);
756 extern "C" SANITIZER_WEAK_ATTRIBUTE
757 int async_safe_write_log(int pri
, const char* tag
, const char* msg
);
758 extern "C" SANITIZER_WEAK_ATTRIBUTE
759 int __android_log_write(int prio
, const char* tag
, const char* msg
);
761 // ANDROID_LOG_INFO is 4, but can't be resolved at runtime.
762 #define SANITIZER_ANDROID_LOG_INFO 4
764 // async_safe_write_log is a new public version of __libc_write_log that is
765 // used behind syslog. It is preferable to syslog as it will not do any dynamic
766 // memory allocation or formatting.
767 // If the function is not available, syslog is preferred for L+ (it was broken
768 // pre-L) as __android_log_write triggers a racey behavior with the strncpy
769 // interceptor. Fallback to __android_log_write pre-L.
770 void WriteOneLineToSyslog(const char *s
) {
771 if (&async_safe_write_log
) {
772 async_safe_write_log(SANITIZER_ANDROID_LOG_INFO
, GetProcessName(), s
);
773 } else if (AndroidGetApiLevel() > ANDROID_KITKAT
) {
774 syslog(LOG_INFO
, "%s", s
);
776 CHECK(&__android_log_write
);
777 __android_log_write(SANITIZER_ANDROID_LOG_INFO
, nullptr, s
);
781 extern "C" SANITIZER_WEAK_ATTRIBUTE
782 void android_set_abort_message(const char *);
784 void SetAbortMessage(const char *str
) {
785 if (&android_set_abort_message
)
786 android_set_abort_message(str
);
789 void AndroidLogInit() {}
791 static bool ShouldLogAfterPrintf() { return true; }
793 void WriteOneLineToSyslog(const char *s
) { syslog(LOG_INFO
, "%s", s
); }
795 void SetAbortMessage(const char *str
) {}
796 #endif // SANITIZER_ANDROID
798 void LogMessageOnPrintf(const char *str
) {
799 if (common_flags()->log_to_syslog
&& ShouldLogAfterPrintf())
803 #endif // SANITIZER_LINUX
805 #if SANITIZER_LINUX && !SANITIZER_GO
806 // glibc crashes when using clock_gettime from a preinit_array function as the
807 // vDSO function pointers haven't been initialized yet. __progname is
808 // initialized after the vDSO function pointers, so if it exists, is not null
809 // and is not empty, we can use clock_gettime.
810 extern "C" SANITIZER_WEAK_ATTRIBUTE
char *__progname
;
811 inline bool CanUseVDSO() {
812 // Bionic is safe, it checks for the vDSO function pointers to be initialized.
813 if (SANITIZER_ANDROID
)
815 if (&__progname
&& __progname
&& *__progname
)
820 // MonotonicNanoTime is a timing function that can leverage the vDSO by calling
821 // clock_gettime. real_clock_gettime only exists if clock_gettime is
822 // intercepted, so define it weakly and use it if available.
823 extern "C" SANITIZER_WEAK_ATTRIBUTE
824 int real_clock_gettime(u32 clk_id
, void *tp
);
825 u64
MonotonicNanoTime() {
828 if (&real_clock_gettime
)
829 real_clock_gettime(CLOCK_MONOTONIC
, &ts
);
831 clock_gettime(CLOCK_MONOTONIC
, &ts
);
833 internal_clock_gettime(CLOCK_MONOTONIC
, &ts
);
835 return (u64
)ts
.tv_sec
* (1000ULL * 1000 * 1000) + ts
.tv_nsec
;
838 // Non-Linux & Go always use the syscall.
839 u64
MonotonicNanoTime() {
841 internal_clock_gettime(CLOCK_MONOTONIC
, &ts
);
842 return (u64
)ts
.tv_sec
* (1000ULL * 1000 * 1000) + ts
.tv_nsec
;
844 #endif // SANITIZER_LINUX && !SANITIZER_GO
847 const char *pathname
= "/proc/self/exe";
850 static const int name
[] = {
860 if (internal_sysctl(name
, ARRAY_SIZE(name
), path
, &len
, NULL
, 0) != -1)
862 #elif SANITIZER_SOLARIS
863 pathname
= getexecname();
864 CHECK_NE(pathname
, NULL
);
865 #elif SANITIZER_USE_GETAUXVAL
866 // Calling execve with /proc/self/exe sets that as $EXEC_ORIGIN. Binaries that
867 // rely on that will fail to load shared libraries. Query AT_EXECFN instead.
868 pathname
= reinterpret_cast<const char *>(getauxval(AT_EXECFN
));
871 uptr rv
= internal_execve(pathname
, GetArgv(), GetEnviron());
873 CHECK_EQ(internal_iserror(rv
, &rverrno
), true);
874 Printf("execve failed, errno %d\n", rverrno
);
878 void UnmapFromTo(uptr from
, uptr to
) {
882 uptr res
= internal_munmap(reinterpret_cast<void *>(from
), to
- from
);
883 if (UNLIKELY(internal_iserror(res
))) {
884 Report("ERROR: %s failed to unmap 0x%zx (%zd) bytes at address %p\n",
885 SanitizerToolName
, to
- from
, to
- from
, (void *)from
);
886 CHECK("unable to unmap" && 0);
890 uptr
MapDynamicShadow(uptr shadow_size_bytes
, uptr shadow_scale
,
891 uptr min_shadow_base_alignment
,
892 UNUSED uptr
&high_mem_end
) {
893 const uptr granularity
= GetMmapGranularity();
894 const uptr alignment
=
895 Max
<uptr
>(granularity
<< shadow_scale
, 1ULL << min_shadow_base_alignment
);
896 const uptr left_padding
=
897 Max
<uptr
>(granularity
, 1ULL << min_shadow_base_alignment
);
899 const uptr shadow_size
= RoundUpTo(shadow_size_bytes
, granularity
);
900 const uptr map_size
= shadow_size
+ left_padding
+ alignment
;
902 const uptr map_start
= (uptr
)MmapNoAccess(map_size
);
903 CHECK_NE(map_start
, ~(uptr
)0);
905 const uptr shadow_start
= RoundUpTo(map_start
+ left_padding
, alignment
);
907 UnmapFromTo(map_start
, shadow_start
- left_padding
);
908 UnmapFromTo(shadow_start
+ shadow_size
, map_start
+ map_size
);
913 void InitializePlatformCommonFlags(CommonFlags
*cf
) {
914 #if SANITIZER_ANDROID
915 if (&__libc_get_static_tls_bounds
== nullptr)
916 cf
->detect_leaks
= false;
920 } // namespace __sanitizer