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 || \
18 #include "asan_interceptors.h"
19 #include "asan_internal.h"
20 #include "asan_premap_shadow.h"
21 #include "asan_thread.h"
22 #include "sanitizer_common/sanitizer_flags.h"
23 #include "sanitizer_common/sanitizer_freebsd.h"
24 #include "sanitizer_common/sanitizer_libc.h"
25 #include "sanitizer_common/sanitizer_procmaps.h"
28 #include <sys/resource.h>
30 #include <sys/syscall.h>
31 #include <sys/types.h>
41 #include <sys/link_elf.h>
48 #if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS
50 extern "C" void* _DYNAMIC
;
51 #elif SANITIZER_NETBSD
54 extern Elf_Dyn _DYNAMIC
;
56 #include <sys/ucontext.h>
58 extern ElfW(Dyn
) _DYNAMIC
[];
61 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
63 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
64 __FreeBSD_version <= 902001 // v9.2
65 #define ucontext_t xucontext_t
69 ASAN_RT_VERSION_UNDEFINED
= 0,
70 ASAN_RT_VERSION_DYNAMIC
,
71 ASAN_RT_VERSION_STATIC
,
74 // FIXME: perhaps also store abi version here?
76 SANITIZER_INTERFACE_ATTRIBUTE
77 asan_rt_version_t __asan_rt_version
;
82 void InitializePlatformInterceptors() {}
83 void InitializePlatformExceptionHandlers() {}
84 bool IsSystemHeapAddress (uptr addr
) { return false; }
86 void *AsanDoesNotSupportStaticLinkage() {
87 // This will fail to link with -static.
91 #if ASAN_PREMAP_SHADOW
92 uptr
FindPremappedShadowStart(uptr shadow_size_bytes
) {
93 uptr granularity
= GetMmapGranularity();
94 uptr shadow_start
= reinterpret_cast<uptr
>(&__asan_shadow
);
95 uptr premap_shadow_size
= PremapShadowSize();
96 uptr shadow_size
= RoundUpTo(shadow_size_bytes
, granularity
);
97 // We may have mapped too much. Release extra memory.
98 UnmapFromTo(shadow_start
+ shadow_size
, shadow_start
+ premap_shadow_size
);
103 uptr
FindDynamicShadowStart() {
104 uptr shadow_size_bytes
= MemToShadowSize(kHighMemEnd
);
105 #if ASAN_PREMAP_SHADOW
106 if (!PremapShadowFailed())
107 return FindPremappedShadowStart(shadow_size_bytes
);
110 return MapDynamicShadow(shadow_size_bytes
, ASAN_SHADOW_SCALE
,
111 /*min_shadow_base_alignment*/ 0, kHighMemEnd
);
114 void AsanApplyToGlobals(globals_op_fptr op
, const void *needle
) {
118 void FlushUnneededASanShadowMemory(uptr p
, uptr size
) {
119 // Since asan's mapping is compacting, the shadow chunk may be
120 // not page-aligned, so we only flush the page-aligned portion.
121 ReleaseMemoryPagesToOS(MemToShadow(p
), MemToShadow(p
+ size
));
124 #if SANITIZER_ANDROID
125 // FIXME: should we do anything for Android?
126 void AsanCheckDynamicRTPrereqs() {}
127 void AsanCheckIncompatibleRT() {}
129 static int FindFirstDSOCallback(struct dl_phdr_info
*info
, size_t size
,
131 VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n", info
->dlpi_name
,
132 (void *)info
->dlpi_addr
);
134 const char **name
= (const char **)data
;
136 // Ignore first entry (the main program)
143 // Ignore vDSO. glibc versions earlier than 2.15 (and some patched
144 // by distributors) return an empty name for the vDSO entry, so
145 // detect this as well.
146 if (!info
->dlpi_name
[0] ||
147 internal_strncmp(info
->dlpi_name
, "linux-", sizeof("linux-") - 1) == 0)
151 *name
= info
->dlpi_name
;
155 static bool IsDynamicRTName(const char *libname
) {
156 return internal_strstr(libname
, "libclang_rt.asan") ||
157 internal_strstr(libname
, "libasan.so");
160 static void ReportIncompatibleRT() {
161 Report("Your application is linked against incompatible ASan runtimes.\n");
165 void AsanCheckDynamicRTPrereqs() {
166 if (!ASAN_DYNAMIC
|| !flags()->verify_asan_link_order
)
169 // Ensure that dynamic RT is the first DSO in the list
170 const char *first_dso_name
= nullptr;
171 dl_iterate_phdr(FindFirstDSOCallback
, &first_dso_name
);
172 if (first_dso_name
&& first_dso_name
[0] && !IsDynamicRTName(first_dso_name
)) {
173 Report("ASan runtime does not come first in initial library list; "
174 "you should either link runtime to your application or "
175 "manually preload it with LD_PRELOAD.\n");
180 void AsanCheckIncompatibleRT() {
182 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
183 __asan_rt_version
= ASAN_RT_VERSION_DYNAMIC
;
184 } else if (__asan_rt_version
!= ASAN_RT_VERSION_DYNAMIC
) {
185 ReportIncompatibleRT();
188 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
189 // Ensure that dynamic runtime is not present. We should detect it
190 // as early as possible, otherwise ASan interceptors could bind to
191 // the functions in dynamic ASan runtime instead of the functions in
192 // system libraries, causing crashes later in ASan initialization.
193 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
194 char filename
[PATH_MAX
];
195 MemoryMappedSegment
segment(filename
, sizeof(filename
));
196 while (proc_maps
.Next(&segment
)) {
197 if (IsDynamicRTName(segment
.filename
)) {
198 Report("Your application is linked against "
199 "incompatible ASan runtimes.\n");
203 __asan_rt_version
= ASAN_RT_VERSION_STATIC
;
204 } else if (__asan_rt_version
!= ASAN_RT_VERSION_STATIC
) {
205 ReportIncompatibleRT();
209 #endif // SANITIZER_ANDROID
211 #if !SANITIZER_ANDROID
212 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
213 ucontext_t
*ucp
= (ucontext_t
*)context
;
214 *stack
= (uptr
)ucp
->uc_stack
.ss_sp
;
215 *ssize
= ucp
->uc_stack
.ss_size
;
218 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
223 void *AsanDlSymNext(const char *sym
) {
224 return dlsym(RTLD_NEXT
, sym
);
227 bool HandleDlopenInit() {
228 // Not supported on this platform.
229 static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN
,
230 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
234 } // namespace __asan
236 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||