asan_internal.h revision 30e110edf92303237d471f1cb8e3ad07954fb145
1//===-- 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
17#include "asan_flags.h"
18#include "asan_interface_internal.h"
19#include "sanitizer_common/sanitizer_common.h"
20#include "sanitizer_common/sanitizer_internal_defs.h"
21#include "sanitizer_common/sanitizer_stacktrace.h"
22#include "sanitizer_common/sanitizer_libc.h"
23
24#define ASAN_DEFAULT_FAILURE_EXITCODE 1
25
26#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
27# error "The AddressSanitizer run-time should not be"
28        " instrumented by AddressSanitizer"
29#endif
30
31// Build-time configuration options.
32
33// If set, asan will install its own SEGV signal handler.
34#ifndef ASAN_NEEDS_SEGV
35# if SANITIZER_ANDROID == 1
36#  define ASAN_NEEDS_SEGV 0
37# else
38#  define ASAN_NEEDS_SEGV 1
39# endif
40#endif
41
42// If set, asan will intercept C++ exception api call(s).
43#ifndef ASAN_HAS_EXCEPTIONS
44# define ASAN_HAS_EXCEPTIONS 1
45#endif
46
47// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
48// provided by the instrumented objects. Otherwise constants are used.
49#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
50# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
51#endif
52
53// If set, values like allocator chunk size, as well as defaults for some flags
54// will be changed towards less memory overhead.
55#ifndef ASAN_LOW_MEMORY
56#if SANITIZER_WORDSIZE == 32
57#  define ASAN_LOW_MEMORY 1
58#else
59#  define ASAN_LOW_MEMORY 0
60# endif
61#endif
62
63#ifndef ASAN_USE_PREINIT_ARRAY
64# define ASAN_USE_PREINIT_ARRAY (SANITIZER_LINUX && !SANITIZER_ANDROID)
65#endif
66
67// All internal functions in asan reside inside the __asan namespace
68// to avoid namespace collisions with the user programs.
69// Seperate namespace also makes it simpler to distinguish the asan run-time
70// functions from the instrumented user code in a profile.
71namespace __asan {
72
73class AsanThread;
74using __sanitizer::StackTrace;
75
76// asan_rtl.cc
77void NORETURN ShowStatsAndAbort();
78
79void ReplaceOperatorsNewAndDelete();
80// asan_malloc_linux.cc / asan_malloc_mac.cc
81void ReplaceSystemMalloc();
82
83// asan_linux.cc / asan_mac.cc / asan_win.cc
84void *AsanDoesNotSupportStaticLinkage();
85
86void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
87
88void MaybeReexec();
89bool AsanInterceptsSignal(int signum);
90void SetAlternateSignalStack();
91void UnsetAlternateSignalStack();
92void InstallSignalHandlers();
93void ReadContextStack(void *context, uptr *stack, uptr *ssize);
94void AsanPlatformThreadInit();
95
96// Wrapper for TLS/TSD.
97void AsanTSDInit(void (*destructor)(void *tsd));
98void *AsanTSDGet();
99void AsanTSDSet(void *tsd);
100
101void AppendToErrorMessageBuffer(const char *buffer);
102
103// asan_poisoning.cc
104// Poisons the shadow memory for "size" bytes starting from "addr".
105void PoisonShadow(uptr addr, uptr size, u8 value);
106// Poisons the shadow memory for "redzone_size" bytes starting from
107// "addr + size".
108void PoisonShadowPartialRightRedzone(uptr addr,
109                                     uptr size,
110                                     uptr redzone_size,
111                                     u8 value);
112
113// Platfrom-specific options.
114#if SANITIZER_MAC
115bool PlatformHasDifferentMemcpyAndMemmove();
116# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
117    (PlatformHasDifferentMemcpyAndMemmove())
118#else
119# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
120#endif  // __APPLE__
121
122// Add convenient macro for interface functions that may be represented as
123// weak hooks.
124#define ASAN_MALLOC_HOOK(ptr, size) \
125  if (&__asan_malloc_hook) __asan_malloc_hook(ptr, size)
126#define ASAN_FREE_HOOK(ptr) \
127  if (&__asan_free_hook) __asan_free_hook(ptr)
128#define ASAN_ON_ERROR() \
129  if (&__asan_on_error) __asan_on_error()
130
131extern int asan_inited;
132// Used to avoid infinite recursion in __asan_init().
133extern bool asan_init_is_running;
134extern void (*death_callback)(void);
135
136// These magic values are written to shadow for better error reporting.
137const int kAsanHeapLeftRedzoneMagic = 0xfa;
138const int kAsanHeapRightRedzoneMagic = 0xfb;
139const int kAsanHeapFreeMagic = 0xfd;
140const int kAsanStackLeftRedzoneMagic = 0xf1;
141const int kAsanStackMidRedzoneMagic = 0xf2;
142const int kAsanStackRightRedzoneMagic = 0xf3;
143const int kAsanStackPartialRedzoneMagic = 0xf4;
144const int kAsanStackAfterReturnMagic = 0xf5;
145const int kAsanInitializationOrderMagic = 0xf6;
146const int kAsanUserPoisonedMemoryMagic = 0xf7;
147const int kAsanStackUseAfterScopeMagic = 0xf8;
148const int kAsanGlobalRedzoneMagic = 0xf9;
149const int kAsanInternalHeapMagic = 0xfe;
150
151static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
152static const uptr kRetiredStackFrameMagic = 0x45E0360E;
153
154}  // namespace __asan
155
156#endif  // ASAN_INTERNAL_H
157