blob: a4578a6f2d27b936b9e65cf4793ca03878e812f4 [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
Alexey Samsonov94b50362012-06-05 14:25:27 +000017#include "sanitizer_common/sanitizer_internal_defs.h"
Kostya Serebryany16e00752012-05-31 13:42:53 +000018#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000019
Alexander Potapenko6f045292012-01-27 15:15:04 +000020#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000021# error "This operating system is not supported by AddressSanitizer"
22#endif
23
Kostya Serebryany85822082012-01-30 20:55:02 +000024#if defined(_WIN32)
Alexander Potapenko71d3b392012-02-08 14:14:18 +000025extern "C" void* _ReturnAddress(void);
26# pragma intrinsic(_ReturnAddress)
Alexey Samsonovadf2b032012-02-03 08:37:19 +000027#endif // defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000028
Alexey Samsonove4092f62012-02-22 12:54:04 +000029// Limits for integral types. We have to redefine it in case we don't
30// have stdint.h (like in Visual Studio 9).
31#if __WORDSIZE == 64
32# define __INT64_C(c) c ## L
33# define __UINT64_C(c) c ## UL
34#else
35# define __INT64_C(c) c ## LL
36# define __UINT64_C(c) c ## ULL
37#endif // __WORDSIZE == 64
Alexey Samsonovbfc694d2012-02-22 16:12:46 +000038#undef INT32_MIN
39#define INT32_MIN (-2147483647-1)
40#undef INT32_MAX
41#define INT32_MAX (2147483647)
42#undef UINT32_MAX
43#define UINT32_MAX (4294967295U)
44#undef INT64_MIN
45#define INT64_MIN (-__INT64_C(9223372036854775807)-1)
46#undef INT64_MAX
47#define INT64_MAX (__INT64_C(9223372036854775807))
48#undef UINT64_MAX
49#define UINT64_MAX (__UINT64_C(18446744073709551615))
Alexey Samsonove4092f62012-02-22 12:54:04 +000050
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000051#define ASAN_DEFAULT_FAILURE_EXITCODE 1
52
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000053#if defined(__linux__)
54# define ASAN_LINUX 1
55#else
56# define ASAN_LINUX 0
57#endif
58
59#if defined(__APPLE__)
60# define ASAN_MAC 1
61#else
62# define ASAN_MAC 0
63#endif
64
65#if defined(_WIN32)
66# define ASAN_WINDOWS 1
67#else
68# define ASAN_WINDOWS 0
69#endif
70
Alexey Samsonov34a32022012-03-26 09:07:29 +000071#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
72
Kostya Serebryany850a49e2012-04-06 20:36:18 +000073#if __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074# error "The AddressSanitizer run-time should not be"
75 " instrumented by AddressSanitizer"
76#endif
77
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000078// Build-time configuration options.
79
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000080// If set, asan will install its own SEGV signal handler.
81#ifndef ASAN_NEEDS_SEGV
82# define ASAN_NEEDS_SEGV 1
83#endif
84
85// If set, asan will intercept C++ exception api call(s).
86#ifndef ASAN_HAS_EXCEPTIONS
87# define ASAN_HAS_EXCEPTIONS 1
88#endif
89
90// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
91// provided by the instrumented objects. Otherwise constants are used.
92#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
93# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
94#endif
95
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000096// If set, values like allocator chunk size, as well as defaults for some flags
97// will be changed towards less memory overhead.
98#ifndef ASAN_LOW_MEMORY
99# define ASAN_LOW_MEMORY 0
100#endif
101
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000102// All internal functions in asan reside inside the __asan namespace
103// to avoid namespace collisions with the user programs.
104// Seperate namespace also makes it simpler to distinguish the asan run-time
105// functions from the instrumented user code in a profile.
106namespace __asan {
107
108class AsanThread;
109struct AsanStackTrace;
110
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000111// asan_rtl.cc
Timur Iskhodzhanov23bd2bb2012-03-13 16:12:03 +0000112void NORETURN CheckFailed(const char *cond, const char *file, int line);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000113void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000114
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000115// asan_globals.cc
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000116bool DescribeAddrIfGlobal(uptr addr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000117
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000118void ReplaceOperatorsNewAndDelete();
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000119// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000120void ReplaceSystemMalloc();
121
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000122void OutOfMemoryMessageAndDie(const char *mem_type, uptr size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000123
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000124// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000125void *AsanDoesNotSupportStaticLinkage();
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000126bool AsanShadowRangeIsAvailable();
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000127const char *AsanGetEnv(const char *name);
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000128void AsanDumpProcessMap();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000129
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000130void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size);
131void *AsanMmapFixedReserve(uptr fixed_addr, uptr size);
132void *AsanMprotect(uptr fixed_addr, uptr size);
133void *AsanMmapSomewhereOrDie(uptr size, const char *where);
134void AsanUnmapOrDie(void *ptr, uptr size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000135
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000136void AsanDisableCoreDumper();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000137void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000138
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000139bool AsanInterceptsSignal(int signum);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000140void SetAlternateSignalStack();
141void UnsetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000142void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000143int GetPid();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000144uptr GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000145int AtomicInc(int *a);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000146u16 AtomicExchange(u16 *a, u16 new_val);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000147
148// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000149void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000150void *AsanTSDGet();
151void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000152
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000153// Opens the file 'file_name" and reads up to 'max_len' bytes.
154// The resulting buffer is mmaped and stored in '*buff'.
155// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000156// Returns the number of read bytes or 0 if file can not be opened.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000157uptr ReadFileToBuffer(const char *file_name, char **buff,
158 uptr *buff_size, uptr max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000159
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000160// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000161void RawWrite(const char *buffer);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000162int SNPrintf(char *buffer, uptr length, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000163void Printf(const char *format, ...);
164void Report(const char *format, ...);
165
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000166// Don't use std::min and std::max, to minimize dependency on libstdc++.
167template<class T> T Min(T a, T b) { return a < b ? a : b; }
168template<class T> T Max(T a, T b) { return a > b ? a : b; }
169
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000170void SortArray(uptr *array, uptr size);
Kostya Serebryany25c71782012-03-10 01:30:01 +0000171
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000172// asan_poisoning.cc
173// Poisons the shadow memory for "size" bytes starting from "addr".
Kostya Serebryanyee392552012-05-31 15:02:07 +0000174void PoisonShadow(uptr addr, uptr size, u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000175// Poisons the shadow memory for "redzone_size" bytes starting from
176// "addr + size".
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000177void PoisonShadowPartialRightRedzone(uptr addr,
178 uptr size,
179 uptr redzone_size,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000180 u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000181
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000182// Platfrom-specific options.
183#ifdef __APPLE__
184bool PlatformHasDifferentMemcpyAndMemmove();
185# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
186 (PlatformHasDifferentMemcpyAndMemmove())
187#else
188# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
189#endif // __APPLE__
190
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000191extern uptr FLAG_quarantine_size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000192extern s64 FLAG_demangle;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000193extern bool FLAG_symbolize;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000194extern s64 FLAG_v;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000195extern uptr FLAG_redzone;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000196extern s64 FLAG_debug;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000197extern bool FLAG_poison_shadow;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000198extern s64 FLAG_report_globals;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000199extern uptr FLAG_malloc_context_size;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000200extern bool FLAG_replace_str;
201extern bool FLAG_replace_intrin;
202extern bool FLAG_replace_cfallocator;
203extern bool FLAG_fast_unwind;
204extern bool FLAG_use_fake_stack;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000205extern uptr FLAG_max_malloc_fill_size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000206extern s64 FLAG_exitcode;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000207extern bool FLAG_allow_user_poisoning;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000208extern s64 FLAG_sleep_before_dying;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000209extern bool FLAG_handle_segv;
210extern bool FLAG_use_sigaltstack;
211extern bool FLAG_check_malloc_usable_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000212
213extern int asan_inited;
214// Used to avoid infinite recursion in __asan_init().
215extern bool asan_init_is_running;
216
217enum LinkerInitialized { LINKER_INITIALIZED = 0 };
218
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000219void NORETURN AsanDie();
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000220void SleepForSeconds(int seconds);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000221void NORETURN Exit(int exitcode);
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000222void NORETURN Abort();
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000223int Atexit(void (*function)(void));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000224
225#define CHECK(cond) do { if (!(cond)) { \
226 CheckFailed(#cond, __FILE__, __LINE__); \
227}}while(0)
228
229#define RAW_CHECK_MSG(expr, msg) do { \
230 if (!(expr)) { \
231 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000232 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000233 } \
234} while (0)
235
236#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
237
238#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
239
240#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
241
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000242const uptr kWordSize = __WORDSIZE / 8;
243const uptr kWordSizeInBits = 8 * kWordSize;
244const uptr kPageSizeBits = 12;
245const uptr kPageSize = 1UL << kPageSizeBits;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000246
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000247#if !defined(_WIN32) || defined(__clang__)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000248# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
249# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000250#else
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000251# define GET_CALLER_PC() (uptr)_ReturnAddress()
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000252// CaptureStackBackTrace doesn't need to know BP on Windows.
253// FIXME: This macro is still used when printing error reports though it's not
254// clear if the BP value is needed in the ASan reports on Windows.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000255# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000256#endif
257
258#ifndef _WIN32
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000259const uptr kMmapGranularity = kPageSize;
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000260# define THREAD_CALLING_CONV
261typedef void* thread_return_t;
262#else
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000263const uptr kMmapGranularity = 1UL << 16;
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000264# define THREAD_CALLING_CONV __stdcall
265typedef DWORD thread_return_t;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000266
267# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000268# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
269bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000270# endif
Alexander Potapenko6f045292012-01-27 15:15:04 +0000271#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000272
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000273typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
274
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000275// These magic values are written to shadow for better error reporting.
276const int kAsanHeapLeftRedzoneMagic = 0xfa;
277const int kAsanHeapRightRedzoneMagic = 0xfb;
278const int kAsanHeapFreeMagic = 0xfd;
279const int kAsanStackLeftRedzoneMagic = 0xf1;
280const int kAsanStackMidRedzoneMagic = 0xf2;
281const int kAsanStackRightRedzoneMagic = 0xf3;
282const int kAsanStackPartialRedzoneMagic = 0xf4;
283const int kAsanStackAfterReturnMagic = 0xf5;
284const int kAsanUserPoisonedMemoryMagic = 0xf7;
285const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000286const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000287
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000288static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
289static const uptr kRetiredStackFrameMagic = 0x45E0360E;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000290
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000291// --------------------------- Bit twiddling ------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000292inline bool IsPowerOfTwo(uptr x) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000293 return (x & (x - 1)) == 0;
294}
295
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000296inline uptr RoundUpTo(uptr size, uptr boundary) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000297 CHECK(IsPowerOfTwo(boundary));
298 return (size + boundary - 1) & ~(boundary - 1);
299}
300
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000301// -------------------------- LowLevelAllocator ----- {{{1
302// A simple low-level memory allocator for internal use.
303class LowLevelAllocator {
304 public:
305 explicit LowLevelAllocator(LinkerInitialized) {}
306 // 'size' must be a power of two.
307 // Requires an external lock.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000308 void *Allocate(uptr size);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000309 private:
310 char *allocated_end_;
311 char *allocated_current_;
312};
313
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000314} // namespace __asan
315
316#endif // ASAN_INTERNAL_H