1//===-- asan_internal.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of AddressSanitizer, an address sanity checker.
10//
11// ASan-private header which defines various general utilities.
12//===----------------------------------------------------------------------===//
13#ifndef ASAN_INTERNAL_H
14#define ASAN_INTERNAL_H
15
16#include "asan_flags.h"
17#include "asan_interface_internal.h"
18#include "sanitizer_common/sanitizer_common.h"
19#include "sanitizer_common/sanitizer_internal_defs.h"
20#include "sanitizer_common/sanitizer_stacktrace.h"
21#include "sanitizer_common/sanitizer_libc.h"
22
23#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
24# error "The AddressSanitizer run-time should not be"
25        " instrumented by AddressSanitizer"
26#endif
27
28// Build-time configuration options.
29
30// If set, asan will intercept C++ exception api call(s).
31#ifndef ASAN_HAS_EXCEPTIONS
32# define ASAN_HAS_EXCEPTIONS 1
33#endif
34
35// If set, values like allocator chunk size, as well as defaults for some flags
36// will be changed towards less memory overhead.
37#ifndef ASAN_LOW_MEMORY
38# if SANITIZER_IOS || SANITIZER_ANDROID || SANITIZER_RTEMS
39#  define ASAN_LOW_MEMORY 1
40# else
41#  define ASAN_LOW_MEMORY 0
42# endif
43#endif
44
45#ifndef ASAN_DYNAMIC
46# ifdef PIC
47#  define ASAN_DYNAMIC 1
48# else
49#  define ASAN_DYNAMIC 0
50# endif
51#endif
52
53// All internal functions in asan reside inside the __asan namespace
54// to avoid namespace collisions with the user programs.
55// Separate namespace also makes it simpler to distinguish the asan run-time
56// functions from the instrumented user code in a profile.
57namespace __asan {
58
59class AsanThread;
60using __sanitizer::StackTrace;
61
62void AsanInitFromRtl();
63
64// asan_win.cpp
65void InitializePlatformExceptionHandlers();
66// Returns whether an address is a valid allocated system heap block.
67// 'addr' must point to the beginning of the block.
68bool IsSystemHeapAddress(uptr addr);
69
70// asan_rtl.cpp
71void PrintAddressSpaceLayout();
72void NORETURN ShowStatsAndAbort();
73
74// asan_shadow_setup.cpp
75void InitializeShadowMemory();
76
77// asan_malloc_linux.cpp / asan_malloc_mac.cpp
78void ReplaceSystemMalloc();
79
80// asan_linux.cpp / asan_mac.cpp / asan_rtems.cpp / asan_win.cpp
81uptr FindDynamicShadowStart();
82void *AsanDoesNotSupportStaticLinkage();
83void AsanCheckDynamicRTPrereqs();
84void AsanCheckIncompatibleRT();
85
86// asan_thread.cpp
87AsanThread *CreateMainThread();
88
89// Support function for __asan_(un)register_image_globals. Searches for the
90// loaded image containing `needle' and then enumerates all global metadata
91// structures declared in that image, applying `op' (e.g.,
92// __asan_(un)register_globals) to them.
93typedef void (*globals_op_fptr)(__asan_global *, uptr);
94void AsanApplyToGlobals(globals_op_fptr op, const void *needle);
95
96void AsanOnDeadlySignal(int, void *siginfo, void *context);
97
98void ReadContextStack(void *context, uptr *stack, uptr *ssize);
99void StopInitOrderChecking();
100
101// Wrapper for TLS/TSD.
102void AsanTSDInit(void (*destructor)(void *tsd));
103void *AsanTSDGet();
104void AsanTSDSet(void *tsd);
105void PlatformTSDDtor(void *tsd);
106
107void AppendToErrorMessageBuffer(const char *buffer);
108
109void *AsanDlSymNext(const char *sym);
110
111void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name);
112
113// Returns `true` iff most of ASan init process should be skipped due to the
114// ASan library being loaded via `dlopen()`. Platforms may perform any
115// `dlopen()` specific initialization inside this function.
116bool HandleDlopenInit();
117
118// Add convenient macro for interface functions that may be represented as
119// weak hooks.
120#define ASAN_MALLOC_HOOK(ptr, size)                                   \
121  do {                                                                \
122    if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \
123    RunMallocHooks(ptr, size);                                        \
124  } while (false)
125#define ASAN_FREE_HOOK(ptr)                                 \
126  do {                                                      \
127    if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \
128    RunFreeHooks(ptr);                                      \
129  } while (false)
130#define ASAN_ON_ERROR() \
131  if (&__asan_on_error) __asan_on_error()
132
133extern int asan_inited;
134// Used to avoid infinite recursion in __asan_init().
135extern bool asan_init_is_running;
136extern void (*death_callback)(void);
137// These magic values are written to shadow for better error reporting.
138const int kAsanHeapLeftRedzoneMagic = 0xfa;
139const int kAsanHeapFreeMagic = 0xfd;
140const int kAsanStackLeftRedzoneMagic = 0xf1;
141const int kAsanStackMidRedzoneMagic = 0xf2;
142const int kAsanStackRightRedzoneMagic = 0xf3;
143const int kAsanStackAfterReturnMagic = 0xf5;
144const int kAsanInitializationOrderMagic = 0xf6;
145const int kAsanUserPoisonedMemoryMagic = 0xf7;
146const int kAsanContiguousContainerOOBMagic = 0xfc;
147const int kAsanStackUseAfterScopeMagic = 0xf8;
148const int kAsanGlobalRedzoneMagic = 0xf9;
149const int kAsanInternalHeapMagic = 0xfe;
150const int kAsanArrayCookieMagic = 0xac;
151const int kAsanIntraObjectRedzone = 0xbb;
152const int kAsanAllocaLeftMagic = 0xca;
153const int kAsanAllocaRightMagic = 0xcb;
154// Used to populate the shadow gap for systems without memory
155// protection there (i.e. Myriad).
156const int kAsanShadowGap = 0xcc;
157
158static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
159static const uptr kRetiredStackFrameMagic = 0x45E0360E;
160
161}  // namespace __asan
162
163#endif  // ASAN_INTERNAL_H
164