xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp (revision 9dba64be9536c28e4800e06512b7f29b43ade345)
1 //===-- sanitizer_linux_libcdep.cpp ---------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements linux-specific functions from
11 // sanitizer_libc.h.
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_platform.h"
15 
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||                \
17     SANITIZER_OPENBSD || SANITIZER_SOLARIS
18 
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"
30 
31 #include <dlfcn.h>  // for dlsym()
32 #include <link.h>
33 #include <pthread.h>
34 #include <signal.h>
35 #include <sys/resource.h>
36 #include <syslog.h>
37 
38 #if SANITIZER_FREEBSD
39 #include <pthread_np.h>
40 #include <osreldate.h>
41 #include <sys/sysctl.h>
42 #define pthread_getattr_np pthread_attr_get_np
43 #endif
44 
45 #if SANITIZER_OPENBSD
46 #include <pthread_np.h>
47 #include <sys/sysctl.h>
48 #endif
49 
50 #if SANITIZER_NETBSD
51 #include <sys/sysctl.h>
52 #include <sys/tls.h>
53 #endif
54 
55 #if SANITIZER_SOLARIS
56 #include <stdlib.h>
57 #include <thread.h>
58 #endif
59 
60 #if SANITIZER_ANDROID
61 #include <android/api-level.h>
62 #if !defined(CPU_COUNT) && !defined(__aarch64__)
63 #include <dirent.h>
64 #include <fcntl.h>
65 struct __sanitizer::linux_dirent {
66   long           d_ino;
67   off_t          d_off;
68   unsigned short d_reclen;
69   char           d_name[];
70 };
71 #endif
72 #endif
73 
74 #if !SANITIZER_ANDROID
75 #include <elf.h>
76 #include <unistd.h>
77 #endif
78 
79 namespace __sanitizer {
80 
81 SANITIZER_WEAK_ATTRIBUTE int
82 real_sigaction(int signum, const void *act, void *oldact);
83 
84 int internal_sigaction(int signum, const void *act, void *oldact) {
85 #if !SANITIZER_GO
86   if (&real_sigaction)
87     return real_sigaction(signum, act, oldact);
88 #endif
89   return sigaction(signum, (const struct sigaction *)act,
90                    (struct sigaction *)oldact);
91 }
92 
93 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
94                                 uptr *stack_bottom) {
95   CHECK(stack_top);
96   CHECK(stack_bottom);
97   if (at_initialization) {
98     // This is the main thread. Libpthread may not be initialized yet.
99     struct rlimit rl;
100     CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
101 
102     // Find the mapping that contains a stack variable.
103     MemoryMappingLayout proc_maps(/*cache_enabled*/true);
104     if (proc_maps.Error()) {
105       *stack_top = *stack_bottom = 0;
106       return;
107     }
108     MemoryMappedSegment segment;
109     uptr prev_end = 0;
110     while (proc_maps.Next(&segment)) {
111       if ((uptr)&rl < segment.end) break;
112       prev_end = segment.end;
113     }
114     CHECK((uptr)&rl >= segment.start && (uptr)&rl < segment.end);
115 
116     // Get stacksize from rlimit, but clip it so that it does not overlap
117     // with other mappings.
118     uptr stacksize = rl.rlim_cur;
119     if (stacksize > segment.end - prev_end) stacksize = segment.end - prev_end;
120     // When running with unlimited stack size, we still want to set some limit.
121     // The unlimited stack size is caused by 'ulimit -s unlimited'.
122     // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
123     if (stacksize > kMaxThreadStackSize)
124       stacksize = kMaxThreadStackSize;
125     *stack_top = segment.end;
126     *stack_bottom = segment.end - stacksize;
127     return;
128   }
129   uptr stacksize = 0;
130   void *stackaddr = nullptr;
131 #if SANITIZER_SOLARIS
132   stack_t ss;
133   CHECK_EQ(thr_stksegment(&ss), 0);
134   stacksize = ss.ss_size;
135   stackaddr = (char *)ss.ss_sp - stacksize;
136 #elif SANITIZER_OPENBSD
137   stack_t sattr;
138   CHECK_EQ(pthread_stackseg_np(pthread_self(), &sattr), 0);
139   stackaddr = sattr.ss_sp;
140   stacksize = sattr.ss_size;
141 #else  // !SANITIZER_SOLARIS
142   pthread_attr_t attr;
143   pthread_attr_init(&attr);
144   CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
145   my_pthread_attr_getstack(&attr, &stackaddr, &stacksize);
146   pthread_attr_destroy(&attr);
147 #endif // SANITIZER_SOLARIS
148 
149   *stack_top = (uptr)stackaddr + stacksize;
150   *stack_bottom = (uptr)stackaddr;
151 }
152 
153 #if !SANITIZER_GO
154 bool SetEnv(const char *name, const char *value) {
155   void *f = dlsym(RTLD_NEXT, "setenv");
156   if (!f)
157     return false;
158   typedef int(*setenv_ft)(const char *name, const char *value, int overwrite);
159   setenv_ft setenv_f;
160   CHECK_EQ(sizeof(setenv_f), sizeof(f));
161   internal_memcpy(&setenv_f, &f, sizeof(f));
162   return setenv_f(name, value, 1) == 0;
163 }
164 #endif
165 
166 __attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
167                                                    int *patch) {
168 #ifdef _CS_GNU_LIBC_VERSION
169   char buf[64];
170   uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
171   if (len >= sizeof(buf))
172     return false;
173   buf[len] = 0;
174   static const char kGLibC[] = "glibc ";
175   if (internal_strncmp(buf, kGLibC, sizeof(kGLibC) - 1) != 0)
176     return false;
177   const char *p = buf + sizeof(kGLibC) - 1;
178   *major = internal_simple_strtoll(p, &p, 10);
179   *minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
180   *patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
181   return true;
182 #else
183   return false;
184 #endif
185 }
186 
187 #if !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO &&               \
188     !SANITIZER_NETBSD && !SANITIZER_OPENBSD && !SANITIZER_SOLARIS
189 static uptr g_tls_size;
190 
191 #ifdef __i386__
192 # define CHECK_GET_TLS_STATIC_INFO_VERSION (!__GLIBC_PREREQ(2, 27))
193 #else
194 # define CHECK_GET_TLS_STATIC_INFO_VERSION 0
195 #endif
196 
197 #if CHECK_GET_TLS_STATIC_INFO_VERSION
198 # define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
199 #else
200 # define DL_INTERNAL_FUNCTION
201 #endif
202 
203 namespace {
204 struct GetTlsStaticInfoCall {
205   typedef void (*get_tls_func)(size_t*, size_t*);
206 };
207 struct GetTlsStaticInfoRegparmCall {
208   typedef void (*get_tls_func)(size_t*, size_t*) DL_INTERNAL_FUNCTION;
209 };
210 
211 template <typename T>
212 void CallGetTls(void* ptr, size_t* size, size_t* align) {
213   typename T::get_tls_func get_tls;
214   CHECK_EQ(sizeof(get_tls), sizeof(ptr));
215   internal_memcpy(&get_tls, &ptr, sizeof(ptr));
216   CHECK_NE(get_tls, 0);
217   get_tls(size, align);
218 }
219 
220 bool CmpLibcVersion(int major, int minor, int patch) {
221   int ma;
222   int mi;
223   int pa;
224   if (!GetLibcVersion(&ma, &mi, &pa))
225     return false;
226   if (ma > major)
227     return true;
228   if (ma < major)
229     return false;
230   if (mi > minor)
231     return true;
232   if (mi < minor)
233     return false;
234   return pa >= patch;
235 }
236 
237 }  // namespace
238 
239 void InitTlsSize() {
240   // all current supported platforms have 16 bytes stack alignment
241   const size_t kStackAlign = 16;
242   void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info");
243   size_t tls_size = 0;
244   size_t tls_align = 0;
245   // On i?86, _dl_get_tls_static_info used to be internal_function, i.e.
246   // __attribute__((regparm(3), stdcall)) before glibc 2.27 and is normal
247   // function in 2.27 and later.
248   if (CHECK_GET_TLS_STATIC_INFO_VERSION && !CmpLibcVersion(2, 27, 0))
249     CallGetTls<GetTlsStaticInfoRegparmCall>(get_tls_static_info_ptr,
250                                             &tls_size, &tls_align);
251   else
252     CallGetTls<GetTlsStaticInfoCall>(get_tls_static_info_ptr,
253                                      &tls_size, &tls_align);
254   if (tls_align < kStackAlign)
255     tls_align = kStackAlign;
256   g_tls_size = RoundUpTo(tls_size, tls_align);
257 }
258 #else
259 void InitTlsSize() { }
260 #endif  // !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO &&
261         // !SANITIZER_NETBSD && !SANITIZER_SOLARIS
262 
263 #if (defined(__x86_64__) || defined(__i386__) || defined(__mips__) ||          \
264      defined(__aarch64__) || defined(__powerpc64__) || defined(__s390__) ||    \
265      defined(__arm__)) &&                                                      \
266     SANITIZER_LINUX && !SANITIZER_ANDROID
267 // sizeof(struct pthread) from glibc.
268 static atomic_uintptr_t thread_descriptor_size;
269 
270 uptr ThreadDescriptorSize() {
271   uptr val = atomic_load_relaxed(&thread_descriptor_size);
272   if (val)
273     return val;
274 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
275   int major;
276   int minor;
277   int patch;
278   if (GetLibcVersion(&major, &minor, &patch) && major == 2) {
279     /* sizeof(struct pthread) values from various glibc versions.  */
280     if (SANITIZER_X32)
281       val = 1728; // Assume only one particular version for x32.
282     // For ARM sizeof(struct pthread) changed in Glibc 2.23.
283     else if (SANITIZER_ARM)
284       val = minor <= 22 ? 1120 : 1216;
285     else if (minor <= 3)
286       val = FIRST_32_SECOND_64(1104, 1696);
287     else if (minor == 4)
288       val = FIRST_32_SECOND_64(1120, 1728);
289     else if (minor == 5)
290       val = FIRST_32_SECOND_64(1136, 1728);
291     else if (minor <= 9)
292       val = FIRST_32_SECOND_64(1136, 1712);
293     else if (minor == 10)
294       val = FIRST_32_SECOND_64(1168, 1776);
295     else if (minor == 11 || (minor == 12 && patch == 1))
296       val = FIRST_32_SECOND_64(1168, 2288);
297     else if (minor <= 14)
298       val = FIRST_32_SECOND_64(1168, 2304);
299     else
300       val = FIRST_32_SECOND_64(1216, 2304);
301   }
302 #elif defined(__mips__)
303   // TODO(sagarthakur): add more values as per different glibc versions.
304   val = FIRST_32_SECOND_64(1152, 1776);
305 #elif defined(__aarch64__)
306   // The sizeof (struct pthread) is the same from GLIBC 2.17 to 2.22.
307   val = 1776;
308 #elif defined(__powerpc64__)
309   val = 1776; // from glibc.ppc64le 2.20-8.fc21
310 #elif defined(__s390__)
311   val = FIRST_32_SECOND_64(1152, 1776); // valid for glibc 2.22
312 #endif
313   if (val)
314     atomic_store_relaxed(&thread_descriptor_size, val);
315   return val;
316 }
317 
318 // The offset at which pointer to self is located in the thread descriptor.
319 const uptr kThreadSelfOffset = FIRST_32_SECOND_64(8, 16);
320 
321 uptr ThreadSelfOffset() {
322   return kThreadSelfOffset;
323 }
324 
325 #if defined(__mips__) || defined(__powerpc64__)
326 // TlsPreTcbSize includes size of struct pthread_descr and size of tcb
327 // head structure. It lies before the static tls blocks.
328 static uptr TlsPreTcbSize() {
329 # if defined(__mips__)
330   const uptr kTcbHead = 16; // sizeof (tcbhead_t)
331 # elif defined(__powerpc64__)
332   const uptr kTcbHead = 88; // sizeof (tcbhead_t)
333 # endif
334   const uptr kTlsAlign = 16;
335   const uptr kTlsPreTcbSize =
336       RoundUpTo(ThreadDescriptorSize() + kTcbHead, kTlsAlign);
337   return kTlsPreTcbSize;
338 }
339 #endif
340 
341 uptr ThreadSelf() {
342   uptr descr_addr;
343 # if defined(__i386__)
344   asm("mov %%gs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
345 # elif defined(__x86_64__)
346   asm("mov %%fs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
347 # elif defined(__mips__)
348   // MIPS uses TLS variant I. The thread pointer (in hardware register $29)
349   // points to the end of the TCB + 0x7000. The pthread_descr structure is
350   // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
351   // TCB and the size of pthread_descr.
352   const uptr kTlsTcbOffset = 0x7000;
353   uptr thread_pointer;
354   asm volatile(".set push;\
355                 .set mips64r2;\
356                 rdhwr %0,$29;\
357                 .set pop" : "=r" (thread_pointer));
358   descr_addr = thread_pointer - kTlsTcbOffset - TlsPreTcbSize();
359 # elif defined(__aarch64__) || defined(__arm__)
360   descr_addr = reinterpret_cast<uptr>(__builtin_thread_pointer()) -
361                                       ThreadDescriptorSize();
362 # elif defined(__s390__)
363   descr_addr = reinterpret_cast<uptr>(__builtin_thread_pointer());
364 # elif defined(__powerpc64__)
365   // PPC64LE uses TLS variant I. The thread pointer (in GPR 13)
366   // points to the end of the TCB + 0x7000. The pthread_descr structure is
367   // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
368   // TCB and the size of pthread_descr.
369   const uptr kTlsTcbOffset = 0x7000;
370   uptr thread_pointer;
371   asm("addi %0,13,%1" : "=r"(thread_pointer) : "I"(-kTlsTcbOffset));
372   descr_addr = thread_pointer - TlsPreTcbSize();
373 # else
374 #  error "unsupported CPU arch"
375 # endif
376   return descr_addr;
377 }
378 #endif  // (x86_64 || i386 || MIPS) && SANITIZER_LINUX
379 
380 #if SANITIZER_FREEBSD
381 static void **ThreadSelfSegbase() {
382   void **segbase = 0;
383 # if defined(__i386__)
384   // sysarch(I386_GET_GSBASE, segbase);
385   __asm __volatile("mov %%gs:0, %0" : "=r" (segbase));
386 # elif defined(__x86_64__)
387   // sysarch(AMD64_GET_FSBASE, segbase);
388   __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
389 # else
390 #  error "unsupported CPU arch"
391 # endif
392   return segbase;
393 }
394 
395 uptr ThreadSelf() {
396   return (uptr)ThreadSelfSegbase()[2];
397 }
398 #endif  // SANITIZER_FREEBSD
399 
400 #if SANITIZER_NETBSD
401 static struct tls_tcb * ThreadSelfTlsTcb() {
402   struct tls_tcb * tcb;
403 # ifdef __HAVE___LWP_GETTCB_FAST
404   tcb = (struct tls_tcb *)__lwp_gettcb_fast();
405 # elif defined(__HAVE___LWP_GETPRIVATE_FAST)
406   tcb = (struct tls_tcb *)__lwp_getprivate_fast();
407 # endif
408   return tcb;
409 }
410 
411 uptr ThreadSelf() {
412   return (uptr)ThreadSelfTlsTcb()->tcb_pthread;
413 }
414 
415 int GetSizeFromHdr(struct dl_phdr_info *info, size_t size, void *data) {
416   const Elf_Phdr *hdr = info->dlpi_phdr;
417   const Elf_Phdr *last_hdr = hdr + info->dlpi_phnum;
418 
419   for (; hdr != last_hdr; ++hdr) {
420     if (hdr->p_type == PT_TLS && info->dlpi_tls_modid == 1) {
421       *(uptr*)data = hdr->p_memsz;
422       break;
423     }
424   }
425   return 0;
426 }
427 #endif  // SANITIZER_NETBSD
428 
429 #if !SANITIZER_GO
430 static void GetTls(uptr *addr, uptr *size) {
431 #if SANITIZER_LINUX && !SANITIZER_ANDROID
432 # if defined(__x86_64__) || defined(__i386__) || defined(__s390__)
433   *addr = ThreadSelf();
434   *size = GetTlsSize();
435   *addr -= *size;
436   *addr += ThreadDescriptorSize();
437 # elif defined(__mips__) || defined(__aarch64__) || defined(__powerpc64__) \
438     || defined(__arm__)
439   *addr = ThreadSelf();
440   *size = GetTlsSize();
441 # else
442   *addr = 0;
443   *size = 0;
444 # endif
445 #elif SANITIZER_FREEBSD
446   void** segbase = ThreadSelfSegbase();
447   *addr = 0;
448   *size = 0;
449   if (segbase != 0) {
450     // tcbalign = 16
451     // tls_size = round(tls_static_space, tcbalign);
452     // dtv = segbase[1];
453     // dtv[2] = segbase - tls_static_space;
454     void **dtv = (void**) segbase[1];
455     *addr = (uptr) dtv[2];
456     *size = (*addr == 0) ? 0 : ((uptr) segbase[0] - (uptr) dtv[2]);
457   }
458 #elif SANITIZER_NETBSD
459   struct tls_tcb * const tcb = ThreadSelfTlsTcb();
460   *addr = 0;
461   *size = 0;
462   if (tcb != 0) {
463     // Find size (p_memsz) of dlpi_tls_modid 1 (TLS block of the main program).
464     // ld.elf_so hardcodes the index 1.
465     dl_iterate_phdr(GetSizeFromHdr, size);
466 
467     if (*size != 0) {
468       // The block has been found and tcb_dtv[1] contains the base address
469       *addr = (uptr)tcb->tcb_dtv[1];
470     }
471   }
472 #elif SANITIZER_OPENBSD
473   *addr = 0;
474   *size = 0;
475 #elif SANITIZER_ANDROID
476   *addr = 0;
477   *size = 0;
478 #elif SANITIZER_SOLARIS
479   // FIXME
480   *addr = 0;
481   *size = 0;
482 #else
483 # error "Unknown OS"
484 #endif
485 }
486 #endif
487 
488 #if !SANITIZER_GO
489 uptr GetTlsSize() {
490 #if SANITIZER_FREEBSD || SANITIZER_ANDROID || SANITIZER_NETBSD ||              \
491     SANITIZER_OPENBSD || SANITIZER_SOLARIS
492   uptr addr, size;
493   GetTls(&addr, &size);
494   return size;
495 #elif defined(__mips__) || defined(__powerpc64__)
496   return RoundUpTo(g_tls_size + TlsPreTcbSize(), 16);
497 #else
498   return g_tls_size;
499 #endif
500 }
501 #endif
502 
503 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
504                           uptr *tls_addr, uptr *tls_size) {
505 #if SANITIZER_GO
506   // Stub implementation for Go.
507   *stk_addr = *stk_size = *tls_addr = *tls_size = 0;
508 #else
509   GetTls(tls_addr, tls_size);
510 
511   uptr stack_top, stack_bottom;
512   GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
513   *stk_addr = stack_bottom;
514   *stk_size = stack_top - stack_bottom;
515 
516   if (!main) {
517     // If stack and tls intersect, make them non-intersecting.
518     if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) {
519       CHECK_GT(*tls_addr + *tls_size, *stk_addr);
520       CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size);
521       *stk_size -= *tls_size;
522       *tls_addr = *stk_addr + *stk_size;
523     }
524   }
525 #endif
526 }
527 
528 #if !SANITIZER_FREEBSD && !SANITIZER_OPENBSD
529 typedef ElfW(Phdr) Elf_Phdr;
530 #elif SANITIZER_WORDSIZE == 32 && __FreeBSD_version <= 902001 // v9.2
531 #define Elf_Phdr XElf32_Phdr
532 #define dl_phdr_info xdl_phdr_info
533 #define dl_iterate_phdr(c, b) xdl_iterate_phdr((c), (b))
534 #endif // !SANITIZER_FREEBSD && !SANITIZER_OPENBSD
535 
536 struct DlIteratePhdrData {
537   InternalMmapVectorNoCtor<LoadedModule> *modules;
538   bool first;
539 };
540 
541 static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
542   DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
543   InternalScopedString module_name(kMaxPathLength);
544   if (data->first) {
545     data->first = false;
546     // First module is the binary itself.
547     ReadBinaryNameCached(module_name.data(), module_name.size());
548   } else if (info->dlpi_name) {
549     module_name.append("%s", info->dlpi_name);
550   }
551   if (module_name[0] == '\0')
552     return 0;
553   LoadedModule cur_module;
554   cur_module.set(module_name.data(), info->dlpi_addr);
555   for (int i = 0; i < (int)info->dlpi_phnum; i++) {
556     const Elf_Phdr *phdr = &info->dlpi_phdr[i];
557     if (phdr->p_type == PT_LOAD) {
558       uptr cur_beg = info->dlpi_addr + phdr->p_vaddr;
559       uptr cur_end = cur_beg + phdr->p_memsz;
560       bool executable = phdr->p_flags & PF_X;
561       bool writable = phdr->p_flags & PF_W;
562       cur_module.addAddressRange(cur_beg, cur_end, executable,
563                                  writable);
564     }
565   }
566   data->modules->push_back(cur_module);
567   return 0;
568 }
569 
570 #if SANITIZER_ANDROID && __ANDROID_API__ < 21
571 extern "C" __attribute__((weak)) int dl_iterate_phdr(
572     int (*)(struct dl_phdr_info *, size_t, void *), void *);
573 #endif
574 
575 static bool requiresProcmaps() {
576 #if SANITIZER_ANDROID && __ANDROID_API__ <= 22
577   // Fall back to /proc/maps if dl_iterate_phdr is unavailable or broken.
578   // The runtime check allows the same library to work with
579   // both K and L (and future) Android releases.
580   return AndroidGetApiLevel() <= ANDROID_LOLLIPOP_MR1;
581 #else
582   return false;
583 #endif
584 }
585 
586 static void procmapsInit(InternalMmapVectorNoCtor<LoadedModule> *modules) {
587   MemoryMappingLayout memory_mapping(/*cache_enabled*/true);
588   memory_mapping.DumpListOfModules(modules);
589 }
590 
591 void ListOfModules::init() {
592   clearOrInit();
593   if (requiresProcmaps()) {
594     procmapsInit(&modules_);
595   } else {
596     DlIteratePhdrData data = {&modules_, true};
597     dl_iterate_phdr(dl_iterate_phdr_cb, &data);
598   }
599 }
600 
601 // When a custom loader is used, dl_iterate_phdr may not contain the full
602 // list of modules. Allow callers to fall back to using procmaps.
603 void ListOfModules::fallbackInit() {
604   if (!requiresProcmaps()) {
605     clearOrInit();
606     procmapsInit(&modules_);
607   } else {
608     clear();
609   }
610 }
611 
612 // getrusage does not give us the current RSS, only the max RSS.
613 // Still, this is better than nothing if /proc/self/statm is not available
614 // for some reason, e.g. due to a sandbox.
615 static uptr GetRSSFromGetrusage() {
616   struct rusage usage;
617   if (getrusage(RUSAGE_SELF, &usage))  // Failed, probably due to a sandbox.
618     return 0;
619   return usage.ru_maxrss << 10;  // ru_maxrss is in Kb.
620 }
621 
622 uptr GetRSS() {
623   if (!common_flags()->can_use_proc_maps_statm)
624     return GetRSSFromGetrusage();
625   fd_t fd = OpenFile("/proc/self/statm", RdOnly);
626   if (fd == kInvalidFd)
627     return GetRSSFromGetrusage();
628   char buf[64];
629   uptr len = internal_read(fd, buf, sizeof(buf) - 1);
630   internal_close(fd);
631   if ((sptr)len <= 0)
632     return 0;
633   buf[len] = 0;
634   // The format of the file is:
635   // 1084 89 69 11 0 79 0
636   // We need the second number which is RSS in pages.
637   char *pos = buf;
638   // Skip the first number.
639   while (*pos >= '0' && *pos <= '9')
640     pos++;
641   // Skip whitespaces.
642   while (!(*pos >= '0' && *pos <= '9') && *pos != 0)
643     pos++;
644   // Read the number.
645   uptr rss = 0;
646   while (*pos >= '0' && *pos <= '9')
647     rss = rss * 10 + *pos++ - '0';
648   return rss * GetPageSizeCached();
649 }
650 
651 // sysconf(_SC_NPROCESSORS_{CONF,ONLN}) cannot be used on most platforms as
652 // they allocate memory.
653 u32 GetNumberOfCPUs() {
654 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
655   u32 ncpu;
656   int req[2];
657   uptr len = sizeof(ncpu);
658   req[0] = CTL_HW;
659   req[1] = HW_NCPU;
660   CHECK_EQ(internal_sysctl(req, 2, &ncpu, &len, NULL, 0), 0);
661   return ncpu;
662 #elif SANITIZER_ANDROID && !defined(CPU_COUNT) && !defined(__aarch64__)
663   // Fall back to /sys/devices/system/cpu on Android when cpu_set_t doesn't
664   // exist in sched.h. That is the case for toolchains generated with older
665   // NDKs.
666   // This code doesn't work on AArch64 because internal_getdents makes use of
667   // the 64bit getdents syscall, but cpu_set_t seems to always exist on AArch64.
668   uptr fd = internal_open("/sys/devices/system/cpu", O_RDONLY | O_DIRECTORY);
669   if (internal_iserror(fd))
670     return 0;
671   InternalMmapVector<u8> buffer(4096);
672   uptr bytes_read = buffer.size();
673   uptr n_cpus = 0;
674   u8 *d_type;
675   struct linux_dirent *entry = (struct linux_dirent *)&buffer[bytes_read];
676   while (true) {
677     if ((u8 *)entry >= &buffer[bytes_read]) {
678       bytes_read = internal_getdents(fd, (struct linux_dirent *)buffer.data(),
679                                      buffer.size());
680       if (internal_iserror(bytes_read) || !bytes_read)
681         break;
682       entry = (struct linux_dirent *)buffer.data();
683     }
684     d_type = (u8 *)entry + entry->d_reclen - 1;
685     if (d_type >= &buffer[bytes_read] ||
686         (u8 *)&entry->d_name[3] >= &buffer[bytes_read])
687       break;
688     if (entry->d_ino != 0 && *d_type == DT_DIR) {
689       if (entry->d_name[0] == 'c' && entry->d_name[1] == 'p' &&
690           entry->d_name[2] == 'u' &&
691           entry->d_name[3] >= '0' && entry->d_name[3] <= '9')
692         n_cpus++;
693     }
694     entry = (struct linux_dirent *)(((u8 *)entry) + entry->d_reclen);
695   }
696   internal_close(fd);
697   return n_cpus;
698 #elif SANITIZER_SOLARIS
699   return sysconf(_SC_NPROCESSORS_ONLN);
700 #else
701   cpu_set_t CPUs;
702   CHECK_EQ(sched_getaffinity(0, sizeof(cpu_set_t), &CPUs), 0);
703   return CPU_COUNT(&CPUs);
704 #endif
705 }
706 
707 #if SANITIZER_LINUX
708 
709 # if SANITIZER_ANDROID
710 static atomic_uint8_t android_log_initialized;
711 
712 void AndroidLogInit() {
713   openlog(GetProcessName(), 0, LOG_USER);
714   atomic_store(&android_log_initialized, 1, memory_order_release);
715 }
716 
717 static bool ShouldLogAfterPrintf() {
718   return atomic_load(&android_log_initialized, memory_order_acquire);
719 }
720 
721 extern "C" SANITIZER_WEAK_ATTRIBUTE
722 int async_safe_write_log(int pri, const char* tag, const char* msg);
723 extern "C" SANITIZER_WEAK_ATTRIBUTE
724 int __android_log_write(int prio, const char* tag, const char* msg);
725 
726 // ANDROID_LOG_INFO is 4, but can't be resolved at runtime.
727 #define SANITIZER_ANDROID_LOG_INFO 4
728 
729 // async_safe_write_log is a new public version of __libc_write_log that is
730 // used behind syslog. It is preferable to syslog as it will not do any dynamic
731 // memory allocation or formatting.
732 // If the function is not available, syslog is preferred for L+ (it was broken
733 // pre-L) as __android_log_write triggers a racey behavior with the strncpy
734 // interceptor. Fallback to __android_log_write pre-L.
735 void WriteOneLineToSyslog(const char *s) {
736   if (&async_safe_write_log) {
737     async_safe_write_log(SANITIZER_ANDROID_LOG_INFO, GetProcessName(), s);
738   } else if (AndroidGetApiLevel() > ANDROID_KITKAT) {
739     syslog(LOG_INFO, "%s", s);
740   } else {
741     CHECK(&__android_log_write);
742     __android_log_write(SANITIZER_ANDROID_LOG_INFO, nullptr, s);
743   }
744 }
745 
746 extern "C" SANITIZER_WEAK_ATTRIBUTE
747 void android_set_abort_message(const char *);
748 
749 void SetAbortMessage(const char *str) {
750   if (&android_set_abort_message)
751     android_set_abort_message(str);
752 }
753 # else
754 void AndroidLogInit() {}
755 
756 static bool ShouldLogAfterPrintf() { return true; }
757 
758 void WriteOneLineToSyslog(const char *s) { syslog(LOG_INFO, "%s", s); }
759 
760 void SetAbortMessage(const char *str) {}
761 # endif  // SANITIZER_ANDROID
762 
763 void LogMessageOnPrintf(const char *str) {
764   if (common_flags()->log_to_syslog && ShouldLogAfterPrintf())
765     WriteToSyslog(str);
766 }
767 
768 #endif  // SANITIZER_LINUX
769 
770 #if SANITIZER_LINUX && !SANITIZER_GO
771 // glibc crashes when using clock_gettime from a preinit_array function as the
772 // vDSO function pointers haven't been initialized yet. __progname is
773 // initialized after the vDSO function pointers, so if it exists, is not null
774 // and is not empty, we can use clock_gettime.
775 extern "C" SANITIZER_WEAK_ATTRIBUTE char *__progname;
776 INLINE bool CanUseVDSO() {
777   // Bionic is safe, it checks for the vDSO function pointers to be initialized.
778   if (SANITIZER_ANDROID)
779     return true;
780   if (&__progname && __progname && *__progname)
781     return true;
782   return false;
783 }
784 
785 // MonotonicNanoTime is a timing function that can leverage the vDSO by calling
786 // clock_gettime. real_clock_gettime only exists if clock_gettime is
787 // intercepted, so define it weakly and use it if available.
788 extern "C" SANITIZER_WEAK_ATTRIBUTE
789 int real_clock_gettime(u32 clk_id, void *tp);
790 u64 MonotonicNanoTime() {
791   timespec ts;
792   if (CanUseVDSO()) {
793     if (&real_clock_gettime)
794       real_clock_gettime(CLOCK_MONOTONIC, &ts);
795     else
796       clock_gettime(CLOCK_MONOTONIC, &ts);
797   } else {
798     internal_clock_gettime(CLOCK_MONOTONIC, &ts);
799   }
800   return (u64)ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
801 }
802 #else
803 // Non-Linux & Go always use the syscall.
804 u64 MonotonicNanoTime() {
805   timespec ts;
806   internal_clock_gettime(CLOCK_MONOTONIC, &ts);
807   return (u64)ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
808 }
809 #endif  // SANITIZER_LINUX && !SANITIZER_GO
810 
811 #if !SANITIZER_OPENBSD
812 void ReExec() {
813   const char *pathname = "/proc/self/exe";
814 
815 #if SANITIZER_NETBSD
816   static const int name[] = {
817       CTL_KERN,
818       KERN_PROC_ARGS,
819       -1,
820       KERN_PROC_PATHNAME,
821   };
822   char path[400];
823   uptr len;
824 
825   len = sizeof(path);
826   if (internal_sysctl(name, ARRAY_SIZE(name), path, &len, NULL, 0) != -1)
827     pathname = path;
828 #elif SANITIZER_SOLARIS
829   pathname = getexecname();
830   CHECK_NE(pathname, NULL);
831 #elif SANITIZER_USE_GETAUXVAL
832   // Calling execve with /proc/self/exe sets that as $EXEC_ORIGIN. Binaries that
833   // rely on that will fail to load shared libraries. Query AT_EXECFN instead.
834   pathname = reinterpret_cast<const char *>(getauxval(AT_EXECFN));
835 #endif
836 
837   uptr rv = internal_execve(pathname, GetArgv(), GetEnviron());
838   int rverrno;
839   CHECK_EQ(internal_iserror(rv, &rverrno), true);
840   Printf("execve failed, errno %d\n", rverrno);
841   Die();
842 }
843 #endif  // !SANITIZER_OPENBSD
844 
845 } // namespace __sanitizer
846 
847 #endif
848