sanitizer_common.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is shared between AddressSanitizer and ThreadSanitizer
  9. // run-time libraries.
  10. // It declares common functions and classes that are used in both runtimes.
  11. // Implementation of some functions are provided in sanitizer_common, while
  12. // others must be defined by run-time library itself.
  13. //===----------------------------------------------------------------------===//
  14. #ifndef SANITIZER_COMMON_H
  15. #define SANITIZER_COMMON_H
  16. #include "sanitizer_internal_defs.h"
  17. #include "sanitizer_libc.h"
  18. #include "sanitizer_mutex.h"
  19. #include "sanitizer_flags.h"
  20. namespace __sanitizer {
  21. struct StackTrace;
  22. // Constants.
  23. const uptr kWordSize = SANITIZER_WORDSIZE / 8;
  24. const uptr kWordSizeInBits = 8 * kWordSize;
  25. #if defined(__powerpc__) || defined(__powerpc64__)
  26. const uptr kCacheLineSize = 128;
  27. #else
  28. const uptr kCacheLineSize = 64;
  29. #endif
  30. const uptr kMaxPathLength = 512;
  31. const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
  32. extern const char *SanitizerToolName; // Can be changed by the tool.
  33. uptr GetPageSize();
  34. uptr GetPageSizeCached();
  35. uptr GetMmapGranularity();
  36. uptr GetMaxVirtualAddress();
  37. // Threads
  38. uptr GetTid();
  39. uptr GetThreadSelf();
  40. void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
  41. uptr *stack_bottom);
  42. void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
  43. uptr *tls_addr, uptr *tls_size);
  44. // Memory management
  45. void *MmapOrDie(uptr size, const char *mem_type);
  46. void UnmapOrDie(void *addr, uptr size);
  47. void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
  48. void *MmapNoReserveOrDie(uptr size, const char *mem_type);
  49. void *MmapFixedOrDie(uptr fixed_addr, uptr size);
  50. void *Mprotect(uptr fixed_addr, uptr size);
  51. // Map aligned chunk of address space; size and alignment are powers of two.
  52. void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type);
  53. // Used to check if we can map shadow memory to a fixed location.
  54. bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
  55. void FlushUnneededShadowMemory(uptr addr, uptr size);
  56. void IncreaseTotalMmap(uptr size);
  57. void DecreaseTotalMmap(uptr size);
  58. // InternalScopedBuffer can be used instead of large stack arrays to
  59. // keep frame size low.
  60. // FIXME: use InternalAlloc instead of MmapOrDie once
  61. // InternalAlloc is made libc-free.
  62. template<typename T>
  63. class InternalScopedBuffer {
  64. public:
  65. explicit InternalScopedBuffer(uptr cnt) {
  66. cnt_ = cnt;
  67. ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
  68. }
  69. ~InternalScopedBuffer() {
  70. UnmapOrDie(ptr_, cnt_ * sizeof(T));
  71. }
  72. T &operator[](uptr i) { return ptr_[i]; }
  73. T *data() { return ptr_; }
  74. uptr size() { return cnt_ * sizeof(T); }
  75. private:
  76. T *ptr_;
  77. uptr cnt_;
  78. // Disallow evil constructors.
  79. InternalScopedBuffer(const InternalScopedBuffer&);
  80. void operator=(const InternalScopedBuffer&);
  81. };
  82. class InternalScopedString : public InternalScopedBuffer<char> {
  83. public:
  84. explicit InternalScopedString(uptr max_length)
  85. : InternalScopedBuffer<char>(max_length), length_(0) {
  86. (*this)[0] = '\0';
  87. }
  88. uptr length() { return length_; }
  89. void clear() {
  90. (*this)[0] = '\0';
  91. length_ = 0;
  92. }
  93. void append(const char *format, ...);
  94. private:
  95. uptr length_;
  96. };
  97. // Simple low-level (mmap-based) allocator for internal use. Doesn't have
  98. // constructor, so all instances of LowLevelAllocator should be
  99. // linker initialized.
  100. class LowLevelAllocator {
  101. public:
  102. // Requires an external lock.
  103. void *Allocate(uptr size);
  104. private:
  105. char *allocated_end_;
  106. char *allocated_current_;
  107. };
  108. typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
  109. // Allows to register tool-specific callbacks for LowLevelAllocator.
  110. // Passing NULL removes the callback.
  111. void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
  112. // IO
  113. void RawWrite(const char *buffer);
  114. bool PrintsToTty();
  115. // Caching version of PrintsToTty(). Not thread-safe.
  116. bool PrintsToTtyCached();
  117. bool ColorizeReports();
  118. void Printf(const char *format, ...);
  119. void Report(const char *format, ...);
  120. void SetPrintfAndReportCallback(void (*callback)(const char *));
  121. #define VReport(level, ...) \
  122. do { \
  123. if ((uptr)common_flags()->verbosity >= (level)) Report(__VA_ARGS__); \
  124. } while (0)
  125. #define VPrintf(level, ...) \
  126. do { \
  127. if ((uptr)common_flags()->verbosity >= (level)) Printf(__VA_ARGS__); \
  128. } while (0)
  129. // Can be used to prevent mixing error reports from different sanitizers.
  130. extern StaticSpinMutex CommonSanitizerReportMutex;
  131. void MaybeOpenReportFile();
  132. extern fd_t report_fd;
  133. extern bool log_to_file;
  134. extern char report_path_prefix[4096];
  135. extern uptr report_fd_pid;
  136. extern uptr stoptheworld_tracer_pid;
  137. extern uptr stoptheworld_tracer_ppid;
  138. uptr OpenFile(const char *filename, bool write);
  139. // Opens the file 'file_name" and reads up to 'max_len' bytes.
  140. // The resulting buffer is mmaped and stored in '*buff'.
  141. // The size of the mmaped region is stored in '*buff_size',
  142. // Returns the number of read bytes or 0 if file can not be opened.
  143. uptr ReadFileToBuffer(const char *file_name, char **buff,
  144. uptr *buff_size, uptr max_len);
  145. // Maps given file to virtual memory, and returns pointer to it
  146. // (or NULL if the mapping failes). Stores the size of mmaped region
  147. // in '*buff_size'.
  148. void *MapFileToMemory(const char *file_name, uptr *buff_size);
  149. void *MapWritableFileToMemory(void *addr, uptr size, uptr fd, uptr offset);
  150. bool IsAccessibleMemoryRange(uptr beg, uptr size);
  151. // Error report formatting.
  152. const char *StripPathPrefix(const char *filepath,
  153. const char *strip_file_prefix);
  154. // Strip the directories from the module name.
  155. const char *StripModuleName(const char *module);
  156. // OS
  157. void DisableCoreDumperIfNecessary();
  158. void DumpProcessMap();
  159. bool FileExists(const char *filename);
  160. const char *GetEnv(const char *name);
  161. bool SetEnv(const char *name, const char *value);
  162. const char *GetPwd();
  163. char *FindPathToBinary(const char *name);
  164. u32 GetUid();
  165. void ReExec();
  166. bool StackSizeIsUnlimited();
  167. void SetStackSizeLimitInBytes(uptr limit);
  168. bool AddressSpaceIsUnlimited();
  169. void SetAddressSpaceUnlimited();
  170. void AdjustStackSize(void *attr);
  171. void PrepareForSandboxing(__sanitizer_sandbox_arguments *args);
  172. void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args);
  173. void SetSandboxingCallback(void (*f)());
  174. void CovUpdateMapping(uptr caller_pc = 0);
  175. void CovBeforeFork();
  176. void CovAfterFork(int child_pid);
  177. void InitTlsSize();
  178. uptr GetTlsSize();
  179. // Other
  180. void SleepForSeconds(int seconds);
  181. void SleepForMillis(int millis);
  182. u64 NanoTime();
  183. int Atexit(void (*function)(void));
  184. void SortArray(uptr *array, uptr size);
  185. // Exit
  186. void NORETURN Abort();
  187. void NORETURN Die();
  188. void NORETURN
  189. CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
  190. // Set the name of the current thread to 'name', return true on succees.
  191. // The name may be truncated to a system-dependent limit.
  192. bool SanitizerSetThreadName(const char *name);
  193. // Get the name of the current thread (no more than max_len bytes),
  194. // return true on succees. name should have space for at least max_len+1 bytes.
  195. bool SanitizerGetThreadName(char *name, int max_len);
  196. // Specific tools may override behavior of "Die" and "CheckFailed" functions
  197. // to do tool-specific job.
  198. typedef void (*DieCallbackType)(void);
  199. void SetDieCallback(DieCallbackType);
  200. DieCallbackType GetDieCallback();
  201. typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
  202. u64, u64);
  203. void SetCheckFailedCallback(CheckFailedCallbackType callback);
  204. // Functions related to signal handling.
  205. typedef void (*SignalHandlerType)(int, void *, void *);
  206. bool IsDeadlySignal(int signum);
  207. void InstallDeadlySignalHandlers(SignalHandlerType handler);
  208. // Alternative signal stack (POSIX-only).
  209. void SetAlternateSignalStack();
  210. void UnsetAlternateSignalStack();
  211. // We don't want a summary too long.
  212. const int kMaxSummaryLength = 1024;
  213. // Construct a one-line string:
  214. // SUMMARY: SanitizerToolName: error_message
  215. // and pass it to __sanitizer_report_error_summary.
  216. void ReportErrorSummary(const char *error_message);
  217. // Same as above, but construct error_message as:
  218. // error_type file:line function
  219. void ReportErrorSummary(const char *error_type, const char *file,
  220. int line, const char *function);
  221. void ReportErrorSummary(const char *error_type, StackTrace *trace);
  222. // Math
  223. #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
  224. extern "C" {
  225. unsigned char _BitScanForward(unsigned long *index, unsigned long mask); // NOLINT
  226. unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); // NOLINT
  227. #if defined(_WIN64)
  228. unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask); // NOLINT
  229. unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask); // NOLINT
  230. #endif
  231. }
  232. #endif
  233. INLINE uptr MostSignificantSetBitIndex(uptr x) {
  234. CHECK_NE(x, 0U);
  235. unsigned long up; // NOLINT
  236. #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
  237. up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
  238. #elif defined(_WIN64)
  239. _BitScanReverse64(&up, x);
  240. #else
  241. _BitScanReverse(&up, x);
  242. #endif
  243. return up;
  244. }
  245. INLINE uptr LeastSignificantSetBitIndex(uptr x) {
  246. CHECK_NE(x, 0U);
  247. unsigned long up; // NOLINT
  248. #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
  249. up = __builtin_ctzl(x);
  250. #elif defined(_WIN64)
  251. _BitScanForward64(&up, x);
  252. #else
  253. _BitScanForward(&up, x);
  254. #endif
  255. return up;
  256. }
  257. INLINE bool IsPowerOfTwo(uptr x) {
  258. return (x & (x - 1)) == 0;
  259. }
  260. INLINE uptr RoundUpToPowerOfTwo(uptr size) {
  261. CHECK(size);
  262. if (IsPowerOfTwo(size)) return size;
  263. uptr up = MostSignificantSetBitIndex(size);
  264. CHECK(size < (1ULL << (up + 1)));
  265. CHECK(size > (1ULL << up));
  266. return 1UL << (up + 1);
  267. }
  268. INLINE uptr RoundUpTo(uptr size, uptr boundary) {
  269. CHECK(IsPowerOfTwo(boundary));
  270. return (size + boundary - 1) & ~(boundary - 1);
  271. }
  272. INLINE uptr RoundDownTo(uptr x, uptr boundary) {
  273. return x & ~(boundary - 1);
  274. }
  275. INLINE bool IsAligned(uptr a, uptr alignment) {
  276. return (a & (alignment - 1)) == 0;
  277. }
  278. INLINE uptr Log2(uptr x) {
  279. CHECK(IsPowerOfTwo(x));
  280. #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
  281. return __builtin_ctzl(x);
  282. #elif defined(_WIN64)
  283. unsigned long ret; // NOLINT
  284. _BitScanForward64(&ret, x);
  285. return ret;
  286. #else
  287. unsigned long ret; // NOLINT
  288. _BitScanForward(&ret, x);
  289. return ret;
  290. #endif
  291. }
  292. // Don't use std::min, std::max or std::swap, to minimize dependency
  293. // on libstdc++.
  294. template<class T> T Min(T a, T b) { return a < b ? a : b; }
  295. template<class T> T Max(T a, T b) { return a > b ? a : b; }
  296. template<class T> void Swap(T& a, T& b) {
  297. T tmp = a;
  298. a = b;
  299. b = tmp;
  300. }
  301. // Char handling
  302. INLINE bool IsSpace(int c) {
  303. return (c == ' ') || (c == '\n') || (c == '\t') ||
  304. (c == '\f') || (c == '\r') || (c == '\v');
  305. }
  306. INLINE bool IsDigit(int c) {
  307. return (c >= '0') && (c <= '9');
  308. }
  309. INLINE int ToLower(int c) {
  310. return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
  311. }
  312. // A low-level vector based on mmap. May incur a significant memory overhead for
  313. // small vectors.
  314. // WARNING: The current implementation supports only POD types.
  315. template<typename T>
  316. class InternalMmapVector {
  317. public:
  318. explicit InternalMmapVector(uptr initial_capacity) {
  319. capacity_ = Max(initial_capacity, (uptr)1);
  320. size_ = 0;
  321. data_ = (T *)MmapOrDie(capacity_ * sizeof(T), "InternalMmapVector");
  322. }
  323. ~InternalMmapVector() {
  324. UnmapOrDie(data_, capacity_ * sizeof(T));
  325. }
  326. T &operator[](uptr i) {
  327. CHECK_LT(i, size_);
  328. return data_[i];
  329. }
  330. const T &operator[](uptr i) const {
  331. CHECK_LT(i, size_);
  332. return data_[i];
  333. }
  334. void push_back(const T &element) {
  335. CHECK_LE(size_, capacity_);
  336. if (size_ == capacity_) {
  337. uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
  338. Resize(new_capacity);
  339. }
  340. data_[size_++] = element;
  341. }
  342. T &back() {
  343. CHECK_GT(size_, 0);
  344. return data_[size_ - 1];
  345. }
  346. void pop_back() {
  347. CHECK_GT(size_, 0);
  348. size_--;
  349. }
  350. uptr size() const {
  351. return size_;
  352. }
  353. const T *data() const {
  354. return data_;
  355. }
  356. uptr capacity() const {
  357. return capacity_;
  358. }
  359. void clear() { size_ = 0; }
  360. private:
  361. void Resize(uptr new_capacity) {
  362. CHECK_GT(new_capacity, 0);
  363. CHECK_LE(size_, new_capacity);
  364. T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),
  365. "InternalMmapVector");
  366. internal_memcpy(new_data, data_, size_ * sizeof(T));
  367. T *old_data = data_;
  368. data_ = new_data;
  369. UnmapOrDie(old_data, capacity_ * sizeof(T));
  370. capacity_ = new_capacity;
  371. }
  372. // Disallow evil constructors.
  373. InternalMmapVector(const InternalMmapVector&);
  374. void operator=(const InternalMmapVector&);
  375. T *data_;
  376. uptr capacity_;
  377. uptr size_;
  378. };
  379. // HeapSort for arrays and InternalMmapVector.
  380. template<class Container, class Compare>
  381. void InternalSort(Container *v, uptr size, Compare comp) {
  382. if (size < 2)
  383. return;
  384. // Stage 1: insert elements to the heap.
  385. for (uptr i = 1; i < size; i++) {
  386. uptr j, p;
  387. for (j = i; j > 0; j = p) {
  388. p = (j - 1) / 2;
  389. if (comp((*v)[p], (*v)[j]))
  390. Swap((*v)[j], (*v)[p]);
  391. else
  392. break;
  393. }
  394. }
  395. // Stage 2: swap largest element with the last one,
  396. // and sink the new top.
  397. for (uptr i = size - 1; i > 0; i--) {
  398. Swap((*v)[0], (*v)[i]);
  399. uptr j, max_ind;
  400. for (j = 0; j < i; j = max_ind) {
  401. uptr left = 2 * j + 1;
  402. uptr right = 2 * j + 2;
  403. max_ind = j;
  404. if (left < i && comp((*v)[max_ind], (*v)[left]))
  405. max_ind = left;
  406. if (right < i && comp((*v)[max_ind], (*v)[right]))
  407. max_ind = right;
  408. if (max_ind != j)
  409. Swap((*v)[j], (*v)[max_ind]);
  410. else
  411. break;
  412. }
  413. }
  414. }
  415. template<class Container, class Value, class Compare>
  416. uptr InternalBinarySearch(const Container &v, uptr first, uptr last,
  417. const Value &val, Compare comp) {
  418. uptr not_found = last + 1;
  419. while (last >= first) {
  420. uptr mid = (first + last) / 2;
  421. if (comp(v[mid], val))
  422. first = mid + 1;
  423. else if (comp(val, v[mid]))
  424. last = mid - 1;
  425. else
  426. return mid;
  427. }
  428. return not_found;
  429. }
  430. // Represents a binary loaded into virtual memory (e.g. this can be an
  431. // executable or a shared object).
  432. class LoadedModule {
  433. public:
  434. LoadedModule(const char *module_name, uptr base_address);
  435. void addAddressRange(uptr beg, uptr end, bool executable);
  436. bool containsAddress(uptr address) const;
  437. const char *full_name() const { return full_name_; }
  438. uptr base_address() const { return base_address_; }
  439. uptr n_ranges() const { return n_ranges_; }
  440. uptr address_range_start(int i) const { return ranges_[i].beg; }
  441. uptr address_range_end(int i) const { return ranges_[i].end; }
  442. bool address_range_executable(int i) const { return exec_[i]; }
  443. private:
  444. struct AddressRange {
  445. uptr beg;
  446. uptr end;
  447. };
  448. char *full_name_;
  449. uptr base_address_;
  450. static const uptr kMaxNumberOfAddressRanges = 6;
  451. AddressRange ranges_[kMaxNumberOfAddressRanges];
  452. bool exec_[kMaxNumberOfAddressRanges];
  453. uptr n_ranges_;
  454. };
  455. // OS-dependent function that fills array with descriptions of at most
  456. // "max_modules" currently loaded modules. Returns the number of
  457. // initialized modules. If filter is nonzero, ignores modules for which
  458. // filter(full_name) is false.
  459. typedef bool (*string_predicate_t)(const char *);
  460. uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
  461. string_predicate_t filter);
  462. #if SANITIZER_POSIX
  463. const uptr kPthreadDestructorIterations = 4;
  464. #else
  465. // Unused on Windows.
  466. const uptr kPthreadDestructorIterations = 0;
  467. #endif
  468. // Callback type for iterating over a set of memory ranges.
  469. typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
  470. #if (SANITIZER_FREEBSD || SANITIZER_LINUX) && !defined(SANITIZER_GO)
  471. extern uptr indirect_call_wrapper;
  472. void SetIndirectCallWrapper(uptr wrapper);
  473. template <typename F>
  474. F IndirectExternCall(F f) {
  475. typedef F (*WrapF)(F);
  476. return indirect_call_wrapper ? ((WrapF)indirect_call_wrapper)(f) : f;
  477. }
  478. #else
  479. INLINE void SetIndirectCallWrapper(uptr wrapper) {}
  480. template <typename F>
  481. F IndirectExternCall(F f) {
  482. return f;
  483. }
  484. #endif
  485. #if SANITIZER_ANDROID
  486. // Initialize Android logging. Any writes before this are silently lost.
  487. void AndroidLogInit();
  488. void AndroidLogWrite(const char *buffer);
  489. void GetExtraActivationFlags(char *buf, uptr size);
  490. void SanitizerInitializeUnwinder();
  491. #else
  492. INLINE void AndroidLogInit() {}
  493. INLINE void AndroidLogWrite(const char *buffer_unused) {}
  494. INLINE void GetExtraActivationFlags(char *buf, uptr size) { *buf = '\0'; }
  495. INLINE void SanitizerInitializeUnwinder() {}
  496. #endif
  497. } // namespace __sanitizer
  498. inline void *operator new(__sanitizer::operator_new_size_type size,
  499. __sanitizer::LowLevelAllocator &alloc) {
  500. return alloc.Allocate(size);
  501. }
  502. struct StackDepotStats {
  503. uptr n_uniq_ids;
  504. uptr allocated;
  505. };
  506. #endif // SANITIZER_COMMON_H