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