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