sanitizer_linux.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //===-- sanitizer_linux.h ---------------------------------------*- C++ -*-===//
  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. // Linux-specific syscall wrappers and classes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_LINUX_H
  13. #define SANITIZER_LINUX_H
  14. #include "sanitizer_platform.h"
  15. #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
  16. SANITIZER_SOLARIS
  17. # include "sanitizer_common.h"
  18. # include "sanitizer_internal_defs.h"
  19. # include "sanitizer_platform_limits_freebsd.h"
  20. # include "sanitizer_platform_limits_netbsd.h"
  21. # include "sanitizer_platform_limits_posix.h"
  22. # include "sanitizer_platform_limits_solaris.h"
  23. # include "sanitizer_posix.h"
  24. struct link_map; // Opaque type returned by dlopen().
  25. struct utsname;
  26. namespace __sanitizer {
  27. // Dirent structure for getdents(). Note that this structure is different from
  28. // the one in <dirent.h>, which is used by readdir().
  29. struct linux_dirent;
  30. struct ProcSelfMapsBuff {
  31. char *data;
  32. uptr mmaped_size;
  33. uptr len;
  34. };
  35. struct MemoryMappingLayoutData {
  36. ProcSelfMapsBuff proc_self_maps;
  37. const char *current;
  38. };
  39. void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
  40. // Syscall wrappers.
  41. uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
  42. uptr internal_sigaltstack(const void *ss, void *oss);
  43. uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
  44. __sanitizer_sigset_t *oldset);
  45. void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset);
  46. void BlockSignals(__sanitizer_sigset_t *oldset = nullptr);
  47. struct ScopedBlockSignals {
  48. explicit ScopedBlockSignals(__sanitizer_sigset_t *copy);
  49. ~ScopedBlockSignals();
  50. ScopedBlockSignals &operator=(const ScopedBlockSignals &) = delete;
  51. ScopedBlockSignals(const ScopedBlockSignals &) = delete;
  52. private:
  53. __sanitizer_sigset_t saved_;
  54. };
  55. # if SANITIZER_GLIBC
  56. uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp);
  57. # endif
  58. // Linux-only syscalls.
  59. # if SANITIZER_LINUX
  60. uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5);
  61. # if defined(__x86_64__)
  62. uptr internal_arch_prctl(int option, uptr arg2);
  63. # endif
  64. // Used only by sanitizer_stoptheworld. Signal handlers that are actually used
  65. // (like the process-wide error reporting SEGV handler) must use
  66. // internal_sigaction instead.
  67. int internal_sigaction_norestorer(int signum, const void *act, void *oldact);
  68. void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
  69. # if defined(__x86_64__) || defined(__mips__) || defined(__aarch64__) || \
  70. defined(__powerpc64__) || defined(__s390__) || defined(__i386__) || \
  71. defined(__arm__) || SANITIZER_RISCV64 || SANITIZER_LOONGARCH64
  72. uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
  73. int *parent_tidptr, void *newtls, int *child_tidptr);
  74. # endif
  75. int internal_uname(struct utsname *buf);
  76. # elif SANITIZER_FREEBSD
  77. uptr internal_procctl(int type, int id, int cmd, void *data);
  78. void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
  79. # elif SANITIZER_NETBSD
  80. void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
  81. uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
  82. # endif // SANITIZER_LINUX
  83. // This class reads thread IDs from /proc/<pid>/task using only syscalls.
  84. class ThreadLister {
  85. public:
  86. explicit ThreadLister(pid_t pid);
  87. ~ThreadLister();
  88. enum Result {
  89. Error,
  90. Incomplete,
  91. Ok,
  92. };
  93. Result ListThreads(InternalMmapVector<tid_t> *threads);
  94. private:
  95. bool IsAlive(int tid);
  96. pid_t pid_;
  97. int descriptor_ = -1;
  98. InternalMmapVector<char> buffer_;
  99. };
  100. // Exposed for testing.
  101. uptr ThreadDescriptorSize();
  102. uptr ThreadSelf();
  103. // Matches a library's file name against a base name (stripping path and version
  104. // information).
  105. bool LibraryNameIs(const char *full_name, const char *base_name);
  106. // Call cb for each region mapped by map.
  107. void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr));
  108. // Releases memory pages entirely within the [beg, end] address range.
  109. // The pages no longer count toward RSS; reads are guaranteed to return 0.
  110. // Requires (but does not verify!) that pages are MAP_PRIVATE.
  111. inline void ReleaseMemoryPagesToOSAndZeroFill(uptr beg, uptr end) {
  112. // man madvise on Linux promises zero-fill for anonymous private pages.
  113. // Testing shows the same behaviour for private (but not anonymous) mappings
  114. // of shm_open() files, as long as the underlying file is untouched.
  115. CHECK(SANITIZER_LINUX);
  116. ReleaseMemoryPagesToOS(beg, end);
  117. }
  118. # if SANITIZER_ANDROID
  119. # if defined(__aarch64__)
  120. # define __get_tls() \
  121. ({ \
  122. void **__v; \
  123. __asm__("mrs %0, tpidr_el0" : "=r"(__v)); \
  124. __v; \
  125. })
  126. # elif defined(__arm__)
  127. # define __get_tls() \
  128. ({ \
  129. void **__v; \
  130. __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__v)); \
  131. __v; \
  132. })
  133. # elif defined(__mips__)
  134. // On mips32r1, this goes via a kernel illegal instruction trap that's
  135. // optimized for v1.
  136. # define __get_tls() \
  137. ({ \
  138. register void **__v asm("v1"); \
  139. __asm__( \
  140. ".set push\n" \
  141. ".set mips32r2\n" \
  142. "rdhwr %0,$29\n" \
  143. ".set pop\n" \
  144. : "=r"(__v)); \
  145. __v; \
  146. })
  147. # elif defined(__riscv)
  148. # define __get_tls() \
  149. ({ \
  150. void **__v; \
  151. __asm__("mv %0, tp" : "=r"(__v)); \
  152. __v; \
  153. })
  154. # elif defined(__i386__)
  155. # define __get_tls() \
  156. ({ \
  157. void **__v; \
  158. __asm__("movl %%gs:0, %0" : "=r"(__v)); \
  159. __v; \
  160. })
  161. # elif defined(__x86_64__)
  162. # define __get_tls() \
  163. ({ \
  164. void **__v; \
  165. __asm__("mov %%fs:0, %0" : "=r"(__v)); \
  166. __v; \
  167. })
  168. # else
  169. # error "Unsupported architecture."
  170. # endif
  171. // The Android Bionic team has allocated a TLS slot for sanitizers starting
  172. // with Q, given that Android currently doesn't support ELF TLS. It is used to
  173. // store sanitizer thread specific data.
  174. static const int TLS_SLOT_SANITIZER = 6;
  175. ALWAYS_INLINE uptr *get_android_tls_ptr() {
  176. return reinterpret_cast<uptr *>(&__get_tls()[TLS_SLOT_SANITIZER]);
  177. }
  178. # endif // SANITIZER_ANDROID
  179. } // namespace __sanitizer
  180. #endif
  181. #endif // SANITIZER_LINUX_H