blob: 4478cdfd564b0ee1f9e6fe76ab0c0026b8c98bd8 [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
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000021#include <stddef.h> // for size_t, uintptr_t, etc.
Alexander Potapenko6f045292012-01-27 15:15:04 +000022
Kostya Serebryany85822082012-01-30 20:55:02 +000023#if defined(_WIN32)
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +000024# if defined(__clang__)
25typedef int intptr_t;
26typedef unsigned int uintptr_t;
27# endif
28
Alexander Potapenko6f045292012-01-27 15:15:04 +000029// There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t.
30typedef unsigned __int8 uint8_t;
31typedef unsigned __int16 uint16_t;
32typedef unsigned __int32 uint32_t;
33typedef unsigned __int64 uint64_t;
34typedef __int8 int8_t;
35typedef __int16 int16_t;
36typedef __int32 int32_t;
37typedef __int64 int64_t;
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000038typedef unsigned long DWORD; // NOLINT
Alexey Samsonovadf2b032012-02-03 08:37:19 +000039
Alexander Potapenko71d3b392012-02-08 14:14:18 +000040extern "C" void* _ReturnAddress(void);
41# pragma intrinsic(_ReturnAddress)
42
Alexey Samsonovadf2b032012-02-03 08:37:19 +000043# define ALIAS(x) // TODO(timurrrr): do we need this on Windows?
44# define ALIGNED(x) __declspec(align(x))
45# define NOINLINE __declspec(noinline)
Timur Iskhodzhanov23bd2bb2012-03-13 16:12:03 +000046# define NORETURN __declspec(noreturn)
Alexey Samsonovadf2b032012-02-03 08:37:19 +000047
48# define ASAN_INTERFACE_ATTRIBUTE // TODO(timurrrr): do we need this on Win?
49#else // defined(_WIN32)
Kostya Serebryany85822082012-01-30 20:55:02 +000050# include <stdint.h> // for __WORDSIZE
Alexey Samsonovadf2b032012-02-03 08:37:19 +000051
52# define ALIAS(x) __attribute__((alias(x)))
53# define ALIGNED(x) __attribute__((aligned(x)))
54# define NOINLINE __attribute__((noinline))
Alexey Samsonov95d6b332012-03-20 10:14:55 +000055# define NORETURN __attribute__((noreturn))
Alexey Samsonovadf2b032012-02-03 08:37:19 +000056
57# define ASAN_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
58#endif // defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059
Daniel Dunbar46166332011-12-02 01:32:27 +000060// If __WORDSIZE was undefined by the platform, define it in terms of the
Kostya Serebryany669f5432012-01-31 18:13:50 +000061// compiler built-ins __LP64__ and _WIN64.
Daniel Dunbar46166332011-12-02 01:32:27 +000062#ifndef __WORDSIZE
Kostya Serebryany669f5432012-01-31 18:13:50 +000063#if __LP64__ || defined(_WIN64)
Daniel Dunbar46166332011-12-02 01:32:27 +000064#define __WORDSIZE 64
65#else
66#define __WORDSIZE 32
67#endif
68#endif
69
Alexey Samsonove4092f62012-02-22 12:54:04 +000070// Limits for integral types. We have to redefine it in case we don't
71// have stdint.h (like in Visual Studio 9).
72#if __WORDSIZE == 64
73# define __INT64_C(c) c ## L
74# define __UINT64_C(c) c ## UL
75#else
76# define __INT64_C(c) c ## LL
77# define __UINT64_C(c) c ## ULL
78#endif // __WORDSIZE == 64
Alexey Samsonovbfc694d2012-02-22 16:12:46 +000079#undef INT32_MIN
80#define INT32_MIN (-2147483647-1)
81#undef INT32_MAX
82#define INT32_MAX (2147483647)
83#undef UINT32_MAX
84#define UINT32_MAX (4294967295U)
85#undef INT64_MIN
86#define INT64_MIN (-__INT64_C(9223372036854775807)-1)
87#undef INT64_MAX
88#define INT64_MAX (__INT64_C(9223372036854775807))
89#undef UINT64_MAX
90#define UINT64_MAX (__UINT64_C(18446744073709551615))
Alexey Samsonove4092f62012-02-22 12:54:04 +000091
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000092#define ASAN_DEFAULT_FAILURE_EXITCODE 1
93
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000094#if defined(__linux__)
95# define ASAN_LINUX 1
96#else
97# define ASAN_LINUX 0
98#endif
99
100#if defined(__APPLE__)
101# define ASAN_MAC 1
102#else
103# define ASAN_MAC 0
104#endif
105
106#if defined(_WIN32)
107# define ASAN_WINDOWS 1
108#else
109# define ASAN_WINDOWS 0
110#endif
111
Alexey Samsonov34a32022012-03-26 09:07:29 +0000112#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
113
Alexander Potapenkoc8365232012-01-27 10:52:37 +0000114#if !defined(__has_feature)
115#define __has_feature(x) 0
116#endif
117
Kostya Serebryany850a49e2012-04-06 20:36:18 +0000118#if __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119# error "The AddressSanitizer run-time should not be"
120 " instrumented by AddressSanitizer"
121#endif
122
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000123// Build-time configuration options.
124
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000125// If set, asan will install its own SEGV signal handler.
126#ifndef ASAN_NEEDS_SEGV
127# define ASAN_NEEDS_SEGV 1
128#endif
129
130// If set, asan will intercept C++ exception api call(s).
131#ifndef ASAN_HAS_EXCEPTIONS
132# define ASAN_HAS_EXCEPTIONS 1
133#endif
134
135// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
136// provided by the instrumented objects. Otherwise constants are used.
137#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
138# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
139#endif
140
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +0000141// If set, values like allocator chunk size, as well as defaults for some flags
142// will be changed towards less memory overhead.
143#ifndef ASAN_LOW_MEMORY
144# define ASAN_LOW_MEMORY 0
145#endif
146
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147// All internal functions in asan reside inside the __asan namespace
148// to avoid namespace collisions with the user programs.
149// Seperate namespace also makes it simpler to distinguish the asan run-time
150// functions from the instrumented user code in a profile.
151namespace __asan {
152
153class AsanThread;
154struct AsanStackTrace;
155
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000156// asan_rtl.cc
Timur Iskhodzhanov23bd2bb2012-03-13 16:12:03 +0000157void NORETURN CheckFailed(const char *cond, const char *file, int line);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000158void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000159
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000160// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000161bool DescribeAddrIfGlobal(uintptr_t addr);
162
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000163void ReplaceOperatorsNewAndDelete();
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000164// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165void ReplaceSystemMalloc();
166
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000167void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
168
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000169// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170void *AsanDoesNotSupportStaticLinkage();
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000171bool AsanShadowRangeIsAvailable();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000172int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000173const char *AsanGetEnv(const char *name);
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000174void AsanDumpProcessMap();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000175
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000176void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
177void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
178void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000179void *AsanMmapSomewhereOrDie(size_t size, const char *where);
180void AsanUnmapOrDie(void *ptr, size_t size);
181
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000182void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +0000183void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000184
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000185size_t AsanRead(int fd, void *buf, size_t count);
186size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000187int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000188
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000189bool AsanInterceptsSignal(int signum);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000190void SetAlternateSignalStack();
191void UnsetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000192void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000193int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000194uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000195int AtomicInc(int *a);
Kostya Serebryanyf1e82b82012-04-05 15:55:09 +0000196uint16_t AtomicExchange(uint16_t *a, uint16_t new_val);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000197
198// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000199void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000200void *AsanTSDGet();
201void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000202
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000203// Opens the file 'file_name" and reads up to 'max_len' bytes.
204// The resulting buffer is mmaped and stored in '*buff'.
205// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000206// Returns the number of read bytes or 0 if file can not be opened.
207size_t ReadFileToBuffer(const char *file_name, char **buff,
208 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000209
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000210// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000211void RawWrite(const char *buffer);
Alexander Potapenkoa0935fa2012-02-08 11:45:09 +0000212int SNPrintf(char *buffer, size_t length, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000213void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000214int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000215void Report(const char *format, ...);
216
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000217// Don't use std::min and std::max, to minimize dependency on libstdc++.
218template<class T> T Min(T a, T b) { return a < b ? a : b; }
219template<class T> T Max(T a, T b) { return a > b ? a : b; }
220
Kostya Serebryany25c71782012-03-10 01:30:01 +0000221void SortArray(uintptr_t *array, size_t size);
222
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000223// asan_poisoning.cc
224// Poisons the shadow memory for "size" bytes starting from "addr".
225void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
226// Poisons the shadow memory for "redzone_size" bytes starting from
227// "addr + size".
228void PoisonShadowPartialRightRedzone(uintptr_t addr,
229 uintptr_t size,
230 uintptr_t redzone_size,
231 uint8_t value);
232
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000233// Platfrom-specific options.
234#ifdef __APPLE__
235bool PlatformHasDifferentMemcpyAndMemmove();
236# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
237 (PlatformHasDifferentMemcpyAndMemmove())
238#else
239# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
240#endif // __APPLE__
241
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000242extern size_t FLAG_quarantine_size;
243extern int FLAG_demangle;
244extern bool FLAG_symbolize;
245extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000246extern size_t FLAG_redzone;
247extern int FLAG_debug;
248extern bool FLAG_poison_shadow;
249extern int FLAG_report_globals;
250extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000251extern bool FLAG_replace_str;
252extern bool FLAG_replace_intrin;
253extern bool FLAG_replace_cfallocator;
254extern bool FLAG_fast_unwind;
255extern bool FLAG_use_fake_stack;
256extern size_t FLAG_max_malloc_fill_size;
257extern int FLAG_exitcode;
258extern bool FLAG_allow_user_poisoning;
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000259extern int FLAG_sleep_before_dying;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000260extern bool FLAG_handle_segv;
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000261extern bool FLAG_use_sigaltstack;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000262
263extern int asan_inited;
264// Used to avoid infinite recursion in __asan_init().
265extern bool asan_init_is_running;
266
267enum LinkerInitialized { LINKER_INITIALIZED = 0 };
268
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000269void NORETURN AsanDie();
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000270void SleepForSeconds(int seconds);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000271void NORETURN Exit(int exitcode);
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000272void NORETURN Abort();
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000273int Atexit(void (*function)(void));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000274
275#define CHECK(cond) do { if (!(cond)) { \
276 CheckFailed(#cond, __FILE__, __LINE__); \
277}}while(0)
278
279#define RAW_CHECK_MSG(expr, msg) do { \
280 if (!(expr)) { \
281 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000282 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000283 } \
284} while (0)
285
286#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
287
288#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
289
290#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
291
292const size_t kWordSize = __WORDSIZE / 8;
293const size_t kWordSizeInBits = 8 * kWordSize;
294const size_t kPageSizeBits = 12;
295const size_t kPageSize = 1UL << kPageSizeBits;
296
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000297#if !defined(_WIN32) || defined(__clang__)
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000298# define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
299# define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000300#else
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000301# define GET_CALLER_PC() (uintptr_t)_ReturnAddress()
302// CaptureStackBackTrace doesn't need to know BP on Windows.
303// FIXME: This macro is still used when printing error reports though it's not
304// clear if the BP value is needed in the ASan reports on Windows.
305# define GET_CURRENT_FRAME() (uintptr_t)0xDEADBEEF
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000306#endif
307
308#ifndef _WIN32
309const size_t kMmapGranularity = kPageSize;
310# define THREAD_CALLING_CONV
311typedef void* thread_return_t;
312#else
313const size_t kMmapGranularity = 1UL << 16;
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000314# define THREAD_CALLING_CONV __stdcall
315typedef DWORD thread_return_t;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000316
317# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000318# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
319bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000320# endif
Alexander Potapenko6f045292012-01-27 15:15:04 +0000321#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000322
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000323typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
324
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000325// These magic values are written to shadow for better error reporting.
326const int kAsanHeapLeftRedzoneMagic = 0xfa;
327const int kAsanHeapRightRedzoneMagic = 0xfb;
328const int kAsanHeapFreeMagic = 0xfd;
329const int kAsanStackLeftRedzoneMagic = 0xf1;
330const int kAsanStackMidRedzoneMagic = 0xf2;
331const int kAsanStackRightRedzoneMagic = 0xf3;
332const int kAsanStackPartialRedzoneMagic = 0xf4;
333const int kAsanStackAfterReturnMagic = 0xf5;
334const int kAsanUserPoisonedMemoryMagic = 0xf7;
335const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000336const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000337
338static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
339static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
340
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000341// --------------------------- Bit twiddling ------- {{{1
342inline bool IsPowerOfTwo(size_t x) {
343 return (x & (x - 1)) == 0;
344}
345
346inline size_t RoundUpTo(size_t size, size_t boundary) {
347 CHECK(IsPowerOfTwo(boundary));
348 return (size + boundary - 1) & ~(boundary - 1);
349}
350
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000351// -------------------------- LowLevelAllocator ----- {{{1
352// A simple low-level memory allocator for internal use.
353class LowLevelAllocator {
354 public:
355 explicit LowLevelAllocator(LinkerInitialized) {}
356 // 'size' must be a power of two.
357 // Requires an external lock.
358 void *Allocate(size_t size);
359 private:
360 char *allocated_end_;
361 char *allocated_current_;
362};
363
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000364} // namespace __asan
365
366#endif // ASAN_INTERNAL_H