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