blob: 1edd8a78fc8acbdbd7d39bc85fd8c0722e3eda83 [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 Samsonovc70fa282013-01-31 13:46:14 +000018#include "asan_interface_internal.h"
Alexey Samsonov47657ce2012-06-06 07:02:44 +000019#include "sanitizer_common/sanitizer_common.h"
Alexey Samsonov94b50362012-06-05 14:25:27 +000020#include "sanitizer_common/sanitizer_internal_defs.h"
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +000021#include "sanitizer_common/sanitizer_stacktrace.h"
Kostya Serebryany16e00752012-05-31 13:42:53 +000022#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000023
Alexander Potapenko6f045292012-01-27 15:15:04 +000024#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000025# error "This operating system is not supported by AddressSanitizer"
26#endif
27
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000028#define ASAN_DEFAULT_FAILURE_EXITCODE 1
29
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000030#if defined(__linux__)
31# define ASAN_LINUX 1
32#else
33# define ASAN_LINUX 0
34#endif
35
36#if defined(__APPLE__)
37# define ASAN_MAC 1
38#else
39# define ASAN_MAC 0
40#endif
41
42#if defined(_WIN32)
43# define ASAN_WINDOWS 1
44#else
45# define ASAN_WINDOWS 0
46#endif
47
Evgeniy Stepanov3891ce62012-08-27 11:15:55 +000048#if defined(__ANDROID__) || defined(ANDROID)
49# define ASAN_ANDROID 1
50#else
51# define ASAN_ANDROID 0
52#endif
53
54
Alexey Samsonov34a32022012-03-26 09:07:29 +000055#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
56
Kostya Serebryanye31eca92013-02-15 12:00:24 +000057#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000058# error "The AddressSanitizer run-time should not be"
59 " instrumented by AddressSanitizer"
60#endif
61
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000062// Build-time configuration options.
63
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000064// If set, asan will install its own SEGV signal handler.
65#ifndef ASAN_NEEDS_SEGV
Alexander Potapenko3be33842012-10-03 15:09:17 +000066# if ASAN_ANDROID == 1
Evgeniy Stepanov9712af92012-09-28 10:07:53 +000067# define ASAN_NEEDS_SEGV 0
68# else
69# define ASAN_NEEDS_SEGV 1
70# endif
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000071#endif
72
73// If set, asan will intercept C++ exception api call(s).
74#ifndef ASAN_HAS_EXCEPTIONS
75# define ASAN_HAS_EXCEPTIONS 1
76#endif
77
78// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
79// provided by the instrumented objects. Otherwise constants are used.
80#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
81# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
82#endif
83
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000084// If set, values like allocator chunk size, as well as defaults for some flags
85// will be changed towards less memory overhead.
86#ifndef ASAN_LOW_MEMORY
Kostya Serebryanye52810d2012-12-25 07:17:17 +000087#if SANITIZER_WORDSIZE == 32
Evgeniy Stepanov9712af92012-09-28 10:07:53 +000088# define ASAN_LOW_MEMORY 1
Kostya Serebryanye52810d2012-12-25 07:17:17 +000089#else
Evgeniy Stepanov9712af92012-09-28 10:07:53 +000090# define ASAN_LOW_MEMORY 0
91# endif
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000092#endif
93
Kostya Serebryanye1353432013-02-20 14:28:08 +000094#ifndef ASAN_USE_PREINIT_ARRAY
95# define ASAN_USE_PREINIT_ARRAY (ASAN_LINUX && !ASAN_ANDROID)
96#endif
97
Kostya Serebryany1e172b42011-11-30 01:07:02 +000098// All internal functions in asan reside inside the __asan namespace
99// to avoid namespace collisions with the user programs.
100// Seperate namespace also makes it simpler to distinguish the asan run-time
101// functions from the instrumented user code in a profile.
102namespace __asan {
103
104class AsanThread;
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +0000105using __sanitizer::StackTrace;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000106
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000107// asan_rtl.cc
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000108void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000109
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000110void ReplaceOperatorsNewAndDelete();
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000111// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112void ReplaceSystemMalloc();
113
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000114// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000116
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000117void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000118
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +0000119void MaybeReexec();
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000120bool AsanInterceptsSignal(int signum);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000121void SetAlternateSignalStack();
122void UnsetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000123void InstallSignalHandlers();
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000124void ReadContextStack(void *context, uptr *stack, uptr *ssize);
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000125void AsanPlatformThreadInit();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000126
127// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000128void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000129void *AsanTSDGet();
130void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000131
Alexey Samsonove9541012012-06-06 13:11:29 +0000132void AppendToErrorMessageBuffer(const char *buffer);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000134// asan_poisoning.cc
135// Poisons the shadow memory for "size" bytes starting from "addr".
Kostya Serebryanyee392552012-05-31 15:02:07 +0000136void PoisonShadow(uptr addr, uptr size, u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000137// Poisons the shadow memory for "redzone_size" bytes starting from
138// "addr + size".
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000139void PoisonShadowPartialRightRedzone(uptr addr,
140 uptr size,
141 uptr redzone_size,
Kostya Serebryanyee392552012-05-31 15:02:07 +0000142 u8 value);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000143
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000144// Platfrom-specific options.
145#ifdef __APPLE__
146bool PlatformHasDifferentMemcpyAndMemmove();
147# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
148 (PlatformHasDifferentMemcpyAndMemmove())
149#else
150# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
151#endif // __APPLE__
152
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000153// Add convenient macro for interface functions that may be represented as
154// weak hooks.
155#define ASAN_MALLOC_HOOK(ptr, size) \
156 if (&__asan_malloc_hook) __asan_malloc_hook(ptr, size)
157#define ASAN_FREE_HOOK(ptr) \
158 if (&__asan_free_hook) __asan_free_hook(ptr)
159#define ASAN_ON_ERROR() \
160 if (&__asan_on_error) __asan_on_error()
161
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000162extern int asan_inited;
163// Used to avoid infinite recursion in __asan_init().
164extern bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000165extern void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +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;
Kostya Serebryany3945c582012-08-21 14:10:25 +0000176const int kAsanInitializationOrderMagic = 0xf6;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000177const int kAsanUserPoisonedMemoryMagic = 0xf7;
Alexey Samsonovd4b5db82012-12-04 01:38:15 +0000178const int kAsanStackUseAfterScopeMagic = 0xf8;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000179const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000180const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000181
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000182static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
183static const uptr kRetiredStackFrameMagic = 0x45E0360E;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000184
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000185} // namespace __asan
186
187#endif // ASAN_INTERNAL_H