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#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
25# error "The AddressSanitizer run-time should not be"
26        " instrumented by AddressSanitizer"
27#endif
28
29// Build-time configuration options.
30
31// If set, asan will intercept C++ exception api call(s).
32#ifndef ASAN_HAS_EXCEPTIONS
33# define ASAN_HAS_EXCEPTIONS 1
34#endif
35
36// If set, values like allocator chunk size, as well as defaults for some flags
37// will be changed towards less memory overhead.
38#ifndef ASAN_LOW_MEMORY
39# if SANITIZER_IOS || (SANITIZER_WORDSIZE == 32)
40#  define ASAN_LOW_MEMORY 1
41# else
42#  define ASAN_LOW_MEMORY 0
43# endif
44#endif
45
46#ifndef ASAN_DYNAMIC
47# ifdef PIC
48#  define ASAN_DYNAMIC 1
49# else
50#  define ASAN_DYNAMIC 0
51# endif
52#endif
53
54// All internal functions in asan reside inside the __asan namespace
55// to avoid namespace collisions with the user programs.
56// Separate namespace also makes it simpler to distinguish the asan run-time
57// functions from the instrumented user code in a profile.
58namespace __asan {
59
60class AsanThread;
61using __sanitizer::StackTrace;
62
63void AsanInitFromRtl();
64
65// asan_win.cc
66void InitializePlatformExceptionHandlers();
67
68// asan_rtl.cc
69void NORETURN ShowStatsAndAbort();
70
71// asan_malloc_linux.cc / asan_malloc_mac.cc
72void ReplaceSystemMalloc();
73
74// asan_linux.cc / asan_mac.cc / asan_win.cc
75void *AsanDoesNotSupportStaticLinkage();
76void AsanCheckDynamicRTPrereqs();
77void AsanCheckIncompatibleRT();
78
79// Support function for __asan_(un)register_image_globals. Searches for the
80// loaded image containing `needle' and then enumerates all global metadata
81// structures declared in that image, applying `op' (e.g.,
82// __asan_(un)register_globals) to them.
83typedef void (*globals_op_fptr)(__asan_global *, uptr);
84void AsanApplyToGlobals(globals_op_fptr op, const void *needle);
85
86void AsanOnDeadlySignal(int, void *siginfo, void *context);
87
88void ReadContextStack(void *context, uptr *stack, uptr *ssize);
89void StopInitOrderChecking();
90
91// Wrapper for TLS/TSD.
92void AsanTSDInit(void (*destructor)(void *tsd));
93void *AsanTSDGet();
94void AsanTSDSet(void *tsd);
95void PlatformTSDDtor(void *tsd);
96
97void AppendToErrorMessageBuffer(const char *buffer);
98
99void *AsanDlSymNext(const char *sym);
100
101void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name);
102
103// Platform-specific options.
104#if SANITIZER_MAC
105bool PlatformHasDifferentMemcpyAndMemmove();
106# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
107    (PlatformHasDifferentMemcpyAndMemmove())
108#elif SANITIZER_WINDOWS64
109# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
110#else
111# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
112#endif  // SANITIZER_MAC
113
114// Add convenient macro for interface functions that may be represented as
115// weak hooks.
116#define ASAN_MALLOC_HOOK(ptr, size)                                   \
117  do {                                                                \
118    if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \
119    RunMallocHooks(ptr, size);                                        \
120  } while (false)
121#define ASAN_FREE_HOOK(ptr)                                 \
122  do {                                                      \
123    if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \
124    RunFreeHooks(ptr);                                      \
125  } while (false)
126#define ASAN_ON_ERROR() \
127  if (&__asan_on_error) __asan_on_error()
128
129extern int asan_inited;
130// Used to avoid infinite recursion in __asan_init().
131extern bool asan_init_is_running;
132extern void (*death_callback)(void);
133// These magic values are written to shadow for better error reporting.
134const int kAsanHeapLeftRedzoneMagic = 0xfa;
135const int kAsanHeapRightRedzoneMagic = 0xfb;
136const int kAsanHeapFreeMagic = 0xfd;
137const int kAsanStackLeftRedzoneMagic = 0xf1;
138const int kAsanStackMidRedzoneMagic = 0xf2;
139const int kAsanStackRightRedzoneMagic = 0xf3;
140const int kAsanStackPartialRedzoneMagic = 0xf4;
141const int kAsanStackAfterReturnMagic = 0xf5;
142const int kAsanInitializationOrderMagic = 0xf6;
143const int kAsanUserPoisonedMemoryMagic = 0xf7;
144const int kAsanContiguousContainerOOBMagic = 0xfc;
145const int kAsanStackUseAfterScopeMagic = 0xf8;
146const int kAsanGlobalRedzoneMagic = 0xf9;
147const int kAsanInternalHeapMagic = 0xfe;
148const int kAsanArrayCookieMagic = 0xac;
149const int kAsanIntraObjectRedzone = 0xbb;
150const int kAsanAllocaLeftMagic = 0xca;
151const int kAsanAllocaRightMagic = 0xcb;
152
153static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
154static const uptr kRetiredStackFrameMagic = 0x45E0360E;
155
156}  // namespace __asan
157
158#endif  // ASAN_INTERNAL_H
159