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"
19 namespace __sanitizer
{
22 const uptr kWordSize
= SANITIZER_WORDSIZE
/ 8;
23 const uptr kWordSizeInBits
= 8 * kWordSize
;
24 #if defined(__powerpc__) || defined(__powerpc64__)
25 // Current PPC64 kernels use 64K pages sizes, but they can be
26 // configured with 4K or even other sizes.
27 // We may want to use getpagesize() or sysconf(_SC_PAGESIZE) here rather than
28 // hardcoding the values, but today these values need to be compile-time
30 const uptr kPageSize
= 1UL << 16;
31 const uptr kCacheLineSize
= 128;
32 const uptr kMmapGranularity
= kPageSize
;
33 #elif !defined(_WIN32)
34 const uptr kPageSize
= 1UL << 12;
35 const uptr kCacheLineSize
= 64;
36 const uptr kMmapGranularity
= kPageSize
;
38 const uptr kPageSize
= 1UL << 12;
39 const uptr kCacheLineSize
= 64;
40 const uptr kMmapGranularity
= 1UL << 16;
47 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
51 void *MmapOrDie(uptr size
, const char *mem_type
);
52 void UnmapOrDie(void *addr
, uptr size
);
53 void *MmapFixedNoReserve(uptr fixed_addr
, uptr size
);
54 void *Mprotect(uptr fixed_addr
, uptr size
);
55 // Used to check if we can map shadow memory to a fixed location.
56 bool MemoryRangeIsAvailable(uptr range_start
, uptr range_end
);
59 void *InternalAlloc(uptr size
);
60 void InternalFree(void *p
);
61 // Given the pointer p into a valid allocated block,
62 // returns a pointer to the beginning of the block.
63 void *InternalAllocBlock(void *p
);
65 // InternalScopedBuffer can be used instead of large stack arrays to
66 // keep frame size low.
67 // FIXME: use InternalAlloc instead of MmapOrDie once
68 // InternalAlloc is made libc-free.
70 class InternalScopedBuffer
{
72 explicit InternalScopedBuffer(uptr cnt
) {
74 ptr_
= (T
*)MmapOrDie(cnt
* sizeof(T
), "InternalScopedBuffer");
76 ~InternalScopedBuffer() {
77 UnmapOrDie(ptr_
, cnt_
* sizeof(T
));
79 T
&operator[](uptr i
) { return ptr_
[i
]; }
80 T
*data() { return ptr_
; }
81 uptr
size() { return cnt_
* sizeof(T
); }
86 // Disallow evil constructors.
87 InternalScopedBuffer(const InternalScopedBuffer
&);
88 void operator=(const InternalScopedBuffer
&);
91 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
92 // constructor, so all instances of LowLevelAllocator should be
93 // linker initialized.
94 class LowLevelAllocator
{
96 // Requires an external lock.
97 void *Allocate(uptr size
);
100 char *allocated_current_
;
102 typedef void (*LowLevelAllocateCallback
)(uptr ptr
, uptr size
);
103 // Allows to register tool-specific callbacks for LowLevelAllocator.
104 // Passing NULL removes the callback.
105 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback
);
108 void RawWrite(const char *buffer
);
110 void Printf(const char *format
, ...);
111 void Report(const char *format
, ...);
112 void SetPrintfAndReportCallback(void (*callback
)(const char *));
114 // Opens the file 'file_name" and reads up to 'max_len' bytes.
115 // The resulting buffer is mmaped and stored in '*buff'.
116 // The size of the mmaped region is stored in '*buff_size',
117 // Returns the number of read bytes or 0 if file can not be opened.
118 uptr
ReadFileToBuffer(const char *file_name
, char **buff
,
119 uptr
*buff_size
, uptr max_len
);
120 // Maps given file to virtual memory, and returns pointer to it
121 // (or NULL if the mapping failes). Stores the size of mmaped region
123 void *MapFileToMemory(const char *file_name
, uptr
*buff_size
);
126 void DisableCoreDumper();
127 void DumpProcessMap();
128 bool FileExists(const char *filename
);
129 const char *GetEnv(const char *name
);
130 const char *GetPwd();
132 bool StackSizeIsUnlimited();
133 void SetStackSizeLimitInBytes(uptr limit
);
136 void SleepForSeconds(int seconds
);
137 void SleepForMillis(int millis
);
138 int Atexit(void (*function
)(void));
139 void SortArray(uptr
*array
, uptr size
);
142 void NORETURN
Abort();
143 void NORETURN
Exit(int exitcode
);
145 void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
146 CheckFailed(const char *file
, int line
, const char *cond
, u64 v1
, u64 v2
);
148 // Specific tools may override behavior of "Die" and "CheckFailed" functions
149 // to do tool-specific job.
150 void SetDieCallback(void (*callback
)(void));
151 typedef void (*CheckFailedCallbackType
)(const char *, int, const char *,
153 void SetCheckFailedCallback(CheckFailedCallbackType callback
);
156 INLINE
bool IsPowerOfTwo(uptr x
) {
157 return (x
& (x
- 1)) == 0;
159 INLINE uptr
RoundUpTo(uptr size
, uptr boundary
) {
160 CHECK(IsPowerOfTwo(boundary
));
161 return (size
+ boundary
- 1) & ~(boundary
- 1);
163 // Don't use std::min, std::max or std::swap, to minimize dependency
165 template<class T
> T
Min(T a
, T b
) { return a
< b
? a
: b
; }
166 template<class T
> T
Max(T a
, T b
) { return a
> b
? a
: b
; }
167 template<class T
> void Swap(T
& a
, T
& b
) {
174 INLINE
bool IsSpace(int c
) {
175 return (c
== ' ') || (c
== '\n') || (c
== '\t') ||
176 (c
== '\f') || (c
== '\r') || (c
== '\v');
178 INLINE
bool IsDigit(int c
) {
179 return (c
>= '0') && (c
<= '9');
181 INLINE
int ToLower(int c
) {
182 return (c
>= 'A' && c
<= 'Z') ? (c
+ 'a' - 'A') : c
;
185 #if SANITIZER_WORDSIZE == 64
186 # define FIRST_32_SECOND_64(a, b) (b)
188 # define FIRST_32_SECOND_64(a, b) (a)
191 } // namespace __sanitizer
193 #endif // SANITIZER_COMMON_H