1 //===-- asan_linux.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Linux-specific details.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
23 # include <sys/mman.h>
24 # include <sys/resource.h>
25 # include <sys/syscall.h>
26 # include <sys/time.h>
27 # include <sys/types.h>
31 # include "asan_interceptors.h"
32 # include "asan_internal.h"
33 # include "asan_premap_shadow.h"
34 # include "asan_thread.h"
35 # include "sanitizer_common/sanitizer_flags.h"
36 # include "sanitizer_common/sanitizer_freebsd.h"
37 # include "sanitizer_common/sanitizer_hash.h"
38 # include "sanitizer_common/sanitizer_libc.h"
39 # include "sanitizer_common/sanitizer_procmaps.h"
41 # if SANITIZER_FREEBSD
42 # include <sys/link_elf.h>
45 # if SANITIZER_SOLARIS
49 # if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS
50 # include <ucontext.h>
51 extern "C" void *_DYNAMIC
;
52 # elif SANITIZER_NETBSD
53 # include <link_elf.h>
54 # include <ucontext.h>
55 extern Elf_Dyn _DYNAMIC
;
58 # include <sys/ucontext.h>
59 extern ElfW(Dyn
) _DYNAMIC
[];
62 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
64 # if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
65 __FreeBSD_version <= 902001 // v9.2
66 # define ucontext_t xucontext_t
70 ASAN_RT_VERSION_UNDEFINED
= 0,
71 ASAN_RT_VERSION_DYNAMIC
,
72 ASAN_RT_VERSION_STATIC
,
75 // FIXME: perhaps also store abi version here?
77 SANITIZER_INTERFACE_ATTRIBUTE
78 asan_rt_version_t __asan_rt_version
;
83 void InitializePlatformInterceptors() {}
84 void InitializePlatformExceptionHandlers() {}
85 bool IsSystemHeapAddress(uptr addr
) { return false; }
87 void *AsanDoesNotSupportStaticLinkage() {
88 // This will fail to link with -static.
92 # if ASAN_PREMAP_SHADOW
93 uptr
FindPremappedShadowStart(uptr shadow_size_bytes
) {
94 uptr granularity
= GetMmapGranularity();
95 uptr shadow_start
= reinterpret_cast<uptr
>(&__asan_shadow
);
96 uptr premap_shadow_size
= PremapShadowSize();
97 uptr shadow_size
= RoundUpTo(shadow_size_bytes
, granularity
);
98 // We may have mapped too much. Release extra memory.
99 UnmapFromTo(shadow_start
+ shadow_size
, shadow_start
+ premap_shadow_size
);
104 uptr
FindDynamicShadowStart() {
105 uptr shadow_size_bytes
= MemToShadowSize(kHighMemEnd
);
106 # if ASAN_PREMAP_SHADOW
107 if (!PremapShadowFailed())
108 return FindPremappedShadowStart(shadow_size_bytes
);
111 return MapDynamicShadow(shadow_size_bytes
, ASAN_SHADOW_SCALE
,
112 /*min_shadow_base_alignment*/ 0, kHighMemEnd
);
115 void AsanApplyToGlobals(globals_op_fptr op
, const void *needle
) {
119 void FlushUnneededASanShadowMemory(uptr p
, uptr size
) {
120 // Since asan's mapping is compacting, the shadow chunk may be
121 // not page-aligned, so we only flush the page-aligned portion.
122 ReleaseMemoryPagesToOS(MemToShadow(p
), MemToShadow(p
+ size
));
125 # if SANITIZER_ANDROID
126 // FIXME: should we do anything for Android?
127 void AsanCheckDynamicRTPrereqs() {}
128 void AsanCheckIncompatibleRT() {}
130 static int FindFirstDSOCallback(struct dl_phdr_info
*info
, size_t size
,
132 VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n", info
->dlpi_name
,
133 (void *)info
->dlpi_addr
);
135 const char **name
= (const char **)data
;
137 // Ignore first entry (the main program)
144 // Ignore vDSO. glibc versions earlier than 2.15 (and some patched
145 // by distributors) return an empty name for the vDSO entry, so
146 // detect this as well.
147 if (!info
->dlpi_name
[0] ||
148 internal_strncmp(info
->dlpi_name
, "linux-", sizeof("linux-") - 1) == 0)
152 *name
= info
->dlpi_name
;
156 static bool IsDynamicRTName(const char *libname
) {
157 return internal_strstr(libname
, "libclang_rt.asan") ||
158 internal_strstr(libname
, "libasan.so");
161 static void ReportIncompatibleRT() {
162 Report("Your application is linked against incompatible ASan runtimes.\n");
166 void AsanCheckDynamicRTPrereqs() {
167 if (!ASAN_DYNAMIC
|| !flags()->verify_asan_link_order
)
170 // Ensure that dynamic RT is the first DSO in the list
171 const char *first_dso_name
= nullptr;
172 dl_iterate_phdr(FindFirstDSOCallback
, &first_dso_name
);
173 if (first_dso_name
&& first_dso_name
[0] && !IsDynamicRTName(first_dso_name
)) {
175 "ASan runtime does not come first in initial library list; "
176 "you should either link runtime to your application or "
177 "manually preload it with LD_PRELOAD.\n");
182 void AsanCheckIncompatibleRT() {
184 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
185 __asan_rt_version
= ASAN_RT_VERSION_DYNAMIC
;
186 } else if (__asan_rt_version
!= ASAN_RT_VERSION_DYNAMIC
) {
187 ReportIncompatibleRT();
190 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
191 // Ensure that dynamic runtime is not present. We should detect it
192 // as early as possible, otherwise ASan interceptors could bind to
193 // the functions in dynamic ASan runtime instead of the functions in
194 // system libraries, causing crashes later in ASan initialization.
195 MemoryMappingLayout
proc_maps(/*cache_enabled*/ true);
196 char filename
[PATH_MAX
];
197 MemoryMappedSegment
segment(filename
, sizeof(filename
));
198 while (proc_maps
.Next(&segment
)) {
199 if (IsDynamicRTName(segment
.filename
)) {
201 "Your application is linked against "
202 "incompatible ASan runtimes.\n");
206 __asan_rt_version
= ASAN_RT_VERSION_STATIC
;
207 } else if (__asan_rt_version
!= ASAN_RT_VERSION_STATIC
) {
208 ReportIncompatibleRT();
212 # endif // SANITIZER_ANDROID
214 # if ASAN_INTERCEPT_SWAPCONTEXT
215 constexpr u32 kAsanContextStackFlagsMagic
= 0x51260eea;
217 static int HashContextStack(const ucontext_t
&ucp
) {
218 MurMur2Hash64Builder
hash(kAsanContextStackFlagsMagic
);
219 hash
.add(reinterpret_cast<uptr
>(ucp
.uc_stack
.ss_sp
));
220 hash
.add(ucp
.uc_stack
.ss_size
);
221 return static_cast<int>(hash
.get());
224 void SignContextStack(void *context
) {
225 ucontext_t
*ucp
= reinterpret_cast<ucontext_t
*>(context
);
226 ucp
->uc_stack
.ss_flags
= HashContextStack(*ucp
);
229 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
230 const ucontext_t
*ucp
= reinterpret_cast<const ucontext_t
*>(context
);
231 if (HashContextStack(*ucp
) == ucp
->uc_stack
.ss_flags
) {
232 *stack
= reinterpret_cast<uptr
>(ucp
->uc_stack
.ss_sp
);
233 *ssize
= ucp
->uc_stack
.ss_size
;
239 # endif // ASAN_INTERCEPT_SWAPCONTEXT
241 void *AsanDlSymNext(const char *sym
) { return dlsym(RTLD_NEXT
, sym
); }
243 bool HandleDlopenInit() {
244 // Not supported on this platform.
245 static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN
,
246 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
250 } // namespace __asan
252 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||