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
;
25 #if defined(__powerpc__) || defined(__powerpc64__)
26 const uptr kCacheLineSize
= 128;
28 const uptr kCacheLineSize
= 64;
32 uptr
GetPageSizeCached();
33 uptr
GetMmapGranularity();
38 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
42 void *MmapOrDie(uptr size
, const char *mem_type
);
43 void UnmapOrDie(void *addr
, uptr size
);
44 void *MmapFixedNoReserve(uptr fixed_addr
, uptr size
);
45 void *MmapFixedOrDie(uptr fixed_addr
, uptr size
);
46 void *Mprotect(uptr fixed_addr
, uptr size
);
47 // Map aligned chunk of address space; size and alignment are powers of two.
48 void *MmapAlignedOrDie(uptr size
, uptr alignment
, const char *mem_type
);
49 // Used to check if we can map shadow memory to a fixed location.
50 bool MemoryRangeIsAvailable(uptr range_start
, uptr range_end
);
51 void FlushUnneededShadowMemory(uptr addr
, uptr size
);
54 void *InternalAlloc(uptr size
);
55 void InternalFree(void *p
);
57 // InternalScopedBuffer can be used instead of large stack arrays to
58 // keep frame size low.
59 // FIXME: use InternalAlloc instead of MmapOrDie once
60 // InternalAlloc is made libc-free.
62 class InternalScopedBuffer
{
64 explicit InternalScopedBuffer(uptr cnt
) {
66 ptr_
= (T
*)MmapOrDie(cnt
* sizeof(T
), "InternalScopedBuffer");
68 ~InternalScopedBuffer() {
69 UnmapOrDie(ptr_
, cnt_
* sizeof(T
));
71 T
&operator[](uptr i
) { return ptr_
[i
]; }
72 T
*data() { return ptr_
; }
73 uptr
size() { return cnt_
* sizeof(T
); }
78 // Disallow evil constructors.
79 InternalScopedBuffer(const InternalScopedBuffer
&);
80 void operator=(const InternalScopedBuffer
&);
83 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
84 // constructor, so all instances of LowLevelAllocator should be
85 // linker initialized.
86 class LowLevelAllocator
{
88 // Requires an external lock.
89 void *Allocate(uptr size
);
92 char *allocated_current_
;
94 typedef void (*LowLevelAllocateCallback
)(uptr ptr
, uptr size
);
95 // Allows to register tool-specific callbacks for LowLevelAllocator.
96 // Passing NULL removes the callback.
97 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback
);
100 void RawWrite(const char *buffer
);
102 void Printf(const char *format
, ...);
103 void Report(const char *format
, ...);
104 void SetPrintfAndReportCallback(void (*callback
)(const char *));
106 // Opens the file 'file_name" and reads up to 'max_len' bytes.
107 // The resulting buffer is mmaped and stored in '*buff'.
108 // The size of the mmaped region is stored in '*buff_size',
109 // Returns the number of read bytes or 0 if file can not be opened.
110 uptr
ReadFileToBuffer(const char *file_name
, char **buff
,
111 uptr
*buff_size
, uptr max_len
);
112 // Maps given file to virtual memory, and returns pointer to it
113 // (or NULL if the mapping failes). Stores the size of mmaped region
115 void *MapFileToMemory(const char *file_name
, uptr
*buff_size
);
118 void DisableCoreDumper();
119 void DumpProcessMap();
120 bool FileExists(const char *filename
);
121 const char *GetEnv(const char *name
);
122 const char *GetPwd();
124 bool StackSizeIsUnlimited();
125 void SetStackSizeLimitInBytes(uptr limit
);
126 void PrepareForSandboxing();
129 void SleepForSeconds(int seconds
);
130 void SleepForMillis(int millis
);
131 int Atexit(void (*function
)(void));
132 void SortArray(uptr
*array
, uptr size
);
135 void NORETURN
Abort();
136 void NORETURN
Exit(int exitcode
);
138 void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
139 CheckFailed(const char *file
, int line
, const char *cond
, u64 v1
, u64 v2
);
141 // Set the name of the current thread to 'name', return true on succees.
142 // The name may be truncated to a system-dependent limit.
143 bool SanitizerSetThreadName(const char *name
);
144 // Get the name of the current thread (no more than max_len bytes),
145 // return true on succees. name should have space for at least max_len+1 bytes.
146 bool SanitizerGetThreadName(char *name
, int max_len
);
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 INLINE uptr
RoundDownTo(uptr x
, uptr boundary
) {
164 return x
& ~(boundary
- 1);
166 INLINE
bool IsAligned(uptr a
, uptr alignment
) {
167 return (a
& (alignment
- 1)) == 0;
169 // Don't use std::min, std::max or std::swap, to minimize dependency
171 template<class T
> T
Min(T a
, T b
) { return a
< b
? a
: b
; }
172 template<class T
> T
Max(T a
, T b
) { return a
> b
? a
: b
; }
173 template<class T
> void Swap(T
& a
, T
& b
) {
180 INLINE
bool IsSpace(int c
) {
181 return (c
== ' ') || (c
== '\n') || (c
== '\t') ||
182 (c
== '\f') || (c
== '\r') || (c
== '\v');
184 INLINE
bool IsDigit(int c
) {
185 return (c
>= '0') && (c
<= '9');
187 INLINE
int ToLower(int c
) {
188 return (c
>= 'A' && c
<= 'Z') ? (c
+ 'a' - 'A') : c
;
191 #if SANITIZER_WORDSIZE == 64
192 # define FIRST_32_SECOND_64(a, b) (b)
194 # define FIRST_32_SECOND_64(a, b) (a)
197 } // namespace __sanitizer
199 #endif // SANITIZER_COMMON_H