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
, 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",
132 info
->dlpi_name
, info
->dlpi_addr
);
134 // Continue until the first dynamic library is found
135 if (!info
->dlpi_name
|| info
->dlpi_name
[0] == 0)
139 if (internal_strncmp(info
->dlpi_name
, "linux-", sizeof("linux-") - 1) == 0)
142 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
143 // Ignore first entry (the main program)
144 char **p
= (char **)data
;
151 #if SANITIZER_SOLARIS
152 // Ignore executable on Solaris
153 if (info
->dlpi_addr
== 0)
157 *(const char **)data
= info
->dlpi_name
;
161 static bool IsDynamicRTName(const char *libname
) {
162 return internal_strstr(libname
, "libclang_rt.asan") ||
163 internal_strstr(libname
, "libasan.so");
166 static void ReportIncompatibleRT() {
167 Report("Your application is linked against incompatible ASan runtimes.\n");
171 void AsanCheckDynamicRTPrereqs() {
172 if (!ASAN_DYNAMIC
|| !flags()->verify_asan_link_order
)
175 // Ensure that dynamic RT is the first DSO in the list
176 const char *first_dso_name
= nullptr;
177 dl_iterate_phdr(FindFirstDSOCallback
, &first_dso_name
);
178 if (first_dso_name
&& !IsDynamicRTName(first_dso_name
)) {
179 Report("ASan runtime does not come first in initial library list; "
180 "you should either link runtime to your application or "
181 "manually preload it with LD_PRELOAD.\n");
186 void AsanCheckIncompatibleRT() {
188 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
189 __asan_rt_version
= ASAN_RT_VERSION_DYNAMIC
;
190 } else if (__asan_rt_version
!= ASAN_RT_VERSION_DYNAMIC
) {
191 ReportIncompatibleRT();
194 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
195 // Ensure that dynamic runtime is not present. We should detect it
196 // as early as possible, otherwise ASan interceptors could bind to
197 // the functions in dynamic ASan runtime instead of the functions in
198 // system libraries, causing crashes later in ASan initialization.
199 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
200 char filename
[PATH_MAX
];
201 MemoryMappedSegment
segment(filename
, sizeof(filename
));
202 while (proc_maps
.Next(&segment
)) {
203 if (IsDynamicRTName(segment
.filename
)) {
204 Report("Your application is linked against "
205 "incompatible ASan runtimes.\n");
209 __asan_rt_version
= ASAN_RT_VERSION_STATIC
;
210 } else if (__asan_rt_version
!= ASAN_RT_VERSION_STATIC
) {
211 ReportIncompatibleRT();
215 #endif // SANITIZER_ANDROID
217 #if !SANITIZER_ANDROID
218 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
219 ucontext_t
*ucp
= (ucontext_t
*)context
;
220 *stack
= (uptr
)ucp
->uc_stack
.ss_sp
;
221 *ssize
= ucp
->uc_stack
.ss_size
;
224 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
229 void *AsanDlSymNext(const char *sym
) {
230 return dlsym(RTLD_NEXT
, sym
);
233 bool HandleDlopenInit() {
234 // Not supported on this platform.
235 static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN
,
236 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
240 } // namespace __asan
242 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||