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 {
36 const uptr kWordSize = SANITIZER_WORDSIZE / 8;
37 const uptr kWordSizeInBits = 8 * kWordSize;
39 #if defined(__powerpc__) || defined(__powerpc64__)
40 const uptr kCacheLineSize = 128;
42 const uptr kCacheLineSize = 64;
45 const uptr kMaxPathLength = 4096;
47 const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
49 static const uptr kErrorMessageBufferSize = 1 << 16;
51 // Denotes fake PC values that come from JIT/JAVA/etc.
52 // For such PC values __tsan_symbolize_external() will be called.
53 const u64 kExternalPCBit = 1ULL << 60;
55 extern const char *SanitizerToolName; // Can be changed by the tool.
57 extern atomic_uint32_t current_verbosity;
58 INLINE void SetVerbosity(int verbosity) {
59 atomic_store(¤t_verbosity, verbosity, memory_order_relaxed);
61 INLINE int Verbosity() {
62 return atomic_load(¤t_verbosity, memory_order_relaxed);
66 extern uptr PageSizeCached;
67 INLINE uptr GetPageSizeCached() {
69 PageSizeCached = GetPageSize();
70 return PageSizeCached;
72 uptr GetMmapGranularity();
73 uptr GetMaxVirtualAddress();
77 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
79 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
80 uptr *tls_addr, uptr *tls_size);
83 void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false);
84 INLINE void *MmapOrDieQuietly(uptr size, const char *mem_type) {
85 return MmapOrDie(size, mem_type, /*raw_report*/ true);
87 void UnmapOrDie(void *addr, uptr size);
88 void *MmapFixedNoReserve(uptr fixed_addr, uptr size,
89 const char *name = nullptr);
90 void *MmapNoReserveOrDie(uptr size, const char *mem_type);
91 void *MmapFixedOrDie(uptr fixed_addr, uptr size);
92 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name = nullptr);
93 void *MmapNoAccess(uptr size);
94 // Map aligned chunk of address space; size and alignment are powers of two.
95 void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type);
96 // Disallow access to a memory range. Use MmapFixedNoAccess to allocate an
97 // unaccessible memory.
98 bool MprotectNoAccess(uptr addr, uptr size);
99 bool MprotectReadOnly(uptr addr, uptr size);
101 // Used to check if we can map shadow memory to a fixed location.
102 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
103 void FlushUnneededShadowMemory(uptr addr, uptr size);
104 void IncreaseTotalMmap(uptr size);
105 void DecreaseTotalMmap(uptr size);
107 void NoHugePagesInRegion(uptr addr, uptr length);
108 void DontDumpShadowMemory(uptr addr, uptr length);
109 // Check if the built VMA size matches the runtime one.
111 void RunMallocHooks(const void *ptr, uptr size);
112 void RunFreeHooks(const void *ptr);
114 // InternalScopedBuffer can be used instead of large stack arrays to
115 // keep frame size low.
116 // FIXME: use InternalAlloc instead of MmapOrDie once
117 // InternalAlloc is made libc-free.
119 class InternalScopedBuffer {
121 explicit InternalScopedBuffer(uptr cnt) {
123 ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
125 ~InternalScopedBuffer() {
126 UnmapOrDie(ptr_, cnt_ * sizeof(T));
128 T &operator[](uptr i) { return ptr_[i]; }
129 T *data() { return ptr_; }
130 uptr size() { return cnt_ * sizeof(T); }
135 // Disallow evil constructors.
136 InternalScopedBuffer(const InternalScopedBuffer&);
137 void operator=(const InternalScopedBuffer&);
140 class InternalScopedString : public InternalScopedBuffer<char> {
142 explicit InternalScopedString(uptr max_length)
143 : InternalScopedBuffer<char>(max_length), length_(0) {
146 uptr length() { return length_; }
151 void append(const char *format, ...);
157 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
158 // constructor, so all instances of LowLevelAllocator should be
159 // linker initialized.
160 class LowLevelAllocator {
162 // Requires an external lock.
163 void *Allocate(uptr size);
165 char *allocated_end_;
166 char *allocated_current_;
168 typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
169 // Allows to register tool-specific callbacks for LowLevelAllocator.
170 // Passing NULL removes the callback.
171 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
174 void RawWrite(const char *buffer);
175 bool ColorizeReports();
176 void RemoveANSIEscapeSequencesFromString(char *buffer);
177 void Printf(const char *format, ...);
178 void Report(const char *format, ...);
179 void SetPrintfAndReportCallback(void (*callback)(const char *));
180 #define VReport(level, ...) \
182 if ((uptr)Verbosity() >= (level)) Report(__VA_ARGS__); \
184 #define VPrintf(level, ...) \
186 if ((uptr)Verbosity() >= (level)) Printf(__VA_ARGS__); \
189 // Can be used to prevent mixing error reports from different sanitizers.
190 extern StaticSpinMutex CommonSanitizerReportMutex;
193 void Write(const char *buffer, uptr length);
194 bool SupportsColors();
195 void SetReportPath(const char *path);
197 // Don't use fields directly. They are only declared public to allow
198 // aggregate initialization.
200 // Protects fields below.
202 // Opened file descriptor. Defaults to stderr. It may be equal to
203 // kInvalidFd, in which case new file will be opened when necessary.
205 // Path prefix of report file, set via __sanitizer_set_report_path.
206 char path_prefix[kMaxPathLength];
207 // Full path to report, obtained as <path_prefix>.PID
208 char full_path[kMaxPathLength];
209 // PID of the process that opened fd. If a fork() occurs,
210 // the PID of child will be different from fd_pid.
214 void ReopenIfNecessary();
216 extern ReportFile report_file;
218 extern uptr stoptheworld_tracer_pid;
219 extern uptr stoptheworld_tracer_ppid;
221 enum FileAccessMode {
227 // Returns kInvalidFd on error.
228 fd_t OpenFile(const char *filename, FileAccessMode mode,
229 error_t *errno_p = nullptr);
230 void CloseFile(fd_t);
232 // Return true on success, false on error.
233 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size,
234 uptr *bytes_read = nullptr, error_t *error_p = nullptr);
235 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size,
236 uptr *bytes_written = nullptr, error_t *error_p = nullptr);
238 bool RenameFile(const char *oldpath, const char *newpath,
239 error_t *error_p = nullptr);
241 // Scoped file handle closer.
243 explicit FileCloser(fd_t fd) : fd(fd) {}
244 ~FileCloser() { CloseFile(fd); }
248 bool SupportsColoredOutput(fd_t fd);
250 // Opens the file 'file_name" and reads up to 'max_len' bytes.
251 // The resulting buffer is mmaped and stored in '*buff'.
252 // The size of the mmaped region is stored in '*buff_size'.
253 // The total number of read bytes is stored in '*read_len'.
254 // Returns true if file was successfully opened and read.
255 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
256 uptr *read_len, uptr max_len = 1 << 26,
257 error_t *errno_p = nullptr);
258 // Maps given file to virtual memory, and returns pointer to it
259 // (or NULL if mapping fails). Stores the size of mmaped region
261 void *MapFileToMemory(const char *file_name, uptr *buff_size);
262 void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset);
264 bool IsAccessibleMemoryRange(uptr beg, uptr size);
266 // Error report formatting.
267 const char *StripPathPrefix(const char *filepath,
268 const char *strip_file_prefix);
269 // Strip the directories from the module name.
270 const char *StripModuleName(const char *module);
273 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
274 uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len);
275 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len);
276 const char *GetProcessName();
277 void UpdateProcessName();
278 void CacheBinaryName();
279 void DisableCoreDumperIfNecessary();
280 void DumpProcessMap();
281 bool FileExists(const char *filename);
282 const char *GetEnv(const char *name);
283 bool SetEnv(const char *name, const char *value);
284 const char *GetPwd();
285 char *FindPathToBinary(const char *name);
286 bool IsPathSeparator(const char c);
287 bool IsAbsolutePath(const char *path);
288 // Starts a subprocess and returs its pid.
289 // If *_fd parameters are not kInvalidFd their corresponding input/output
290 // streams will be redirect to the file. The files will always be closed
291 // in parent process even in case of an error.
292 // The child process will close all fds after STDERR_FILENO
293 // before passing control to a program.
294 pid_t StartSubprocess(const char *filename, const char *const argv[],
295 fd_t stdin_fd = kInvalidFd, fd_t stdout_fd = kInvalidFd,
296 fd_t stderr_fd = kInvalidFd);
297 // Checks if specified process is still running
298 bool IsProcessRunning(pid_t pid);
299 // Waits for the process to finish and returns its exit code.
300 // Returns -1 in case of an error.
301 int WaitForProcess(pid_t pid);
307 bool StackSizeIsUnlimited();
308 uptr GetStackSizeLimitInBytes();
309 void SetStackSizeLimitInBytes(uptr limit);
310 bool AddressSpaceIsUnlimited();
311 void SetAddressSpaceUnlimited();
312 void AdjustStackSize(void *attr);
313 void PrepareForSandboxing(__sanitizer_sandbox_arguments *args);
314 void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args);
315 void SetSandboxingCallback(void (*f)());
317 void CoverageUpdateMapping();
318 void CovBeforeFork();
319 void CovAfterFork(int child_pid);
321 void InitializeCoverage(bool enabled, const char *coverage_dir);
322 void ReInitializeCoverage(bool enabled, const char *coverage_dir);
328 void SleepForSeconds(int seconds);
329 void SleepForMillis(int millis);
331 int Atexit(void (*function)(void));
332 void SortArray(uptr *array, uptr size);
333 bool TemplateMatch(const char *templ, const char *str);
336 void NORETURN Abort();
339 CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
340 void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
341 const char *mmap_type, error_t err,
342 bool raw_report = false);
344 // Set the name of the current thread to 'name', return true on succees.
345 // The name may be truncated to a system-dependent limit.
346 bool SanitizerSetThreadName(const char *name);
347 // Get the name of the current thread (no more than max_len bytes),
348 // return true on succees. name should have space for at least max_len+1 bytes.
349 bool SanitizerGetThreadName(char *name, int max_len);
351 // Specific tools may override behavior of "Die" and "CheckFailed" functions
352 // to do tool-specific job.
353 typedef void (*DieCallbackType)(void);
355 // It's possible to add several callbacks that would be run when "Die" is
356 // called. The callbacks will be run in the opposite order. The tools are
357 // strongly recommended to setup all callbacks during initialization, when there
358 // is only a single thread.
359 bool AddDieCallback(DieCallbackType callback);
360 bool RemoveDieCallback(DieCallbackType callback);
362 void SetUserDieCallback(DieCallbackType callback);
364 typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
366 void SetCheckFailedCallback(CheckFailedCallbackType callback);
368 // Callback will be called if soft_rss_limit_mb is given and the limit is
369 // exceeded (exceeded==true) or if rss went down below the limit
370 // (exceeded==false).
371 // The callback should be registered once at the tool init time.
372 void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded));
374 // Functions related to signal handling.
375 typedef void (*SignalHandlerType)(int, void *, void *);
376 bool IsHandledDeadlySignal(int signum);
377 void InstallDeadlySignalHandlers(SignalHandlerType handler);
378 // Alternative signal stack (POSIX-only).
379 void SetAlternateSignalStack();
380 void UnsetAlternateSignalStack();
382 // We don't want a summary too long.
383 const int kMaxSummaryLength = 1024;
384 // Construct a one-line string:
385 // SUMMARY: SanitizerToolName: error_message
386 // and pass it to __sanitizer_report_error_summary.
387 void ReportErrorSummary(const char *error_message);
388 // Same as above, but construct error_message as:
389 // error_type file:line[:column][ function]
390 void ReportErrorSummary(const char *error_type, const AddressInfo &info);
391 // Same as above, but obtains AddressInfo by symbolizing top stack trace frame.
392 void ReportErrorSummary(const char *error_type, StackTrace *trace);
395 #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
397 unsigned char _BitScanForward(unsigned long *index, unsigned long mask); // NOLINT
398 unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); // NOLINT
400 unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask); // NOLINT
401 unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask); // NOLINT
406 INLINE uptr MostSignificantSetBitIndex(uptr x) {
408 unsigned long up; // NOLINT
409 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
411 up = SANITIZER_WORDSIZE - 1 - __builtin_clzll(x);
413 up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
415 #elif defined(_WIN64)
416 _BitScanReverse64(&up, x);
418 _BitScanReverse(&up, x);
423 INLINE uptr LeastSignificantSetBitIndex(uptr x) {
425 unsigned long up; // NOLINT
426 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
428 up = __builtin_ctzll(x);
430 up = __builtin_ctzl(x);
432 #elif defined(_WIN64)
433 _BitScanForward64(&up, x);
435 _BitScanForward(&up, x);
440 INLINE bool IsPowerOfTwo(uptr x) {
441 return (x & (x - 1)) == 0;
444 INLINE uptr RoundUpToPowerOfTwo(uptr size) {
446 if (IsPowerOfTwo(size)) return size;
448 uptr up = MostSignificantSetBitIndex(size);
449 CHECK(size < (1ULL << (up + 1)));
450 CHECK(size > (1ULL << up));
451 return 1ULL << (up + 1);
454 INLINE uptr RoundUpTo(uptr size, uptr boundary) {
455 RAW_CHECK(IsPowerOfTwo(boundary));
456 return (size + boundary - 1) & ~(boundary - 1);
459 INLINE uptr RoundDownTo(uptr x, uptr boundary) {
460 return x & ~(boundary - 1);
463 INLINE bool IsAligned(uptr a, uptr alignment) {
464 return (a & (alignment - 1)) == 0;
467 INLINE uptr Log2(uptr x) {
468 CHECK(IsPowerOfTwo(x));
469 return LeastSignificantSetBitIndex(x);
472 // Don't use std::min, std::max or std::swap, to minimize dependency
474 template<class T> T Min(T a, T b) { return a < b ? a : b; }
475 template<class T> T Max(T a, T b) { return a > b ? a : b; }
476 template<class T> void Swap(T& a, T& b) {
483 INLINE bool IsSpace(int c) {
484 return (c == ' ') || (c == '\n') || (c == '\t') ||
485 (c == '\f') || (c == '\r') || (c == '\v');
487 INLINE bool IsDigit(int c) {
488 return (c >= '0') && (c <= '9');
490 INLINE int ToLower(int c) {
491 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
494 // A low-level vector based on mmap. May incur a significant memory overhead for
496 // WARNING: The current implementation supports only POD types.
498 class InternalMmapVectorNoCtor {
500 void Initialize(uptr initial_capacity) {
501 capacity_ = Max(initial_capacity, (uptr)1);
503 data_ = (T *)MmapOrDie(capacity_ * sizeof(T), "InternalMmapVectorNoCtor");
506 UnmapOrDie(data_, capacity_ * sizeof(T));
508 T &operator[](uptr i) {
512 const T &operator[](uptr i) const {
516 void push_back(const T &element) {
517 CHECK_LE(size_, capacity_);
518 if (size_ == capacity_) {
519 uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
520 Resize(new_capacity);
522 internal_memcpy(&data_[size_++], &element, sizeof(T));
526 return data_[size_ - 1];
535 const T *data() const {
541 uptr capacity() const {
545 void clear() { size_ = 0; }
546 bool empty() const { return size() == 0; }
548 const T *begin() const {
554 const T *end() const {
555 return data() + size();
558 return data() + size();
562 void Resize(uptr new_capacity) {
563 CHECK_GT(new_capacity, 0);
564 CHECK_LE(size_, new_capacity);
565 T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),
566 "InternalMmapVector");
567 internal_memcpy(new_data, data_, size_ * sizeof(T));
570 UnmapOrDie(old_data, capacity_ * sizeof(T));
571 capacity_ = new_capacity;
580 class InternalMmapVector : public InternalMmapVectorNoCtor<T> {
582 explicit InternalMmapVector(uptr initial_capacity) {
583 InternalMmapVectorNoCtor<T>::Initialize(initial_capacity);
585 ~InternalMmapVector() { InternalMmapVectorNoCtor<T>::Destroy(); }
586 // Disallow evil constructors.
587 InternalMmapVector(const InternalMmapVector&);
588 void operator=(const InternalMmapVector&);
591 // HeapSort for arrays and InternalMmapVector.
592 template<class Container, class Compare>
593 void InternalSort(Container *v, uptr size, Compare comp) {
596 // Stage 1: insert elements to the heap.
597 for (uptr i = 1; i < size; i++) {
599 for (j = i; j > 0; j = p) {
601 if (comp((*v)[p], (*v)[j]))
602 Swap((*v)[j], (*v)[p]);
607 // Stage 2: swap largest element with the last one,
608 // and sink the new top.
609 for (uptr i = size - 1; i > 0; i--) {
610 Swap((*v)[0], (*v)[i]);
612 for (j = 0; j < i; j = max_ind) {
613 uptr left = 2 * j + 1;
614 uptr right = 2 * j + 2;
616 if (left < i && comp((*v)[max_ind], (*v)[left]))
618 if (right < i && comp((*v)[max_ind], (*v)[right]))
621 Swap((*v)[j], (*v)[max_ind]);
628 template<class Container, class Value, class Compare>
629 uptr InternalBinarySearch(const Container &v, uptr first, uptr last,
630 const Value &val, Compare comp) {
631 uptr not_found = last + 1;
632 while (last >= first) {
633 uptr mid = (first + last) / 2;
634 if (comp(v[mid], val))
636 else if (comp(val, v[mid]))
644 // Represents a binary loaded into virtual memory (e.g. this can be an
645 // executable or a shared object).
648 LoadedModule() : full_name_(nullptr), base_address_(0) { ranges_.clear(); }
649 void set(const char *module_name, uptr base_address);
651 void addAddressRange(uptr beg, uptr end, bool executable);
652 bool containsAddress(uptr address) const;
654 const char *full_name() const { return full_name_; }
655 uptr base_address() const { return base_address_; }
657 struct AddressRange {
663 AddressRange(uptr beg, uptr end, bool executable)
664 : next(nullptr), beg(beg), end(end), executable(executable) {}
667 const IntrusiveList<AddressRange> &ranges() const { return ranges_; }
670 char *full_name_; // Owned.
672 IntrusiveList<AddressRange> ranges_;
675 // List of LoadedModules. OS-dependent implementation is responsible for
676 // filling this information.
677 class ListOfModules {
679 ListOfModules() : modules_(kInitialCapacity) {}
680 ~ListOfModules() { clear(); }
682 const LoadedModule *begin() const { return modules_.begin(); }
683 LoadedModule *begin() { return modules_.begin(); }
684 const LoadedModule *end() const { return modules_.end(); }
685 LoadedModule *end() { return modules_.end(); }
686 uptr size() const { return modules_.size(); }
687 const LoadedModule &operator[](uptr i) const {
688 CHECK_LT(i, modules_.size());
694 for (auto &module : modules_) module.clear();
698 InternalMmapVector<LoadedModule> modules_;
699 // We rarely have more than 16K loaded modules.
700 static const uptr kInitialCapacity = 1 << 14;
703 // Callback type for iterating over a set of memory ranges.
704 typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
706 enum AndroidApiLevel {
707 ANDROID_NOT_ANDROID = 0,
709 ANDROID_LOLLIPOP_MR1 = 22,
710 ANDROID_POST_LOLLIPOP = 23
713 void WriteToSyslog(const char *buffer);
716 void LogFullErrorReport(const char *buffer);
718 INLINE void LogFullErrorReport(const char *buffer) {}
721 #if SANITIZER_LINUX || SANITIZER_MAC
722 void WriteOneLineToSyslog(const char *s);
723 void LogMessageOnPrintf(const char *str);
725 INLINE void WriteOneLineToSyslog(const char *s) {}
726 INLINE void LogMessageOnPrintf(const char *str) {}
730 // Initialize Android logging. Any writes before this are silently lost.
731 void AndroidLogInit();
733 INLINE void AndroidLogInit() {}
736 #if SANITIZER_ANDROID
737 void SanitizerInitializeUnwinder();
738 AndroidApiLevel AndroidGetApiLevel();
740 INLINE void AndroidLogWrite(const char *buffer_unused) {}
741 INLINE void SanitizerInitializeUnwinder() {}
742 INLINE AndroidApiLevel AndroidGetApiLevel() { return ANDROID_NOT_ANDROID; }
745 INLINE uptr GetPthreadDestructorIterations() {
746 #if SANITIZER_ANDROID
747 return (AndroidGetApiLevel() == ANDROID_LOLLIPOP_MR1) ? 8 : 4;
748 #elif SANITIZER_POSIX
751 // Unused on Windows.
756 void *internal_start_thread(void(*func)(void*), void *arg);
757 void internal_join_thread(void *th);
758 void MaybeStartBackgroudThread();
760 // Make the compiler think that something is going on there.
761 // Use this inside a loop that looks like memset/memcpy/etc to prevent the
762 // compiler from recognising it and turning it into an actual call to
763 // memset/memcpy/etc.
764 static inline void SanitizerBreakOptimization(void *arg) {
765 #if defined(_MSC_VER) && !defined(__clang__)
768 __asm__ __volatile__("" : : "r" (arg) : "memory");
772 struct SignalContext {
778 bool is_memory_access;
780 enum WriteFlag { UNKNOWN, READ, WRITE } write_flag;
782 SignalContext(void *context, uptr addr, uptr pc, uptr sp, uptr bp,
783 bool is_memory_access, WriteFlag write_flag)
789 is_memory_access(is_memory_access),
790 write_flag(write_flag) {}
792 // Creates signal context in a platform-specific manner.
793 static SignalContext Create(void *siginfo, void *context);
795 // Returns true if the "context" indicates a memory write.
796 static WriteFlag GetWriteFlag(void *context);
799 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
803 template <typename Fn>
804 class RunOnDestruction {
806 explicit RunOnDestruction(Fn fn) : fn_(fn) {}
807 ~RunOnDestruction() { fn_(); }
813 // A simple scope guard. Usage:
814 // auto cleanup = at_scope_exit([]{ do_cleanup; });
815 template <typename Fn>
816 RunOnDestruction<Fn> at_scope_exit(Fn fn) {
817 return RunOnDestruction<Fn>(fn);
820 // Linux on 64-bit s390 had a nasty bug that crashes the whole machine
821 // if a process uses virtual memory over 4TB (as many sanitizers like
822 // to do). This function will abort the process if running on a kernel
823 // that looks vulnerable.
824 #if SANITIZER_LINUX && SANITIZER_S390_64
825 void AvoidCVE_2016_2143();
827 INLINE void AvoidCVE_2016_2143() {}
830 } // namespace __sanitizer
832 inline void *operator new(__sanitizer::operator_new_size_type size,
833 __sanitizer::LowLevelAllocator &alloc) {
834 return alloc.Allocate(size);
837 struct StackDepotStats {
842 #endif // SANITIZER_COMMON_H