blob: 54556a29ad887d1b955d307076d1e2957e8c6df6 [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
Alexander Potapenko6f045292012-01-27 15:15:04 +000017#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000018# error "This operating system is not supported by AddressSanitizer"
19#endif
20
Alexander Potapenko6f045292012-01-27 15:15:04 +000021#include <stdlib.h> // for size_t, uintptr_t, etc.
22
Kostya Serebryany85822082012-01-30 20:55:02 +000023#if defined(_WIN32)
Alexander Potapenko6f045292012-01-27 15:15:04 +000024// There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t.
25typedef unsigned __int8 uint8_t;
26typedef unsigned __int16 uint16_t;
27typedef unsigned __int32 uint32_t;
28typedef unsigned __int64 uint64_t;
29typedef __int8 int8_t;
30typedef __int16 int16_t;
31typedef __int32 int32_t;
32typedef __int64 int64_t;
Alexey Samsonovadf2b032012-02-03 08:37:19 +000033
Alexander Potapenko71d3b392012-02-08 14:14:18 +000034extern "C" void* _ReturnAddress(void);
35# pragma intrinsic(_ReturnAddress)
36
Alexey Samsonovadf2b032012-02-03 08:37:19 +000037# define ALIAS(x) // TODO(timurrrr): do we need this on Windows?
38# define ALIGNED(x) __declspec(align(x))
39# define NOINLINE __declspec(noinline)
40
41# define ASAN_INTERFACE_ATTRIBUTE // TODO(timurrrr): do we need this on Win?
42#else // defined(_WIN32)
Kostya Serebryany85822082012-01-30 20:55:02 +000043# include <stdint.h> // for __WORDSIZE
Alexey Samsonovadf2b032012-02-03 08:37:19 +000044
45# define ALIAS(x) __attribute__((alias(x)))
46# define ALIGNED(x) __attribute__((aligned(x)))
47# define NOINLINE __attribute__((noinline))
48
49# define ASAN_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
50#endif // defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000051
Daniel Dunbar46166332011-12-02 01:32:27 +000052// If __WORDSIZE was undefined by the platform, define it in terms of the
Kostya Serebryany669f5432012-01-31 18:13:50 +000053// compiler built-ins __LP64__ and _WIN64.
Daniel Dunbar46166332011-12-02 01:32:27 +000054#ifndef __WORDSIZE
Kostya Serebryany669f5432012-01-31 18:13:50 +000055#if __LP64__ || defined(_WIN64)
Daniel Dunbar46166332011-12-02 01:32:27 +000056#define __WORDSIZE 64
57#else
58#define __WORDSIZE 32
59#endif
60#endif
61
Alexander Potapenkoc8365232012-01-27 10:52:37 +000062#if !defined(__has_feature)
63#define __has_feature(x) 0
64#endif
65
Kostya Serebryany13ebae62011-12-27 21:57:12 +000066#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000067# error "The AddressSanitizer run-time should not be"
68 " instrumented by AddressSanitizer"
69#endif
70
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000071// Build-time configuration options.
72
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000073// If set, asan will install its own SEGV signal handler.
74#ifndef ASAN_NEEDS_SEGV
75# define ASAN_NEEDS_SEGV 1
76#endif
77
78// If set, asan will intercept C++ exception api call(s).
79#ifndef ASAN_HAS_EXCEPTIONS
80# define ASAN_HAS_EXCEPTIONS 1
81#endif
82
83// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
84// provided by the instrumented objects. Otherwise constants are used.
85#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
86# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
87#endif
88
Kostya Serebryany1e172b42011-11-30 01:07:02 +000089// All internal functions in asan reside inside the __asan namespace
90// to avoid namespace collisions with the user programs.
91// Seperate namespace also makes it simpler to distinguish the asan run-time
92// functions from the instrumented user code in a profile.
93namespace __asan {
94
95class AsanThread;
96struct AsanStackTrace;
97
Kostya Serebryany218a9b72011-11-30 18:50:23 +000098// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000099void CheckFailed(const char *cond, const char *file, int line);
100void ShowStatsAndAbort();
101
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000102// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103bool DescribeAddrIfGlobal(uintptr_t addr);
104
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000105// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000106void ReplaceSystemMalloc();
107
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000108void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
109
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000110// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000111void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000112int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000113const char *AsanGetEnv(const char *name);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000114
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000115void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
116void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
117void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000118void *AsanMmapSomewhereOrDie(size_t size, const char *where);
119void AsanUnmapOrDie(void *ptr, size_t size);
120
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000121void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +0000122void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000123
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000124size_t AsanRead(int fd, void *buf, size_t count);
125size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000126int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000128bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000129void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000130int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000131uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000132int AtomicInc(int *a);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000133
134// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000135void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000136void *AsanTSDGet();
137void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000138
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000139// Opens the file 'file_name" and reads up to 'max_len' bytes.
140// The resulting buffer is mmaped and stored in '*buff'.
141// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000142// Returns the number of read bytes or 0 if file can not be opened.
143size_t ReadFileToBuffer(const char *file_name, char **buff,
144 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000145
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000146// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147void RawWrite(const char *buffer);
Alexander Potapenkoa0935fa2012-02-08 11:45:09 +0000148int SNPrintf(char *buffer, size_t length, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000150int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000151void Report(const char *format, ...);
152
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000153// Don't use std::min and std::max, to minimize dependency on libstdc++.
154template<class T> T Min(T a, T b) { return a < b ? a : b; }
155template<class T> T Max(T a, T b) { return a > b ? a : b; }
156
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000157// asan_poisoning.cc
158// Poisons the shadow memory for "size" bytes starting from "addr".
159void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
160// Poisons the shadow memory for "redzone_size" bytes starting from
161// "addr + size".
162void PoisonShadowPartialRightRedzone(uintptr_t addr,
163 uintptr_t size,
164 uintptr_t redzone_size,
165 uint8_t value);
166
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000167extern size_t FLAG_quarantine_size;
168extern int FLAG_demangle;
169extern bool FLAG_symbolize;
170extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171extern size_t FLAG_redzone;
172extern int FLAG_debug;
173extern bool FLAG_poison_shadow;
174extern int FLAG_report_globals;
175extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000176extern bool FLAG_replace_str;
177extern bool FLAG_replace_intrin;
178extern bool FLAG_replace_cfallocator;
179extern bool FLAG_fast_unwind;
180extern bool FLAG_use_fake_stack;
181extern size_t FLAG_max_malloc_fill_size;
182extern int FLAG_exitcode;
183extern bool FLAG_allow_user_poisoning;
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000184extern int FLAG_sleep_before_dying;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000185extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000186
187extern int asan_inited;
188// Used to avoid infinite recursion in __asan_init().
189extern bool asan_init_is_running;
190
191enum LinkerInitialized { LINKER_INITIALIZED = 0 };
192
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000193void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000194
195#define CHECK(cond) do { if (!(cond)) { \
196 CheckFailed(#cond, __FILE__, __LINE__); \
197}}while(0)
198
199#define RAW_CHECK_MSG(expr, msg) do { \
200 if (!(expr)) { \
201 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000202 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000203 } \
204} while (0)
205
206#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
207
208#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
209
210#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
211
212const size_t kWordSize = __WORDSIZE / 8;
213const size_t kWordSizeInBits = 8 * kWordSize;
214const size_t kPageSizeBits = 12;
215const size_t kPageSize = 1UL << kPageSizeBits;
216
Alexander Potapenko6f045292012-01-27 15:15:04 +0000217#ifndef _WIN32
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000218# define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
219# define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000220#else
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000221# define GET_CALLER_PC() (uintptr_t)_ReturnAddress()
222// CaptureStackBackTrace doesn't need to know BP on Windows.
223// FIXME: This macro is still used when printing error reports though it's not
224// clear if the BP value is needed in the ASan reports on Windows.
225# define GET_CURRENT_FRAME() (uintptr_t)0xDEADBEEF
Alexander Potapenko6f045292012-01-27 15:15:04 +0000226#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000227
228#define GET_BP_PC_SP \
229 uintptr_t bp = GET_CURRENT_FRAME(); \
230 uintptr_t pc = GET_CALLER_PC(); \
231 uintptr_t local_stack; \
232 uintptr_t sp = (uintptr_t)&local_stack;
233
234// These magic values are written to shadow for better error reporting.
235const int kAsanHeapLeftRedzoneMagic = 0xfa;
236const int kAsanHeapRightRedzoneMagic = 0xfb;
237const int kAsanHeapFreeMagic = 0xfd;
238const int kAsanStackLeftRedzoneMagic = 0xf1;
239const int kAsanStackMidRedzoneMagic = 0xf2;
240const int kAsanStackRightRedzoneMagic = 0xf3;
241const int kAsanStackPartialRedzoneMagic = 0xf4;
242const int kAsanStackAfterReturnMagic = 0xf5;
243const int kAsanUserPoisonedMemoryMagic = 0xf7;
244const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000245const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000246
247static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
248static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
249
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000250// --------------------------- Bit twiddling ------- {{{1
251inline bool IsPowerOfTwo(size_t x) {
252 return (x & (x - 1)) == 0;
253}
254
255inline size_t RoundUpTo(size_t size, size_t boundary) {
256 CHECK(IsPowerOfTwo(boundary));
257 return (size + boundary - 1) & ~(boundary - 1);
258}
259
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000260// -------------------------- LowLevelAllocator ----- {{{1
261// A simple low-level memory allocator for internal use.
262class LowLevelAllocator {
263 public:
264 explicit LowLevelAllocator(LinkerInitialized) {}
265 // 'size' must be a power of two.
266 // Requires an external lock.
267 void *Allocate(size_t size);
268 private:
269 char *allocated_end_;
270 char *allocated_current_;
271};
272
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000273} // namespace __asan
274
275#endif // ASAN_INTERNAL_H