sanitizer_common.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. //===-- sanitizer_common.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. // This file is shared between run-time libraries of sanitizers.
  10. //
  11. // It declares common functions and classes that are used in both runtimes.
  12. // Implementation of some functions are provided in sanitizer_common, while
  13. // others must be defined by run-time library itself.
  14. //===----------------------------------------------------------------------===//
  15. #ifndef SANITIZER_COMMON_H
  16. #define SANITIZER_COMMON_H
  17. #include "sanitizer_flags.h"
  18. #include "sanitizer_interface_internal.h"
  19. #include "sanitizer_internal_defs.h"
  20. #include "sanitizer_libc.h"
  21. #include "sanitizer_list.h"
  22. #include "sanitizer_mutex.h"
  23. #if defined(_MSC_VER) && !defined(__clang__)
  24. extern "C" void _ReadWriteBarrier();
  25. #pragma intrinsic(_ReadWriteBarrier)
  26. #endif
  27. namespace __sanitizer {
  28. struct AddressInfo;
  29. struct BufferedStackTrace;
  30. struct SignalContext;
  31. struct StackTrace;
  32. // Constants.
  33. const uptr kWordSize = SANITIZER_WORDSIZE / 8;
  34. const uptr kWordSizeInBits = 8 * kWordSize;
  35. const uptr kCacheLineSize = SANITIZER_CACHE_LINE_SIZE;
  36. const uptr kMaxPathLength = 4096;
  37. const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
  38. const uptr kErrorMessageBufferSize = 1 << 16;
  39. // Denotes fake PC values that come from JIT/JAVA/etc.
  40. // For such PC values __tsan_symbolize_external_ex() will be called.
  41. const u64 kExternalPCBit = 1ULL << 60;
  42. extern const char *SanitizerToolName; // Can be changed by the tool.
  43. extern atomic_uint32_t current_verbosity;
  44. inline void SetVerbosity(int verbosity) {
  45. atomic_store(&current_verbosity, verbosity, memory_order_relaxed);
  46. }
  47. inline int Verbosity() {
  48. return atomic_load(&current_verbosity, memory_order_relaxed);
  49. }
  50. #if SANITIZER_ANDROID
  51. inline uptr GetPageSize() {
  52. // Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
  53. return 4096;
  54. }
  55. inline uptr GetPageSizeCached() {
  56. return 4096;
  57. }
  58. #else
  59. uptr GetPageSize();
  60. extern uptr PageSizeCached;
  61. inline uptr GetPageSizeCached() {
  62. if (!PageSizeCached)
  63. PageSizeCached = GetPageSize();
  64. return PageSizeCached;
  65. }
  66. #endif
  67. uptr GetMmapGranularity();
  68. uptr GetMaxVirtualAddress();
  69. uptr GetMaxUserVirtualAddress();
  70. // Threads
  71. tid_t GetTid();
  72. int TgKill(pid_t pid, tid_t tid, int sig);
  73. uptr GetThreadSelf();
  74. void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
  75. uptr *stack_bottom);
  76. void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
  77. uptr *tls_addr, uptr *tls_size);
  78. // Memory management
  79. void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false);
  80. inline void *MmapOrDieQuietly(uptr size, const char *mem_type) {
  81. return MmapOrDie(size, mem_type, /*raw_report*/ true);
  82. }
  83. void UnmapOrDie(void *addr, uptr size);
  84. // Behaves just like MmapOrDie, but tolerates out of memory condition, in that
  85. // case returns nullptr.
  86. void *MmapOrDieOnFatalError(uptr size, const char *mem_type);
  87. bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name = nullptr)
  88. WARN_UNUSED_RESULT;
  89. bool MmapFixedSuperNoReserve(uptr fixed_addr, uptr size,
  90. const char *name = nullptr) WARN_UNUSED_RESULT;
  91. void *MmapNoReserveOrDie(uptr size, const char *mem_type);
  92. void *MmapFixedOrDie(uptr fixed_addr, uptr size, const char *name = nullptr);
  93. // Behaves just like MmapFixedOrDie, but tolerates out of memory condition, in
  94. // that case returns nullptr.
  95. void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size,
  96. const char *name = nullptr);
  97. void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name = nullptr);
  98. void *MmapNoAccess(uptr size);
  99. // Map aligned chunk of address space; size and alignment are powers of two.
  100. // Dies on all but out of memory errors, in the latter case returns nullptr.
  101. void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
  102. const char *mem_type);
  103. // Disallow access to a memory range. Use MmapFixedNoAccess to allocate an
  104. // unaccessible memory.
  105. bool MprotectNoAccess(uptr addr, uptr size);
  106. bool MprotectReadOnly(uptr addr, uptr size);
  107. void MprotectMallocZones(void *addr, int prot);
  108. #if SANITIZER_LINUX
  109. // Unmap memory. Currently only used on Linux.
  110. void UnmapFromTo(uptr from, uptr to);
  111. #endif
  112. // Maps shadow_size_bytes of shadow memory and returns shadow address. It will
  113. // be aligned to the mmap granularity * 2^shadow_scale, or to
  114. // 2^min_shadow_base_alignment if that is larger. The returned address will
  115. // have max(2^min_shadow_base_alignment, mmap granularity) on the left, and
  116. // shadow_size_bytes bytes on the right, which on linux is mapped no access.
  117. // The high_mem_end may be updated if the original shadow size doesn't fit.
  118. uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
  119. uptr min_shadow_base_alignment, uptr &high_mem_end);
  120. // Let S = max(shadow_size, num_aliases * alias_size, ring_buffer_size).
  121. // Reserves 2*S bytes of address space to the right of the returned address and
  122. // ring_buffer_size bytes to the left. The returned address is aligned to 2*S.
  123. // Also creates num_aliases regions of accessible memory starting at offset S
  124. // from the returned address. Each region has size alias_size and is backed by
  125. // the same physical memory.
  126. uptr MapDynamicShadowAndAliases(uptr shadow_size, uptr alias_size,
  127. uptr num_aliases, uptr ring_buffer_size);
  128. // Reserve memory range [beg, end]. If madvise_shadow is true then apply
  129. // madvise (e.g. hugepages, core dumping) requested by options.
  130. void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name,
  131. bool madvise_shadow = true);
  132. // Protect size bytes of memory starting at addr. Also try to protect
  133. // several pages at the start of the address space as specified by
  134. // zero_base_shadow_start, at most up to the size or zero_base_max_shadow_start.
  135. void ProtectGap(uptr addr, uptr size, uptr zero_base_shadow_start,
  136. uptr zero_base_max_shadow_start);
  137. // Find an available address space.
  138. uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
  139. uptr *largest_gap_found, uptr *max_occupied_addr);
  140. // Used to check if we can map shadow memory to a fixed location.
  141. bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
  142. // Releases memory pages entirely within the [beg, end] address range. Noop if
  143. // the provided range does not contain at least one entire page.
  144. void ReleaseMemoryPagesToOS(uptr beg, uptr end);
  145. void IncreaseTotalMmap(uptr size);
  146. void DecreaseTotalMmap(uptr size);
  147. uptr GetRSS();
  148. void SetShadowRegionHugePageMode(uptr addr, uptr length);
  149. bool DontDumpShadowMemory(uptr addr, uptr length);
  150. // Check if the built VMA size matches the runtime one.
  151. void CheckVMASize();
  152. void RunMallocHooks(const void *ptr, uptr size);
  153. void RunFreeHooks(const void *ptr);
  154. class ReservedAddressRange {
  155. public:
  156. uptr Init(uptr size, const char *name = nullptr, uptr fixed_addr = 0);
  157. uptr InitAligned(uptr size, uptr align, const char *name = nullptr);
  158. uptr Map(uptr fixed_addr, uptr size, const char *name = nullptr);
  159. uptr MapOrDie(uptr fixed_addr, uptr size, const char *name = nullptr);
  160. void Unmap(uptr addr, uptr size);
  161. void *base() const { return base_; }
  162. uptr size() const { return size_; }
  163. private:
  164. void* base_;
  165. uptr size_;
  166. const char* name_;
  167. uptr os_handle_;
  168. };
  169. typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
  170. /*out*/ uptr *stats);
  171. // Parse the contents of /proc/self/smaps and generate a memory profile.
  172. // |cb| is a tool-specific callback that fills the |stats| array.
  173. void GetMemoryProfile(fill_profile_f cb, uptr *stats);
  174. void ParseUnixMemoryProfile(fill_profile_f cb, uptr *stats, char *smaps,
  175. uptr smaps_len);
  176. // Simple low-level (mmap-based) allocator for internal use. Doesn't have
  177. // constructor, so all instances of LowLevelAllocator should be
  178. // linker initialized.
  179. class LowLevelAllocator {
  180. public:
  181. // Requires an external lock.
  182. void *Allocate(uptr size);
  183. private:
  184. char *allocated_end_;
  185. char *allocated_current_;
  186. };
  187. // Set the min alignment of LowLevelAllocator to at least alignment.
  188. void SetLowLevelAllocateMinAlignment(uptr alignment);
  189. typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
  190. // Allows to register tool-specific callbacks for LowLevelAllocator.
  191. // Passing NULL removes the callback.
  192. void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
  193. // IO
  194. void CatastrophicErrorWrite(const char *buffer, uptr length);
  195. void RawWrite(const char *buffer);
  196. bool ColorizeReports();
  197. void RemoveANSIEscapeSequencesFromString(char *buffer);
  198. void Printf(const char *format, ...) FORMAT(1, 2);
  199. void Report(const char *format, ...) FORMAT(1, 2);
  200. void SetPrintfAndReportCallback(void (*callback)(const char *));
  201. #define VReport(level, ...) \
  202. do { \
  203. if ((uptr)Verbosity() >= (level)) Report(__VA_ARGS__); \
  204. } while (0)
  205. #define VPrintf(level, ...) \
  206. do { \
  207. if ((uptr)Verbosity() >= (level)) Printf(__VA_ARGS__); \
  208. } while (0)
  209. // Lock sanitizer error reporting and protects against nested errors.
  210. class ScopedErrorReportLock {
  211. public:
  212. ScopedErrorReportLock() SANITIZER_ACQUIRE(mutex_) { Lock(); }
  213. ~ScopedErrorReportLock() SANITIZER_RELEASE(mutex_) { Unlock(); }
  214. static void Lock() SANITIZER_ACQUIRE(mutex_);
  215. static void Unlock() SANITIZER_RELEASE(mutex_);
  216. static void CheckLocked() SANITIZER_CHECK_LOCKED(mutex_);
  217. private:
  218. static atomic_uintptr_t reporting_thread_;
  219. static StaticSpinMutex mutex_;
  220. };
  221. extern uptr stoptheworld_tracer_pid;
  222. extern uptr stoptheworld_tracer_ppid;
  223. bool IsAccessibleMemoryRange(uptr beg, uptr size);
  224. // Error report formatting.
  225. const char *StripPathPrefix(const char *filepath,
  226. const char *strip_file_prefix);
  227. // Strip the directories from the module name.
  228. const char *StripModuleName(const char *module);
  229. // OS
  230. uptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
  231. uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len);
  232. uptr ReadBinaryDir(/*out*/ char *buf, uptr buf_len);
  233. uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len);
  234. const char *GetProcessName();
  235. void UpdateProcessName();
  236. void CacheBinaryName();
  237. void DisableCoreDumperIfNecessary();
  238. void DumpProcessMap();
  239. const char *GetEnv(const char *name);
  240. bool SetEnv(const char *name, const char *value);
  241. u32 GetUid();
  242. void ReExec();
  243. void CheckASLR();
  244. void CheckMPROTECT();
  245. char **GetArgv();
  246. char **GetEnviron();
  247. void PrintCmdline();
  248. bool StackSizeIsUnlimited();
  249. void SetStackSizeLimitInBytes(uptr limit);
  250. bool AddressSpaceIsUnlimited();
  251. void SetAddressSpaceUnlimited();
  252. void AdjustStackSize(void *attr);
  253. void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args);
  254. void SetSandboxingCallback(void (*f)());
  255. void InitializeCoverage(bool enabled, const char *coverage_dir);
  256. void InitTlsSize();
  257. uptr GetTlsSize();
  258. // Other
  259. void SleepForSeconds(unsigned seconds);
  260. void SleepForMillis(unsigned millis);
  261. u64 NanoTime();
  262. u64 MonotonicNanoTime();
  263. int Atexit(void (*function)(void));
  264. bool TemplateMatch(const char *templ, const char *str);
  265. // Exit
  266. void NORETURN Abort();
  267. void NORETURN Die();
  268. void NORETURN
  269. CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
  270. void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
  271. const char *mmap_type, error_t err,
  272. bool raw_report = false);
  273. // Specific tools may override behavior of "Die" function to do tool-specific
  274. // job.
  275. typedef void (*DieCallbackType)(void);
  276. // It's possible to add several callbacks that would be run when "Die" is
  277. // called. The callbacks will be run in the opposite order. The tools are
  278. // strongly recommended to setup all callbacks during initialization, when there
  279. // is only a single thread.
  280. bool AddDieCallback(DieCallbackType callback);
  281. bool RemoveDieCallback(DieCallbackType callback);
  282. void SetUserDieCallback(DieCallbackType callback);
  283. void SetCheckUnwindCallback(void (*callback)());
  284. // Functions related to signal handling.
  285. typedef void (*SignalHandlerType)(int, void *, void *);
  286. HandleSignalMode GetHandleSignalMode(int signum);
  287. void InstallDeadlySignalHandlers(SignalHandlerType handler);
  288. // Signal reporting.
  289. // Each sanitizer uses slightly different implementation of stack unwinding.
  290. typedef void (*UnwindSignalStackCallbackType)(const SignalContext &sig,
  291. const void *callback_context,
  292. BufferedStackTrace *stack);
  293. // Print deadly signal report and die.
  294. void HandleDeadlySignal(void *siginfo, void *context, u32 tid,
  295. UnwindSignalStackCallbackType unwind,
  296. const void *unwind_context);
  297. // Part of HandleDeadlySignal, exposed for asan.
  298. void StartReportDeadlySignal();
  299. // Part of HandleDeadlySignal, exposed for asan.
  300. void ReportDeadlySignal(const SignalContext &sig, u32 tid,
  301. UnwindSignalStackCallbackType unwind,
  302. const void *unwind_context);
  303. // Alternative signal stack (POSIX-only).
  304. void SetAlternateSignalStack();
  305. void UnsetAlternateSignalStack();
  306. // Construct a one-line string:
  307. // SUMMARY: SanitizerToolName: error_message
  308. // and pass it to __sanitizer_report_error_summary.
  309. // If alt_tool_name is provided, it's used in place of SanitizerToolName.
  310. void ReportErrorSummary(const char *error_message,
  311. const char *alt_tool_name = nullptr);
  312. // Same as above, but construct error_message as:
  313. // error_type file:line[:column][ function]
  314. void ReportErrorSummary(const char *error_type, const AddressInfo &info,
  315. const char *alt_tool_name = nullptr);
  316. // Same as above, but obtains AddressInfo by symbolizing top stack trace frame.
  317. void ReportErrorSummary(const char *error_type, const StackTrace *trace,
  318. const char *alt_tool_name = nullptr);
  319. void ReportMmapWriteExec(int prot, int mflags);
  320. // Math
  321. #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
  322. extern "C" {
  323. unsigned char _BitScanForward(unsigned long *index, unsigned long mask);
  324. unsigned char _BitScanReverse(unsigned long *index, unsigned long mask);
  325. #if defined(_WIN64)
  326. unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask);
  327. unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask);
  328. #endif
  329. }
  330. #endif
  331. inline uptr MostSignificantSetBitIndex(uptr x) {
  332. CHECK_NE(x, 0U);
  333. unsigned long up;
  334. #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
  335. # ifdef _WIN64
  336. up = SANITIZER_WORDSIZE - 1 - __builtin_clzll(x);
  337. # else
  338. up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
  339. # endif
  340. #elif defined(_WIN64)
  341. _BitScanReverse64(&up, x);
  342. #else
  343. _BitScanReverse(&up, x);
  344. #endif
  345. return up;
  346. }
  347. inline uptr LeastSignificantSetBitIndex(uptr x) {
  348. CHECK_NE(x, 0U);
  349. unsigned long up;
  350. #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
  351. # ifdef _WIN64
  352. up = __builtin_ctzll(x);
  353. # else
  354. up = __builtin_ctzl(x);
  355. # endif
  356. #elif defined(_WIN64)
  357. _BitScanForward64(&up, x);
  358. #else
  359. _BitScanForward(&up, x);
  360. #endif
  361. return up;
  362. }
  363. inline constexpr bool IsPowerOfTwo(uptr x) { return (x & (x - 1)) == 0; }
  364. inline uptr RoundUpToPowerOfTwo(uptr size) {
  365. CHECK(size);
  366. if (IsPowerOfTwo(size)) return size;
  367. uptr up = MostSignificantSetBitIndex(size);
  368. CHECK_LT(size, (1ULL << (up + 1)));
  369. CHECK_GT(size, (1ULL << up));
  370. return 1ULL << (up + 1);
  371. }
  372. inline constexpr uptr RoundUpTo(uptr size, uptr boundary) {
  373. RAW_CHECK(IsPowerOfTwo(boundary));
  374. return (size + boundary - 1) & ~(boundary - 1);
  375. }
  376. inline constexpr uptr RoundDownTo(uptr x, uptr boundary) {
  377. return x & ~(boundary - 1);
  378. }
  379. inline constexpr bool IsAligned(uptr a, uptr alignment) {
  380. return (a & (alignment - 1)) == 0;
  381. }
  382. inline uptr Log2(uptr x) {
  383. CHECK(IsPowerOfTwo(x));
  384. return LeastSignificantSetBitIndex(x);
  385. }
  386. // Don't use std::min, std::max or std::swap, to minimize dependency
  387. // on libstdc++.
  388. template <class T>
  389. constexpr T Min(T a, T b) {
  390. return a < b ? a : b;
  391. }
  392. template <class T>
  393. constexpr T Max(T a, T b) {
  394. return a > b ? a : b;
  395. }
  396. template <class T>
  397. constexpr T Abs(T a) {
  398. return a < 0 ? -a : a;
  399. }
  400. template<class T> void Swap(T& a, T& b) {
  401. T tmp = a;
  402. a = b;
  403. b = tmp;
  404. }
  405. // Char handling
  406. inline bool IsSpace(int c) {
  407. return (c == ' ') || (c == '\n') || (c == '\t') ||
  408. (c == '\f') || (c == '\r') || (c == '\v');
  409. }
  410. inline bool IsDigit(int c) {
  411. return (c >= '0') && (c <= '9');
  412. }
  413. inline int ToLower(int c) {
  414. return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
  415. }
  416. // A low-level vector based on mmap. May incur a significant memory overhead for
  417. // small vectors.
  418. // WARNING: The current implementation supports only POD types.
  419. template<typename T>
  420. class InternalMmapVectorNoCtor {
  421. public:
  422. using value_type = T;
  423. void Initialize(uptr initial_capacity) {
  424. capacity_bytes_ = 0;
  425. size_ = 0;
  426. data_ = 0;
  427. reserve(initial_capacity);
  428. }
  429. void Destroy() { UnmapOrDie(data_, capacity_bytes_); }
  430. T &operator[](uptr i) {
  431. CHECK_LT(i, size_);
  432. return data_[i];
  433. }
  434. const T &operator[](uptr i) const {
  435. CHECK_LT(i, size_);
  436. return data_[i];
  437. }
  438. void push_back(const T &element) {
  439. CHECK_LE(size_, capacity());
  440. if (size_ == capacity()) {
  441. uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
  442. Realloc(new_capacity);
  443. }
  444. internal_memcpy(&data_[size_++], &element, sizeof(T));
  445. }
  446. T &back() {
  447. CHECK_GT(size_, 0);
  448. return data_[size_ - 1];
  449. }
  450. void pop_back() {
  451. CHECK_GT(size_, 0);
  452. size_--;
  453. }
  454. uptr size() const {
  455. return size_;
  456. }
  457. const T *data() const {
  458. return data_;
  459. }
  460. T *data() {
  461. return data_;
  462. }
  463. uptr capacity() const { return capacity_bytes_ / sizeof(T); }
  464. void reserve(uptr new_size) {
  465. // Never downsize internal buffer.
  466. if (new_size > capacity())
  467. Realloc(new_size);
  468. }
  469. void resize(uptr new_size) {
  470. if (new_size > size_) {
  471. reserve(new_size);
  472. internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
  473. }
  474. size_ = new_size;
  475. }
  476. void clear() { size_ = 0; }
  477. bool empty() const { return size() == 0; }
  478. const T *begin() const {
  479. return data();
  480. }
  481. T *begin() {
  482. return data();
  483. }
  484. const T *end() const {
  485. return data() + size();
  486. }
  487. T *end() {
  488. return data() + size();
  489. }
  490. void swap(InternalMmapVectorNoCtor &other) {
  491. Swap(data_, other.data_);
  492. Swap(capacity_bytes_, other.capacity_bytes_);
  493. Swap(size_, other.size_);
  494. }
  495. private:
  496. void Realloc(uptr new_capacity) {
  497. CHECK_GT(new_capacity, 0);
  498. CHECK_LE(size_, new_capacity);
  499. uptr new_capacity_bytes =
  500. RoundUpTo(new_capacity * sizeof(T), GetPageSizeCached());
  501. T *new_data = (T *)MmapOrDie(new_capacity_bytes, "InternalMmapVector");
  502. internal_memcpy(new_data, data_, size_ * sizeof(T));
  503. UnmapOrDie(data_, capacity_bytes_);
  504. data_ = new_data;
  505. capacity_bytes_ = new_capacity_bytes;
  506. }
  507. T *data_;
  508. uptr capacity_bytes_;
  509. uptr size_;
  510. };
  511. template <typename T>
  512. bool operator==(const InternalMmapVectorNoCtor<T> &lhs,
  513. const InternalMmapVectorNoCtor<T> &rhs) {
  514. if (lhs.size() != rhs.size()) return false;
  515. return internal_memcmp(lhs.data(), rhs.data(), lhs.size() * sizeof(T)) == 0;
  516. }
  517. template <typename T>
  518. bool operator!=(const InternalMmapVectorNoCtor<T> &lhs,
  519. const InternalMmapVectorNoCtor<T> &rhs) {
  520. return !(lhs == rhs);
  521. }
  522. template<typename T>
  523. class InternalMmapVector : public InternalMmapVectorNoCtor<T> {
  524. public:
  525. InternalMmapVector() { InternalMmapVectorNoCtor<T>::Initialize(0); }
  526. explicit InternalMmapVector(uptr cnt) {
  527. InternalMmapVectorNoCtor<T>::Initialize(cnt);
  528. this->resize(cnt);
  529. }
  530. ~InternalMmapVector() { InternalMmapVectorNoCtor<T>::Destroy(); }
  531. // Disallow copies and moves.
  532. InternalMmapVector(const InternalMmapVector &) = delete;
  533. InternalMmapVector &operator=(const InternalMmapVector &) = delete;
  534. InternalMmapVector(InternalMmapVector &&) = delete;
  535. InternalMmapVector &operator=(InternalMmapVector &&) = delete;
  536. };
  537. class InternalScopedString {
  538. public:
  539. InternalScopedString() : buffer_(1) { buffer_[0] = '\0'; }
  540. uptr length() const { return buffer_.size() - 1; }
  541. void clear() {
  542. buffer_.resize(1);
  543. buffer_[0] = '\0';
  544. }
  545. void append(const char *format, ...) FORMAT(2, 3);
  546. const char *data() const { return buffer_.data(); }
  547. char *data() { return buffer_.data(); }
  548. private:
  549. InternalMmapVector<char> buffer_;
  550. };
  551. template <class T>
  552. struct CompareLess {
  553. bool operator()(const T &a, const T &b) const { return a < b; }
  554. };
  555. // HeapSort for arrays and InternalMmapVector.
  556. template <class T, class Compare = CompareLess<T>>
  557. void Sort(T *v, uptr size, Compare comp = {}) {
  558. if (size < 2)
  559. return;
  560. // Stage 1: insert elements to the heap.
  561. for (uptr i = 1; i < size; i++) {
  562. uptr j, p;
  563. for (j = i; j > 0; j = p) {
  564. p = (j - 1) / 2;
  565. if (comp(v[p], v[j]))
  566. Swap(v[j], v[p]);
  567. else
  568. break;
  569. }
  570. }
  571. // Stage 2: swap largest element with the last one,
  572. // and sink the new top.
  573. for (uptr i = size - 1; i > 0; i--) {
  574. Swap(v[0], v[i]);
  575. uptr j, max_ind;
  576. for (j = 0; j < i; j = max_ind) {
  577. uptr left = 2 * j + 1;
  578. uptr right = 2 * j + 2;
  579. max_ind = j;
  580. if (left < i && comp(v[max_ind], v[left]))
  581. max_ind = left;
  582. if (right < i && comp(v[max_ind], v[right]))
  583. max_ind = right;
  584. if (max_ind != j)
  585. Swap(v[j], v[max_ind]);
  586. else
  587. break;
  588. }
  589. }
  590. }
  591. // Works like std::lower_bound: finds the first element that is not less
  592. // than the val.
  593. template <class Container, class T,
  594. class Compare = CompareLess<typename Container::value_type>>
  595. uptr InternalLowerBound(const Container &v, const T &val, Compare comp = {}) {
  596. uptr first = 0;
  597. uptr last = v.size();
  598. while (last > first) {
  599. uptr mid = (first + last) / 2;
  600. if (comp(v[mid], val))
  601. first = mid + 1;
  602. else
  603. last = mid;
  604. }
  605. return first;
  606. }
  607. enum ModuleArch {
  608. kModuleArchUnknown,
  609. kModuleArchI386,
  610. kModuleArchX86_64,
  611. kModuleArchX86_64H,
  612. kModuleArchARMV6,
  613. kModuleArchARMV7,
  614. kModuleArchARMV7S,
  615. kModuleArchARMV7K,
  616. kModuleArchARM64,
  617. kModuleArchRISCV64,
  618. kModuleArchHexagon
  619. };
  620. // Sorts and removes duplicates from the container.
  621. template <class Container,
  622. class Compare = CompareLess<typename Container::value_type>>
  623. void SortAndDedup(Container &v, Compare comp = {}) {
  624. Sort(v.data(), v.size(), comp);
  625. uptr size = v.size();
  626. if (size < 2)
  627. return;
  628. uptr last = 0;
  629. for (uptr i = 1; i < size; ++i) {
  630. if (comp(v[last], v[i])) {
  631. ++last;
  632. if (last != i)
  633. v[last] = v[i];
  634. } else {
  635. CHECK(!comp(v[i], v[last]));
  636. }
  637. }
  638. v.resize(last + 1);
  639. }
  640. constexpr uptr kDefaultFileMaxSize = FIRST_32_SECOND_64(1 << 26, 1 << 28);
  641. // Opens the file 'file_name" and reads up to 'max_len' bytes.
  642. // The resulting buffer is mmaped and stored in '*buff'.
  643. // Returns true if file was successfully opened and read.
  644. bool ReadFileToVector(const char *file_name,
  645. InternalMmapVectorNoCtor<char> *buff,
  646. uptr max_len = kDefaultFileMaxSize,
  647. error_t *errno_p = nullptr);
  648. // Opens the file 'file_name" and reads up to 'max_len' bytes.
  649. // This function is less I/O efficient than ReadFileToVector as it may reread
  650. // file multiple times to avoid mmap during read attempts. It's used to read
  651. // procmap, so short reads with mmap in between can produce inconsistent result.
  652. // The resulting buffer is mmaped and stored in '*buff'.
  653. // The size of the mmaped region is stored in '*buff_size'.
  654. // The total number of read bytes is stored in '*read_len'.
  655. // Returns true if file was successfully opened and read.
  656. bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
  657. uptr *read_len, uptr max_len = kDefaultFileMaxSize,
  658. error_t *errno_p = nullptr);
  659. // When adding a new architecture, don't forget to also update
  660. // script/asan_symbolize.py and sanitizer_symbolizer_libcdep.cpp.
  661. inline const char *ModuleArchToString(ModuleArch arch) {
  662. switch (arch) {
  663. case kModuleArchUnknown:
  664. return "";
  665. case kModuleArchI386:
  666. return "i386";
  667. case kModuleArchX86_64:
  668. return "x86_64";
  669. case kModuleArchX86_64H:
  670. return "x86_64h";
  671. case kModuleArchARMV6:
  672. return "armv6";
  673. case kModuleArchARMV7:
  674. return "armv7";
  675. case kModuleArchARMV7S:
  676. return "armv7s";
  677. case kModuleArchARMV7K:
  678. return "armv7k";
  679. case kModuleArchARM64:
  680. return "arm64";
  681. case kModuleArchRISCV64:
  682. return "riscv64";
  683. case kModuleArchHexagon:
  684. return "hexagon";
  685. }
  686. CHECK(0 && "Invalid module arch");
  687. return "";
  688. }
  689. const uptr kModuleUUIDSize = 32;
  690. const uptr kMaxSegName = 16;
  691. // Represents a binary loaded into virtual memory (e.g. this can be an
  692. // executable or a shared object).
  693. class LoadedModule {
  694. public:
  695. LoadedModule()
  696. : full_name_(nullptr),
  697. base_address_(0),
  698. max_executable_address_(0),
  699. arch_(kModuleArchUnknown),
  700. uuid_size_(0),
  701. instrumented_(false) {
  702. internal_memset(uuid_, 0, kModuleUUIDSize);
  703. ranges_.clear();
  704. }
  705. void set(const char *module_name, uptr base_address);
  706. void set(const char *module_name, uptr base_address, ModuleArch arch,
  707. u8 uuid[kModuleUUIDSize], bool instrumented);
  708. void setUuid(const char *uuid, uptr size);
  709. void clear();
  710. void addAddressRange(uptr beg, uptr end, bool executable, bool writable,
  711. const char *name = nullptr);
  712. bool containsAddress(uptr address) const;
  713. const char *full_name() const { return full_name_; }
  714. uptr base_address() const { return base_address_; }
  715. uptr max_executable_address() const { return max_executable_address_; }
  716. ModuleArch arch() const { return arch_; }
  717. const u8 *uuid() const { return uuid_; }
  718. uptr uuid_size() const { return uuid_size_; }
  719. bool instrumented() const { return instrumented_; }
  720. struct AddressRange {
  721. AddressRange *next;
  722. uptr beg;
  723. uptr end;
  724. bool executable;
  725. bool writable;
  726. char name[kMaxSegName];
  727. AddressRange(uptr beg, uptr end, bool executable, bool writable,
  728. const char *name)
  729. : next(nullptr),
  730. beg(beg),
  731. end(end),
  732. executable(executable),
  733. writable(writable) {
  734. internal_strncpy(this->name, (name ? name : ""), ARRAY_SIZE(this->name));
  735. }
  736. };
  737. const IntrusiveList<AddressRange> &ranges() const { return ranges_; }
  738. private:
  739. char *full_name_; // Owned.
  740. uptr base_address_;
  741. uptr max_executable_address_;
  742. ModuleArch arch_;
  743. uptr uuid_size_;
  744. u8 uuid_[kModuleUUIDSize];
  745. bool instrumented_;
  746. IntrusiveList<AddressRange> ranges_;
  747. };
  748. // List of LoadedModules. OS-dependent implementation is responsible for
  749. // filling this information.
  750. class ListOfModules {
  751. public:
  752. ListOfModules() : initialized(false) {}
  753. ~ListOfModules() { clear(); }
  754. void init();
  755. void fallbackInit(); // Uses fallback init if available, otherwise clears
  756. const LoadedModule *begin() const { return modules_.begin(); }
  757. LoadedModule *begin() { return modules_.begin(); }
  758. const LoadedModule *end() const { return modules_.end(); }
  759. LoadedModule *end() { return modules_.end(); }
  760. uptr size() const { return modules_.size(); }
  761. const LoadedModule &operator[](uptr i) const {
  762. CHECK_LT(i, modules_.size());
  763. return modules_[i];
  764. }
  765. private:
  766. void clear() {
  767. for (auto &module : modules_) module.clear();
  768. modules_.clear();
  769. }
  770. void clearOrInit() {
  771. initialized ? clear() : modules_.Initialize(kInitialCapacity);
  772. initialized = true;
  773. }
  774. InternalMmapVectorNoCtor<LoadedModule> modules_;
  775. // We rarely have more than 16K loaded modules.
  776. static const uptr kInitialCapacity = 1 << 14;
  777. bool initialized;
  778. };
  779. // Callback type for iterating over a set of memory ranges.
  780. typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
  781. enum AndroidApiLevel {
  782. ANDROID_NOT_ANDROID = 0,
  783. ANDROID_KITKAT = 19,
  784. ANDROID_LOLLIPOP_MR1 = 22,
  785. ANDROID_POST_LOLLIPOP = 23
  786. };
  787. void WriteToSyslog(const char *buffer);
  788. #if defined(SANITIZER_WINDOWS) && defined(_MSC_VER) && !defined(__clang__)
  789. #define SANITIZER_WIN_TRACE 1
  790. #else
  791. #define SANITIZER_WIN_TRACE 0
  792. #endif
  793. #if SANITIZER_MAC || SANITIZER_WIN_TRACE
  794. void LogFullErrorReport(const char *buffer);
  795. #else
  796. inline void LogFullErrorReport(const char *buffer) {}
  797. #endif
  798. #if SANITIZER_LINUX || SANITIZER_MAC
  799. void WriteOneLineToSyslog(const char *s);
  800. void LogMessageOnPrintf(const char *str);
  801. #else
  802. inline void WriteOneLineToSyslog(const char *s) {}
  803. inline void LogMessageOnPrintf(const char *str) {}
  804. #endif
  805. #if SANITIZER_LINUX || SANITIZER_WIN_TRACE
  806. // Initialize Android logging. Any writes before this are silently lost.
  807. void AndroidLogInit();
  808. void SetAbortMessage(const char *);
  809. #else
  810. inline void AndroidLogInit() {}
  811. // FIXME: MacOS implementation could use CRSetCrashLogMessage.
  812. inline void SetAbortMessage(const char *) {}
  813. #endif
  814. #if SANITIZER_ANDROID
  815. void SanitizerInitializeUnwinder();
  816. AndroidApiLevel AndroidGetApiLevel();
  817. #else
  818. inline void AndroidLogWrite(const char *buffer_unused) {}
  819. inline void SanitizerInitializeUnwinder() {}
  820. inline AndroidApiLevel AndroidGetApiLevel() { return ANDROID_NOT_ANDROID; }
  821. #endif
  822. inline uptr GetPthreadDestructorIterations() {
  823. #if SANITIZER_ANDROID
  824. return (AndroidGetApiLevel() == ANDROID_LOLLIPOP_MR1) ? 8 : 4;
  825. #elif SANITIZER_POSIX
  826. return 4;
  827. #else
  828. // Unused on Windows.
  829. return 0;
  830. #endif
  831. }
  832. void *internal_start_thread(void *(*func)(void*), void *arg);
  833. void internal_join_thread(void *th);
  834. void MaybeStartBackgroudThread();
  835. // Make the compiler think that something is going on there.
  836. // Use this inside a loop that looks like memset/memcpy/etc to prevent the
  837. // compiler from recognising it and turning it into an actual call to
  838. // memset/memcpy/etc.
  839. static inline void SanitizerBreakOptimization(void *arg) {
  840. #if defined(_MSC_VER) && !defined(__clang__)
  841. _ReadWriteBarrier();
  842. #else
  843. __asm__ __volatile__("" : : "r" (arg) : "memory");
  844. #endif
  845. }
  846. struct SignalContext {
  847. void *siginfo;
  848. void *context;
  849. uptr addr;
  850. uptr pc;
  851. uptr sp;
  852. uptr bp;
  853. bool is_memory_access;
  854. enum WriteFlag { Unknown, Read, Write } write_flag;
  855. // In some cases the kernel cannot provide the true faulting address; `addr`
  856. // will be zero then. This field allows to distinguish between these cases
  857. // and dereferences of null.
  858. bool is_true_faulting_addr;
  859. // VS2013 doesn't implement unrestricted unions, so we need a trivial default
  860. // constructor
  861. SignalContext() = default;
  862. // Creates signal context in a platform-specific manner.
  863. // SignalContext is going to keep pointers to siginfo and context without
  864. // owning them.
  865. SignalContext(void *siginfo, void *context)
  866. : siginfo(siginfo),
  867. context(context),
  868. addr(GetAddress()),
  869. is_memory_access(IsMemoryAccess()),
  870. write_flag(GetWriteFlag()),
  871. is_true_faulting_addr(IsTrueFaultingAddress()) {
  872. InitPcSpBp();
  873. }
  874. static void DumpAllRegisters(void *context);
  875. // Type of signal e.g. SIGSEGV or EXCEPTION_ACCESS_VIOLATION.
  876. int GetType() const;
  877. // String description of the signal.
  878. const char *Describe() const;
  879. // Returns true if signal is stack overflow.
  880. bool IsStackOverflow() const;
  881. private:
  882. // Platform specific initialization.
  883. void InitPcSpBp();
  884. uptr GetAddress() const;
  885. WriteFlag GetWriteFlag() const;
  886. bool IsMemoryAccess() const;
  887. bool IsTrueFaultingAddress() const;
  888. };
  889. void InitializePlatformEarly();
  890. void MaybeReexec();
  891. template <typename Fn>
  892. class RunOnDestruction {
  893. public:
  894. explicit RunOnDestruction(Fn fn) : fn_(fn) {}
  895. ~RunOnDestruction() { fn_(); }
  896. private:
  897. Fn fn_;
  898. };
  899. // A simple scope guard. Usage:
  900. // auto cleanup = at_scope_exit([]{ do_cleanup; });
  901. template <typename Fn>
  902. RunOnDestruction<Fn> at_scope_exit(Fn fn) {
  903. return RunOnDestruction<Fn>(fn);
  904. }
  905. // Linux on 64-bit s390 had a nasty bug that crashes the whole machine
  906. // if a process uses virtual memory over 4TB (as many sanitizers like
  907. // to do). This function will abort the process if running on a kernel
  908. // that looks vulnerable.
  909. #if SANITIZER_LINUX && SANITIZER_S390_64
  910. void AvoidCVE_2016_2143();
  911. #else
  912. inline void AvoidCVE_2016_2143() {}
  913. #endif
  914. struct StackDepotStats {
  915. uptr n_uniq_ids;
  916. uptr allocated;
  917. };
  918. // The default value for allocator_release_to_os_interval_ms common flag to
  919. // indicate that sanitizer allocator should not attempt to release memory to OS.
  920. const s32 kReleaseToOSIntervalNever = -1;
  921. void CheckNoDeepBind(const char *filename, int flag);
  922. // Returns the requested amount of random data (up to 256 bytes) that can then
  923. // be used to seed a PRNG. Defaults to blocking like the underlying syscall.
  924. bool GetRandom(void *buffer, uptr length, bool blocking = true);
  925. // Returns the number of logical processors on the system.
  926. u32 GetNumberOfCPUs();
  927. extern u32 NumberOfCPUsCached;
  928. inline u32 GetNumberOfCPUsCached() {
  929. if (!NumberOfCPUsCached)
  930. NumberOfCPUsCached = GetNumberOfCPUs();
  931. return NumberOfCPUsCached;
  932. }
  933. template <typename T>
  934. class ArrayRef {
  935. public:
  936. ArrayRef() {}
  937. ArrayRef(T *begin, T *end) : begin_(begin), end_(end) {}
  938. T *begin() { return begin_; }
  939. T *end() { return end_; }
  940. private:
  941. T *begin_ = nullptr;
  942. T *end_ = nullptr;
  943. };
  944. } // namespace __sanitizer
  945. inline void *operator new(__sanitizer::operator_new_size_type size,
  946. __sanitizer::LowLevelAllocator &alloc) {
  947. return alloc.Allocate(size);
  948. }
  949. #endif // SANITIZER_COMMON_H