blob: 0d1d208bfd5d5fe296df7e1afa8151de552784b7 [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 Samsonov47657ce2012-06-06 07:02:44 +000017#include "sanitizer_common/sanitizer_common.h"
Alexey Samsonov94b50362012-06-05 14:25:27 +000018#include "sanitizer_common/sanitizer_internal_defs.h"
Kostya Serebryany16e00752012-05-31 13:42:53 +000019#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000020
Alexander Potapenko6f045292012-01-27 15:15:04 +000021#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000022# error "This operating system is not supported by AddressSanitizer"
23#endif
24
Kostya Serebryany85822082012-01-30 20:55:02 +000025#if defined(_WIN32)
Alexander Potapenko71d3b392012-02-08 14:14:18 +000026extern "C" void* _ReturnAddress(void);
27# pragma intrinsic(_ReturnAddress)
Alexey Samsonovadf2b032012-02-03 08:37:19 +000028#endif // defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000029
Alexey Samsonove4092f62012-02-22 12:54:04 +000030// Limits for integral types. We have to redefine it in case we don't
31// have stdint.h (like in Visual Studio 9).
32#if __WORDSIZE == 64
33# define __INT64_C(c) c ## L
34# define __UINT64_C(c) c ## UL
35#else
36# define __INT64_C(c) c ## LL
37# define __UINT64_C(c) c ## ULL
38#endif // __WORDSIZE == 64
Alexey Samsonovbfc694d2012-02-22 16:12:46 +000039#undef INT32_MIN
40#define INT32_MIN (-2147483647-1)
41#undef INT32_MAX
42#define INT32_MAX (2147483647)
43#undef UINT32_MAX
44#define UINT32_MAX (4294967295U)
45#undef INT64_MIN
46#define INT64_MIN (-__INT64_C(9223372036854775807)-1)
47#undef INT64_MAX
48#define INT64_MAX (__INT64_C(9223372036854775807))
49#undef UINT64_MAX
50#define UINT64_MAX (__UINT64_C(18446744073709551615))
Alexey Samsonove4092f62012-02-22 12:54:04 +000051
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000052#define ASAN_DEFAULT_FAILURE_EXITCODE 1
53
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000054#if defined(__linux__)
55# define ASAN_LINUX 1
56#else
57# define ASAN_LINUX 0
58#endif
59
60#if defined(__APPLE__)
61# define ASAN_MAC 1
62#else
63# define ASAN_MAC 0
64#endif
65
66#if defined(_WIN32)
67# define ASAN_WINDOWS 1
68#else
69# define ASAN_WINDOWS 0
70#endif
71
Alexey Samsonov34a32022012-03-26 09:07:29 +000072#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
73
Kostya Serebryany850a49e2012-04-06 20:36:18 +000074#if __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000075# error "The AddressSanitizer run-time should not be"
76 " instrumented by AddressSanitizer"
77#endif
78
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000079// Build-time configuration options.
80
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000081// If set, asan will install its own SEGV signal handler.
82#ifndef ASAN_NEEDS_SEGV
83# define ASAN_NEEDS_SEGV 1
84#endif
85
86// If set, asan will intercept C++ exception api call(s).
87#ifndef ASAN_HAS_EXCEPTIONS
88# define ASAN_HAS_EXCEPTIONS 1
89#endif
90
91// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
92// provided by the instrumented objects. Otherwise constants are used.
93#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
94# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
95#endif
96
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000097// If set, values like allocator chunk size, as well as defaults for some flags
98// will be changed towards less memory overhead.
99#ifndef ASAN_LOW_MEMORY
100# define ASAN_LOW_MEMORY 0
101#endif
102
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103// All internal functions in asan reside inside the __asan namespace
104// to avoid namespace collisions with the user programs.
105// Seperate namespace also makes it simpler to distinguish the asan run-time
106// functions from the instrumented user code in a profile.
107namespace __asan {
108
109class AsanThread;
110struct AsanStackTrace;
111
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000112// asan_rtl.cc
Timur Iskhodzhanov23bd2bb2012-03-13 16:12:03 +0000113void NORETURN CheckFailed(const char *cond, const char *file, int line);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000114void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000116// asan_globals.cc
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000117bool DescribeAddrIfGlobal(uptr addr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000118
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000119void ReplaceOperatorsNewAndDelete();
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000120// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121void ReplaceSystemMalloc();
122
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000123void OutOfMemoryMessageAndDie(const char *mem_type, uptr size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000124
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000125// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126void *AsanDoesNotSupportStaticLinkage();
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000127bool AsanShadowRangeIsAvailable();
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000128const char *AsanGetEnv(const char *name);
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000129void AsanDumpProcessMap();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000130
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000131void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size);
132void *AsanMmapFixedReserve(uptr fixed_addr, uptr size);
133void *AsanMprotect(uptr fixed_addr, uptr size);
134void *AsanMmapSomewhereOrDie(uptr size, const char *where);
135void AsanUnmapOrDie(void *ptr, uptr size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000136
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000137void AsanDisableCoreDumper();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000138void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000139
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000140bool AsanInterceptsSignal(int signum);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000141void SetAlternateSignalStack();
142void UnsetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000143void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000144int GetPid();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000145uptr GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000146int AtomicInc(int *a);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000147u16 AtomicExchange(u16 *a, u16 new_val);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000148
149// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000150void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000151void *AsanTSDGet();
152void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000153
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000154// Opens the file 'file_name" and reads up to 'max_len' bytes.
155// The resulting buffer is mmaped and stored in '*buff'.
156// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000157// Returns the number of read bytes or 0 if file can not be opened.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000158uptr ReadFileToBuffer(const char *file_name, char **buff,
159 uptr *buff_size, uptr max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000160
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000161// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000162void RawWrite(const char *buffer);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000163int SNPrintf(char *buffer, uptr length, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164void Printf(const char *format, ...);
165void Report(const char *format, ...);
166
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000167// Don't use std::min and std::max, to minimize dependency on libstdc++.
168template<class T> T Min(T a, T b) { return a < b ? a : b; }
169template<class T> T Max(T a, T b) { return a > b ? a : b; }
170
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000171void SortArray(uptr *array, uptr size);
Kostya Serebryany25c71782012-03-10 01:30:01 +0000172
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000173// asan_poisoning.cc
174// Poisons the shadow memory for "size" bytes starting from "addr".
Kostya Serebryanyee392552012-05-31 15:02:07 +0000175void PoisonShadow(uptr addr, uptr size, u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000176// Poisons the shadow memory for "redzone_size" bytes starting from
177// "addr + size".
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000178void PoisonShadowPartialRightRedzone(uptr addr,
179 uptr size,
180 uptr redzone_size,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000181 u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000182
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000183// Platfrom-specific options.
184#ifdef __APPLE__
185bool PlatformHasDifferentMemcpyAndMemmove();
186# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
187 (PlatformHasDifferentMemcpyAndMemmove())
188#else
189# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
190#endif // __APPLE__
191
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000192extern uptr FLAG_quarantine_size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000193extern s64 FLAG_demangle;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000194extern bool FLAG_symbolize;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000195extern s64 FLAG_v;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000196extern uptr FLAG_redzone;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000197extern s64 FLAG_debug;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000198extern bool FLAG_poison_shadow;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000199extern s64 FLAG_report_globals;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000200extern uptr FLAG_malloc_context_size;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000201extern bool FLAG_replace_str;
202extern bool FLAG_replace_intrin;
203extern bool FLAG_replace_cfallocator;
204extern bool FLAG_fast_unwind;
205extern bool FLAG_use_fake_stack;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000206extern uptr FLAG_max_malloc_fill_size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000207extern s64 FLAG_exitcode;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000208extern bool FLAG_allow_user_poisoning;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000209extern s64 FLAG_sleep_before_dying;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000210extern bool FLAG_handle_segv;
211extern bool FLAG_use_sigaltstack;
212extern bool FLAG_check_malloc_usable_size;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000213extern bool FLAG_unmap_shadow_on_exit;
214extern bool FLAG_abort_on_error;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000215
216extern int asan_inited;
217// Used to avoid infinite recursion in __asan_init().
218extern bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000219extern void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000220
221enum LinkerInitialized { LINKER_INITIALIZED = 0 };
222
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000223void SleepForSeconds(int seconds);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000224void NORETURN Exit(int exitcode);
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000225void NORETURN Abort();
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000226int Atexit(void (*function)(void));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000227
228#define CHECK(cond) do { if (!(cond)) { \
229 CheckFailed(#cond, __FILE__, __LINE__); \
230}}while(0)
231
232#define RAW_CHECK_MSG(expr, msg) do { \
233 if (!(expr)) { \
234 RawWrite(msg); \
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000235 Die(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000236 } \
237} while (0)
238
239#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
240
241#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
242
243#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
244
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000245const uptr kWordSize = __WORDSIZE / 8;
246const uptr kWordSizeInBits = 8 * kWordSize;
247const uptr kPageSizeBits = 12;
248const uptr kPageSize = 1UL << kPageSizeBits;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000249
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000250#if !defined(_WIN32) || defined(__clang__)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000251# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
252# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000253#else
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000254# define GET_CALLER_PC() (uptr)_ReturnAddress()
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000255// CaptureStackBackTrace doesn't need to know BP on Windows.
256// FIXME: This macro is still used when printing error reports though it's not
257// clear if the BP value is needed in the ASan reports on Windows.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000258# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000259#endif
260
261#ifndef _WIN32
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000262const uptr kMmapGranularity = kPageSize;
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000263# define THREAD_CALLING_CONV
264typedef void* thread_return_t;
265#else
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000266const uptr kMmapGranularity = 1UL << 16;
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000267# define THREAD_CALLING_CONV __stdcall
268typedef DWORD thread_return_t;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000269
270# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000271# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
272bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000273# endif
Alexander Potapenko6f045292012-01-27 15:15:04 +0000274#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000275
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000276typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
277
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000278// These magic values are written to shadow for better error reporting.
279const int kAsanHeapLeftRedzoneMagic = 0xfa;
280const int kAsanHeapRightRedzoneMagic = 0xfb;
281const int kAsanHeapFreeMagic = 0xfd;
282const int kAsanStackLeftRedzoneMagic = 0xf1;
283const int kAsanStackMidRedzoneMagic = 0xf2;
284const int kAsanStackRightRedzoneMagic = 0xf3;
285const int kAsanStackPartialRedzoneMagic = 0xf4;
286const int kAsanStackAfterReturnMagic = 0xf5;
287const int kAsanUserPoisonedMemoryMagic = 0xf7;
288const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000289const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000290
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000291static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
292static const uptr kRetiredStackFrameMagic = 0x45E0360E;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000293
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000294// --------------------------- Bit twiddling ------- {{{1
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000295inline bool IsPowerOfTwo(uptr x) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000296 return (x & (x - 1)) == 0;
297}
298
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000299inline uptr RoundUpTo(uptr size, uptr boundary) {
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000300 CHECK(IsPowerOfTwo(boundary));
301 return (size + boundary - 1) & ~(boundary - 1);
302}
303
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000304// -------------------------- LowLevelAllocator ----- {{{1
305// A simple low-level memory allocator for internal use.
306class LowLevelAllocator {
307 public:
308 explicit LowLevelAllocator(LinkerInitialized) {}
309 // 'size' must be a power of two.
310 // Requires an external lock.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000311 void *Allocate(uptr size);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000312 private:
313 char *allocated_end_;
314 char *allocated_current_;
315};
316
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000317} // namespace __asan
318
319#endif // ASAN_INTERNAL_H