blob: a67d034d61aa330d4e593511000bddfdf15b36d6 [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
Alexey Samsonove4092f62012-02-22 12:54:04 +000062// Limits for integral types. We have to redefine it in case we don't
63// have stdint.h (like in Visual Studio 9).
64#if __WORDSIZE == 64
65# define __INT64_C(c) c ## L
66# define __UINT64_C(c) c ## UL
67#else
68# define __INT64_C(c) c ## LL
69# define __UINT64_C(c) c ## ULL
70#endif // __WORDSIZE == 64
71# define INT32_MIN (-2147483647-1)
72# define INT32_MAX (2147483647)
73# define UINT32_MAX (4294967295U)
74# define INT64_MIN (-__INT64_C(9223372036854775807)-1)
75# define INT64_MAX (__INT64_C(9223372036854775807))
76# define UINT64_MAX (__UINT64_C(18446744073709551615))
77
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000078#if defined(__linux__)
79# define ASAN_LINUX 1
80#else
81# define ASAN_LINUX 0
82#endif
83
84#if defined(__APPLE__)
85# define ASAN_MAC 1
86#else
87# define ASAN_MAC 0
88#endif
89
90#if defined(_WIN32)
91# define ASAN_WINDOWS 1
92#else
93# define ASAN_WINDOWS 0
94#endif
95
Alexander Potapenkoc8365232012-01-27 10:52:37 +000096#if !defined(__has_feature)
97#define __has_feature(x) 0
98#endif
99
Kostya Serebryany13ebae62011-12-27 21:57:12 +0000100#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101# error "The AddressSanitizer run-time should not be"
102 " instrumented by AddressSanitizer"
103#endif
104
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000105// Build-time configuration options.
106
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000107// If set, asan will install its own SEGV signal handler.
108#ifndef ASAN_NEEDS_SEGV
109# define ASAN_NEEDS_SEGV 1
110#endif
111
112// If set, asan will intercept C++ exception api call(s).
113#ifndef ASAN_HAS_EXCEPTIONS
114# define ASAN_HAS_EXCEPTIONS 1
115#endif
116
117// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
118// provided by the instrumented objects. Otherwise constants are used.
119#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
120# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
121#endif
122
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000123// All internal functions in asan reside inside the __asan namespace
124// to avoid namespace collisions with the user programs.
125// Seperate namespace also makes it simpler to distinguish the asan run-time
126// functions from the instrumented user code in a profile.
127namespace __asan {
128
129class AsanThread;
130struct AsanStackTrace;
131
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000132// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133void CheckFailed(const char *cond, const char *file, int line);
134void ShowStatsAndAbort();
135
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000136// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000137bool DescribeAddrIfGlobal(uintptr_t addr);
138
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000139// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140void ReplaceSystemMalloc();
141
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000142void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
143
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000144// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000145void *AsanDoesNotSupportStaticLinkage();
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000146bool AsanShadowRangeIsAvailable();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000147int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000148const char *AsanGetEnv(const char *name);
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000149void AsanDumpProcessMap();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000150
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000151void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
152void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
153void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000154void *AsanMmapSomewhereOrDie(size_t size, const char *where);
155void AsanUnmapOrDie(void *ptr, size_t size);
156
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000157void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +0000158void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000159
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000160size_t AsanRead(int fd, void *buf, size_t count);
161size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000162int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000163
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000164bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000165void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000166int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000167uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000168int AtomicInc(int *a);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000169
170// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000171void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000172void *AsanTSDGet();
173void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000174
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000175// Opens the file 'file_name" and reads up to 'max_len' bytes.
176// The resulting buffer is mmaped and stored in '*buff'.
177// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000178// Returns the number of read bytes or 0 if file can not be opened.
179size_t ReadFileToBuffer(const char *file_name, char **buff,
180 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000181
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000182// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000183void RawWrite(const char *buffer);
Alexander Potapenkoa0935fa2012-02-08 11:45:09 +0000184int SNPrintf(char *buffer, size_t length, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000185void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000186int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000187void Report(const char *format, ...);
188
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000189// Don't use std::min and std::max, to minimize dependency on libstdc++.
190template<class T> T Min(T a, T b) { return a < b ? a : b; }
191template<class T> T Max(T a, T b) { return a > b ? a : b; }
192
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000193// asan_poisoning.cc
194// Poisons the shadow memory for "size" bytes starting from "addr".
195void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
196// Poisons the shadow memory for "redzone_size" bytes starting from
197// "addr + size".
198void PoisonShadowPartialRightRedzone(uintptr_t addr,
199 uintptr_t size,
200 uintptr_t redzone_size,
201 uint8_t value);
202
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000203extern size_t FLAG_quarantine_size;
204extern int FLAG_demangle;
205extern bool FLAG_symbolize;
206extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000207extern size_t FLAG_redzone;
208extern int FLAG_debug;
209extern bool FLAG_poison_shadow;
210extern int FLAG_report_globals;
211extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000212extern bool FLAG_replace_str;
213extern bool FLAG_replace_intrin;
214extern bool FLAG_replace_cfallocator;
215extern bool FLAG_fast_unwind;
216extern bool FLAG_use_fake_stack;
217extern size_t FLAG_max_malloc_fill_size;
218extern int FLAG_exitcode;
219extern bool FLAG_allow_user_poisoning;
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000220extern int FLAG_sleep_before_dying;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000221extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000222
223extern int asan_inited;
224// Used to avoid infinite recursion in __asan_init().
225extern bool asan_init_is_running;
226
227enum LinkerInitialized { LINKER_INITIALIZED = 0 };
228
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000229void AsanDie();
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000230void SleepForSeconds(int seconds);
231void Exit(int exitcode);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000232
233#define CHECK(cond) do { if (!(cond)) { \
234 CheckFailed(#cond, __FILE__, __LINE__); \
235}}while(0)
236
237#define RAW_CHECK_MSG(expr, msg) do { \
238 if (!(expr)) { \
239 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000240 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000241 } \
242} while (0)
243
244#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
245
246#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
247
248#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
249
250const size_t kWordSize = __WORDSIZE / 8;
251const size_t kWordSizeInBits = 8 * kWordSize;
252const size_t kPageSizeBits = 12;
253const size_t kPageSize = 1UL << kPageSizeBits;
254
Alexander Potapenko6f045292012-01-27 15:15:04 +0000255#ifndef _WIN32
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000256const size_t kMmapGranularity = kPageSize;
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000257# define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
258# define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000259#else
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000260const size_t kMmapGranularity = 1UL << 16;
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000261# define GET_CALLER_PC() (uintptr_t)_ReturnAddress()
262// CaptureStackBackTrace doesn't need to know BP on Windows.
263// FIXME: This macro is still used when printing error reports though it's not
264// clear if the BP value is needed in the ASan reports on Windows.
265# define GET_CURRENT_FRAME() (uintptr_t)0xDEADBEEF
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000266
267# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
268# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan::WinSymbolize
269bool WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
270# endif
Alexander Potapenko6f045292012-01-27 15:15:04 +0000271#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000272
273#define GET_BP_PC_SP \
274 uintptr_t bp = GET_CURRENT_FRAME(); \
275 uintptr_t pc = GET_CALLER_PC(); \
276 uintptr_t local_stack; \
277 uintptr_t sp = (uintptr_t)&local_stack;
278
279// These magic values are written to shadow for better error reporting.
280const int kAsanHeapLeftRedzoneMagic = 0xfa;
281const int kAsanHeapRightRedzoneMagic = 0xfb;
282const int kAsanHeapFreeMagic = 0xfd;
283const int kAsanStackLeftRedzoneMagic = 0xf1;
284const int kAsanStackMidRedzoneMagic = 0xf2;
285const int kAsanStackRightRedzoneMagic = 0xf3;
286const int kAsanStackPartialRedzoneMagic = 0xf4;
287const int kAsanStackAfterReturnMagic = 0xf5;
288const int kAsanUserPoisonedMemoryMagic = 0xf7;
289const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000290const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000291
292static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
293static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
294
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000295// --------------------------- Bit twiddling ------- {{{1
296inline bool IsPowerOfTwo(size_t x) {
297 return (x & (x - 1)) == 0;
298}
299
300inline size_t RoundUpTo(size_t size, size_t boundary) {
301 CHECK(IsPowerOfTwo(boundary));
302 return (size + boundary - 1) & ~(boundary - 1);
303}
304
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000305// -------------------------- LowLevelAllocator ----- {{{1
306// A simple low-level memory allocator for internal use.
307class LowLevelAllocator {
308 public:
309 explicit LowLevelAllocator(LinkerInitialized) {}
310 // 'size' must be a power of two.
311 // Requires an external lock.
312 void *Allocate(size_t size);
313 private:
314 char *allocated_end_;
315 char *allocated_current_;
316};
317
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000318} // namespace __asan
319
320#endif // ASAN_INTERNAL_H