sanitizer_common.h 34 KB

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