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
= __WORDSIZE
/ 8;
23 const uptr kWordSizeInBits
= 8 * kWordSize
;
24 const uptr kPageSizeBits
= 12;
25 const uptr kPageSize
= 1UL << kPageSizeBits
;
26 const uptr kCacheLineSize
= 64;
28 const uptr kMmapGranularity
= kPageSize
;
30 const uptr kMmapGranularity
= 1UL << 16;
37 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
41 void *MmapOrDie(uptr size
, const char *mem_type
);
42 void UnmapOrDie(void *addr
, uptr size
);
43 void *MmapFixedNoReserve(uptr fixed_addr
, uptr size
);
44 void *Mprotect(uptr fixed_addr
, uptr size
);
45 // Used to check if we can map shadow memory to a fixed location.
46 bool MemoryRangeIsAvailable(uptr range_start
, uptr range_end
);
49 void *InternalAlloc(uptr size
);
50 void InternalFree(void *p
);
51 // Given the pointer p into a valid allocated block,
52 // returns a pointer to the beginning of the block.
53 void *InternalAllocBlock(void *p
);
55 // InternalScopedBuffer can be used instead of large stack arrays to
56 // keep frame size low.
57 // FIXME: use InternalAlloc instead of MmapOrDie once
58 // InternalAlloc is made libc-free.
60 class InternalScopedBuffer
{
62 explicit InternalScopedBuffer(uptr cnt
) {
64 ptr_
= (T
*)MmapOrDie(cnt
* sizeof(T
), "InternalScopedBuffer");
66 ~InternalScopedBuffer() {
67 UnmapOrDie(ptr_
, cnt_
* sizeof(T
));
69 T
&operator[](uptr i
) { return ptr_
[i
]; }
70 T
*data() { return ptr_
; }
71 uptr
size() { return cnt_
* sizeof(T
); }
76 // Disallow evil constructors.
77 InternalScopedBuffer(const InternalScopedBuffer
&);
78 void operator=(const InternalScopedBuffer
&);
81 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
82 // constructor, so all instances of LowLevelAllocator should be
83 // linker initialized.
84 class LowLevelAllocator
{
86 // Requires an external lock.
87 void *Allocate(uptr size
);
90 char *allocated_current_
;
92 typedef void (*LowLevelAllocateCallback
)(uptr ptr
, uptr size
);
93 // Allows to register tool-specific callbacks for LowLevelAllocator.
94 // Passing NULL removes the callback.
95 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback
);
98 void RawWrite(const char *buffer
);
99 void Printf(const char *format
, ...);
100 void Report(const char *format
, ...);
101 void SetPrintfAndReportCallback(void (*callback
)(const char *));
103 // Opens the file 'file_name" and reads up to 'max_len' bytes.
104 // The resulting buffer is mmaped and stored in '*buff'.
105 // The size of the mmaped region is stored in '*buff_size',
106 // Returns the number of read bytes or 0 if file can not be opened.
107 uptr
ReadFileToBuffer(const char *file_name
, char **buff
,
108 uptr
*buff_size
, uptr max_len
);
109 // Maps given file to virtual memory, and returns pointer to it
110 // (or NULL if the mapping failes). Stores the size of mmaped region
112 void *MapFileToMemory(const char *file_name
, uptr
*buff_size
);
115 void DisableCoreDumper();
116 void DumpProcessMap();
117 const char *GetEnv(const char *name
);
118 const char *GetPwd();
120 bool StackSizeIsUnlimited();
121 void SetStackSizeLimitInBytes(uptr limit
);
124 void SleepForSeconds(int seconds
);
125 void SleepForMillis(int millis
);
126 int Atexit(void (*function
)(void));
127 void SortArray(uptr
*array
, uptr size
);
130 void NORETURN
Abort();
131 void NORETURN
Exit(int exitcode
);
133 void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
134 CheckFailed(const char *file
, int line
, const char *cond
, u64 v1
, u64 v2
);
136 // Specific tools may override behavior of "Die" and "CheckFailed" functions
137 // to do tool-specific job.
138 void SetDieCallback(void (*callback
)(void));
139 typedef void (*CheckFailedCallbackType
)(const char *, int, const char *,
141 void SetCheckFailedCallback(CheckFailedCallbackType callback
);
144 INLINE
bool IsPowerOfTwo(uptr x
) {
145 return (x
& (x
- 1)) == 0;
147 INLINE uptr
RoundUpTo(uptr size
, uptr boundary
) {
148 CHECK(IsPowerOfTwo(boundary
));
149 return (size
+ boundary
- 1) & ~(boundary
- 1);
151 // Don't use std::min, std::max or std::swap, to minimize dependency
153 template<class T
> T
Min(T a
, T b
) { return a
< b
? a
: b
; }
154 template<class T
> T
Max(T a
, T b
) { return a
> b
? a
: b
; }
155 template<class T
> void Swap(T
& a
, T
& b
) {
162 INLINE
bool IsSpace(int c
) {
163 return (c
== ' ') || (c
== '\n') || (c
== '\t') ||
164 (c
== '\f') || (c
== '\r') || (c
== '\v');
166 INLINE
bool IsDigit(int c
) {
167 return (c
>= '0') && (c
<= '9');
169 INLINE
int ToLower(int c
) {
170 return (c
>= 'A' && c
<= 'Z') ? (c
+ 'a' - 'A') : c
;
174 # define FIRST_32_SECOND_64(a, b) (b)
176 # define FIRST_32_SECOND_64(a, b) (a)
179 } // namespace __sanitizer
181 #endif // SANITIZER_COMMON_H