blob: e361c26ee4ed476e3e9f95f9de3bddbddd613adf [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- asan_internal.h -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// ASan-private header which defines various general utilities.
13//===----------------------------------------------------------------------===//
14#ifndef ASAN_INTERNAL_H
15#define ASAN_INTERNAL_H
16
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000017#if !defined(__linux__) && !defined(__APPLE__)
18# error "This operating system is not supported by AddressSanitizer"
19#endif
20
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021#include <stdint.h> // for __WORDSIZE
22#include <stdlib.h> // for size_t
Kostya Serebryany1e172b42011-11-30 01:07:02 +000023
Daniel Dunbar46166332011-12-02 01:32:27 +000024// If __WORDSIZE was undefined by the platform, define it in terms of the
25// compiler built-in __LP64__.
26#ifndef __WORDSIZE
27#if __LP64__
28#define __WORDSIZE 64
29#else
30#define __WORDSIZE 32
31#endif
32#endif
33
Alexander Potapenkoc8365232012-01-27 10:52:37 +000034#if !defined(__has_feature)
35#define __has_feature(x) 0
36#endif
37
Kostya Serebryany13ebae62011-12-27 21:57:12 +000038#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000039# error "The AddressSanitizer run-time should not be"
40 " instrumented by AddressSanitizer"
41#endif
42
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000043// Build-time configuration options.
44
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000045// If set, asan will install its own SEGV signal handler.
46#ifndef ASAN_NEEDS_SEGV
47# define ASAN_NEEDS_SEGV 1
48#endif
49
50// If set, asan will intercept C++ exception api call(s).
51#ifndef ASAN_HAS_EXCEPTIONS
52# define ASAN_HAS_EXCEPTIONS 1
53#endif
54
55// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
56// provided by the instrumented objects. Otherwise constants are used.
57#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
58# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
59#endif
60
Kostya Serebryany1e172b42011-11-30 01:07:02 +000061// All internal functions in asan reside inside the __asan namespace
62// to avoid namespace collisions with the user programs.
63// Seperate namespace also makes it simpler to distinguish the asan run-time
64// functions from the instrumented user code in a profile.
65namespace __asan {
66
67class AsanThread;
68struct AsanStackTrace;
69
Kostya Serebryany218a9b72011-11-30 18:50:23 +000070// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000071void CheckFailed(const char *cond, const char *file, int line);
72void ShowStatsAndAbort();
73
Kostya Serebryany218a9b72011-11-30 18:50:23 +000074// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000075bool DescribeAddrIfGlobal(uintptr_t addr);
76
Kostya Serebryany218a9b72011-11-30 18:50:23 +000077// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000078void ReplaceSystemMalloc();
79
Kostya Serebryanyde496f42011-12-28 22:58:01 +000080void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
81
Kostya Serebryany218a9b72011-11-30 18:50:23 +000082// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000083void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +000084int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +000085const char *AsanGetEnv(const char *name);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000086
Kostya Serebryanya874fe52011-12-28 23:28:54 +000087void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
88void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
89void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000090void *AsanMmapSomewhereOrDie(size_t size, const char *where);
91void AsanUnmapOrDie(void *ptr, size_t size);
92
Kostya Serebryanyef14ff62012-01-06 02:12:25 +000093void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +000094void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +000095
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000096size_t AsanRead(int fd, void *buf, size_t count);
97size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000098int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000099
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000100bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000101void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000102int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000103uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000104int AtomicInc(int *a);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000105
106// Wrapper for TLS/TSD.
107void AsanTSDInit();
108void *AsanTSDGet();
109void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000110
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000111// 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',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000114// Returns the number of read bytes or 0 if file can not be opened.
115size_t ReadFileToBuffer(const char *file_name, char **buff,
116 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000117
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000118// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119void RawWrite(const char *buffer);
120int SNPrint(char *buffer, size_t length, const char *format, ...);
121void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000122int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000123void Report(const char *format, ...);
124
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000125// Don't use std::min and std::max, to minimize dependency on libstdc++.
126template<class T> T Min(T a, T b) { return a < b ? a : b; }
127template<class T> T Max(T a, T b) { return a > b ? a : b; }
128
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000129// asan_poisoning.cc
130// Poisons the shadow memory for "size" bytes starting from "addr".
131void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
132// Poisons the shadow memory for "redzone_size" bytes starting from
133// "addr + size".
134void PoisonShadowPartialRightRedzone(uintptr_t addr,
135 uintptr_t size,
136 uintptr_t redzone_size,
137 uint8_t value);
138
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139extern size_t FLAG_quarantine_size;
140extern int FLAG_demangle;
141extern bool FLAG_symbolize;
142extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000143extern size_t FLAG_redzone;
144extern int FLAG_debug;
145extern bool FLAG_poison_shadow;
146extern int FLAG_report_globals;
147extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000148extern bool FLAG_replace_str;
149extern bool FLAG_replace_intrin;
150extern bool FLAG_replace_cfallocator;
151extern bool FLAG_fast_unwind;
152extern bool FLAG_use_fake_stack;
153extern size_t FLAG_max_malloc_fill_size;
154extern int FLAG_exitcode;
155extern bool FLAG_allow_user_poisoning;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000156extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000157
158extern int asan_inited;
159// Used to avoid infinite recursion in __asan_init().
160extern bool asan_init_is_running;
161
162enum LinkerInitialized { LINKER_INITIALIZED = 0 };
163
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000164void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165
166#define CHECK(cond) do { if (!(cond)) { \
167 CheckFailed(#cond, __FILE__, __LINE__); \
168}}while(0)
169
170#define RAW_CHECK_MSG(expr, msg) do { \
171 if (!(expr)) { \
172 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000173 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174 } \
175} while (0)
176
177#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
178
179#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
180
181#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
182
183const size_t kWordSize = __WORDSIZE / 8;
184const size_t kWordSizeInBits = 8 * kWordSize;
185const size_t kPageSizeBits = 12;
186const size_t kPageSize = 1UL << kPageSizeBits;
187
188#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
189#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
190
191#define GET_BP_PC_SP \
192 uintptr_t bp = GET_CURRENT_FRAME(); \
193 uintptr_t pc = GET_CALLER_PC(); \
194 uintptr_t local_stack; \
195 uintptr_t sp = (uintptr_t)&local_stack;
196
197// These magic values are written to shadow for better error reporting.
198const int kAsanHeapLeftRedzoneMagic = 0xfa;
199const int kAsanHeapRightRedzoneMagic = 0xfb;
200const int kAsanHeapFreeMagic = 0xfd;
201const int kAsanStackLeftRedzoneMagic = 0xf1;
202const int kAsanStackMidRedzoneMagic = 0xf2;
203const int kAsanStackRightRedzoneMagic = 0xf3;
204const int kAsanStackPartialRedzoneMagic = 0xf4;
205const int kAsanStackAfterReturnMagic = 0xf5;
206const int kAsanUserPoisonedMemoryMagic = 0xf7;
207const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000208const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000209
210static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
211static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
212
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000213// --------------------------- Bit twiddling ------- {{{1
214inline bool IsPowerOfTwo(size_t x) {
215 return (x & (x - 1)) == 0;
216}
217
218inline size_t RoundUpTo(size_t size, size_t boundary) {
219 CHECK(IsPowerOfTwo(boundary));
220 return (size + boundary - 1) & ~(boundary - 1);
221}
222
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000223// -------------------------- LowLevelAllocator ----- {{{1
224// A simple low-level memory allocator for internal use.
225class LowLevelAllocator {
226 public:
227 explicit LowLevelAllocator(LinkerInitialized) {}
228 // 'size' must be a power of two.
229 // Requires an external lock.
230 void *Allocate(size_t size);
231 private:
232 char *allocated_end_;
233 char *allocated_current_;
234};
235
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000236} // namespace __asan
237
238#endif // ASAN_INTERNAL_H