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_libc.h"
21#include "sanitizer_common/sanitizer_stacktrace.h"
22
23#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
24#  error \
25      "The AddressSanitizer run-time should not be 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
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();
63bool TryAsanInitFromRtl();
64
65// asan_win.cpp
66void InitializePlatformExceptionHandlers();
67// Returns whether an address is a valid allocated system heap block.
68// 'addr' must point to the beginning of the block.
69bool IsSystemHeapAddress(uptr addr);
70
71// asan_rtl.cpp
72void PrintAddressSpaceLayout();
73void NORETURN ShowStatsAndAbort();
74
75// asan_shadow_setup.cpp
76void InitializeShadowMemory();
77
78// asan_malloc_linux.cpp / asan_malloc_mac.cpp
79void ReplaceSystemMalloc();
80
81// asan_linux.cpp / asan_mac.cpp / asan_win.cpp
82uptr FindDynamicShadowStart();
83void *AsanDoesNotSupportStaticLinkage();
84void AsanCheckDynamicRTPrereqs();
85void AsanCheckIncompatibleRT();
86
87// Unpoisons platform-specific stacks.
88// Returns true if all stacks have been unpoisoned.
89bool PlatformUnpoisonStacks();
90
91// asan_rtl.cpp
92// Unpoison a region containing a stack.
93// Performs a sanity check and warns if the bounds don't look right.
94// The warning contains the type string to identify the stack type.
95void UnpoisonStack(uptr bottom, uptr top, const char *type);
96
97// asan_thread.cpp
98AsanThread *CreateMainThread();
99
100// Support function for __asan_(un)register_image_globals. Searches for the
101// loaded image containing `needle' and then enumerates all global metadata
102// structures declared in that image, applying `op' (e.g.,
103// __asan_(un)register_globals) to them.
104typedef void (*globals_op_fptr)(__asan_global *, uptr);
105void AsanApplyToGlobals(globals_op_fptr op, const void *needle);
106
107void AsanOnDeadlySignal(int, void *siginfo, void *context);
108
109void SignContextStack(void *context);
110void ReadContextStack(void *context, uptr *stack, uptr *ssize);
111void StopInitOrderChecking();
112
113// Wrapper for TLS/TSD.
114void AsanTSDInit(void (*destructor)(void *tsd));
115void *AsanTSDGet();
116void AsanTSDSet(void *tsd);
117void PlatformTSDDtor(void *tsd);
118
119void AppendToErrorMessageBuffer(const char *buffer);
120
121void *AsanDlSymNext(const char *sym);
122
123// Returns `true` iff most of ASan init process should be skipped due to the
124// ASan library being loaded via `dlopen()`. Platforms may perform any
125// `dlopen()` specific initialization inside this function.
126bool HandleDlopenInit();
127
128void InstallAtExitCheckLeaks();
129void InstallAtForkHandler();
130
131#define ASAN_ON_ERROR() \
132  if (&__asan_on_error) \
133  __asan_on_error()
134
135bool AsanInited();
136extern bool replace_intrin_cached;
137extern void (*death_callback)(void);
138// These magic values are written to shadow for better error
139// reporting.
140const int kAsanHeapLeftRedzoneMagic = 0xfa;
141const int kAsanHeapFreeMagic = 0xfd;
142const int kAsanStackLeftRedzoneMagic = 0xf1;
143const int kAsanStackMidRedzoneMagic = 0xf2;
144const int kAsanStackRightRedzoneMagic = 0xf3;
145const int kAsanStackAfterReturnMagic = 0xf5;
146const int kAsanInitializationOrderMagic = 0xf6;
147const int kAsanUserPoisonedMemoryMagic = 0xf7;
148const int kAsanContiguousContainerOOBMagic = 0xfc;
149const int kAsanStackUseAfterScopeMagic = 0xf8;
150const int kAsanGlobalRedzoneMagic = 0xf9;
151const int kAsanInternalHeapMagic = 0xfe;
152const int kAsanArrayCookieMagic = 0xac;
153const int kAsanIntraObjectRedzone = 0xbb;
154const int kAsanAllocaLeftMagic = 0xca;
155const int kAsanAllocaRightMagic = 0xcb;
156
157static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
158static const uptr kRetiredStackFrameMagic = 0x45E0360E;
159
160}  // namespace __asan
161
162#endif  // ASAN_INTERNAL_H
163