1 //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
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
19 #include "sanitizer_internal_defs.h"
21 namespace __sanitizer
{
24 const uptr kWordSize
= SANITIZER_WORDSIZE
/ 8;
25 const uptr kWordSizeInBits
= 8 * kWordSize
;
27 #if defined(__powerpc__) || defined(__powerpc64__)
28 const uptr kCacheLineSize
= 128;
30 const uptr kCacheLineSize
= 64;
33 extern const char *SanitizerToolName
; // Can be changed by the tool.
36 uptr
GetPageSizeCached();
37 uptr
GetMmapGranularity();
42 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
46 void *MmapOrDie(uptr size
, const char *mem_type
);
47 void UnmapOrDie(void *addr
, uptr size
);
48 void *MmapFixedNoReserve(uptr fixed_addr
, uptr size
);
49 void *MmapFixedOrDie(uptr fixed_addr
, uptr size
);
50 void *Mprotect(uptr fixed_addr
, uptr size
);
51 // Map aligned chunk of address space; size and alignment are powers of two.
52 void *MmapAlignedOrDie(uptr size
, uptr alignment
, const char *mem_type
);
53 // Used to check if we can map shadow memory to a fixed location.
54 bool MemoryRangeIsAvailable(uptr range_start
, uptr range_end
);
55 void FlushUnneededShadowMemory(uptr addr
, uptr size
);
58 void *InternalAlloc(uptr size
);
59 void InternalFree(void *p
);
61 // InternalScopedBuffer can be used instead of large stack arrays to
62 // keep frame size low.
63 // FIXME: use InternalAlloc instead of MmapOrDie once
64 // InternalAlloc is made libc-free.
66 class InternalScopedBuffer
{
68 explicit InternalScopedBuffer(uptr cnt
) {
70 ptr_
= (T
*)MmapOrDie(cnt
* sizeof(T
), "InternalScopedBuffer");
72 ~InternalScopedBuffer() {
73 UnmapOrDie(ptr_
, cnt_
* sizeof(T
));
75 T
&operator[](uptr i
) { return ptr_
[i
]; }
76 T
*data() { return ptr_
; }
77 uptr
size() { return cnt_
* sizeof(T
); }
82 // Disallow evil constructors.
83 InternalScopedBuffer(const InternalScopedBuffer
&);
84 void operator=(const InternalScopedBuffer
&);
87 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
88 // constructor, so all instances of LowLevelAllocator should be
89 // linker initialized.
90 class LowLevelAllocator
{
92 // Requires an external lock.
93 void *Allocate(uptr size
);
96 char *allocated_current_
;
98 typedef void (*LowLevelAllocateCallback
)(uptr ptr
, uptr size
);
99 // Allows to register tool-specific callbacks for LowLevelAllocator.
100 // Passing NULL removes the callback.
101 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback
);
104 void RawWrite(const char *buffer
);
106 void Printf(const char *format
, ...);
107 void Report(const char *format
, ...);
108 void SetPrintfAndReportCallback(void (*callback
)(const char *));
110 fd_t
OpenFile(const char *filename
, bool write
);
111 // Opens the file 'file_name" and reads up to 'max_len' bytes.
112 // The resulting buffer is mmaped and stored in '*buff'.
113 // The size of the mmaped region is stored in '*buff_size',
114 // Returns the number of read bytes or 0 if file can not be opened.
115 uptr
ReadFileToBuffer(const char *file_name
, char **buff
,
116 uptr
*buff_size
, uptr max_len
);
117 // Maps given file to virtual memory, and returns pointer to it
118 // (or NULL if the mapping failes). Stores the size of mmaped region
120 void *MapFileToMemory(const char *file_name
, uptr
*buff_size
);
123 void DisableCoreDumper();
124 void DumpProcessMap();
125 bool FileExists(const char *filename
);
126 const char *GetEnv(const char *name
);
127 const char *GetPwd();
129 bool StackSizeIsUnlimited();
130 void SetStackSizeLimitInBytes(uptr limit
);
131 void PrepareForSandboxing();
134 void SleepForSeconds(int seconds
);
135 void SleepForMillis(int millis
);
136 int Atexit(void (*function
)(void));
137 void SortArray(uptr
*array
, uptr size
);
140 void NORETURN
Abort();
141 void NORETURN
Exit(int exitcode
);
143 void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
144 CheckFailed(const char *file
, int line
, const char *cond
, u64 v1
, u64 v2
);
146 // Set the name of the current thread to 'name', return true on succees.
147 // The name may be truncated to a system-dependent limit.
148 bool SanitizerSetThreadName(const char *name
);
149 // Get the name of the current thread (no more than max_len bytes),
150 // return true on succees. name should have space for at least max_len+1 bytes.
151 bool SanitizerGetThreadName(char *name
, int max_len
);
153 // Specific tools may override behavior of "Die" and "CheckFailed" functions
154 // to do tool-specific job.
155 void SetDieCallback(void (*callback
)(void));
156 typedef void (*CheckFailedCallbackType
)(const char *, int, const char *,
158 void SetCheckFailedCallback(CheckFailedCallbackType callback
);
161 INLINE
bool IsPowerOfTwo(uptr x
) {
162 return (x
& (x
- 1)) == 0;
164 INLINE uptr
RoundUpTo(uptr size
, uptr boundary
) {
165 CHECK(IsPowerOfTwo(boundary
));
166 return (size
+ boundary
- 1) & ~(boundary
- 1);
168 INLINE uptr
RoundDownTo(uptr x
, uptr boundary
) {
169 return x
& ~(boundary
- 1);
171 INLINE
bool IsAligned(uptr a
, uptr alignment
) {
172 return (a
& (alignment
- 1)) == 0;
174 // Don't use std::min, std::max or std::swap, to minimize dependency
176 template<class T
> T
Min(T a
, T b
) { return a
< b
? a
: b
; }
177 template<class T
> T
Max(T a
, T b
) { return a
> b
? a
: b
; }
178 template<class T
> void Swap(T
& a
, T
& b
) {
185 INLINE
bool IsSpace(int c
) {
186 return (c
== ' ') || (c
== '\n') || (c
== '\t') ||
187 (c
== '\f') || (c
== '\r') || (c
== '\v');
189 INLINE
bool IsDigit(int c
) {
190 return (c
>= '0') && (c
<= '9');
192 INLINE
int ToLower(int c
) {
193 return (c
>= 'A' && c
<= 'Z') ? (c
+ 'a' - 'A') : c
;
196 #if SANITIZER_WORDSIZE == 64
197 # define FIRST_32_SECOND_64(a, b) (b)
199 # define FIRST_32_SECOND_64(a, b) (a)
202 } // namespace __sanitizer
204 #endif // SANITIZER_COMMON_H