blob: fb59402e5b7e38bc9b4940d8619e3296760b1f05 [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
Kostya Serebryany13ebae62011-12-27 21:57:12 +000034#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000035# error "The AddressSanitizer run-time should not be"
36 " instrumented by AddressSanitizer"
37#endif
38
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000039// Build-time configuration options.
40
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000041// If set, asan will install its own SEGV signal handler.
42#ifndef ASAN_NEEDS_SEGV
43# define ASAN_NEEDS_SEGV 1
44#endif
45
46// If set, asan will intercept C++ exception api call(s).
47#ifndef ASAN_HAS_EXCEPTIONS
48# define ASAN_HAS_EXCEPTIONS 1
49#endif
50
51// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
52// provided by the instrumented objects. Otherwise constants are used.
53#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
54# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
55#endif
56
Kostya Serebryany1e172b42011-11-30 01:07:02 +000057// All internal functions in asan reside inside the __asan namespace
58// to avoid namespace collisions with the user programs.
59// Seperate namespace also makes it simpler to distinguish the asan run-time
60// functions from the instrumented user code in a profile.
61namespace __asan {
62
63class AsanThread;
64struct AsanStackTrace;
65
Kostya Serebryany218a9b72011-11-30 18:50:23 +000066// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000067void CheckFailed(const char *cond, const char *file, int line);
68void ShowStatsAndAbort();
69
Kostya Serebryany218a9b72011-11-30 18:50:23 +000070// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000071bool DescribeAddrIfGlobal(uintptr_t addr);
72
Kostya Serebryany218a9b72011-11-30 18:50:23 +000073// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074void ReplaceSystemMalloc();
75
Kostya Serebryanyde496f42011-12-28 22:58:01 +000076void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
77
Kostya Serebryany218a9b72011-11-30 18:50:23 +000078// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000079void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +000080int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +000081const char *AsanGetEnv(const char *name);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000082
Kostya Serebryanya874fe52011-12-28 23:28:54 +000083void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
84void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
85void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000086void *AsanMmapSomewhereOrDie(size_t size, const char *where);
87void AsanUnmapOrDie(void *ptr, size_t size);
88
Kostya Serebryanyef14ff62012-01-06 02:12:25 +000089void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +000090void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +000091
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000092size_t AsanRead(int fd, void *buf, size_t count);
93size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000094int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095
Kostya Serebryany4803ab92012-01-09 18:53:15 +000096bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000097void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000098int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000099uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000100int AtomicInc(int *a);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000101
102// Wrapper for TLS/TSD.
103void AsanTSDInit();
104void *AsanTSDGet();
105void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000106
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000107// Opens the file 'file_name" and reads up to 'max_len' bytes.
108// The resulting buffer is mmaped and stored in '*buff'.
109// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000110// Returns the number of read bytes or 0 if file can not be opened.
111size_t ReadFileToBuffer(const char *file_name, char **buff,
112 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000113
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000114// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115void RawWrite(const char *buffer);
116int SNPrint(char *buffer, size_t length, const char *format, ...);
117void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000118int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119void Report(const char *format, ...);
120
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000121// Don't use std::min and std::max, to minimize dependency on libstdc++.
122template<class T> T Min(T a, T b) { return a < b ? a : b; }
123template<class T> T Max(T a, T b) { return a > b ? a : b; }
124
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000125// asan_poisoning.cc
126// Poisons the shadow memory for "size" bytes starting from "addr".
127void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
128// Poisons the shadow memory for "redzone_size" bytes starting from
129// "addr + size".
130void PoisonShadowPartialRightRedzone(uintptr_t addr,
131 uintptr_t size,
132 uintptr_t redzone_size,
133 uint8_t value);
134
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135extern size_t FLAG_quarantine_size;
136extern int FLAG_demangle;
137extern bool FLAG_symbolize;
138extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139extern size_t FLAG_redzone;
140extern int FLAG_debug;
141extern bool FLAG_poison_shadow;
142extern int FLAG_report_globals;
143extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144extern bool FLAG_replace_str;
145extern bool FLAG_replace_intrin;
146extern bool FLAG_replace_cfallocator;
147extern bool FLAG_fast_unwind;
148extern bool FLAG_use_fake_stack;
149extern size_t FLAG_max_malloc_fill_size;
150extern int FLAG_exitcode;
151extern bool FLAG_allow_user_poisoning;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000152extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000153
154extern int asan_inited;
155// Used to avoid infinite recursion in __asan_init().
156extern bool asan_init_is_running;
157
158enum LinkerInitialized { LINKER_INITIALIZED = 0 };
159
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000160void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000161
162#define CHECK(cond) do { if (!(cond)) { \
163 CheckFailed(#cond, __FILE__, __LINE__); \
164}}while(0)
165
166#define RAW_CHECK_MSG(expr, msg) do { \
167 if (!(expr)) { \
168 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000169 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170 } \
171} while (0)
172
173#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
174
175#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
176
177#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
178
179const size_t kWordSize = __WORDSIZE / 8;
180const size_t kWordSizeInBits = 8 * kWordSize;
181const size_t kPageSizeBits = 12;
182const size_t kPageSize = 1UL << kPageSizeBits;
183
184#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
185#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
186
187#define GET_BP_PC_SP \
188 uintptr_t bp = GET_CURRENT_FRAME(); \
189 uintptr_t pc = GET_CALLER_PC(); \
190 uintptr_t local_stack; \
191 uintptr_t sp = (uintptr_t)&local_stack;
192
193// These magic values are written to shadow for better error reporting.
194const int kAsanHeapLeftRedzoneMagic = 0xfa;
195const int kAsanHeapRightRedzoneMagic = 0xfb;
196const int kAsanHeapFreeMagic = 0xfd;
197const int kAsanStackLeftRedzoneMagic = 0xf1;
198const int kAsanStackMidRedzoneMagic = 0xf2;
199const int kAsanStackRightRedzoneMagic = 0xf3;
200const int kAsanStackPartialRedzoneMagic = 0xf4;
201const int kAsanStackAfterReturnMagic = 0xf5;
202const int kAsanUserPoisonedMemoryMagic = 0xf7;
203const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000204const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000205
206static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
207static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
208
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000209// --------------------------- Bit twiddling ------- {{{1
210inline bool IsPowerOfTwo(size_t x) {
211 return (x & (x - 1)) == 0;
212}
213
214inline size_t RoundUpTo(size_t size, size_t boundary) {
215 CHECK(IsPowerOfTwo(boundary));
216 return (size + boundary - 1) & ~(boundary - 1);
217}
218
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000219// -------------------------- LowLevelAllocator ----- {{{1
220// A simple low-level memory allocator for internal use.
221class LowLevelAllocator {
222 public:
223 explicit LowLevelAllocator(LinkerInitialized) {}
224 // 'size' must be a power of two.
225 // Requires an external lock.
226 void *Allocate(size_t size);
227 private:
228 char *allocated_end_;
229 char *allocated_current_;
230};
231
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000232} // namespace __asan
233
234#endif // ASAN_INTERNAL_H