]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_common.h
Import mandoc 1.14.3
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_common.h
1 //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between run-time libraries of sanitizers.
11 //
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
18
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"
25
26 #if defined(_MSC_VER) && !defined(__clang__)
27 extern "C" void _ReadWriteBarrier();
28 #pragma intrinsic(_ReadWriteBarrier)
29 #endif
30
31 namespace __sanitizer {
32 struct StackTrace;
33 struct AddressInfo;
34
35 // Constants.
36 const uptr kWordSize = SANITIZER_WORDSIZE / 8;
37 const uptr kWordSizeInBits = 8 * kWordSize;
38
39 #if defined(__powerpc__) || defined(__powerpc64__)
40   const uptr kCacheLineSize = 128;
41 #else
42   const uptr kCacheLineSize = 64;
43 #endif
44
45 const uptr kMaxPathLength = 4096;
46
47 const uptr kMaxThreadStackSize = 1 << 30;  // 1Gb
48
49 static const uptr kErrorMessageBufferSize = 1 << 16;
50
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;
54
55 extern const char *SanitizerToolName;  // Can be changed by the tool.
56
57 extern atomic_uint32_t current_verbosity;
58 INLINE void SetVerbosity(int verbosity) {
59   atomic_store(&current_verbosity, verbosity, memory_order_relaxed);
60 }
61 INLINE int Verbosity() {
62   return atomic_load(&current_verbosity, memory_order_relaxed);
63 }
64
65 uptr GetPageSize();
66 extern uptr PageSizeCached;
67 INLINE uptr GetPageSizeCached() {
68   if (!PageSizeCached)
69     PageSizeCached = GetPageSize();
70   return PageSizeCached;
71 }
72 uptr GetMmapGranularity();
73 uptr GetMaxVirtualAddress();
74 // Threads
75 tid_t GetTid();
76 uptr GetThreadSelf();
77 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
78                                 uptr *stack_bottom);
79 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
80                           uptr *tls_addr, uptr *tls_size);
81
82 // Memory management
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);
86 }
87 void UnmapOrDie(void *addr, uptr size);
88 // Behaves just like MmapOrDie, but tolerates out of memory condition, in that
89 // case returns nullptr.
90 void *MmapOrDieOnFatalError(uptr size, const char *mem_type);
91 void *MmapFixedNoReserve(uptr fixed_addr, uptr size,
92                          const char *name = nullptr);
93 void *MmapNoReserveOrDie(uptr size, const char *mem_type);
94 void *MmapFixedOrDie(uptr fixed_addr, uptr size);
95 // Behaves just like MmapFixedOrDie, but tolerates out of memory condition, in
96 // that case returns nullptr.
97 void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size);
98 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name = nullptr);
99 void *MmapNoAccess(uptr size);
100 // Map aligned chunk of address space; size and alignment are powers of two.
101 // Dies on all but out of memory errors, in the latter case returns nullptr.
102 void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
103                                    const char *mem_type);
104 // Disallow access to a memory range.  Use MmapFixedNoAccess to allocate an
105 // unaccessible memory.
106 bool MprotectNoAccess(uptr addr, uptr size);
107 bool MprotectReadOnly(uptr addr, uptr size);
108
109 // Find an available address space.
110 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
111                               uptr *largest_gap_found);
112
113 // Used to check if we can map shadow memory to a fixed location.
114 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
115 // Releases memory pages entirely within the [beg, end] address range. Noop if
116 // the provided range does not contain at least one entire page.
117 void ReleaseMemoryPagesToOS(uptr beg, uptr end);
118 void IncreaseTotalMmap(uptr size);
119 void DecreaseTotalMmap(uptr size);
120 uptr GetRSS();
121 void NoHugePagesInRegion(uptr addr, uptr length);
122 void DontDumpShadowMemory(uptr addr, uptr length);
123 // Check if the built VMA size matches the runtime one.
124 void CheckVMASize();
125 void RunMallocHooks(const void *ptr, uptr size);
126 void RunFreeHooks(const void *ptr);
127
128 // InternalScopedBuffer can be used instead of large stack arrays to
129 // keep frame size low.
130 // FIXME: use InternalAlloc instead of MmapOrDie once
131 // InternalAlloc is made libc-free.
132 template <typename T>
133 class InternalScopedBuffer {
134  public:
135   explicit InternalScopedBuffer(uptr cnt) {
136     cnt_ = cnt;
137     ptr_ = (T *)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
138   }
139   ~InternalScopedBuffer() { UnmapOrDie(ptr_, cnt_ * sizeof(T)); }
140   T &operator[](uptr i) { return ptr_[i]; }
141   T *data() { return ptr_; }
142   uptr size() { return cnt_ * sizeof(T); }
143
144  private:
145   T *ptr_;
146   uptr cnt_;
147   // Disallow copies and moves.
148   InternalScopedBuffer(const InternalScopedBuffer &) = delete;
149   InternalScopedBuffer &operator=(const InternalScopedBuffer &) = delete;
150   InternalScopedBuffer(InternalScopedBuffer &&) = delete;
151   InternalScopedBuffer &operator=(InternalScopedBuffer &&) = delete;
152 };
153
154 class InternalScopedString : public InternalScopedBuffer<char> {
155  public:
156   explicit InternalScopedString(uptr max_length)
157       : InternalScopedBuffer<char>(max_length), length_(0) {
158     (*this)[0] = '\0';
159   }
160   uptr length() { return length_; }
161   void clear() {
162     (*this)[0] = '\0';
163     length_ = 0;
164   }
165   void append(const char *format, ...);
166
167  private:
168   uptr length_;
169 };
170
171 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
172 // constructor, so all instances of LowLevelAllocator should be
173 // linker initialized.
174 class LowLevelAllocator {
175  public:
176   // Requires an external lock.
177   void *Allocate(uptr size);
178  private:
179   char *allocated_end_;
180   char *allocated_current_;
181 };
182 typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
183 // Allows to register tool-specific callbacks for LowLevelAllocator.
184 // Passing NULL removes the callback.
185 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
186
187 // IO
188 void RawWrite(const char *buffer);
189 bool ColorizeReports();
190 void RemoveANSIEscapeSequencesFromString(char *buffer);
191 void Printf(const char *format, ...);
192 void Report(const char *format, ...);
193 void SetPrintfAndReportCallback(void (*callback)(const char *));
194 #define VReport(level, ...)                                              \
195   do {                                                                   \
196     if ((uptr)Verbosity() >= (level)) Report(__VA_ARGS__); \
197   } while (0)
198 #define VPrintf(level, ...)                                              \
199   do {                                                                   \
200     if ((uptr)Verbosity() >= (level)) Printf(__VA_ARGS__); \
201   } while (0)
202
203 // Can be used to prevent mixing error reports from different sanitizers.
204 extern StaticSpinMutex CommonSanitizerReportMutex;
205
206 struct ReportFile {
207   void Write(const char *buffer, uptr length);
208   bool SupportsColors();
209   void SetReportPath(const char *path);
210
211   // Don't use fields directly. They are only declared public to allow
212   // aggregate initialization.
213
214   // Protects fields below.
215   StaticSpinMutex *mu;
216   // Opened file descriptor. Defaults to stderr. It may be equal to
217   // kInvalidFd, in which case new file will be opened when necessary.
218   fd_t fd;
219   // Path prefix of report file, set via __sanitizer_set_report_path.
220   char path_prefix[kMaxPathLength];
221   // Full path to report, obtained as <path_prefix>.PID
222   char full_path[kMaxPathLength];
223   // PID of the process that opened fd. If a fork() occurs,
224   // the PID of child will be different from fd_pid.
225   uptr fd_pid;
226
227  private:
228   void ReopenIfNecessary();
229 };
230 extern ReportFile report_file;
231
232 extern uptr stoptheworld_tracer_pid;
233 extern uptr stoptheworld_tracer_ppid;
234
235 enum FileAccessMode {
236   RdOnly,
237   WrOnly,
238   RdWr
239 };
240
241 // Returns kInvalidFd on error.
242 fd_t OpenFile(const char *filename, FileAccessMode mode,
243               error_t *errno_p = nullptr);
244 void CloseFile(fd_t);
245
246 // Return true on success, false on error.
247 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size,
248                   uptr *bytes_read = nullptr, error_t *error_p = nullptr);
249 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size,
250                  uptr *bytes_written = nullptr, error_t *error_p = nullptr);
251
252 bool RenameFile(const char *oldpath, const char *newpath,
253                 error_t *error_p = nullptr);
254
255 // Scoped file handle closer.
256 struct FileCloser {
257   explicit FileCloser(fd_t fd) : fd(fd) {}
258   ~FileCloser() { CloseFile(fd); }
259   fd_t fd;
260 };
261
262 bool SupportsColoredOutput(fd_t fd);
263
264 // Opens the file 'file_name" and reads up to 'max_len' bytes.
265 // The resulting buffer is mmaped and stored in '*buff'.
266 // The size of the mmaped region is stored in '*buff_size'.
267 // The total number of read bytes is stored in '*read_len'.
268 // Returns true if file was successfully opened and read.
269 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
270                       uptr *read_len, uptr max_len = 1 << 26,
271                       error_t *errno_p = nullptr);
272 // Maps given file to virtual memory, and returns pointer to it
273 // (or NULL if mapping fails). Stores the size of mmaped region
274 // in '*buff_size'.
275 void *MapFileToMemory(const char *file_name, uptr *buff_size);
276 void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset);
277
278 bool IsAccessibleMemoryRange(uptr beg, uptr size);
279
280 // Error report formatting.
281 const char *StripPathPrefix(const char *filepath,
282                             const char *strip_file_prefix);
283 // Strip the directories from the module name.
284 const char *StripModuleName(const char *module);
285
286 // OS
287 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
288 uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len);
289 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len);
290 const char *GetProcessName();
291 void UpdateProcessName();
292 void CacheBinaryName();
293 void DisableCoreDumperIfNecessary();
294 void DumpProcessMap();
295 void PrintModuleMap();
296 bool FileExists(const char *filename);
297 const char *GetEnv(const char *name);
298 bool SetEnv(const char *name, const char *value);
299 const char *GetPwd();
300 char *FindPathToBinary(const char *name);
301 bool IsPathSeparator(const char c);
302 bool IsAbsolutePath(const char *path);
303 // Starts a subprocess and returs its pid.
304 // If *_fd parameters are not kInvalidFd their corresponding input/output
305 // streams will be redirect to the file. The files will always be closed
306 // in parent process even in case of an error.
307 // The child process will close all fds after STDERR_FILENO
308 // before passing control to a program.
309 pid_t StartSubprocess(const char *filename, const char *const argv[],
310                       fd_t stdin_fd = kInvalidFd, fd_t stdout_fd = kInvalidFd,
311                       fd_t stderr_fd = kInvalidFd);
312 // Checks if specified process is still running
313 bool IsProcessRunning(pid_t pid);
314 // Waits for the process to finish and returns its exit code.
315 // Returns -1 in case of an error.
316 int WaitForProcess(pid_t pid);
317
318 u32 GetUid();
319 void ReExec();
320 char **GetArgv();
321 void PrintCmdline();
322 bool StackSizeIsUnlimited();
323 uptr GetStackSizeLimitInBytes();
324 void SetStackSizeLimitInBytes(uptr limit);
325 bool AddressSpaceIsUnlimited();
326 void SetAddressSpaceUnlimited();
327 void AdjustStackSize(void *attr);
328 void PrepareForSandboxing(__sanitizer_sandbox_arguments *args);
329 void SetSandboxingCallback(void (*f)());
330
331 void InitializeCoverage(bool enabled, const char *coverage_dir);
332
333 void InitTlsSize();
334 uptr GetTlsSize();
335
336 // Other
337 void SleepForSeconds(int seconds);
338 void SleepForMillis(int millis);
339 u64 NanoTime();
340 int Atexit(void (*function)(void));
341 void SortArray(uptr *array, uptr size);
342 void SortArray(u32 *array, uptr size);
343 bool TemplateMatch(const char *templ, const char *str);
344
345 // Exit
346 void NORETURN Abort();
347 void NORETURN Die();
348 void NORETURN
349 CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
350 void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
351                                       const char *mmap_type, error_t err,
352                                       bool raw_report = false);
353
354 // Set the name of the current thread to 'name', return true on succees.
355 // The name may be truncated to a system-dependent limit.
356 bool SanitizerSetThreadName(const char *name);
357 // Get the name of the current thread (no more than max_len bytes),
358 // return true on succees. name should have space for at least max_len+1 bytes.
359 bool SanitizerGetThreadName(char *name, int max_len);
360
361 // Specific tools may override behavior of "Die" and "CheckFailed" functions
362 // to do tool-specific job.
363 typedef void (*DieCallbackType)(void);
364
365 // It's possible to add several callbacks that would be run when "Die" is
366 // called. The callbacks will be run in the opposite order. The tools are
367 // strongly recommended to setup all callbacks during initialization, when there
368 // is only a single thread.
369 bool AddDieCallback(DieCallbackType callback);
370 bool RemoveDieCallback(DieCallbackType callback);
371
372 void SetUserDieCallback(DieCallbackType callback);
373
374 typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
375                                        u64, u64);
376 void SetCheckFailedCallback(CheckFailedCallbackType callback);
377
378 // Callback will be called if soft_rss_limit_mb is given and the limit is
379 // exceeded (exceeded==true) or if rss went down below the limit
380 // (exceeded==false).
381 // The callback should be registered once at the tool init time.
382 void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded));
383
384 // Functions related to signal handling.
385 typedef void (*SignalHandlerType)(int, void *, void *);
386 HandleSignalMode GetHandleSignalMode(int signum);
387 void InstallDeadlySignalHandlers(SignalHandlerType handler);
388 const char *DescribeSignalOrException(int signo);
389 // Alternative signal stack (POSIX-only).
390 void SetAlternateSignalStack();
391 void UnsetAlternateSignalStack();
392
393 // We don't want a summary too long.
394 const int kMaxSummaryLength = 1024;
395 // Construct a one-line string:
396 //   SUMMARY: SanitizerToolName: error_message
397 // and pass it to __sanitizer_report_error_summary.
398 // If alt_tool_name is provided, it's used in place of SanitizerToolName.
399 void ReportErrorSummary(const char *error_message,
400                         const char *alt_tool_name = nullptr);
401 // Same as above, but construct error_message as:
402 //   error_type file:line[:column][ function]
403 void ReportErrorSummary(const char *error_type, const AddressInfo &info,
404                         const char *alt_tool_name = nullptr);
405 // Same as above, but obtains AddressInfo by symbolizing top stack trace frame.
406 void ReportErrorSummary(const char *error_type, const StackTrace *trace,
407                         const char *alt_tool_name = nullptr);
408
409 // Math
410 #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
411 extern "C" {
412 unsigned char _BitScanForward(unsigned long *index, unsigned long mask);  // NOLINT
413 unsigned char _BitScanReverse(unsigned long *index, unsigned long mask);  // NOLINT
414 #if defined(_WIN64)
415 unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask);  // NOLINT
416 unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask);  // NOLINT
417 #endif
418 }
419 #endif
420
421 INLINE uptr MostSignificantSetBitIndex(uptr x) {
422   CHECK_NE(x, 0U);
423   unsigned long up;  // NOLINT
424 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
425 # ifdef _WIN64
426   up = SANITIZER_WORDSIZE - 1 - __builtin_clzll(x);
427 # else
428   up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
429 # endif
430 #elif defined(_WIN64)
431   _BitScanReverse64(&up, x);
432 #else
433   _BitScanReverse(&up, x);
434 #endif
435   return up;
436 }
437
438 INLINE uptr LeastSignificantSetBitIndex(uptr x) {
439   CHECK_NE(x, 0U);
440   unsigned long up;  // NOLINT
441 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
442 # ifdef _WIN64
443   up = __builtin_ctzll(x);
444 # else
445   up = __builtin_ctzl(x);
446 # endif
447 #elif defined(_WIN64)
448   _BitScanForward64(&up, x);
449 #else
450   _BitScanForward(&up, x);
451 #endif
452   return up;
453 }
454
455 INLINE bool IsPowerOfTwo(uptr x) {
456   return (x & (x - 1)) == 0;
457 }
458
459 INLINE uptr RoundUpToPowerOfTwo(uptr size) {
460   CHECK(size);
461   if (IsPowerOfTwo(size)) return size;
462
463   uptr up = MostSignificantSetBitIndex(size);
464   CHECK_LT(size, (1ULL << (up + 1)));
465   CHECK_GT(size, (1ULL << up));
466   return 1ULL << (up + 1);
467 }
468
469 INLINE uptr RoundUpTo(uptr size, uptr boundary) {
470   RAW_CHECK(IsPowerOfTwo(boundary));
471   return (size + boundary - 1) & ~(boundary - 1);
472 }
473
474 INLINE uptr RoundDownTo(uptr x, uptr boundary) {
475   return x & ~(boundary - 1);
476 }
477
478 INLINE bool IsAligned(uptr a, uptr alignment) {
479   return (a & (alignment - 1)) == 0;
480 }
481
482 INLINE uptr Log2(uptr x) {
483   CHECK(IsPowerOfTwo(x));
484   return LeastSignificantSetBitIndex(x);
485 }
486
487 // Don't use std::min, std::max or std::swap, to minimize dependency
488 // on libstdc++.
489 template<class T> T Min(T a, T b) { return a < b ? a : b; }
490 template<class T> T Max(T a, T b) { return a > b ? a : b; }
491 template<class T> void Swap(T& a, T& b) {
492   T tmp = a;
493   a = b;
494   b = tmp;
495 }
496
497 // Char handling
498 INLINE bool IsSpace(int c) {
499   return (c == ' ') || (c == '\n') || (c == '\t') ||
500          (c == '\f') || (c == '\r') || (c == '\v');
501 }
502 INLINE bool IsDigit(int c) {
503   return (c >= '0') && (c <= '9');
504 }
505 INLINE int ToLower(int c) {
506   return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
507 }
508
509 // A low-level vector based on mmap. May incur a significant memory overhead for
510 // small vectors.
511 // WARNING: The current implementation supports only POD types.
512 template<typename T>
513 class InternalMmapVectorNoCtor {
514  public:
515   void Initialize(uptr initial_capacity) {
516     capacity_ = Max(initial_capacity, (uptr)1);
517     size_ = 0;
518     data_ = (T *)MmapOrDie(capacity_ * sizeof(T), "InternalMmapVectorNoCtor");
519   }
520   void Destroy() {
521     UnmapOrDie(data_, capacity_ * sizeof(T));
522   }
523   T &operator[](uptr i) {
524     CHECK_LT(i, size_);
525     return data_[i];
526   }
527   const T &operator[](uptr i) const {
528     CHECK_LT(i, size_);
529     return data_[i];
530   }
531   void push_back(const T &element) {
532     CHECK_LE(size_, capacity_);
533     if (size_ == capacity_) {
534       uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
535       Resize(new_capacity);
536     }
537     internal_memcpy(&data_[size_++], &element, sizeof(T));
538   }
539   T &back() {
540     CHECK_GT(size_, 0);
541     return data_[size_ - 1];
542   }
543   void pop_back() {
544     CHECK_GT(size_, 0);
545     size_--;
546   }
547   uptr size() const {
548     return size_;
549   }
550   const T *data() const {
551     return data_;
552   }
553   T *data() {
554     return data_;
555   }
556   uptr capacity() const {
557     return capacity_;
558   }
559   void resize(uptr new_size) {
560     Resize(new_size);
561     if (new_size > size_) {
562       internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
563     }
564     size_ = new_size;
565   }
566
567   void clear() { size_ = 0; }
568   bool empty() const { return size() == 0; }
569
570   const T *begin() const {
571     return data();
572   }
573   T *begin() {
574     return data();
575   }
576   const T *end() const {
577     return data() + size();
578   }
579   T *end() {
580     return data() + size();
581   }
582
583  private:
584   void Resize(uptr new_capacity) {
585     CHECK_GT(new_capacity, 0);
586     CHECK_LE(size_, new_capacity);
587     T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),
588                                  "InternalMmapVector");
589     internal_memcpy(new_data, data_, size_ * sizeof(T));
590     T *old_data = data_;
591     data_ = new_data;
592     UnmapOrDie(old_data, capacity_ * sizeof(T));
593     capacity_ = new_capacity;
594   }
595
596   T *data_;
597   uptr capacity_;
598   uptr size_;
599 };
600
601 template<typename T>
602 class InternalMmapVector : public InternalMmapVectorNoCtor<T> {
603  public:
604   explicit InternalMmapVector(uptr initial_capacity) {
605     InternalMmapVectorNoCtor<T>::Initialize(initial_capacity);
606   }
607   ~InternalMmapVector() { InternalMmapVectorNoCtor<T>::Destroy(); }
608   // Disallow evil constructors.
609   InternalMmapVector(const InternalMmapVector&);
610   void operator=(const InternalMmapVector&);
611 };
612
613 // HeapSort for arrays and InternalMmapVector.
614 template<class Container, class Compare>
615 void InternalSort(Container *v, uptr size, Compare comp) {
616   if (size < 2)
617     return;
618   // Stage 1: insert elements to the heap.
619   for (uptr i = 1; i < size; i++) {
620     uptr j, p;
621     for (j = i; j > 0; j = p) {
622       p = (j - 1) / 2;
623       if (comp((*v)[p], (*v)[j]))
624         Swap((*v)[j], (*v)[p]);
625       else
626         break;
627     }
628   }
629   // Stage 2: swap largest element with the last one,
630   // and sink the new top.
631   for (uptr i = size - 1; i > 0; i--) {
632     Swap((*v)[0], (*v)[i]);
633     uptr j, max_ind;
634     for (j = 0; j < i; j = max_ind) {
635       uptr left = 2 * j + 1;
636       uptr right = 2 * j + 2;
637       max_ind = j;
638       if (left < i && comp((*v)[max_ind], (*v)[left]))
639         max_ind = left;
640       if (right < i && comp((*v)[max_ind], (*v)[right]))
641         max_ind = right;
642       if (max_ind != j)
643         Swap((*v)[j], (*v)[max_ind]);
644       else
645         break;
646     }
647   }
648 }
649
650 // Works like std::lower_bound: finds the first element that is not less
651 // than the val.
652 template <class Container, class Value, class Compare>
653 uptr InternalLowerBound(const Container &v, uptr first, uptr last,
654                         const Value &val, Compare comp) {
655   while (last > first) {
656     uptr mid = (first + last) / 2;
657     if (comp(v[mid], val))
658       first = mid + 1;
659     else
660       last = mid;
661   }
662   return first;
663 }
664
665 enum ModuleArch {
666   kModuleArchUnknown,
667   kModuleArchI386,
668   kModuleArchX86_64,
669   kModuleArchX86_64H,
670   kModuleArchARMV6,
671   kModuleArchARMV7,
672   kModuleArchARMV7S,
673   kModuleArchARMV7K,
674   kModuleArchARM64
675 };
676
677 // When adding a new architecture, don't forget to also update
678 // script/asan_symbolize.py and sanitizer_symbolizer_libcdep.cc.
679 inline const char *ModuleArchToString(ModuleArch arch) {
680   switch (arch) {
681     case kModuleArchUnknown:
682       return "";
683     case kModuleArchI386:
684       return "i386";
685     case kModuleArchX86_64:
686       return "x86_64";
687     case kModuleArchX86_64H:
688       return "x86_64h";
689     case kModuleArchARMV6:
690       return "armv6";
691     case kModuleArchARMV7:
692       return "armv7";
693     case kModuleArchARMV7S:
694       return "armv7s";
695     case kModuleArchARMV7K:
696       return "armv7k";
697     case kModuleArchARM64:
698       return "arm64";
699   }
700   CHECK(0 && "Invalid module arch");
701   return "";
702 }
703
704 const uptr kModuleUUIDSize = 16;
705
706 // Represents a binary loaded into virtual memory (e.g. this can be an
707 // executable or a shared object).
708 class LoadedModule {
709  public:
710   LoadedModule()
711       : full_name_(nullptr),
712         base_address_(0),
713         max_executable_address_(0),
714         arch_(kModuleArchUnknown),
715         instrumented_(false) {
716     internal_memset(uuid_, 0, kModuleUUIDSize);
717     ranges_.clear();
718   }
719   void set(const char *module_name, uptr base_address);
720   void set(const char *module_name, uptr base_address, ModuleArch arch,
721            u8 uuid[kModuleUUIDSize], bool instrumented);
722   void clear();
723   void addAddressRange(uptr beg, uptr end, bool executable, bool writable);
724   bool containsAddress(uptr address) const;
725
726   const char *full_name() const { return full_name_; }
727   uptr base_address() const { return base_address_; }
728   uptr max_executable_address() const { return max_executable_address_; }
729   ModuleArch arch() const { return arch_; }
730   const u8 *uuid() const { return uuid_; }
731   bool instrumented() const { return instrumented_; }
732
733   struct AddressRange {
734     AddressRange *next;
735     uptr beg;
736     uptr end;
737     bool executable;
738     bool writable;
739
740     AddressRange(uptr beg, uptr end, bool executable, bool writable)
741         : next(nullptr),
742           beg(beg),
743           end(end),
744           executable(executable),
745           writable(writable) {}
746   };
747
748   const IntrusiveList<AddressRange> &ranges() const { return ranges_; }
749
750  private:
751   char *full_name_;  // Owned.
752   uptr base_address_;
753   uptr max_executable_address_;
754   ModuleArch arch_;
755   u8 uuid_[kModuleUUIDSize];
756   bool instrumented_;
757   IntrusiveList<AddressRange> ranges_;
758 };
759
760 // List of LoadedModules. OS-dependent implementation is responsible for
761 // filling this information.
762 class ListOfModules {
763  public:
764   ListOfModules() : modules_(kInitialCapacity) {}
765   ~ListOfModules() { clear(); }
766   void init();
767   const LoadedModule *begin() const { return modules_.begin(); }
768   LoadedModule *begin() { return modules_.begin(); }
769   const LoadedModule *end() const { return modules_.end(); }
770   LoadedModule *end() { return modules_.end(); }
771   uptr size() const { return modules_.size(); }
772   const LoadedModule &operator[](uptr i) const {
773     CHECK_LT(i, modules_.size());
774     return modules_[i];
775   }
776
777  private:
778   void clear() {
779     for (auto &module : modules_) module.clear();
780     modules_.clear();
781   }
782
783   InternalMmapVector<LoadedModule> modules_;
784   // We rarely have more than 16K loaded modules.
785   static const uptr kInitialCapacity = 1 << 14;
786 };
787
788 // Callback type for iterating over a set of memory ranges.
789 typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
790
791 enum AndroidApiLevel {
792   ANDROID_NOT_ANDROID = 0,
793   ANDROID_KITKAT = 19,
794   ANDROID_LOLLIPOP_MR1 = 22,
795   ANDROID_POST_LOLLIPOP = 23
796 };
797
798 void WriteToSyslog(const char *buffer);
799
800 #if SANITIZER_MAC
801 void LogFullErrorReport(const char *buffer);
802 #else
803 INLINE void LogFullErrorReport(const char *buffer) {}
804 #endif
805
806 #if SANITIZER_LINUX || SANITIZER_MAC
807 void WriteOneLineToSyslog(const char *s);
808 void LogMessageOnPrintf(const char *str);
809 #else
810 INLINE void WriteOneLineToSyslog(const char *s) {}
811 INLINE void LogMessageOnPrintf(const char *str) {}
812 #endif
813
814 #if SANITIZER_LINUX
815 // Initialize Android logging. Any writes before this are silently lost.
816 void AndroidLogInit();
817 void SetAbortMessage(const char *);
818 #else
819 INLINE void AndroidLogInit() {}
820 // FIXME: MacOS implementation could use CRSetCrashLogMessage.
821 INLINE void SetAbortMessage(const char *) {}
822 #endif
823
824 #if SANITIZER_ANDROID
825 void SanitizerInitializeUnwinder();
826 AndroidApiLevel AndroidGetApiLevel();
827 #else
828 INLINE void AndroidLogWrite(const char *buffer_unused) {}
829 INLINE void SanitizerInitializeUnwinder() {}
830 INLINE AndroidApiLevel AndroidGetApiLevel() { return ANDROID_NOT_ANDROID; }
831 #endif
832
833 INLINE uptr GetPthreadDestructorIterations() {
834 #if SANITIZER_ANDROID
835   return (AndroidGetApiLevel() == ANDROID_LOLLIPOP_MR1) ? 8 : 4;
836 #elif SANITIZER_POSIX
837   return 4;
838 #else
839 // Unused on Windows.
840   return 0;
841 #endif
842 }
843
844 void *internal_start_thread(void(*func)(void*), void *arg);
845 void internal_join_thread(void *th);
846 void MaybeStartBackgroudThread();
847
848 // Make the compiler think that something is going on there.
849 // Use this inside a loop that looks like memset/memcpy/etc to prevent the
850 // compiler from recognising it and turning it into an actual call to
851 // memset/memcpy/etc.
852 static inline void SanitizerBreakOptimization(void *arg) {
853 #if defined(_MSC_VER) && !defined(__clang__)
854   _ReadWriteBarrier();
855 #else
856   __asm__ __volatile__("" : : "r" (arg) : "memory");
857 #endif
858 }
859
860 struct SignalContext {
861   void *context;
862   uptr addr;
863   uptr pc;
864   uptr sp;
865   uptr bp;
866   bool is_memory_access;
867
868   enum WriteFlag { UNKNOWN, READ, WRITE } write_flag;
869
870   SignalContext(void *context, uptr addr, uptr pc, uptr sp, uptr bp,
871                 bool is_memory_access, WriteFlag write_flag)
872       : context(context),
873         addr(addr),
874         pc(pc),
875         sp(sp),
876         bp(bp),
877         is_memory_access(is_memory_access),
878         write_flag(write_flag) {}
879
880   static void DumpAllRegisters(void *context);
881
882   // Creates signal context in a platform-specific manner.
883   static SignalContext Create(void *siginfo, void *context);
884
885   // Returns true if the "context" indicates a memory write.
886   static WriteFlag GetWriteFlag(void *context);
887 };
888
889 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
890
891 void MaybeReexec();
892
893 template <typename Fn>
894 class RunOnDestruction {
895  public:
896   explicit RunOnDestruction(Fn fn) : fn_(fn) {}
897   ~RunOnDestruction() { fn_(); }
898
899  private:
900   Fn fn_;
901 };
902
903 // A simple scope guard. Usage:
904 // auto cleanup = at_scope_exit([]{ do_cleanup; });
905 template <typename Fn>
906 RunOnDestruction<Fn> at_scope_exit(Fn fn) {
907   return RunOnDestruction<Fn>(fn);
908 }
909
910 // Linux on 64-bit s390 had a nasty bug that crashes the whole machine
911 // if a process uses virtual memory over 4TB (as many sanitizers like
912 // to do).  This function will abort the process if running on a kernel
913 // that looks vulnerable.
914 #if SANITIZER_LINUX && SANITIZER_S390_64
915 void AvoidCVE_2016_2143();
916 #else
917 INLINE void AvoidCVE_2016_2143() {}
918 #endif
919
920 struct StackDepotStats {
921   uptr n_uniq_ids;
922   uptr allocated;
923 };
924
925 // The default value for allocator_release_to_os_interval_ms common flag to
926 // indicate that sanitizer allocator should not attempt to release memory to OS.
927 const s32 kReleaseToOSIntervalNever = -1;
928
929 void CheckNoDeepBind(const char *filename, int flag);
930
931 // Returns the requested amount of random data (up to 256 bytes) that can then
932 // be used to seed a PRNG.
933 bool GetRandom(void *buffer, uptr length);
934
935 }  // namespace __sanitizer
936
937 inline void *operator new(__sanitizer::operator_new_size_type size,
938                           __sanitizer::LowLevelAllocator &alloc) {
939   return alloc.Allocate(size);
940 }
941
942 #endif  // SANITIZER_COMMON_H