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