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 AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
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_internal_defs.h"
20 #include "sanitizer_libc.h"
21 #include "sanitizer_mutex.h"
22 #include "sanitizer_flags.h"
24 namespace __sanitizer {
28 const uptr kWordSize = SANITIZER_WORDSIZE / 8;
29 const uptr kWordSizeInBits = 8 * kWordSize;
31 #if defined(__powerpc__) || defined(__powerpc64__)
32 const uptr kCacheLineSize = 128;
34 const uptr kCacheLineSize = 64;
37 const uptr kMaxPathLength = 4096;
39 const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
41 extern const char *SanitizerToolName; // Can be changed by the tool.
44 uptr GetPageSizeCached();
45 uptr GetMmapGranularity();
46 uptr GetMaxVirtualAddress();
50 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
52 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
53 uptr *tls_addr, uptr *tls_size);
56 void *MmapOrDie(uptr size, const char *mem_type);
57 void UnmapOrDie(void *addr, uptr size);
58 void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
59 void *MmapNoReserveOrDie(uptr size, const char *mem_type);
60 void *MmapFixedOrDie(uptr fixed_addr, uptr size);
61 void *Mprotect(uptr fixed_addr, uptr size);
62 // Map aligned chunk of address space; size and alignment are powers of two.
63 void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type);
64 // Used to check if we can map shadow memory to a fixed location.
65 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
66 void FlushUnneededShadowMemory(uptr addr, uptr size);
67 void IncreaseTotalMmap(uptr size);
68 void DecreaseTotalMmap(uptr size);
71 // InternalScopedBuffer can be used instead of large stack arrays to
72 // keep frame size low.
73 // FIXME: use InternalAlloc instead of MmapOrDie once
74 // InternalAlloc is made libc-free.
76 class InternalScopedBuffer {
78 explicit InternalScopedBuffer(uptr cnt) {
80 ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
82 ~InternalScopedBuffer() {
83 UnmapOrDie(ptr_, cnt_ * sizeof(T));
85 T &operator[](uptr i) { return ptr_[i]; }
86 T *data() { return ptr_; }
87 uptr size() { return cnt_ * sizeof(T); }
92 // Disallow evil constructors.
93 InternalScopedBuffer(const InternalScopedBuffer&);
94 void operator=(const InternalScopedBuffer&);
97 class InternalScopedString : public InternalScopedBuffer<char> {
99 explicit InternalScopedString(uptr max_length)
100 : InternalScopedBuffer<char>(max_length), length_(0) {
103 uptr length() { return length_; }
108 void append(const char *format, ...);
114 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
115 // constructor, so all instances of LowLevelAllocator should be
116 // linker initialized.
117 class LowLevelAllocator {
119 // Requires an external lock.
120 void *Allocate(uptr size);
122 char *allocated_end_;
123 char *allocated_current_;
125 typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
126 // Allows to register tool-specific callbacks for LowLevelAllocator.
127 // Passing NULL removes the callback.
128 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
131 void RawWrite(const char *buffer);
132 bool ColorizeReports();
133 void Printf(const char *format, ...);
134 void Report(const char *format, ...);
135 void SetPrintfAndReportCallback(void (*callback)(const char *));
136 #define VReport(level, ...) \
138 if ((uptr)common_flags()->verbosity >= (level)) Report(__VA_ARGS__); \
140 #define VPrintf(level, ...) \
142 if ((uptr)common_flags()->verbosity >= (level)) Printf(__VA_ARGS__); \
145 // Can be used to prevent mixing error reports from different sanitizers.
146 extern StaticSpinMutex CommonSanitizerReportMutex;
149 void Write(const char *buffer, uptr length);
151 void SetReportPath(const char *path);
153 // Don't use fields directly. They are only declared public to allow
154 // aggregate initialization.
156 // Protects fields below.
158 // Opened file descriptor. Defaults to stderr. It may be equal to
159 // kInvalidFd, in which case new file will be opened when necessary.
161 // Path prefix of report file, set via __sanitizer_set_report_path.
162 char path_prefix[kMaxPathLength];
163 // Full path to report, obtained as <path_prefix>.PID
164 char full_path[kMaxPathLength];
165 // PID of the process that opened fd. If a fork() occurs,
166 // the PID of child will be different from fd_pid.
170 void ReopenIfNecessary();
172 extern ReportFile report_file;
174 extern uptr stoptheworld_tracer_pid;
175 extern uptr stoptheworld_tracer_ppid;
177 uptr OpenFile(const char *filename, bool write);
178 // Opens the file 'file_name" and reads up to 'max_len' bytes.
179 // The resulting buffer is mmaped and stored in '*buff'.
180 // The size of the mmaped region is stored in '*buff_size',
181 // Returns the number of read bytes or 0 if file can not be opened.
182 uptr ReadFileToBuffer(const char *file_name, char **buff,
183 uptr *buff_size, uptr max_len);
184 // Maps given file to virtual memory, and returns pointer to it
185 // (or NULL if the mapping failes). Stores the size of mmaped region
187 void *MapFileToMemory(const char *file_name, uptr *buff_size);
188 void *MapWritableFileToMemory(void *addr, uptr size, uptr fd, uptr offset);
190 bool IsAccessibleMemoryRange(uptr beg, uptr size);
192 // Error report formatting.
193 const char *StripPathPrefix(const char *filepath,
194 const char *strip_file_prefix);
195 // Strip the directories from the module name.
196 const char *StripModuleName(const char *module);
199 void DisableCoreDumperIfNecessary();
200 void DumpProcessMap();
201 bool FileExists(const char *filename);
202 const char *GetEnv(const char *name);
203 bool SetEnv(const char *name, const char *value);
204 const char *GetPwd();
205 char *FindPathToBinary(const char *name);
208 bool StackSizeIsUnlimited();
209 void SetStackSizeLimitInBytes(uptr limit);
210 bool AddressSpaceIsUnlimited();
211 void SetAddressSpaceUnlimited();
212 void AdjustStackSize(void *attr);
213 void PrepareForSandboxing(__sanitizer_sandbox_arguments *args);
214 void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args);
215 void SetSandboxingCallback(void (*f)());
217 void CovUpdateMapping(uptr caller_pc = 0);
218 void CovBeforeFork();
219 void CovAfterFork(int child_pid);
225 void SleepForSeconds(int seconds);
226 void SleepForMillis(int millis);
228 int Atexit(void (*function)(void));
229 void SortArray(uptr *array, uptr size);
232 void NORETURN Abort();
235 CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
237 // Set the name of the current thread to 'name', return true on succees.
238 // The name may be truncated to a system-dependent limit.
239 bool SanitizerSetThreadName(const char *name);
240 // Get the name of the current thread (no more than max_len bytes),
241 // return true on succees. name should have space for at least max_len+1 bytes.
242 bool SanitizerGetThreadName(char *name, int max_len);
244 // Specific tools may override behavior of "Die" and "CheckFailed" functions
245 // to do tool-specific job.
246 typedef void (*DieCallbackType)(void);
247 void SetDieCallback(DieCallbackType);
248 DieCallbackType GetDieCallback();
249 typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
251 void SetCheckFailedCallback(CheckFailedCallbackType callback);
253 // Functions related to signal handling.
254 typedef void (*SignalHandlerType)(int, void *, void *);
255 bool IsDeadlySignal(int signum);
256 void InstallDeadlySignalHandlers(SignalHandlerType handler);
257 // Alternative signal stack (POSIX-only).
258 void SetAlternateSignalStack();
259 void UnsetAlternateSignalStack();
261 // We don't want a summary too long.
262 const int kMaxSummaryLength = 1024;
263 // Construct a one-line string:
264 // SUMMARY: SanitizerToolName: error_message
265 // and pass it to __sanitizer_report_error_summary.
266 void ReportErrorSummary(const char *error_message);
267 // Same as above, but construct error_message as:
268 // error_type file:line function
269 void ReportErrorSummary(const char *error_type, const char *file,
270 int line, const char *function);
271 void ReportErrorSummary(const char *error_type, StackTrace *trace);
274 #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
276 unsigned char _BitScanForward(unsigned long *index, unsigned long mask); // NOLINT
277 unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); // NOLINT
279 unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask); // NOLINT
280 unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask); // NOLINT
285 INLINE uptr MostSignificantSetBitIndex(uptr x) {
287 unsigned long up; // NOLINT
288 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
289 up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
290 #elif defined(_WIN64)
291 _BitScanReverse64(&up, x);
293 _BitScanReverse(&up, x);
298 INLINE uptr LeastSignificantSetBitIndex(uptr x) {
300 unsigned long up; // NOLINT
301 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
302 up = __builtin_ctzl(x);
303 #elif defined(_WIN64)
304 _BitScanForward64(&up, x);
306 _BitScanForward(&up, x);
311 INLINE bool IsPowerOfTwo(uptr x) {
312 return (x & (x - 1)) == 0;
315 INLINE uptr RoundUpToPowerOfTwo(uptr size) {
317 if (IsPowerOfTwo(size)) return size;
319 uptr up = MostSignificantSetBitIndex(size);
320 CHECK(size < (1ULL << (up + 1)));
321 CHECK(size > (1ULL << up));
322 return 1UL << (up + 1);
325 INLINE uptr RoundUpTo(uptr size, uptr boundary) {
326 CHECK(IsPowerOfTwo(boundary));
327 return (size + boundary - 1) & ~(boundary - 1);
330 INLINE uptr RoundDownTo(uptr x, uptr boundary) {
331 return x & ~(boundary - 1);
334 INLINE bool IsAligned(uptr a, uptr alignment) {
335 return (a & (alignment - 1)) == 0;
338 INLINE uptr Log2(uptr x) {
339 CHECK(IsPowerOfTwo(x));
340 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
341 return __builtin_ctzl(x);
342 #elif defined(_WIN64)
343 unsigned long ret; // NOLINT
344 _BitScanForward64(&ret, x);
347 unsigned long ret; // NOLINT
348 _BitScanForward(&ret, x);
353 // Don't use std::min, std::max or std::swap, to minimize dependency
355 template<class T> T Min(T a, T b) { return a < b ? a : b; }
356 template<class T> T Max(T a, T b) { return a > b ? a : b; }
357 template<class T> void Swap(T& a, T& b) {
364 INLINE bool IsSpace(int c) {
365 return (c == ' ') || (c == '\n') || (c == '\t') ||
366 (c == '\f') || (c == '\r') || (c == '\v');
368 INLINE bool IsDigit(int c) {
369 return (c >= '0') && (c <= '9');
371 INLINE int ToLower(int c) {
372 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
375 // A low-level vector based on mmap. May incur a significant memory overhead for
377 // WARNING: The current implementation supports only POD types.
379 class InternalMmapVector {
381 explicit InternalMmapVector(uptr initial_capacity) {
382 capacity_ = Max(initial_capacity, (uptr)1);
384 data_ = (T *)MmapOrDie(capacity_ * sizeof(T), "InternalMmapVector");
386 ~InternalMmapVector() {
387 UnmapOrDie(data_, capacity_ * sizeof(T));
389 T &operator[](uptr i) {
393 const T &operator[](uptr i) const {
397 void push_back(const T &element) {
398 CHECK_LE(size_, capacity_);
399 if (size_ == capacity_) {
400 uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
401 Resize(new_capacity);
403 data_[size_++] = element;
407 return data_[size_ - 1];
416 const T *data() const {
419 uptr capacity() const {
423 void clear() { size_ = 0; }
426 void Resize(uptr new_capacity) {
427 CHECK_GT(new_capacity, 0);
428 CHECK_LE(size_, new_capacity);
429 T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),
430 "InternalMmapVector");
431 internal_memcpy(new_data, data_, size_ * sizeof(T));
434 UnmapOrDie(old_data, capacity_ * sizeof(T));
435 capacity_ = new_capacity;
437 // Disallow evil constructors.
438 InternalMmapVector(const InternalMmapVector&);
439 void operator=(const InternalMmapVector&);
446 // HeapSort for arrays and InternalMmapVector.
447 template<class Container, class Compare>
448 void InternalSort(Container *v, uptr size, Compare comp) {
451 // Stage 1: insert elements to the heap.
452 for (uptr i = 1; i < size; i++) {
454 for (j = i; j > 0; j = p) {
456 if (comp((*v)[p], (*v)[j]))
457 Swap((*v)[j], (*v)[p]);
462 // Stage 2: swap largest element with the last one,
463 // and sink the new top.
464 for (uptr i = size - 1; i > 0; i--) {
465 Swap((*v)[0], (*v)[i]);
467 for (j = 0; j < i; j = max_ind) {
468 uptr left = 2 * j + 1;
469 uptr right = 2 * j + 2;
471 if (left < i && comp((*v)[max_ind], (*v)[left]))
473 if (right < i && comp((*v)[max_ind], (*v)[right]))
476 Swap((*v)[j], (*v)[max_ind]);
483 template<class Container, class Value, class Compare>
484 uptr InternalBinarySearch(const Container &v, uptr first, uptr last,
485 const Value &val, Compare comp) {
486 uptr not_found = last + 1;
487 while (last >= first) {
488 uptr mid = (first + last) / 2;
489 if (comp(v[mid], val))
491 else if (comp(val, v[mid]))
499 // Represents a binary loaded into virtual memory (e.g. this can be an
500 // executable or a shared object).
503 LoadedModule(const char *module_name, uptr base_address);
504 void addAddressRange(uptr beg, uptr end, bool executable);
505 bool containsAddress(uptr address) const;
507 const char *full_name() const { return full_name_; }
508 uptr base_address() const { return base_address_; }
510 uptr n_ranges() const { return n_ranges_; }
511 uptr address_range_start(int i) const { return ranges_[i].beg; }
512 uptr address_range_end(int i) const { return ranges_[i].end; }
513 bool address_range_executable(int i) const { return exec_[i]; }
516 struct AddressRange {
522 static const uptr kMaxNumberOfAddressRanges = 6;
523 AddressRange ranges_[kMaxNumberOfAddressRanges];
524 bool exec_[kMaxNumberOfAddressRanges];
528 // OS-dependent function that fills array with descriptions of at most
529 // "max_modules" currently loaded modules. Returns the number of
530 // initialized modules. If filter is nonzero, ignores modules for which
531 // filter(full_name) is false.
532 typedef bool (*string_predicate_t)(const char *);
533 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
534 string_predicate_t filter);
537 const uptr kPthreadDestructorIterations = 4;
539 // Unused on Windows.
540 const uptr kPthreadDestructorIterations = 0;
543 // Callback type for iterating over a set of memory ranges.
544 typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
546 #if SANITIZER_ANDROID
547 // Initialize Android logging. Any writes before this are silently lost.
548 void AndroidLogInit();
549 void AndroidLogWrite(const char *buffer);
550 void GetExtraActivationFlags(char *buf, uptr size);
551 void SanitizerInitializeUnwinder();
553 INLINE void AndroidLogInit() {}
554 INLINE void AndroidLogWrite(const char *buffer_unused) {}
555 INLINE void GetExtraActivationFlags(char *buf, uptr size) { *buf = '\0'; }
556 INLINE void SanitizerInitializeUnwinder() {}
559 // Make the compiler think that something is going on there.
560 // Use this inside a loop that looks like memset/memcpy/etc to prevent the
561 // compiler from recognising it and turning it into an actual call to
562 // memset/memcpy/etc.
563 static inline void SanitizerBreakOptimization(void *arg) {
565 // FIXME: make sure this is actually enough.
568 __asm__ __volatile__("" : : "r" (arg) : "memory");
572 } // namespace __sanitizer
574 inline void *operator new(__sanitizer::operator_new_size_type size,
575 __sanitizer::LowLevelAllocator &alloc) {
576 return alloc.Allocate(size);
579 struct StackDepotStats {
584 #endif // SANITIZER_COMMON_H