blob: 4f6d9e37b560cbfd59d5b9eb13e6c6113becb0fb [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 Samsonov7ed1d2b2012-07-10 07:41:27 +000017#include "asan_flags.h"
Alexey Samsonov47657ce2012-06-06 07:02:44 +000018#include "sanitizer_common/sanitizer_common.h"
Alexey Samsonov94b50362012-06-05 14:25:27 +000019#include "sanitizer_common/sanitizer_internal_defs.h"
Kostya Serebryany16e00752012-05-31 13:42:53 +000020#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000021
Alexander Potapenko6f045292012-01-27 15:15:04 +000022#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000023# error "This operating system is not supported by AddressSanitizer"
24#endif
25
Kostya Serebryany85822082012-01-30 20:55:02 +000026#if defined(_WIN32)
Alexander Potapenko71d3b392012-02-08 14:14:18 +000027extern "C" void* _ReturnAddress(void);
28# pragma intrinsic(_ReturnAddress)
Alexey Samsonovadf2b032012-02-03 08:37:19 +000029#endif // defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000030
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000031#define ASAN_DEFAULT_FAILURE_EXITCODE 1
32
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000033#if defined(__linux__)
34# define ASAN_LINUX 1
35#else
36# define ASAN_LINUX 0
37#endif
38
39#if defined(__APPLE__)
40# define ASAN_MAC 1
41#else
42# define ASAN_MAC 0
43#endif
44
45#if defined(_WIN32)
46# define ASAN_WINDOWS 1
47#else
48# define ASAN_WINDOWS 0
49#endif
50
Alexey Samsonov34a32022012-03-26 09:07:29 +000051#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
52
Kostya Serebryany850a49e2012-04-06 20:36:18 +000053#if __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054# error "The AddressSanitizer run-time should not be"
55 " instrumented by AddressSanitizer"
56#endif
57
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000058// Build-time configuration options.
59
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000060// If set, asan will install its own SEGV signal handler.
61#ifndef ASAN_NEEDS_SEGV
62# define ASAN_NEEDS_SEGV 1
63#endif
64
65// If set, asan will intercept C++ exception api call(s).
66#ifndef ASAN_HAS_EXCEPTIONS
67# define ASAN_HAS_EXCEPTIONS 1
68#endif
69
70// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
71// provided by the instrumented objects. Otherwise constants are used.
72#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
73# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
74#endif
75
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000076// If set, values like allocator chunk size, as well as defaults for some flags
77// will be changed towards less memory overhead.
78#ifndef ASAN_LOW_MEMORY
79# define ASAN_LOW_MEMORY 0
80#endif
81
Kostya Serebryany1e172b42011-11-30 01:07:02 +000082// All internal functions in asan reside inside the __asan namespace
83// to avoid namespace collisions with the user programs.
84// Seperate namespace also makes it simpler to distinguish the asan run-time
85// functions from the instrumented user code in a profile.
86namespace __asan {
87
88class AsanThread;
89struct AsanStackTrace;
90
Kostya Serebryany218a9b72011-11-30 18:50:23 +000091// asan_rtl.cc
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +000092void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000093
Kostya Serebryany218a9b72011-11-30 18:50:23 +000094// asan_globals.cc
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000095bool DescribeAddrIfGlobal(uptr addr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000097void ReplaceOperatorsNewAndDelete();
Kostya Serebryany218a9b72011-11-30 18:50:23 +000098// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000099void ReplaceSystemMalloc();
100
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000101// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000102void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000103
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000104void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000105
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000106bool AsanInterceptsSignal(int signum);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000107void SetAlternateSignalStack();
108void UnsetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000109void InstallSignalHandlers();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000110
111// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000112void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000113void *AsanTSDGet();
114void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000115
Alexey Samsonove9541012012-06-06 13:11:29 +0000116void AppendToErrorMessageBuffer(const char *buffer);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000117// asan_printf.cc
Alexey Samsonov7fdcdf52012-06-06 13:58:39 +0000118void AsanPrintf(const char *format, ...);
119void AsanReport(const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000120
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000121// asan_poisoning.cc
122// Poisons the shadow memory for "size" bytes starting from "addr".
Kostya Serebryanyee392552012-05-31 15:02:07 +0000123void PoisonShadow(uptr addr, uptr size, u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000124// Poisons the shadow memory for "redzone_size" bytes starting from
125// "addr + size".
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000126void PoisonShadowPartialRightRedzone(uptr addr,
127 uptr size,
128 uptr redzone_size,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000129 u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000130
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000131// Platfrom-specific options.
132#ifdef __APPLE__
133bool PlatformHasDifferentMemcpyAndMemmove();
134# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
135 (PlatformHasDifferentMemcpyAndMemmove())
136#else
137# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
138#endif // __APPLE__
139
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140extern int asan_inited;
141// Used to avoid infinite recursion in __asan_init().
142extern bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000143extern void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144
145enum LinkerInitialized { LINKER_INITIALIZED = 0 };
146
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
148
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000149#if !defined(_WIN32) || defined(__clang__)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000150# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
151# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000152#else
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000153# define GET_CALLER_PC() (uptr)_ReturnAddress()
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000154// CaptureStackBackTrace doesn't need to know BP on Windows.
155// FIXME: This macro is still used when printing error reports though it's not
156// clear if the BP value is needed in the ASan reports on Windows.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000157# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000158#endif
159
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000160#ifdef _WIN32
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000161# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000162# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
163bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000164# endif
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000165#endif // _WIN32
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000166
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000167// These magic values are written to shadow for better error reporting.
168const int kAsanHeapLeftRedzoneMagic = 0xfa;
169const int kAsanHeapRightRedzoneMagic = 0xfb;
170const int kAsanHeapFreeMagic = 0xfd;
171const int kAsanStackLeftRedzoneMagic = 0xf1;
172const int kAsanStackMidRedzoneMagic = 0xf2;
173const int kAsanStackRightRedzoneMagic = 0xf3;
174const int kAsanStackPartialRedzoneMagic = 0xf4;
175const int kAsanStackAfterReturnMagic = 0xf5;
176const int kAsanUserPoisonedMemoryMagic = 0xf7;
177const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000178const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000179
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000180static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
181static const uptr kRetiredStackFrameMagic = 0x45E0360E;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000182
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000183// -------------------------- LowLevelAllocator ----- {{{1
184// A simple low-level memory allocator for internal use.
185class LowLevelAllocator {
186 public:
187 explicit LowLevelAllocator(LinkerInitialized) {}
188 // 'size' must be a power of two.
189 // Requires an external lock.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000190 void *Allocate(uptr size);
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000191 private:
192 char *allocated_end_;
193 char *allocated_current_;
194};
195
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000196} // namespace __asan
197
198#endif // ASAN_INTERNAL_H