1 //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // It declares common functions and classes that are used in both runtimes.
11 // Implementation of some functions are provided in sanitizer_common, while
12 // others must be defined by run-time library itself.
13 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_COMMON_H
15 #define SANITIZER_COMMON_H
17 #include "sanitizer_internal_defs.h"
18 #include "sanitizer_libc.h"
19 #include "sanitizer_mutex.h"
20 #include "sanitizer_flags.h"
22 namespace __sanitizer
{
26 const uptr kWordSize
= SANITIZER_WORDSIZE
/ 8;
27 const uptr kWordSizeInBits
= 8 * kWordSize
;
29 #if defined(__powerpc__) || defined(__powerpc64__)
30 const uptr kCacheLineSize
= 128;
32 const uptr kCacheLineSize
= 64;
35 const uptr kMaxPathLength
= 512;
37 const uptr kMaxThreadStackSize
= 1 << 30; // 1Gb
39 extern const char *SanitizerToolName
; // Can be changed by the tool.
42 uptr
GetPageSizeCached();
43 uptr
GetMmapGranularity();
44 uptr
GetMaxVirtualAddress();
48 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
50 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
51 uptr
*tls_addr
, uptr
*tls_size
);
54 void *MmapOrDie(uptr size
, const char *mem_type
);
55 void UnmapOrDie(void *addr
, uptr size
);
56 void *MmapFixedNoReserve(uptr fixed_addr
, uptr size
);
57 void *MmapNoReserveOrDie(uptr size
, const char *mem_type
);
58 void *MmapFixedOrDie(uptr fixed_addr
, uptr size
);
59 void *Mprotect(uptr fixed_addr
, uptr size
);
60 // Map aligned chunk of address space; size and alignment are powers of two.
61 void *MmapAlignedOrDie(uptr size
, uptr alignment
, const char *mem_type
);
62 // Used to check if we can map shadow memory to a fixed location.
63 bool MemoryRangeIsAvailable(uptr range_start
, uptr range_end
);
64 void FlushUnneededShadowMemory(uptr addr
, uptr size
);
65 void IncreaseTotalMmap(uptr size
);
66 void DecreaseTotalMmap(uptr size
);
68 // InternalScopedBuffer can be used instead of large stack arrays to
69 // keep frame size low.
70 // FIXME: use InternalAlloc instead of MmapOrDie once
71 // InternalAlloc is made libc-free.
73 class InternalScopedBuffer
{
75 explicit InternalScopedBuffer(uptr cnt
) {
77 ptr_
= (T
*)MmapOrDie(cnt
* sizeof(T
), "InternalScopedBuffer");
79 ~InternalScopedBuffer() {
80 UnmapOrDie(ptr_
, cnt_
* sizeof(T
));
82 T
&operator[](uptr i
) { return ptr_
[i
]; }
83 T
*data() { return ptr_
; }
84 uptr
size() { return cnt_
* sizeof(T
); }
89 // Disallow evil constructors.
90 InternalScopedBuffer(const InternalScopedBuffer
&);
91 void operator=(const InternalScopedBuffer
&);
94 class InternalScopedString
: public InternalScopedBuffer
<char> {
96 explicit InternalScopedString(uptr max_length
)
97 : InternalScopedBuffer
<char>(max_length
), length_(0) {
100 uptr
length() { return length_
; }
105 void append(const char *format
, ...);
111 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
112 // constructor, so all instances of LowLevelAllocator should be
113 // linker initialized.
114 class LowLevelAllocator
{
116 // Requires an external lock.
117 void *Allocate(uptr size
);
119 char *allocated_end_
;
120 char *allocated_current_
;
122 typedef void (*LowLevelAllocateCallback
)(uptr ptr
, uptr size
);
123 // Allows to register tool-specific callbacks for LowLevelAllocator.
124 // Passing NULL removes the callback.
125 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback
);
128 void RawWrite(const char *buffer
);
130 // Caching version of PrintsToTty(). Not thread-safe.
131 bool PrintsToTtyCached();
132 bool ColorizeReports();
133 void Printf(const char *format
, ...);
134 void Report(const char *format
, ...);
135 void SetPrintfAndReportCallback(void (*callback
)(const char *));
136 #define VReport(level, ...) \
138 if ((uptr)common_flags()->verbosity >= (level)) Report(__VA_ARGS__); \
140 #define VPrintf(level, ...) \
142 if ((uptr)common_flags()->verbosity >= (level)) Printf(__VA_ARGS__); \
145 // Can be used to prevent mixing error reports from different sanitizers.
146 extern StaticSpinMutex CommonSanitizerReportMutex
;
147 void MaybeOpenReportFile();
148 extern fd_t report_fd
;
149 extern bool log_to_file
;
150 extern char report_path_prefix
[4096];
151 extern uptr report_fd_pid
;
152 extern uptr stoptheworld_tracer_pid
;
153 extern uptr stoptheworld_tracer_ppid
;
155 uptr
OpenFile(const char *filename
, bool write
);
156 // Opens the file 'file_name" and reads up to 'max_len' bytes.
157 // The resulting buffer is mmaped and stored in '*buff'.
158 // The size of the mmaped region is stored in '*buff_size',
159 // Returns the number of read bytes or 0 if file can not be opened.
160 uptr
ReadFileToBuffer(const char *file_name
, char **buff
,
161 uptr
*buff_size
, uptr max_len
);
162 // Maps given file to virtual memory, and returns pointer to it
163 // (or NULL if the mapping failes). Stores the size of mmaped region
165 void *MapFileToMemory(const char *file_name
, uptr
*buff_size
);
166 void *MapWritableFileToMemory(void *addr
, uptr size
, uptr fd
, uptr offset
);
168 bool IsAccessibleMemoryRange(uptr beg
, uptr size
);
170 // Error report formatting.
171 const char *StripPathPrefix(const char *filepath
,
172 const char *strip_file_prefix
);
173 void PrintSourceLocation(InternalScopedString
*buffer
, const char *file
,
174 int line
, int column
);
175 void PrintModuleAndOffset(InternalScopedString
*buffer
,
176 const char *module
, uptr offset
);
179 void DisableCoreDumperIfNecessary();
180 void DumpProcessMap();
181 bool FileExists(const char *filename
);
182 const char *GetEnv(const char *name
);
183 bool SetEnv(const char *name
, const char *value
);
184 const char *GetPwd();
185 char *FindPathToBinary(const char *name
);
188 bool StackSizeIsUnlimited();
189 void SetStackSizeLimitInBytes(uptr limit
);
190 bool AddressSpaceIsUnlimited();
191 void SetAddressSpaceUnlimited();
192 void AdjustStackSize(void *attr
);
193 void PrepareForSandboxing(__sanitizer_sandbox_arguments
*args
);
194 void CovPrepareForSandboxing(__sanitizer_sandbox_arguments
*args
);
195 void SetSandboxingCallback(void (*f
)());
197 void CovUpdateMapping(uptr caller_pc
= 0);
198 void CovBeforeFork();
199 void CovAfterFork(int child_pid
);
205 void SleepForSeconds(int seconds
);
206 void SleepForMillis(int millis
);
208 int Atexit(void (*function
)(void));
209 void SortArray(uptr
*array
, uptr size
);
210 // Strip the directories from the module name, return a new string allocated
211 // with internal_strdup.
212 char *StripModuleName(const char *module
);
215 void NORETURN
Abort();
218 CheckFailed(const char *file
, int line
, const char *cond
, u64 v1
, u64 v2
);
220 // Set the name of the current thread to 'name', return true on succees.
221 // The name may be truncated to a system-dependent limit.
222 bool SanitizerSetThreadName(const char *name
);
223 // Get the name of the current thread (no more than max_len bytes),
224 // return true on succees. name should have space for at least max_len+1 bytes.
225 bool SanitizerGetThreadName(char *name
, int max_len
);
227 // Specific tools may override behavior of "Die" and "CheckFailed" functions
228 // to do tool-specific job.
229 typedef void (*DieCallbackType
)(void);
230 void SetDieCallback(DieCallbackType
);
231 DieCallbackType
GetDieCallback();
232 typedef void (*CheckFailedCallbackType
)(const char *, int, const char *,
234 void SetCheckFailedCallback(CheckFailedCallbackType callback
);
236 // Functions related to signal handling.
237 typedef void (*SignalHandlerType
)(int, void *, void *);
238 bool IsDeadlySignal(int signum
);
239 void InstallDeadlySignalHandlers(SignalHandlerType handler
);
240 // Alternative signal stack (POSIX-only).
241 void SetAlternateSignalStack();
242 void UnsetAlternateSignalStack();
244 // We don't want a summary too long.
245 const int kMaxSummaryLength
= 1024;
246 // Construct a one-line string:
247 // SUMMARY: SanitizerToolName: error_message
248 // and pass it to __sanitizer_report_error_summary.
249 void ReportErrorSummary(const char *error_message
);
250 // Same as above, but construct error_message as:
251 // error_type file:line function
252 void ReportErrorSummary(const char *error_type
, const char *file
,
253 int line
, const char *function
);
254 void ReportErrorSummary(const char *error_type
, StackTrace
*trace
);
257 #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
259 unsigned char _BitScanForward(unsigned long *index
, unsigned long mask
); // NOLINT
260 unsigned char _BitScanReverse(unsigned long *index
, unsigned long mask
); // NOLINT
262 unsigned char _BitScanForward64(unsigned long *index
, unsigned __int64 mask
); // NOLINT
263 unsigned char _BitScanReverse64(unsigned long *index
, unsigned __int64 mask
); // NOLINT
268 INLINE uptr
MostSignificantSetBitIndex(uptr x
) {
270 unsigned long up
; // NOLINT
271 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
272 up
= SANITIZER_WORDSIZE
- 1 - __builtin_clzl(x
);
273 #elif defined(_WIN64)
274 _BitScanReverse64(&up
, x
);
276 _BitScanReverse(&up
, x
);
281 INLINE uptr
LeastSignificantSetBitIndex(uptr x
) {
283 unsigned long up
; // NOLINT
284 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
285 up
= __builtin_ctzl(x
);
286 #elif defined(_WIN64)
287 _BitScanForward64(&up
, x
);
289 _BitScanForward(&up
, x
);
294 INLINE
bool IsPowerOfTwo(uptr x
) {
295 return (x
& (x
- 1)) == 0;
298 INLINE uptr
RoundUpToPowerOfTwo(uptr size
) {
300 if (IsPowerOfTwo(size
)) return size
;
302 uptr up
= MostSignificantSetBitIndex(size
);
303 CHECK(size
< (1ULL << (up
+ 1)));
304 CHECK(size
> (1ULL << up
));
305 return 1UL << (up
+ 1);
308 INLINE uptr
RoundUpTo(uptr size
, uptr boundary
) {
309 CHECK(IsPowerOfTwo(boundary
));
310 return (size
+ boundary
- 1) & ~(boundary
- 1);
313 INLINE uptr
RoundDownTo(uptr x
, uptr boundary
) {
314 return x
& ~(boundary
- 1);
317 INLINE
bool IsAligned(uptr a
, uptr alignment
) {
318 return (a
& (alignment
- 1)) == 0;
321 INLINE uptr
Log2(uptr x
) {
322 CHECK(IsPowerOfTwo(x
));
323 #if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
324 return __builtin_ctzl(x
);
325 #elif defined(_WIN64)
326 unsigned long ret
; // NOLINT
327 _BitScanForward64(&ret
, x
);
330 unsigned long ret
; // NOLINT
331 _BitScanForward(&ret
, x
);
336 // Don't use std::min, std::max or std::swap, to minimize dependency
338 template<class T
> T
Min(T a
, T b
) { return a
< b
? a
: b
; }
339 template<class T
> T
Max(T a
, T b
) { return a
> b
? a
: b
; }
340 template<class T
> void Swap(T
& a
, T
& b
) {
347 INLINE
bool IsSpace(int c
) {
348 return (c
== ' ') || (c
== '\n') || (c
== '\t') ||
349 (c
== '\f') || (c
== '\r') || (c
== '\v');
351 INLINE
bool IsDigit(int c
) {
352 return (c
>= '0') && (c
<= '9');
354 INLINE
int ToLower(int c
) {
355 return (c
>= 'A' && c
<= 'Z') ? (c
+ 'a' - 'A') : c
;
358 // A low-level vector based on mmap. May incur a significant memory overhead for
360 // WARNING: The current implementation supports only POD types.
362 class InternalMmapVector
{
364 explicit InternalMmapVector(uptr initial_capacity
) {
365 capacity_
= Max(initial_capacity
, (uptr
)1);
367 data_
= (T
*)MmapOrDie(capacity_
* sizeof(T
), "InternalMmapVector");
369 ~InternalMmapVector() {
370 UnmapOrDie(data_
, capacity_
* sizeof(T
));
372 T
&operator[](uptr i
) {
376 const T
&operator[](uptr i
) const {
380 void push_back(const T
&element
) {
381 CHECK_LE(size_
, capacity_
);
382 if (size_
== capacity_
) {
383 uptr new_capacity
= RoundUpToPowerOfTwo(size_
+ 1);
384 Resize(new_capacity
);
386 data_
[size_
++] = element
;
390 return data_
[size_
- 1];
399 const T
*data() const {
402 uptr
capacity() const {
406 void clear() { size_
= 0; }
409 void Resize(uptr new_capacity
) {
410 CHECK_GT(new_capacity
, 0);
411 CHECK_LE(size_
, new_capacity
);
412 T
*new_data
= (T
*)MmapOrDie(new_capacity
* sizeof(T
),
413 "InternalMmapVector");
414 internal_memcpy(new_data
, data_
, size_
* sizeof(T
));
417 UnmapOrDie(old_data
, capacity_
* sizeof(T
));
418 capacity_
= new_capacity
;
420 // Disallow evil constructors.
421 InternalMmapVector(const InternalMmapVector
&);
422 void operator=(const InternalMmapVector
&);
429 // HeapSort for arrays and InternalMmapVector.
430 template<class Container
, class Compare
>
431 void InternalSort(Container
*v
, uptr size
, Compare comp
) {
434 // Stage 1: insert elements to the heap.
435 for (uptr i
= 1; i
< size
; i
++) {
437 for (j
= i
; j
> 0; j
= p
) {
439 if (comp((*v
)[p
], (*v
)[j
]))
440 Swap((*v
)[j
], (*v
)[p
]);
445 // Stage 2: swap largest element with the last one,
446 // and sink the new top.
447 for (uptr i
= size
- 1; i
> 0; i
--) {
448 Swap((*v
)[0], (*v
)[i
]);
450 for (j
= 0; j
< i
; j
= max_ind
) {
451 uptr left
= 2 * j
+ 1;
452 uptr right
= 2 * j
+ 2;
454 if (left
< i
&& comp((*v
)[max_ind
], (*v
)[left
]))
456 if (right
< i
&& comp((*v
)[max_ind
], (*v
)[right
]))
459 Swap((*v
)[j
], (*v
)[max_ind
]);
466 template<class Container
, class Value
, class Compare
>
467 uptr
InternalBinarySearch(const Container
&v
, uptr first
, uptr last
,
468 const Value
&val
, Compare comp
) {
469 uptr not_found
= last
+ 1;
470 while (last
>= first
) {
471 uptr mid
= (first
+ last
) / 2;
472 if (comp(v
[mid
], val
))
474 else if (comp(val
, v
[mid
]))
482 // Represents a binary loaded into virtual memory (e.g. this can be an
483 // executable or a shared object).
486 LoadedModule(const char *module_name
, uptr base_address
);
487 void addAddressRange(uptr beg
, uptr end
, bool executable
);
488 bool containsAddress(uptr address
) const;
490 const char *full_name() const { return full_name_
; }
491 uptr
base_address() const { return base_address_
; }
493 uptr
n_ranges() const { return n_ranges_
; }
494 uptr
address_range_start(int i
) const { return ranges_
[i
].beg
; }
495 uptr
address_range_end(int i
) const { return ranges_
[i
].end
; }
496 bool address_range_executable(int i
) const { return exec_
[i
]; }
499 struct AddressRange
{
505 static const uptr kMaxNumberOfAddressRanges
= 6;
506 AddressRange ranges_
[kMaxNumberOfAddressRanges
];
507 bool exec_
[kMaxNumberOfAddressRanges
];
511 // OS-dependent function that fills array with descriptions of at most
512 // "max_modules" currently loaded modules. Returns the number of
513 // initialized modules. If filter is nonzero, ignores modules for which
514 // filter(full_name) is false.
515 typedef bool (*string_predicate_t
)(const char *);
516 uptr
GetListOfModules(LoadedModule
*modules
, uptr max_modules
,
517 string_predicate_t filter
);
520 const uptr kPthreadDestructorIterations
= 4;
522 // Unused on Windows.
523 const uptr kPthreadDestructorIterations
= 0;
526 // Callback type for iterating over a set of memory ranges.
527 typedef void (*RangeIteratorCallback
)(uptr begin
, uptr end
, void *arg
);
529 #if (SANITIZER_FREEBSD || SANITIZER_LINUX) && !defined(SANITIZER_GO)
530 extern uptr indirect_call_wrapper
;
531 void SetIndirectCallWrapper(uptr wrapper
);
533 template <typename F
>
534 F
IndirectExternCall(F f
) {
535 typedef F (*WrapF
)(F
);
536 return indirect_call_wrapper
? ((WrapF
)indirect_call_wrapper
)(f
) : f
;
539 INLINE
void SetIndirectCallWrapper(uptr wrapper
) {}
540 template <typename F
>
541 F
IndirectExternCall(F f
) {
546 #if SANITIZER_ANDROID
547 // Initialize Android logging. Any writes before this are silently lost.
548 void AndroidLogInit();
549 void AndroidLogWrite(const char *buffer
);
550 void GetExtraActivationFlags(char *buf
, uptr size
);
551 void SanitizerInitializeUnwinder();
553 INLINE
void AndroidLogInit() {}
554 INLINE
void AndroidLogWrite(const char *buffer_unused
) {}
555 INLINE
void GetExtraActivationFlags(char *buf
, uptr size
) { *buf
= '\0'; }
556 INLINE
void SanitizerInitializeUnwinder() {}
558 } // namespace __sanitizer
560 inline void *operator new(__sanitizer::operator_new_size_type size
,
561 __sanitizer::LowLevelAllocator
&alloc
) {
562 return alloc
.Allocate(size
);
565 struct StackDepotStats
{
570 #endif // SANITIZER_COMMON_H