1 //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between run-time libraries of sanitizers.
12 // It declares common functions and classes that are used in both runtimes.
13 // Implementation of some functions are provided in sanitizer_common, while
14 // others must be defined by run-time library itself.
15 //===----------------------------------------------------------------------===//
16 #ifndef SANITIZER_COMMON_H
17 #define SANITIZER_COMMON_H
19 #include "sanitizer_flags.h"
20 #include "sanitizer_interface_internal.h"
21 #include "sanitizer_internal_defs.h"
22 #include "sanitizer_libc.h"
23 #include "sanitizer_list.h"
24 #include "sanitizer_mutex.h"
26 #if defined(_MSC_VER) && !defined(__clang__)
27 extern "C" void _ReadWriteBarrier();
28 #pragma intrinsic(_ReadWriteBarrier)
31 namespace __sanitizer {
34 struct BufferedStackTrace;
39 const uptr kWordSize = SANITIZER_WORDSIZE / 8;
40 const uptr kWordSizeInBits = 8 * kWordSize;
42 const uptr kCacheLineSize = SANITIZER_CACHE_LINE_SIZE;
44 const uptr kMaxPathLength = 4096;
46 const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
48 static const uptr kErrorMessageBufferSize = 1 << 16;
50 // Denotes fake PC values that come from JIT/JAVA/etc.
51 // For such PC values __tsan_symbolize_external_ex() will be called.
52 const u64 kExternalPCBit = 1ULL << 60;
54 extern const char *SanitizerToolName; // Can be changed by the tool.
56 extern atomic_uint32_t current_verbosity;
57 INLINE void SetVerbosity(int verbosity) {
58 atomic_store(¤t_verbosity, verbosity, memory_order_relaxed);
60 INLINE int Verbosity() {
61 return atomic_load(¤t_verbosity, memory_order_relaxed);
65 INLINE uptr GetPageSize() {
66 // Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
69 INLINE uptr GetPageSizeCached() {
74 extern uptr PageSizeCached;
75 INLINE uptr GetPageSizeCached() {
77 PageSizeCached = GetPageSize();
78 return PageSizeCached;
81 uptr GetMmapGranularity();
82 uptr GetMaxVirtualAddress();
83 uptr GetMaxUserVirtualAddress();
86 int TgKill(pid_t pid, tid_t tid, int sig);
88 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
90 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
91 uptr *tls_addr, uptr *tls_size);
94 void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false);
95 INLINE void *MmapOrDieQuietly(uptr size, const char *mem_type) {
96 return MmapOrDie(size, mem_type, /*raw_report*/ true);
98 void UnmapOrDie(void *addr, uptr size);
99 // Behaves just like MmapOrDie, but tolerates out of memory condition, in that
100 // case returns nullptr.
101 void *MmapOrDieOnFatalError(uptr size, const char *mem_type);
102 bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name = nullptr)
104 void *MmapNoReserveOrDie(uptr size, const char *mem_type);
105 void *MmapFixedOrDie(uptr fixed_addr, uptr size);
106 // Behaves just like MmapFixedOrDie, but tolerates out of memory condition, in
107 // that case returns nullptr.
108 void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size);
109 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name = nullptr);
110 void *MmapNoAccess(uptr size);
111 // Map aligned chunk of address space; size and alignment are powers of two.
112 // Dies on all but out of memory errors, in the latter case returns nullptr.
113 void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
114 const char *mem_type);
115 // Disallow access to a memory range. Use MmapFixedNoAccess to allocate an
116 // unaccessible memory.
117 bool MprotectNoAccess(uptr addr, uptr size);
118 bool MprotectReadOnly(uptr addr, uptr size);
120 void MprotectMallocZones(void *addr, int prot);
122 // Find an available address space.
123 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
124 uptr *largest_gap_found, uptr *max_occupied_addr);
126 // Used to check if we can map shadow memory to a fixed location.
127 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
128 // Releases memory pages entirely within the [beg, end] address range. Noop if
129 // the provided range does not contain at least one entire page.
130 void ReleaseMemoryPagesToOS(uptr beg, uptr end);
131 void IncreaseTotalMmap(uptr size);
132 void DecreaseTotalMmap(uptr size);
134 bool NoHugePagesInRegion(uptr addr, uptr length);
135 bool DontDumpShadowMemory(uptr addr, uptr length);
136 // Check if the built VMA size matches the runtime one.
138 void RunMallocHooks(const void *ptr, uptr size);
139 void RunFreeHooks(const void *ptr);
141 class ReservedAddressRange {
143 uptr Init(uptr size, const char *name = nullptr, uptr fixed_addr = 0);
144 uptr Map(uptr fixed_addr, uptr size);
145 uptr MapOrDie(uptr fixed_addr, uptr size);
146 void Unmap(uptr addr, uptr size);
147 void *base() const { return base_; }
148 uptr size() const { return size_; }
157 typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
158 /*out*/uptr *stats, uptr stats_size);
160 // Parse the contents of /proc/self/smaps and generate a memory profile.
161 // |cb| is a tool-specific callback that fills the |stats| array containing
162 // |stats_size| elements.
163 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
165 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
166 // constructor, so all instances of LowLevelAllocator should be
167 // linker initialized.
168 class LowLevelAllocator {
170 // Requires an external lock.
171 void *Allocate(uptr size);
173 char *allocated_end_;
174 char *allocated_current_;
176 // Set the min alignment of LowLevelAllocator to at least alignment.
177 void SetLowLevelAllocateMinAlignment(uptr alignment);
178 typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
179 // Allows to register tool-specific callbacks for LowLevelAllocator.
180 // Passing NULL removes the callback.
181 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
184 void CatastrophicErrorWrite(const char *buffer, uptr length);
185 void RawWrite(const char *buffer);
186 bool ColorizeReports();
187 void RemoveANSIEscapeSequencesFromString(char *buffer);
188 void Printf(const char *format, ...);
189 void Report(const char *format, ...);
190 void SetPrintfAndReportCallback(void (*callback)(const char *));
191 #define VReport(level, ...) \
193 if ((uptr)Verbosity() >= (level)) Report(__VA_ARGS__); \
195 #define VPrintf(level, ...) \
197 if ((uptr)Verbosity() >= (level)) Printf(__VA_ARGS__); \
200 // Lock sanitizer error reporting and protects against nested errors.
201 class ScopedErrorReportLock {
203 ScopedErrorReportLock();
204 ~ScopedErrorReportLock();
206 static void CheckLocked();
209 extern uptr stoptheworld_tracer_pid;
210 extern uptr stoptheworld_tracer_ppid;
212 bool IsAccessibleMemoryRange(uptr beg, uptr size);
214 // Error report formatting.
215 const char *StripPathPrefix(const char *filepath,
216 const char *strip_file_prefix);
217 // Strip the directories from the module name.
218 const char *StripModuleName(const char *module);
221 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
222 uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len);
223 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len);
224 const char *GetProcessName();
225 void UpdateProcessName();
226 void CacheBinaryName();
227 void DisableCoreDumperIfNecessary();
228 void DumpProcessMap();
229 void PrintModuleMap();
230 const char *GetEnv(const char *name);
231 bool SetEnv(const char *name, const char *value);
236 void CheckMPROTECT();
240 bool StackSizeIsUnlimited();
241 uptr GetStackSizeLimitInBytes();
242 void SetStackSizeLimitInBytes(uptr limit);
243 bool AddressSpaceIsUnlimited();
244 void SetAddressSpaceUnlimited();
245 void AdjustStackSize(void *attr);
246 void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args);
247 void SetSandboxingCallback(void (*f)());
249 void InitializeCoverage(bool enabled, const char *coverage_dir);
255 void SleepForSeconds(int seconds);
256 void SleepForMillis(int millis);
258 u64 MonotonicNanoTime();
259 int Atexit(void (*function)(void));
260 bool TemplateMatch(const char *templ, const char *str);
263 void NORETURN Abort();
266 CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
267 void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
268 const char *mmap_type, error_t err,
269 bool raw_report = false);
271 // Specific tools may override behavior of "Die" and "CheckFailed" functions
272 // to do tool-specific job.
273 typedef void (*DieCallbackType)(void);
275 // It's possible to add several callbacks that would be run when "Die" is
276 // called. The callbacks will be run in the opposite order. The tools are
277 // strongly recommended to setup all callbacks during initialization, when there
278 // is only a single thread.
279 bool AddDieCallback(DieCallbackType callback);
280 bool RemoveDieCallback(DieCallbackType callback);
282 void SetUserDieCallback(DieCallbackType callback);
284 typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
286 void SetCheckFailedCallback(CheckFailedCallbackType callback);
288 // Callback will be called if soft_rss_limit_mb is given and the limit is
289 // exceeded (exceeded==true) or if rss went down below the limit
290 // (exceeded==false).
291 // The callback should be registered once at the tool init time.
292 void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded));
294 // Functions related to signal handling.
295 typedef void (*SignalHandlerType)(int, void *, void *);
296 HandleSignalMode GetHandleSignalMode(int signum);
297 void InstallDeadlySignalHandlers(SignalHandlerType handler);
300 // Each sanitizer uses slightly different implementation of stack unwinding.
301 typedef void (*UnwindSignalStackCallbackType)(const SignalContext &sig,
302 const void *callback_context,
303 BufferedStackTrace *stack);
304 // Print deadly signal report and die.
305 void HandleDeadlySignal(void *siginfo, void *context, u32 tid,
306 UnwindSignalStackCallbackType unwind,
307 const void *unwind_context);
309 // Part of HandleDeadlySignal, exposed for asan.
310 void StartReportDeadlySignal();
311 // Part of HandleDeadlySignal, exposed for asan.
312 void ReportDeadlySignal(const SignalContext &sig, u32 tid,
313 UnwindSignalStackCallbackType unwind,
314 const void *unwind_context);
316 // Alternative signal stack (POSIX-only).
317 void SetAlternateSignalStack();
318 void UnsetAlternateSignalStack();
320 // We don't want a summary too long.
321 const int kMaxSummaryLength = 1024;
322 // Construct a one-line string:
323 // SUMMARY: SanitizerToolName: error_message
324 // and pass it to __sanitizer_report_error_summary.
325 // If alt_tool_name is provided, it's used in place of SanitizerToolName.
326 void ReportErrorSummary(const char *error_message,
327 const char *alt_tool_name = nullptr);
328 // Same as above, but construct error_message as:
329 // error_type file:line[:column][ function]
330 void ReportErrorSummary(const char *error_type, const AddressInfo &info,
331 const char *alt_tool_name = nullptr);
332 // Same as above, but obtains AddressInfo by symbolizing top stack trace frame.
333 void ReportErrorSummary(const char *error_type, const StackTrace *trace,
334 const char *alt_tool_name = nullptr);
336 void ReportMmapWriteExec(int prot);
339 #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
341 unsigned char _BitScanForward(unsigned long *index, unsigned long mask); // NOLINT
342 unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); // NOLINT
344 unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask); // NOLINT
345 unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask); // NOLINT
350 INLINE uptr MostSignificantSetBitIndex(uptr x) {
352 unsigned long up; // NOLINT
353 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
355 up = SANITIZER_WORDSIZE - 1 - __builtin_clzll(x);
357 up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
359 #elif defined(_WIN64)
360 _BitScanReverse64(&up, x);
362 _BitScanReverse(&up, x);
367 INLINE uptr LeastSignificantSetBitIndex(uptr x) {
369 unsigned long up; // NOLINT
370 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
372 up = __builtin_ctzll(x);
374 up = __builtin_ctzl(x);
376 #elif defined(_WIN64)
377 _BitScanForward64(&up, x);
379 _BitScanForward(&up, x);
384 INLINE bool IsPowerOfTwo(uptr x) {
385 return (x & (x - 1)) == 0;
388 INLINE uptr RoundUpToPowerOfTwo(uptr size) {
390 if (IsPowerOfTwo(size)) return size;
392 uptr up = MostSignificantSetBitIndex(size);
393 CHECK_LT(size, (1ULL << (up + 1)));
394 CHECK_GT(size, (1ULL << up));
395 return 1ULL << (up + 1);
398 INLINE uptr RoundUpTo(uptr size, uptr boundary) {
399 RAW_CHECK(IsPowerOfTwo(boundary));
400 return (size + boundary - 1) & ~(boundary - 1);
403 INLINE uptr RoundDownTo(uptr x, uptr boundary) {
404 return x & ~(boundary - 1);
407 INLINE bool IsAligned(uptr a, uptr alignment) {
408 return (a & (alignment - 1)) == 0;
411 INLINE uptr Log2(uptr x) {
412 CHECK(IsPowerOfTwo(x));
413 return LeastSignificantSetBitIndex(x);
416 // Don't use std::min, std::max or std::swap, to minimize dependency
418 template<class T> T Min(T a, T b) { return a < b ? a : b; }
419 template<class T> T Max(T a, T b) { return a > b ? a : b; }
420 template<class T> void Swap(T& a, T& b) {
427 INLINE bool IsSpace(int c) {
428 return (c == ' ') || (c == '\n') || (c == '\t') ||
429 (c == '\f') || (c == '\r') || (c == '\v');
431 INLINE bool IsDigit(int c) {
432 return (c >= '0') && (c <= '9');
434 INLINE int ToLower(int c) {
435 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
438 // A low-level vector based on mmap. May incur a significant memory overhead for
440 // WARNING: The current implementation supports only POD types.
442 class InternalMmapVectorNoCtor {
444 void Initialize(uptr initial_capacity) {
448 reserve(initial_capacity);
450 void Destroy() { UnmapOrDie(data_, capacity_bytes_); }
451 T &operator[](uptr i) {
455 const T &operator[](uptr i) const {
459 void push_back(const T &element) {
460 CHECK_LE(size_, capacity());
461 if (size_ == capacity()) {
462 uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
463 Realloc(new_capacity);
465 internal_memcpy(&data_[size_++], &element, sizeof(T));
469 return data_[size_ - 1];
478 const T *data() const {
484 uptr capacity() const { return capacity_bytes_ / sizeof(T); }
485 void reserve(uptr new_size) {
486 // Never downsize internal buffer.
487 if (new_size > capacity())
490 void resize(uptr new_size) {
491 if (new_size > size_) {
493 internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
498 void clear() { size_ = 0; }
499 bool empty() const { return size() == 0; }
501 const T *begin() const {
507 const T *end() const {
508 return data() + size();
511 return data() + size();
514 void swap(InternalMmapVectorNoCtor &other) {
515 Swap(data_, other.data_);
516 Swap(capacity_bytes_, other.capacity_bytes_);
517 Swap(size_, other.size_);
521 void Realloc(uptr new_capacity) {
522 CHECK_GT(new_capacity, 0);
523 CHECK_LE(size_, new_capacity);
524 uptr new_capacity_bytes =
525 RoundUpTo(new_capacity * sizeof(T), GetPageSizeCached());
526 T *new_data = (T *)MmapOrDie(new_capacity_bytes, "InternalMmapVector");
527 internal_memcpy(new_data, data_, size_ * sizeof(T));
528 UnmapOrDie(data_, capacity_bytes_);
530 capacity_bytes_ = new_capacity_bytes;
534 uptr capacity_bytes_;
538 template <typename T>
539 bool operator==(const InternalMmapVectorNoCtor<T> &lhs,
540 const InternalMmapVectorNoCtor<T> &rhs) {
541 if (lhs.size() != rhs.size()) return false;
542 return internal_memcmp(lhs.data(), rhs.data(), lhs.size() * sizeof(T)) == 0;
545 template <typename T>
546 bool operator!=(const InternalMmapVectorNoCtor<T> &lhs,
547 const InternalMmapVectorNoCtor<T> &rhs) {
548 return !(lhs == rhs);
552 class InternalMmapVector : public InternalMmapVectorNoCtor<T> {
554 InternalMmapVector() { InternalMmapVectorNoCtor<T>::Initialize(1); }
555 explicit InternalMmapVector(uptr cnt) {
556 InternalMmapVectorNoCtor<T>::Initialize(cnt);
559 ~InternalMmapVector() { InternalMmapVectorNoCtor<T>::Destroy(); }
560 // Disallow copies and moves.
561 InternalMmapVector(const InternalMmapVector &) = delete;
562 InternalMmapVector &operator=(const InternalMmapVector &) = delete;
563 InternalMmapVector(InternalMmapVector &&) = delete;
564 InternalMmapVector &operator=(InternalMmapVector &&) = delete;
567 class InternalScopedString : public InternalMmapVector<char> {
569 explicit InternalScopedString(uptr max_length)
570 : InternalMmapVector<char>(max_length), length_(0) {
573 uptr length() { return length_; }
578 void append(const char *format, ...);
586 bool operator()(const T &a, const T &b) const { return a < b; }
589 // HeapSort for arrays and InternalMmapVector.
590 template <class T, class Compare = CompareLess<T>>
591 void Sort(T *v, uptr size, Compare comp = {}) {
594 // Stage 1: insert elements to the heap.
595 for (uptr i = 1; i < size; i++) {
597 for (j = i; j > 0; j = p) {
599 if (comp(v[p], v[j]))
605 // Stage 2: swap largest element with the last one,
606 // and sink the new top.
607 for (uptr i = size - 1; i > 0; i--) {
610 for (j = 0; j < i; j = max_ind) {
611 uptr left = 2 * j + 1;
612 uptr right = 2 * j + 2;
614 if (left < i && comp(v[max_ind], v[left]))
616 if (right < i && comp(v[max_ind], v[right]))
619 Swap(v[j], v[max_ind]);
626 // Works like std::lower_bound: finds the first element that is not less
628 template <class Container, class Value, class Compare>
629 uptr InternalLowerBound(const Container &v, uptr first, uptr last,
630 const Value &val, Compare comp) {
631 while (last > first) {
632 uptr mid = (first + last) / 2;
633 if (comp(v[mid], val))
653 // Opens the file 'file_name" and reads up to 'max_len' bytes.
654 // The resulting buffer is mmaped and stored in '*buff'.
655 // Returns true if file was successfully opened and read.
656 bool ReadFileToVector(const char *file_name,
657 InternalMmapVectorNoCtor<char> *buff,
658 uptr max_len = 1 << 26, error_t *errno_p = nullptr);
660 // Opens the file 'file_name" and reads up to 'max_len' bytes.
661 // This function is less I/O efficient than ReadFileToVector as it may reread
662 // file multiple times to avoid mmap during read attempts. It's used to read
663 // procmap, so short reads with mmap in between can produce inconsistent result.
664 // The resulting buffer is mmaped and stored in '*buff'.
665 // The size of the mmaped region is stored in '*buff_size'.
666 // The total number of read bytes is stored in '*read_len'.
667 // Returns true if file was successfully opened and read.
668 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
669 uptr *read_len, uptr max_len = 1 << 26,
670 error_t *errno_p = nullptr);
672 // When adding a new architecture, don't forget to also update
673 // script/asan_symbolize.py and sanitizer_symbolizer_libcdep.cc.
674 inline const char *ModuleArchToString(ModuleArch arch) {
676 case kModuleArchUnknown:
678 case kModuleArchI386:
680 case kModuleArchX86_64:
682 case kModuleArchX86_64H:
684 case kModuleArchARMV6:
686 case kModuleArchARMV7:
688 case kModuleArchARMV7S:
690 case kModuleArchARMV7K:
692 case kModuleArchARM64:
695 CHECK(0 && "Invalid module arch");
699 const uptr kModuleUUIDSize = 16;
700 const uptr kMaxSegName = 16;
702 // Represents a binary loaded into virtual memory (e.g. this can be an
703 // executable or a shared object).
707 : full_name_(nullptr),
709 max_executable_address_(0),
710 arch_(kModuleArchUnknown),
711 instrumented_(false) {
712 internal_memset(uuid_, 0, kModuleUUIDSize);
715 void set(const char *module_name, uptr base_address);
716 void set(const char *module_name, uptr base_address, ModuleArch arch,
717 u8 uuid[kModuleUUIDSize], bool instrumented);
719 void addAddressRange(uptr beg, uptr end, bool executable, bool writable,
720 const char *name = nullptr);
721 bool containsAddress(uptr address) const;
723 const char *full_name() const { return full_name_; }
724 uptr base_address() const { return base_address_; }
725 uptr max_executable_address() const { return max_executable_address_; }
726 ModuleArch arch() const { return arch_; }
727 const u8 *uuid() const { return uuid_; }
728 bool instrumented() const { return instrumented_; }
730 struct AddressRange {
736 char name[kMaxSegName];
738 AddressRange(uptr beg, uptr end, bool executable, bool writable,
743 executable(executable),
745 internal_strncpy(this->name, (name ? name : ""), ARRAY_SIZE(this->name));
749 const IntrusiveList<AddressRange> &ranges() const { return ranges_; }
752 char *full_name_; // Owned.
754 uptr max_executable_address_;
756 u8 uuid_[kModuleUUIDSize];
758 IntrusiveList<AddressRange> ranges_;
761 // List of LoadedModules. OS-dependent implementation is responsible for
762 // filling this information.
763 class ListOfModules {
765 ListOfModules() : initialized(false) {}
766 ~ListOfModules() { clear(); }
768 void fallbackInit(); // Uses fallback init if available, otherwise clears
769 const LoadedModule *begin() const { return modules_.begin(); }
770 LoadedModule *begin() { return modules_.begin(); }
771 const LoadedModule *end() const { return modules_.end(); }
772 LoadedModule *end() { return modules_.end(); }
773 uptr size() const { return modules_.size(); }
774 const LoadedModule &operator[](uptr i) const {
775 CHECK_LT(i, modules_.size());
781 for (auto &module : modules_) module.clear();
785 initialized ? clear() : modules_.Initialize(kInitialCapacity);
789 InternalMmapVectorNoCtor<LoadedModule> modules_;
790 // We rarely have more than 16K loaded modules.
791 static const uptr kInitialCapacity = 1 << 14;
795 // Callback type for iterating over a set of memory ranges.
796 typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
798 enum AndroidApiLevel {
799 ANDROID_NOT_ANDROID = 0,
801 ANDROID_LOLLIPOP_MR1 = 22,
802 ANDROID_POST_LOLLIPOP = 23
805 void WriteToSyslog(const char *buffer);
808 void LogFullErrorReport(const char *buffer);
810 INLINE void LogFullErrorReport(const char *buffer) {}
813 #if SANITIZER_LINUX || SANITIZER_MAC
814 void WriteOneLineToSyslog(const char *s);
815 void LogMessageOnPrintf(const char *str);
817 INLINE void WriteOneLineToSyslog(const char *s) {}
818 INLINE void LogMessageOnPrintf(const char *str) {}
822 // Initialize Android logging. Any writes before this are silently lost.
823 void AndroidLogInit();
824 void SetAbortMessage(const char *);
826 INLINE void AndroidLogInit() {}
827 // FIXME: MacOS implementation could use CRSetCrashLogMessage.
828 INLINE void SetAbortMessage(const char *) {}
831 #if SANITIZER_ANDROID
832 void SanitizerInitializeUnwinder();
833 AndroidApiLevel AndroidGetApiLevel();
835 INLINE void AndroidLogWrite(const char *buffer_unused) {}
836 INLINE void SanitizerInitializeUnwinder() {}
837 INLINE AndroidApiLevel AndroidGetApiLevel() { return ANDROID_NOT_ANDROID; }
840 INLINE uptr GetPthreadDestructorIterations() {
841 #if SANITIZER_ANDROID
842 return (AndroidGetApiLevel() == ANDROID_LOLLIPOP_MR1) ? 8 : 4;
843 #elif SANITIZER_POSIX
846 // Unused on Windows.
851 void *internal_start_thread(void(*func)(void*), void *arg);
852 void internal_join_thread(void *th);
853 void MaybeStartBackgroudThread();
855 // Make the compiler think that something is going on there.
856 // Use this inside a loop that looks like memset/memcpy/etc to prevent the
857 // compiler from recognising it and turning it into an actual call to
858 // memset/memcpy/etc.
859 static inline void SanitizerBreakOptimization(void *arg) {
860 #if defined(_MSC_VER) && !defined(__clang__)
863 __asm__ __volatile__("" : : "r" (arg) : "memory");
867 struct SignalContext {
874 bool is_memory_access;
875 enum WriteFlag { UNKNOWN, READ, WRITE } write_flag;
877 // VS2013 doesn't implement unrestricted unions, so we need a trivial default
879 SignalContext() = default;
881 // Creates signal context in a platform-specific manner.
882 // SignalContext is going to keep pointers to siginfo and context without
884 SignalContext(void *siginfo, void *context)
888 is_memory_access(IsMemoryAccess()),
889 write_flag(GetWriteFlag()) {
893 static void DumpAllRegisters(void *context);
895 // Type of signal e.g. SIGSEGV or EXCEPTION_ACCESS_VIOLATION.
898 // String description of the signal.
899 const char *Describe() const;
901 // Returns true if signal is stack overflow.
902 bool IsStackOverflow() const;
905 // Platform specific initialization.
907 uptr GetAddress() const;
908 WriteFlag GetWriteFlag() const;
909 bool IsMemoryAccess() const;
912 void InitializePlatformEarly();
915 template <typename Fn>
916 class RunOnDestruction {
918 explicit RunOnDestruction(Fn fn) : fn_(fn) {}
919 ~RunOnDestruction() { fn_(); }
925 // A simple scope guard. Usage:
926 // auto cleanup = at_scope_exit([]{ do_cleanup; });
927 template <typename Fn>
928 RunOnDestruction<Fn> at_scope_exit(Fn fn) {
929 return RunOnDestruction<Fn>(fn);
932 // Linux on 64-bit s390 had a nasty bug that crashes the whole machine
933 // if a process uses virtual memory over 4TB (as many sanitizers like
934 // to do). This function will abort the process if running on a kernel
935 // that looks vulnerable.
936 #if SANITIZER_LINUX && SANITIZER_S390_64
937 void AvoidCVE_2016_2143();
939 INLINE void AvoidCVE_2016_2143() {}
942 struct StackDepotStats {
947 // The default value for allocator_release_to_os_interval_ms common flag to
948 // indicate that sanitizer allocator should not attempt to release memory to OS.
949 const s32 kReleaseToOSIntervalNever = -1;
951 void CheckNoDeepBind(const char *filename, int flag);
953 // Returns the requested amount of random data (up to 256 bytes) that can then
954 // be used to seed a PRNG. Defaults to blocking like the underlying syscall.
955 bool GetRandom(void *buffer, uptr length, bool blocking = true);
957 // Returns the number of logical processors on the system.
958 u32 GetNumberOfCPUs();
959 extern u32 NumberOfCPUsCached;
960 INLINE u32 GetNumberOfCPUsCached() {
961 if (!NumberOfCPUsCached)
962 NumberOfCPUsCached = GetNumberOfCPUs();
963 return NumberOfCPUsCached;
966 } // namespace __sanitizer
968 inline void *operator new(__sanitizer::operator_new_size_type size,
969 __sanitizer::LowLevelAllocator &alloc) {
970 return alloc.Allocate(size);
973 #endif // SANITIZER_COMMON_H