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