Home | History | Annotate | Line # | Download | only in sanitizer_common
sanitizer_common.h revision 1.1.1.7
      1 //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 //
      9 // This file is shared between run-time libraries of sanitizers.
     10 //
     11 // It declares common functions and classes that are used in both runtimes.
     12 // Implementation of some functions are provided in sanitizer_common, while
     13 // others must be defined by run-time library itself.
     14 //===----------------------------------------------------------------------===//
     15 #ifndef SANITIZER_COMMON_H
     16 #define SANITIZER_COMMON_H
     17 
     18 #include "sanitizer_flags.h"
     19 #include "sanitizer_interface_internal.h"
     20 #include "sanitizer_internal_defs.h"
     21 #include "sanitizer_libc.h"
     22 #include "sanitizer_list.h"
     23 #include "sanitizer_mutex.h"
     24 
     25 #if defined(_MSC_VER) && !defined(__clang__)
     26 extern "C" void _ReadWriteBarrier();
     27 #pragma intrinsic(_ReadWriteBarrier)
     28 #endif
     29 
     30 namespace __sanitizer {
     31 
     32 struct AddressInfo;
     33 struct BufferedStackTrace;
     34 struct SignalContext;
     35 struct StackTrace;
     36 
     37 // Constants.
     38 const uptr kWordSize = SANITIZER_WORDSIZE / 8;
     39 const uptr kWordSizeInBits = 8 * kWordSize;
     40 
     41 const uptr kCacheLineSize = SANITIZER_CACHE_LINE_SIZE;
     42 
     43 const uptr kMaxPathLength = 4096;
     44 
     45 const uptr kMaxThreadStackSize = 1 << 30;  // 1Gb
     46 
     47 const uptr kErrorMessageBufferSize = 1 << 16;
     48 
     49 // Denotes fake PC values that come from JIT/JAVA/etc.
     50 // For such PC values __tsan_symbolize_external_ex() will be called.
     51 const u64 kExternalPCBit = 1ULL << 60;
     52 
     53 extern const char *SanitizerToolName;  // Can be changed by the tool.
     54 
     55 extern atomic_uint32_t current_verbosity;
     56 inline void SetVerbosity(int verbosity) {
     57   atomic_store(&current_verbosity, verbosity, memory_order_relaxed);
     58 }
     59 inline int Verbosity() {
     60   return atomic_load(&current_verbosity, memory_order_relaxed);
     61 }
     62 
     63 #if SANITIZER_ANDROID
     64 inline uptr GetPageSize() {
     65 // Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
     66   return 4096;
     67 }
     68 inline uptr GetPageSizeCached() {
     69   return 4096;
     70 }
     71 #else
     72 uptr GetPageSize();
     73 extern uptr PageSizeCached;
     74 inline uptr GetPageSizeCached() {
     75   if (!PageSizeCached)
     76     PageSizeCached = GetPageSize();
     77   return PageSizeCached;
     78 }
     79 #endif
     80 uptr GetMmapGranularity();
     81 uptr GetMaxVirtualAddress();
     82 uptr GetMaxUserVirtualAddress();
     83 // Threads
     84 tid_t GetTid();
     85 int TgKill(pid_t pid, tid_t tid, int sig);
     86 uptr GetThreadSelf();
     87 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
     88                                 uptr *stack_bottom);
     89 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
     90                           uptr *tls_addr, uptr *tls_size);
     91 
     92 // Memory management
     93 void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false);
     94 inline void *MmapOrDieQuietly(uptr size, const char *mem_type) {
     95   return MmapOrDie(size, mem_type, /*raw_report*/ true);
     96 }
     97 void UnmapOrDie(void *addr, uptr size);
     98 // Behaves just like MmapOrDie, but tolerates out of memory condition, in that
     99 // case returns nullptr.
    100 void *MmapOrDieOnFatalError(uptr size, const char *mem_type);
    101 bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name = nullptr)
    102      WARN_UNUSED_RESULT;
    103 bool MmapFixedSuperNoReserve(uptr fixed_addr, uptr size,
    104                              const char *name = nullptr) WARN_UNUSED_RESULT;
    105 void *MmapNoReserveOrDie(uptr size, const char *mem_type);
    106 void *MmapFixedOrDie(uptr fixed_addr, uptr size, const char *name = nullptr);
    107 // Behaves just like MmapFixedOrDie, but tolerates out of memory condition, in
    108 // that case returns nullptr.
    109 void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size,
    110                                  const char *name = nullptr);
    111 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name = nullptr);
    112 void *MmapNoAccess(uptr size);
    113 // Map aligned chunk of address space; size and alignment are powers of two.
    114 // Dies on all but out of memory errors, in the latter case returns nullptr.
    115 void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
    116                                    const char *mem_type);
    117 // Disallow access to a memory range.  Use MmapFixedNoAccess to allocate an
    118 // unaccessible memory.
    119 bool MprotectNoAccess(uptr addr, uptr size);
    120 bool MprotectReadOnly(uptr addr, uptr size);
    121 
    122 void MprotectMallocZones(void *addr, int prot);
    123 
    124 #if SANITIZER_LINUX
    125 // Unmap memory. Currently only used on Linux.
    126 void UnmapFromTo(uptr from, uptr to);
    127 #endif
    128 
    129 // Maps shadow_size_bytes of shadow memory and returns shadow address. It will
    130 // be aligned to the mmap granularity * 2^shadow_scale, or to
    131 // 2^min_shadow_base_alignment if that is larger. The returned address will
    132 // have max(2^min_shadow_base_alignment, mmap granularity) on the left, and
    133 // shadow_size_bytes bytes on the right, which on linux is mapped no access.
    134 // The high_mem_end may be updated if the original shadow size doesn't fit.
    135 uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
    136                       uptr min_shadow_base_alignment, uptr &high_mem_end);
    137 
    138 // Let S = max(shadow_size, num_aliases * alias_size, ring_buffer_size).
    139 // Reserves 2*S bytes of address space to the right of the returned address and
    140 // ring_buffer_size bytes to the left.  The returned address is aligned to 2*S.
    141 // Also creates num_aliases regions of accessible memory starting at offset S
    142 // from the returned address.  Each region has size alias_size and is backed by
    143 // the same physical memory.
    144 uptr MapDynamicShadowAndAliases(uptr shadow_size, uptr alias_size,
    145                                 uptr num_aliases, uptr ring_buffer_size);
    146 
    147 // Reserve memory range [beg, end]. If madvise_shadow is true then apply
    148 // madvise (e.g. hugepages, core dumping) requested by options.
    149 void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name,
    150                               bool madvise_shadow = true);
    151 
    152 // Protect size bytes of memory starting at addr. Also try to protect
    153 // several pages at the start of the address space as specified by
    154 // zero_base_shadow_start, at most up to the size or zero_base_max_shadow_start.
    155 void ProtectGap(uptr addr, uptr size, uptr zero_base_shadow_start,
    156                 uptr zero_base_max_shadow_start);
    157 
    158 // Find an available address space.
    159 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
    160                               uptr *largest_gap_found, uptr *max_occupied_addr);
    161 
    162 // Used to check if we can map shadow memory to a fixed location.
    163 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
    164 // Releases memory pages entirely within the [beg, end] address range. Noop if
    165 // the provided range does not contain at least one entire page.
    166 void ReleaseMemoryPagesToOS(uptr beg, uptr end);
    167 void IncreaseTotalMmap(uptr size);
    168 void DecreaseTotalMmap(uptr size);
    169 uptr GetRSS();
    170 void SetShadowRegionHugePageMode(uptr addr, uptr length);
    171 bool DontDumpShadowMemory(uptr addr, uptr length);
    172 // Check if the built VMA size matches the runtime one.
    173 void CheckVMASize();
    174 void RunMallocHooks(const void *ptr, uptr size);
    175 void RunFreeHooks(const void *ptr);
    176 
    177 class ReservedAddressRange {
    178  public:
    179   uptr Init(uptr size, const char *name = nullptr, uptr fixed_addr = 0);
    180   uptr InitAligned(uptr size, uptr align, const char *name = nullptr);
    181   uptr Map(uptr fixed_addr, uptr size, const char *name = nullptr);
    182   uptr MapOrDie(uptr fixed_addr, uptr size, const char *name = nullptr);
    183   void Unmap(uptr addr, uptr size);
    184   void *base() const { return base_; }
    185   uptr size() const { return size_; }
    186 
    187  private:
    188   void* base_;
    189   uptr size_;
    190   const char* name_;
    191   uptr os_handle_;
    192 };
    193 
    194 typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
    195                                /*out*/ uptr *stats);
    196 
    197 // Parse the contents of /proc/self/smaps and generate a memory profile.
    198 // |cb| is a tool-specific callback that fills the |stats| array.
    199 void GetMemoryProfile(fill_profile_f cb, uptr *stats);
    200 void ParseUnixMemoryProfile(fill_profile_f cb, uptr *stats, char *smaps,
    201                             uptr smaps_len);
    202 
    203 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
    204 // constructor, so all instances of LowLevelAllocator should be
    205 // linker initialized.
    206 class LowLevelAllocator {
    207  public:
    208   // Requires an external lock.
    209   void *Allocate(uptr size);
    210  private:
    211   char *allocated_end_;
    212   char *allocated_current_;
    213 };
    214 // Set the min alignment of LowLevelAllocator to at least alignment.
    215 void SetLowLevelAllocateMinAlignment(uptr alignment);
    216 typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
    217 // Allows to register tool-specific callbacks for LowLevelAllocator.
    218 // Passing NULL removes the callback.
    219 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
    220 
    221 // IO
    222 void CatastrophicErrorWrite(const char *buffer, uptr length);
    223 void RawWrite(const char *buffer);
    224 bool ColorizeReports();
    225 void RemoveANSIEscapeSequencesFromString(char *buffer);
    226 void Printf(const char *format, ...) FORMAT(1, 2);
    227 void Report(const char *format, ...) FORMAT(1, 2);
    228 void SetPrintfAndReportCallback(void (*callback)(const char *));
    229 #define VReport(level, ...)                                              \
    230   do {                                                                   \
    231     if ((uptr)Verbosity() >= (level)) Report(__VA_ARGS__); \
    232   } while (0)
    233 #define VPrintf(level, ...)                                              \
    234   do {                                                                   \
    235     if ((uptr)Verbosity() >= (level)) Printf(__VA_ARGS__); \
    236   } while (0)
    237 
    238 // Lock sanitizer error reporting and protects against nested errors.
    239 class ScopedErrorReportLock {
    240  public:
    241   ScopedErrorReportLock() ACQUIRE(mutex_) { Lock(); }
    242   ~ScopedErrorReportLock() RELEASE(mutex_) { Unlock(); }
    243 
    244   static void Lock() ACQUIRE(mutex_);
    245   static void Unlock() RELEASE(mutex_);
    246   static void CheckLocked() CHECK_LOCKED(mutex_);
    247 
    248  private:
    249   static atomic_uintptr_t reporting_thread_;
    250   static StaticSpinMutex mutex_;
    251 };
    252 
    253 extern uptr stoptheworld_tracer_pid;
    254 extern uptr stoptheworld_tracer_ppid;
    255 
    256 bool IsAccessibleMemoryRange(uptr beg, uptr size);
    257 
    258 // Error report formatting.
    259 const char *StripPathPrefix(const char *filepath,
    260                             const char *strip_file_prefix);
    261 // Strip the directories from the module name.
    262 const char *StripModuleName(const char *module);
    263 
    264 // OS
    265 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
    266 uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len);
    267 uptr ReadBinaryDir(/*out*/ char *buf, uptr buf_len);
    268 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len);
    269 const char *GetProcessName();
    270 void UpdateProcessName();
    271 void CacheBinaryName();
    272 void DisableCoreDumperIfNecessary();
    273 void DumpProcessMap();
    274 const char *GetEnv(const char *name);
    275 bool SetEnv(const char *name, const char *value);
    276 
    277 u32 GetUid();
    278 void ReExec();
    279 void CheckASLR();
    280 void CheckMPROTECT();
    281 char **GetArgv();
    282 char **GetEnviron();
    283 void PrintCmdline();
    284 bool StackSizeIsUnlimited();
    285 void SetStackSizeLimitInBytes(uptr limit);
    286 bool AddressSpaceIsUnlimited();
    287 void SetAddressSpaceUnlimited();
    288 void AdjustStackSize(void *attr);
    289 void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args);
    290 void SetSandboxingCallback(void (*f)());
    291 
    292 void InitializeCoverage(bool enabled, const char *coverage_dir);
    293 
    294 void InitTlsSize();
    295 uptr GetTlsSize();
    296 
    297 // Other
    298 void SleepForSeconds(unsigned seconds);
    299 void SleepForMillis(unsigned millis);
    300 u64 NanoTime();
    301 u64 MonotonicNanoTime();
    302 int Atexit(void (*function)(void));
    303 bool TemplateMatch(const char *templ, const char *str);
    304 
    305 // Exit
    306 void NORETURN Abort();
    307 void NORETURN Die();
    308 void NORETURN
    309 CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
    310 void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
    311                                       const char *mmap_type, error_t err,
    312                                       bool raw_report = false);
    313 
    314 // Specific tools may override behavior of "Die" function to do tool-specific
    315 // job.
    316 typedef void (*DieCallbackType)(void);
    317 
    318 // It's possible to add several callbacks that would be run when "Die" is
    319 // called. The callbacks will be run in the opposite order. The tools are
    320 // strongly recommended to setup all callbacks during initialization, when there
    321 // is only a single thread.
    322 bool AddDieCallback(DieCallbackType callback);
    323 bool RemoveDieCallback(DieCallbackType callback);
    324 
    325 void SetUserDieCallback(DieCallbackType callback);
    326 
    327 void SetCheckUnwindCallback(void (*callback)());
    328 
    329 // Callback will be called if soft_rss_limit_mb is given and the limit is
    330 // exceeded (exceeded==true) or if rss went down below the limit
    331 // (exceeded==false).
    332 // The callback should be registered once at the tool init time.
    333 void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded));
    334 
    335 // Functions related to signal handling.
    336 typedef void (*SignalHandlerType)(int, void *, void *);
    337 HandleSignalMode GetHandleSignalMode(int signum);
    338 void InstallDeadlySignalHandlers(SignalHandlerType handler);
    339 
    340 // Signal reporting.
    341 // Each sanitizer uses slightly different implementation of stack unwinding.
    342 typedef void (*UnwindSignalStackCallbackType)(const SignalContext &sig,
    343                                               const void *callback_context,
    344                                               BufferedStackTrace *stack);
    345 // Print deadly signal report and die.
    346 void HandleDeadlySignal(void *siginfo, void *context, u32 tid,
    347                         UnwindSignalStackCallbackType unwind,
    348                         const void *unwind_context);
    349 
    350 // Part of HandleDeadlySignal, exposed for asan.
    351 void StartReportDeadlySignal();
    352 // Part of HandleDeadlySignal, exposed for asan.
    353 void ReportDeadlySignal(const SignalContext &sig, u32 tid,
    354                         UnwindSignalStackCallbackType unwind,
    355                         const void *unwind_context);
    356 
    357 // Alternative signal stack (POSIX-only).
    358 void SetAlternateSignalStack();
    359 void UnsetAlternateSignalStack();
    360 
    361 // Construct a one-line string:
    362 //   SUMMARY: SanitizerToolName: error_message
    363 // and pass it to __sanitizer_report_error_summary.
    364 // If alt_tool_name is provided, it's used in place of SanitizerToolName.
    365 void ReportErrorSummary(const char *error_message,
    366                         const char *alt_tool_name = nullptr);
    367 // Same as above, but construct error_message as:
    368 //   error_type file:line[:column][ function]
    369 void ReportErrorSummary(const char *error_type, const AddressInfo &info,
    370                         const char *alt_tool_name = nullptr);
    371 // Same as above, but obtains AddressInfo by symbolizing top stack trace frame.
    372 void ReportErrorSummary(const char *error_type, const StackTrace *trace,
    373                         const char *alt_tool_name = nullptr);
    374 
    375 void ReportMmapWriteExec(int prot, int mflags);
    376 
    377 // Math
    378 #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
    379 extern "C" {
    380 unsigned char _BitScanForward(unsigned long *index, unsigned long mask);
    381 unsigned char _BitScanReverse(unsigned long *index, unsigned long mask);
    382 #if defined(_WIN64)
    383 unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask);
    384 unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask);
    385 #endif
    386 }
    387 #endif
    388 
    389 inline uptr MostSignificantSetBitIndex(uptr x) {
    390   CHECK_NE(x, 0U);
    391   unsigned long up;
    392 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
    393 # ifdef _WIN64
    394   up = SANITIZER_WORDSIZE - 1 - __builtin_clzll(x);
    395 # else
    396   up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
    397 # endif
    398 #elif defined(_WIN64)
    399   _BitScanReverse64(&up, x);
    400 #else
    401   _BitScanReverse(&up, x);
    402 #endif
    403   return up;
    404 }
    405 
    406 inline uptr LeastSignificantSetBitIndex(uptr x) {
    407   CHECK_NE(x, 0U);
    408   unsigned long up;
    409 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
    410 # ifdef _WIN64
    411   up = __builtin_ctzll(x);
    412 # else
    413   up = __builtin_ctzl(x);
    414 # endif
    415 #elif defined(_WIN64)
    416   _BitScanForward64(&up, x);
    417 #else
    418   _BitScanForward(&up, x);
    419 #endif
    420   return up;
    421 }
    422 
    423 inline constexpr bool IsPowerOfTwo(uptr x) { return (x & (x - 1)) == 0; }
    424 
    425 inline uptr RoundUpToPowerOfTwo(uptr size) {
    426   CHECK(size);
    427   if (IsPowerOfTwo(size)) return size;
    428 
    429   uptr up = MostSignificantSetBitIndex(size);
    430   CHECK_LT(size, (1ULL << (up + 1)));
    431   CHECK_GT(size, (1ULL << up));
    432   return 1ULL << (up + 1);
    433 }
    434 
    435 inline constexpr uptr RoundUpTo(uptr size, uptr boundary) {
    436   RAW_CHECK(IsPowerOfTwo(boundary));
    437   return (size + boundary - 1) & ~(boundary - 1);
    438 }
    439 
    440 inline constexpr uptr RoundDownTo(uptr x, uptr boundary) {
    441   return x & ~(boundary - 1);
    442 }
    443 
    444 inline constexpr bool IsAligned(uptr a, uptr alignment) {
    445   return (a & (alignment - 1)) == 0;
    446 }
    447 
    448 inline uptr Log2(uptr x) {
    449   CHECK(IsPowerOfTwo(x));
    450   return LeastSignificantSetBitIndex(x);
    451 }
    452 
    453 // Don't use std::min, std::max or std::swap, to minimize dependency
    454 // on libstdc++.
    455 template <class T>
    456 constexpr T Min(T a, T b) {
    457   return a < b ? a : b;
    458 }
    459 template <class T>
    460 constexpr T Max(T a, T b) {
    461   return a > b ? a : b;
    462 }
    463 template<class T> void Swap(T& a, T& b) {
    464   T tmp = a;
    465   a = b;
    466   b = tmp;
    467 }
    468 
    469 // Char handling
    470 inline bool IsSpace(int c) {
    471   return (c == ' ') || (c == '\n') || (c == '\t') ||
    472          (c == '\f') || (c == '\r') || (c == '\v');
    473 }
    474 inline bool IsDigit(int c) {
    475   return (c >= '0') && (c <= '9');
    476 }
    477 inline int ToLower(int c) {
    478   return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
    479 }
    480 
    481 // A low-level vector based on mmap. May incur a significant memory overhead for
    482 // small vectors.
    483 // WARNING: The current implementation supports only POD types.
    484 template<typename T>
    485 class InternalMmapVectorNoCtor {
    486  public:
    487   using value_type = T;
    488   void Initialize(uptr initial_capacity) {
    489     capacity_bytes_ = 0;
    490     size_ = 0;
    491     data_ = 0;
    492     reserve(initial_capacity);
    493   }
    494   void Destroy() { UnmapOrDie(data_, capacity_bytes_); }
    495   T &operator[](uptr i) {
    496     CHECK_LT(i, size_);
    497     return data_[i];
    498   }
    499   const T &operator[](uptr i) const {
    500     CHECK_LT(i, size_);
    501     return data_[i];
    502   }
    503   void push_back(const T &element) {
    504     CHECK_LE(size_, capacity());
    505     if (size_ == capacity()) {
    506       uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
    507       Realloc(new_capacity);
    508     }
    509     internal_memcpy(&data_[size_++], &element, sizeof(T));
    510   }
    511   T &back() {
    512     CHECK_GT(size_, 0);
    513     return data_[size_ - 1];
    514   }
    515   void pop_back() {
    516     CHECK_GT(size_, 0);
    517     size_--;
    518   }
    519   uptr size() const {
    520     return size_;
    521   }
    522   const T *data() const {
    523     return data_;
    524   }
    525   T *data() {
    526     return data_;
    527   }
    528   uptr capacity() const { return capacity_bytes_ / sizeof(T); }
    529   void reserve(uptr new_size) {
    530     // Never downsize internal buffer.
    531     if (new_size > capacity())
    532       Realloc(new_size);
    533   }
    534   void resize(uptr new_size) {
    535     if (new_size > size_) {
    536       reserve(new_size);
    537       internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
    538     }
    539     size_ = new_size;
    540   }
    541 
    542   void clear() { size_ = 0; }
    543   bool empty() const { return size() == 0; }
    544 
    545   const T *begin() const {
    546     return data();
    547   }
    548   T *begin() {
    549     return data();
    550   }
    551   const T *end() const {
    552     return data() + size();
    553   }
    554   T *end() {
    555     return data() + size();
    556   }
    557 
    558   void swap(InternalMmapVectorNoCtor &other) {
    559     Swap(data_, other.data_);
    560     Swap(capacity_bytes_, other.capacity_bytes_);
    561     Swap(size_, other.size_);
    562   }
    563 
    564  private:
    565   void Realloc(uptr new_capacity) {
    566     CHECK_GT(new_capacity, 0);
    567     CHECK_LE(size_, new_capacity);
    568     uptr new_capacity_bytes =
    569         RoundUpTo(new_capacity * sizeof(T), GetPageSizeCached());
    570     T *new_data = (T *)MmapOrDie(new_capacity_bytes, "InternalMmapVector");
    571     internal_memcpy(new_data, data_, size_ * sizeof(T));
    572     UnmapOrDie(data_, capacity_bytes_);
    573     data_ = new_data;
    574     capacity_bytes_ = new_capacity_bytes;
    575   }
    576 
    577   T *data_;
    578   uptr capacity_bytes_;
    579   uptr size_;
    580 };
    581 
    582 template <typename T>
    583 bool operator==(const InternalMmapVectorNoCtor<T> &lhs,
    584                 const InternalMmapVectorNoCtor<T> &rhs) {
    585   if (lhs.size() != rhs.size()) return false;
    586   return internal_memcmp(lhs.data(), rhs.data(), lhs.size() * sizeof(T)) == 0;
    587 }
    588 
    589 template <typename T>
    590 bool operator!=(const InternalMmapVectorNoCtor<T> &lhs,
    591                 const InternalMmapVectorNoCtor<T> &rhs) {
    592   return !(lhs == rhs);
    593 }
    594 
    595 template<typename T>
    596 class InternalMmapVector : public InternalMmapVectorNoCtor<T> {
    597  public:
    598   InternalMmapVector() { InternalMmapVectorNoCtor<T>::Initialize(0); }
    599   explicit InternalMmapVector(uptr cnt) {
    600     InternalMmapVectorNoCtor<T>::Initialize(cnt);
    601     this->resize(cnt);
    602   }
    603   ~InternalMmapVector() { InternalMmapVectorNoCtor<T>::Destroy(); }
    604   // Disallow copies and moves.
    605   InternalMmapVector(const InternalMmapVector &) = delete;
    606   InternalMmapVector &operator=(const InternalMmapVector &) = delete;
    607   InternalMmapVector(InternalMmapVector &&) = delete;
    608   InternalMmapVector &operator=(InternalMmapVector &&) = delete;
    609 };
    610 
    611 class InternalScopedString {
    612  public:
    613   InternalScopedString() : buffer_(1) { buffer_[0] = '\0'; }
    614 
    615   uptr length() const { return buffer_.size() - 1; }
    616   void clear() {
    617     buffer_.resize(1);
    618     buffer_[0] = '\0';
    619   }
    620   void append(const char *format, ...) FORMAT(2, 3);
    621   const char *data() const { return buffer_.data(); }
    622   char *data() { return buffer_.data(); }
    623 
    624  private:
    625   InternalMmapVector<char> buffer_;
    626 };
    627 
    628 template <class T>
    629 struct CompareLess {
    630   bool operator()(const T &a, const T &b) const { return a < b; }
    631 };
    632 
    633 // HeapSort for arrays and InternalMmapVector.
    634 template <class T, class Compare = CompareLess<T>>
    635 void Sort(T *v, uptr size, Compare comp = {}) {
    636   if (size < 2)
    637     return;
    638   // Stage 1: insert elements to the heap.
    639   for (uptr i = 1; i < size; i++) {
    640     uptr j, p;
    641     for (j = i; j > 0; j = p) {
    642       p = (j - 1) / 2;
    643       if (comp(v[p], v[j]))
    644         Swap(v[j], v[p]);
    645       else
    646         break;
    647     }
    648   }
    649   // Stage 2: swap largest element with the last one,
    650   // and sink the new top.
    651   for (uptr i = size - 1; i > 0; i--) {
    652     Swap(v[0], v[i]);
    653     uptr j, max_ind;
    654     for (j = 0; j < i; j = max_ind) {
    655       uptr left = 2 * j + 1;
    656       uptr right = 2 * j + 2;
    657       max_ind = j;
    658       if (left < i && comp(v[max_ind], v[left]))
    659         max_ind = left;
    660       if (right < i && comp(v[max_ind], v[right]))
    661         max_ind = right;
    662       if (max_ind != j)
    663         Swap(v[j], v[max_ind]);
    664       else
    665         break;
    666     }
    667   }
    668 }
    669 
    670 // Works like std::lower_bound: finds the first element that is not less
    671 // than the val.
    672 template <class Container,
    673           class Compare = CompareLess<typename Container::value_type>>
    674 uptr InternalLowerBound(const Container &v,
    675                         const typename Container::value_type &val,
    676                         Compare comp = {}) {
    677   uptr first = 0;
    678   uptr last = v.size();
    679   while (last > first) {
    680     uptr mid = (first + last) / 2;
    681     if (comp(v[mid], val))
    682       first = mid + 1;
    683     else
    684       last = mid;
    685   }
    686   return first;
    687 }
    688 
    689 enum ModuleArch {
    690   kModuleArchUnknown,
    691   kModuleArchI386,
    692   kModuleArchX86_64,
    693   kModuleArchX86_64H,
    694   kModuleArchARMV6,
    695   kModuleArchARMV7,
    696   kModuleArchARMV7S,
    697   kModuleArchARMV7K,
    698   kModuleArchARM64,
    699   kModuleArchRISCV64,
    700   kModuleArchHexagon
    701 };
    702 
    703 // Sorts and removes duplicates from the container.
    704 template <class Container,
    705           class Compare = CompareLess<typename Container::value_type>>
    706 void SortAndDedup(Container &v, Compare comp = {}) {
    707   Sort(v.data(), v.size(), comp);
    708   uptr size = v.size();
    709   if (size < 2)
    710     return;
    711   uptr last = 0;
    712   for (uptr i = 1; i < size; ++i) {
    713     if (comp(v[last], v[i])) {
    714       ++last;
    715       if (last != i)
    716         v[last] = v[i];
    717     } else {
    718       CHECK(!comp(v[i], v[last]));
    719     }
    720   }
    721   v.resize(last + 1);
    722 }
    723 
    724 constexpr uptr kDefaultFileMaxSize = FIRST_32_SECOND_64(1 << 26, 1 << 28);
    725 
    726 // Opens the file 'file_name" and reads up to 'max_len' bytes.
    727 // The resulting buffer is mmaped and stored in '*buff'.
    728 // Returns true if file was successfully opened and read.
    729 bool ReadFileToVector(const char *file_name,
    730                       InternalMmapVectorNoCtor<char> *buff,
    731                       uptr max_len = kDefaultFileMaxSize,
    732                       error_t *errno_p = nullptr);
    733 
    734 // Opens the file 'file_name" and reads up to 'max_len' bytes.
    735 // This function is less I/O efficient than ReadFileToVector as it may reread
    736 // file multiple times to avoid mmap during read attempts. It's used to read
    737 // procmap, so short reads with mmap in between can produce inconsistent result.
    738 // The resulting buffer is mmaped and stored in '*buff'.
    739 // The size of the mmaped region is stored in '*buff_size'.
    740 // The total number of read bytes is stored in '*read_len'.
    741 // Returns true if file was successfully opened and read.
    742 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
    743                       uptr *read_len, uptr max_len = kDefaultFileMaxSize,
    744                       error_t *errno_p = nullptr);
    745 
    746 // When adding a new architecture, don't forget to also update
    747 // script/asan_symbolize.py and sanitizer_symbolizer_libcdep.cpp.
    748 inline const char *ModuleArchToString(ModuleArch arch) {
    749   switch (arch) {
    750     case kModuleArchUnknown:
    751       return "";
    752     case kModuleArchI386:
    753       return "i386";
    754     case kModuleArchX86_64:
    755       return "x86_64";
    756     case kModuleArchX86_64H:
    757       return "x86_64h";
    758     case kModuleArchARMV6:
    759       return "armv6";
    760     case kModuleArchARMV7:
    761       return "armv7";
    762     case kModuleArchARMV7S:
    763       return "armv7s";
    764     case kModuleArchARMV7K:
    765       return "armv7k";
    766     case kModuleArchARM64:
    767       return "arm64";
    768     case kModuleArchRISCV64:
    769       return "riscv64";
    770     case kModuleArchHexagon:
    771       return "hexagon";
    772   }
    773   CHECK(0 && "Invalid module arch");
    774   return "";
    775 }
    776 
    777 const uptr kModuleUUIDSize = 16;
    778 const uptr kMaxSegName = 16;
    779 
    780 // Represents a binary loaded into virtual memory (e.g. this can be an
    781 // executable or a shared object).
    782 class LoadedModule {
    783  public:
    784   LoadedModule()
    785       : full_name_(nullptr),
    786         base_address_(0),
    787         max_executable_address_(0),
    788         arch_(kModuleArchUnknown),
    789         instrumented_(false) {
    790     internal_memset(uuid_, 0, kModuleUUIDSize);
    791     ranges_.clear();
    792   }
    793   void set(const char *module_name, uptr base_address);
    794   void set(const char *module_name, uptr base_address, ModuleArch arch,
    795            u8 uuid[kModuleUUIDSize], bool instrumented);
    796   void clear();
    797   void addAddressRange(uptr beg, uptr end, bool executable, bool writable,
    798                        const char *name = nullptr);
    799   bool containsAddress(uptr address) const;
    800 
    801   const char *full_name() const { return full_name_; }
    802   uptr base_address() const { return base_address_; }
    803   uptr max_executable_address() const { return max_executable_address_; }
    804   ModuleArch arch() const { return arch_; }
    805   const u8 *uuid() const { return uuid_; }
    806   bool instrumented() const { return instrumented_; }
    807 
    808   struct AddressRange {
    809     AddressRange *next;
    810     uptr beg;
    811     uptr end;
    812     bool executable;
    813     bool writable;
    814     char name[kMaxSegName];
    815 
    816     AddressRange(uptr beg, uptr end, bool executable, bool writable,
    817                  const char *name)
    818         : next(nullptr),
    819           beg(beg),
    820           end(end),
    821           executable(executable),
    822           writable(writable) {
    823       internal_strncpy(this->name, (name ? name : ""), ARRAY_SIZE(this->name));
    824     }
    825   };
    826 
    827   const IntrusiveList<AddressRange> &ranges() const { return ranges_; }
    828 
    829  private:
    830   char *full_name_;  // Owned.
    831   uptr base_address_;
    832   uptr max_executable_address_;
    833   ModuleArch arch_;
    834   u8 uuid_[kModuleUUIDSize];
    835   bool instrumented_;
    836   IntrusiveList<AddressRange> ranges_;
    837 };
    838 
    839 // List of LoadedModules. OS-dependent implementation is responsible for
    840 // filling this information.
    841 class ListOfModules {
    842  public:
    843   ListOfModules() : initialized(false) {}
    844   ~ListOfModules() { clear(); }
    845   void init();
    846   void fallbackInit();  // Uses fallback init if available, otherwise clears
    847   const LoadedModule *begin() const { return modules_.begin(); }
    848   LoadedModule *begin() { return modules_.begin(); }
    849   const LoadedModule *end() const { return modules_.end(); }
    850   LoadedModule *end() { return modules_.end(); }
    851   uptr size() const { return modules_.size(); }
    852   const LoadedModule &operator[](uptr i) const {
    853     CHECK_LT(i, modules_.size());
    854     return modules_[i];
    855   }
    856 
    857  private:
    858   void clear() {
    859     for (auto &module : modules_) module.clear();
    860     modules_.clear();
    861   }
    862   void clearOrInit() {
    863     initialized ? clear() : modules_.Initialize(kInitialCapacity);
    864     initialized = true;
    865   }
    866 
    867   InternalMmapVectorNoCtor<LoadedModule> modules_;
    868   // We rarely have more than 16K loaded modules.
    869   static const uptr kInitialCapacity = 1 << 14;
    870   bool initialized;
    871 };
    872 
    873 // Callback type for iterating over a set of memory ranges.
    874 typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
    875 
    876 enum AndroidApiLevel {
    877   ANDROID_NOT_ANDROID = 0,
    878   ANDROID_KITKAT = 19,
    879   ANDROID_LOLLIPOP_MR1 = 22,
    880   ANDROID_POST_LOLLIPOP = 23
    881 };
    882 
    883 void WriteToSyslog(const char *buffer);
    884 
    885 #if defined(SANITIZER_WINDOWS) && defined(_MSC_VER) && !defined(__clang__)
    886 #define SANITIZER_WIN_TRACE 1
    887 #else
    888 #define SANITIZER_WIN_TRACE 0
    889 #endif
    890 
    891 #if SANITIZER_MAC || SANITIZER_WIN_TRACE
    892 void LogFullErrorReport(const char *buffer);
    893 #else
    894 inline void LogFullErrorReport(const char *buffer) {}
    895 #endif
    896 
    897 #if SANITIZER_LINUX || SANITIZER_MAC
    898 void WriteOneLineToSyslog(const char *s);
    899 void LogMessageOnPrintf(const char *str);
    900 #else
    901 inline void WriteOneLineToSyslog(const char *s) {}
    902 inline void LogMessageOnPrintf(const char *str) {}
    903 #endif
    904 
    905 #if SANITIZER_LINUX || SANITIZER_WIN_TRACE
    906 // Initialize Android logging. Any writes before this are silently lost.
    907 void AndroidLogInit();
    908 void SetAbortMessage(const char *);
    909 #else
    910 inline void AndroidLogInit() {}
    911 // FIXME: MacOS implementation could use CRSetCrashLogMessage.
    912 inline void SetAbortMessage(const char *) {}
    913 #endif
    914 
    915 #if SANITIZER_ANDROID
    916 void SanitizerInitializeUnwinder();
    917 AndroidApiLevel AndroidGetApiLevel();
    918 #else
    919 inline void AndroidLogWrite(const char *buffer_unused) {}
    920 inline void SanitizerInitializeUnwinder() {}
    921 inline AndroidApiLevel AndroidGetApiLevel() { return ANDROID_NOT_ANDROID; }
    922 #endif
    923 
    924 inline uptr GetPthreadDestructorIterations() {
    925 #if SANITIZER_ANDROID
    926   return (AndroidGetApiLevel() == ANDROID_LOLLIPOP_MR1) ? 8 : 4;
    927 #elif SANITIZER_POSIX
    928   return 4;
    929 #else
    930 // Unused on Windows.
    931   return 0;
    932 #endif
    933 }
    934 
    935 void *internal_start_thread(void *(*func)(void*), void *arg);
    936 void internal_join_thread(void *th);
    937 void MaybeStartBackgroudThread();
    938 
    939 // Make the compiler think that something is going on there.
    940 // Use this inside a loop that looks like memset/memcpy/etc to prevent the
    941 // compiler from recognising it and turning it into an actual call to
    942 // memset/memcpy/etc.
    943 static inline void SanitizerBreakOptimization(void *arg) {
    944 #if defined(_MSC_VER) && !defined(__clang__)
    945   _ReadWriteBarrier();
    946 #else
    947   __asm__ __volatile__("" : : "r" (arg) : "memory");
    948 #endif
    949 }
    950 
    951 struct SignalContext {
    952   void *siginfo;
    953   void *context;
    954   uptr addr;
    955   uptr pc;
    956   uptr sp;
    957   uptr bp;
    958   bool is_memory_access;
    959   enum WriteFlag { UNKNOWN, READ, WRITE } write_flag;
    960 
    961   // In some cases the kernel cannot provide the true faulting address; `addr`
    962   // will be zero then.  This field allows to distinguish between these cases
    963   // and dereferences of null.
    964   bool is_true_faulting_addr;
    965 
    966   // VS2013 doesn't implement unrestricted unions, so we need a trivial default
    967   // constructor
    968   SignalContext() = default;
    969 
    970   // Creates signal context in a platform-specific manner.
    971   // SignalContext is going to keep pointers to siginfo and context without
    972   // owning them.
    973   SignalContext(void *siginfo, void *context)
    974       : siginfo(siginfo),
    975         context(context),
    976         addr(GetAddress()),
    977         is_memory_access(IsMemoryAccess()),
    978         write_flag(GetWriteFlag()),
    979         is_true_faulting_addr(IsTrueFaultingAddress()) {
    980     InitPcSpBp();
    981   }
    982 
    983   static void DumpAllRegisters(void *context);
    984 
    985   // Type of signal e.g. SIGSEGV or EXCEPTION_ACCESS_VIOLATION.
    986   int GetType() const;
    987 
    988   // String description of the signal.
    989   const char *Describe() const;
    990 
    991   // Returns true if signal is stack overflow.
    992   bool IsStackOverflow() const;
    993 
    994  private:
    995   // Platform specific initialization.
    996   void InitPcSpBp();
    997   uptr GetAddress() const;
    998   WriteFlag GetWriteFlag() const;
    999   bool IsMemoryAccess() const;
   1000   bool IsTrueFaultingAddress() const;
   1001 };
   1002 
   1003 void InitializePlatformEarly();
   1004 void MaybeReexec();
   1005 
   1006 template <typename Fn>
   1007 class RunOnDestruction {
   1008  public:
   1009   explicit RunOnDestruction(Fn fn) : fn_(fn) {}
   1010   ~RunOnDestruction() { fn_(); }
   1011 
   1012  private:
   1013   Fn fn_;
   1014 };
   1015 
   1016 // A simple scope guard. Usage:
   1017 // auto cleanup = at_scope_exit([]{ do_cleanup; });
   1018 template <typename Fn>
   1019 RunOnDestruction<Fn> at_scope_exit(Fn fn) {
   1020   return RunOnDestruction<Fn>(fn);
   1021 }
   1022 
   1023 // Linux on 64-bit s390 had a nasty bug that crashes the whole machine
   1024 // if a process uses virtual memory over 4TB (as many sanitizers like
   1025 // to do).  This function will abort the process if running on a kernel
   1026 // that looks vulnerable.
   1027 #if SANITIZER_LINUX && SANITIZER_S390_64
   1028 void AvoidCVE_2016_2143();
   1029 #else
   1030 inline void AvoidCVE_2016_2143() {}
   1031 #endif
   1032 
   1033 struct StackDepotStats {
   1034   uptr n_uniq_ids;
   1035   uptr allocated;
   1036 };
   1037 
   1038 // The default value for allocator_release_to_os_interval_ms common flag to
   1039 // indicate that sanitizer allocator should not attempt to release memory to OS.
   1040 const s32 kReleaseToOSIntervalNever = -1;
   1041 
   1042 void CheckNoDeepBind(const char *filename, int flag);
   1043 
   1044 // Returns the requested amount of random data (up to 256 bytes) that can then
   1045 // be used to seed a PRNG. Defaults to blocking like the underlying syscall.
   1046 bool GetRandom(void *buffer, uptr length, bool blocking = true);
   1047 
   1048 // Returns the number of logical processors on the system.
   1049 u32 GetNumberOfCPUs();
   1050 extern u32 NumberOfCPUsCached;
   1051 inline u32 GetNumberOfCPUsCached() {
   1052   if (!NumberOfCPUsCached)
   1053     NumberOfCPUsCached = GetNumberOfCPUs();
   1054   return NumberOfCPUsCached;
   1055 }
   1056 
   1057 template <typename T>
   1058 class ArrayRef {
   1059  public:
   1060   ArrayRef() {}
   1061   ArrayRef(T *begin, T *end) : begin_(begin), end_(end) {}
   1062 
   1063   T *begin() { return begin_; }
   1064   T *end() { return end_; }
   1065 
   1066  private:
   1067   T *begin_ = nullptr;
   1068   T *end_ = nullptr;
   1069 };
   1070 
   1071 }  // namespace __sanitizer
   1072 
   1073 inline void *operator new(__sanitizer::operator_new_size_type size,
   1074                           __sanitizer::LowLevelAllocator &alloc) {
   1075   return alloc.Allocate(size);
   1076 }
   1077 
   1078 #endif  // SANITIZER_COMMON_H
   1079