blob: 831b76128aaea7ac265a1ced60e69a4553c647f6 [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 Samsonovb823e3c2012-02-22 14:07:06 +000030#define ASAN_DEFAULT_FAILURE_EXITCODE 1
31
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000032#if defined(__linux__)
33# define ASAN_LINUX 1
34#else
35# define ASAN_LINUX 0
36#endif
37
38#if defined(__APPLE__)
39# define ASAN_MAC 1
40#else
41# define ASAN_MAC 0
42#endif
43
44#if defined(_WIN32)
45# define ASAN_WINDOWS 1
46#else
47# define ASAN_WINDOWS 0
48#endif
49
Alexey Samsonov34a32022012-03-26 09:07:29 +000050#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
51
Kostya Serebryany850a49e2012-04-06 20:36:18 +000052#if __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000053# error "The AddressSanitizer run-time should not be"
54 " instrumented by AddressSanitizer"
55#endif
56
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000057// Build-time configuration options.
58
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000059// If set, asan will install its own SEGV signal handler.
60#ifndef ASAN_NEEDS_SEGV
61# define ASAN_NEEDS_SEGV 1
62#endif
63
64// If set, asan will intercept C++ exception api call(s).
65#ifndef ASAN_HAS_EXCEPTIONS
66# define ASAN_HAS_EXCEPTIONS 1
67#endif
68
69// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
70// provided by the instrumented objects. Otherwise constants are used.
71#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
72# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
73#endif
74
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000075// If set, values like allocator chunk size, as well as defaults for some flags
76// will be changed towards less memory overhead.
77#ifndef ASAN_LOW_MEMORY
78# define ASAN_LOW_MEMORY 0
79#endif
80
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081// All internal functions in asan reside inside the __asan namespace
82// to avoid namespace collisions with the user programs.
83// Seperate namespace also makes it simpler to distinguish the asan run-time
84// functions from the instrumented user code in a profile.
85namespace __asan {
86
87class AsanThread;
88struct AsanStackTrace;
89
Kostya Serebryany218a9b72011-11-30 18:50:23 +000090// asan_rtl.cc
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +000091void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000092
Kostya Serebryany218a9b72011-11-30 18:50:23 +000093// asan_globals.cc
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000094bool DescribeAddrIfGlobal(uptr addr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000095
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000096void ReplaceOperatorsNewAndDelete();
Kostya Serebryany218a9b72011-11-30 18:50:23 +000097// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000098void ReplaceSystemMalloc();
99
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000100// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000102
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000103void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000104
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000105bool AsanInterceptsSignal(int signum);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000106void SetAlternateSignalStack();
107void UnsetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000108void InstallSignalHandlers();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000109
110// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000111void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000112void *AsanTSDGet();
113void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000114
Alexey Samsonove9541012012-06-06 13:11:29 +0000115void AppendToErrorMessageBuffer(const char *buffer);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000116// asan_printf.cc
Alexey Samsonov7fdcdf52012-06-06 13:58:39 +0000117void AsanPrintf(const char *format, ...);
118void AsanReport(const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000120// asan_poisoning.cc
121// Poisons the shadow memory for "size" bytes starting from "addr".
Kostya Serebryanyee392552012-05-31 15:02:07 +0000122void PoisonShadow(uptr addr, uptr size, u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000123// Poisons the shadow memory for "redzone_size" bytes starting from
124// "addr + size".
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000125void PoisonShadowPartialRightRedzone(uptr addr,
126 uptr size,
127 uptr redzone_size,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000128 u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000129
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000130// Platfrom-specific options.
131#ifdef __APPLE__
132bool PlatformHasDifferentMemcpyAndMemmove();
133# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
134 (PlatformHasDifferentMemcpyAndMemmove())
135#else
136# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
137#endif // __APPLE__
138
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000139struct Flags {
140 int quarantine_size;
141 bool symbolize;
142 int verbosity;
143 int redzone;
144 int debug;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000145 int report_globals;
146 int malloc_context_size;
147 bool replace_str;
148 bool replace_intrin;
149 bool replace_cfallocator;
150 bool mac_ignore_invalid_free;
151 bool use_fake_stack;
152 int max_malloc_fill_size;
153 int exitcode;
154 bool allow_user_poisoning;
155 int sleep_before_dying;
156 bool handle_segv;
157 bool use_sigaltstack;
158 bool check_malloc_usable_size;
159 bool unmap_shadow_on_exit;
160 bool abort_on_error;
161 bool atexit;
162 bool disable_core;
163};
164Flags *flags();
165void InitializeFlags(Flags *f, const char *env);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166
167extern int asan_inited;
168// Used to avoid infinite recursion in __asan_init().
169extern bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000170extern void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171
172enum LinkerInitialized { LINKER_INITIALIZED = 0 };
173
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
175
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000176#if !defined(_WIN32) || defined(__clang__)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000177# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
178# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000179#else
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000180# define GET_CALLER_PC() (uptr)_ReturnAddress()
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000181// CaptureStackBackTrace doesn't need to know BP on Windows.
182// FIXME: This macro is still used when printing error reports though it's not
183// clear if the BP value is needed in the ASan reports on Windows.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000184# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000185#endif
186
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000187#ifdef _WIN32
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000188# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000189# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
190bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000191# endif
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000192#endif // _WIN32
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000193
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000194// These magic values are written to shadow for better error reporting.
195const int kAsanHeapLeftRedzoneMagic = 0xfa;
196const int kAsanHeapRightRedzoneMagic = 0xfb;
197const int kAsanHeapFreeMagic = 0xfd;
198const int kAsanStackLeftRedzoneMagic = 0xf1;
199const int kAsanStackMidRedzoneMagic = 0xf2;
200const int kAsanStackRightRedzoneMagic = 0xf3;
201const int kAsanStackPartialRedzoneMagic = 0xf4;
202const int kAsanStackAfterReturnMagic = 0xf5;
203const int kAsanUserPoisonedMemoryMagic = 0xf7;
204const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000205const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000206
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000207static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
208static const uptr kRetiredStackFrameMagic = 0x45E0360E;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000209
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000210// -------------------------- LowLevelAllocator ----- {{{1
211// A simple low-level memory allocator for internal use.
212class LowLevelAllocator {
213 public:
214 explicit LowLevelAllocator(LinkerInitialized) {}
215 // 'size' must be a power of two.
216 // Requires an external lock.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000217 void *Allocate(uptr size);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000218 private:
219 char *allocated_end_;
220 char *allocated_current_;
221};
222
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000223} // namespace __asan
224
225#endif // ASAN_INTERNAL_H