blob: facc2df7727258958ac374ac92ee8ab85ec2feca [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 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
Alexey Samsonove9541012012-06-06 13:11:29 +0000160void AppendToErrorMessageBuffer(const char *buffer);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000161// asan_printf.cc
Alexey Samsonove9541012012-06-06 13:11:29 +0000162void AsanPrintf(const char *format, ...) FORMAT(1, 2);
163void AsanReport(const char *format, ...) FORMAT(1, 2);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000165// Don't use std::min and std::max, to minimize dependency on libstdc++.
166template<class T> T Min(T a, T b) { return a < b ? a : b; }
167template<class T> T Max(T a, T b) { return a > b ? a : b; }
168
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000169void SortArray(uptr *array, uptr size);
Kostya Serebryany25c71782012-03-10 01:30:01 +0000170
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000171// asan_poisoning.cc
172// Poisons the shadow memory for "size" bytes starting from "addr".
Kostya Serebryanyee392552012-05-31 15:02:07 +0000173void PoisonShadow(uptr addr, uptr size, u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000174// Poisons the shadow memory for "redzone_size" bytes starting from
175// "addr + size".
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000176void PoisonShadowPartialRightRedzone(uptr addr,
177 uptr size,
178 uptr redzone_size,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000179 u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000180
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000181// Platfrom-specific options.
182#ifdef __APPLE__
183bool PlatformHasDifferentMemcpyAndMemmove();
184# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
185 (PlatformHasDifferentMemcpyAndMemmove())
186#else
187# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
188#endif // __APPLE__
189
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000190extern uptr FLAG_quarantine_size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000191extern s64 FLAG_demangle;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000192extern bool FLAG_symbolize;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000193extern s64 FLAG_v;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000194extern uptr FLAG_redzone;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000195extern s64 FLAG_debug;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000196extern bool FLAG_poison_shadow;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000197extern s64 FLAG_report_globals;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000198extern uptr FLAG_malloc_context_size;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000199extern bool FLAG_replace_str;
200extern bool FLAG_replace_intrin;
201extern bool FLAG_replace_cfallocator;
202extern bool FLAG_fast_unwind;
203extern bool FLAG_use_fake_stack;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000204extern uptr FLAG_max_malloc_fill_size;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000205extern s64 FLAG_exitcode;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000206extern bool FLAG_allow_user_poisoning;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000207extern s64 FLAG_sleep_before_dying;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000208extern bool FLAG_handle_segv;
209extern bool FLAG_use_sigaltstack;
210extern bool FLAG_check_malloc_usable_size;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000211extern bool FLAG_unmap_shadow_on_exit;
212extern bool FLAG_abort_on_error;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000213
214extern int asan_inited;
215// Used to avoid infinite recursion in __asan_init().
216extern bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000217extern void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000218
219enum LinkerInitialized { LINKER_INITIALIZED = 0 };
220
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000221void SleepForSeconds(int seconds);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000222void NORETURN Exit(int exitcode);
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000223void NORETURN Abort();
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000224int Atexit(void (*function)(void));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000225
226#define CHECK(cond) do { if (!(cond)) { \
227 CheckFailed(#cond, __FILE__, __LINE__); \
228}}while(0)
229
230#define RAW_CHECK_MSG(expr, msg) do { \
231 if (!(expr)) { \
232 RawWrite(msg); \
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000233 Die(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000234 } \
235} while (0)
236
237#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
238
239#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
240
241#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
242
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000243#if !defined(_WIN32) || defined(__clang__)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000244# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
245# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000246#else
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000247# define GET_CALLER_PC() (uptr)_ReturnAddress()
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000248// CaptureStackBackTrace doesn't need to know BP on Windows.
249// FIXME: This macro is still used when printing error reports though it's not
250// clear if the BP value is needed in the ASan reports on Windows.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000251# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000252#endif
253
254#ifndef _WIN32
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000255const uptr kMmapGranularity = kPageSize;
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000256# define THREAD_CALLING_CONV
257typedef void* thread_return_t;
258#else
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000259const uptr kMmapGranularity = 1UL << 16;
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000260# define THREAD_CALLING_CONV __stdcall
261typedef DWORD thread_return_t;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000262
263# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000264# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
265bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000266# endif
Alexander Potapenko6f045292012-01-27 15:15:04 +0000267#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000268
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000269typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
270
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000271// These magic values are written to shadow for better error reporting.
272const int kAsanHeapLeftRedzoneMagic = 0xfa;
273const int kAsanHeapRightRedzoneMagic = 0xfb;
274const int kAsanHeapFreeMagic = 0xfd;
275const int kAsanStackLeftRedzoneMagic = 0xf1;
276const int kAsanStackMidRedzoneMagic = 0xf2;
277const int kAsanStackRightRedzoneMagic = 0xf3;
278const int kAsanStackPartialRedzoneMagic = 0xf4;
279const int kAsanStackAfterReturnMagic = 0xf5;
280const int kAsanUserPoisonedMemoryMagic = 0xf7;
281const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000282const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000283
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000284static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
285static const uptr kRetiredStackFrameMagic = 0x45E0360E;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000286
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000287// -------------------------- LowLevelAllocator ----- {{{1
288// A simple low-level memory allocator for internal use.
289class LowLevelAllocator {
290 public:
291 explicit LowLevelAllocator(LinkerInitialized) {}
292 // 'size' must be a power of two.
293 // Requires an external lock.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000294 void *Allocate(uptr size);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000295 private:
296 char *allocated_end_;
297 char *allocated_current_;
298};
299
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000300} // namespace __asan
301
302#endif // ASAN_INTERNAL_H