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