1 //===-- asan_linux.cc -----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Linux-specific details.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
16 #include "asan_interceptors.h"
17 #include "asan_internal.h"
18 #include "asan_thread.h"
19 #include "sanitizer_common/sanitizer_flags.h"
20 #include "sanitizer_common/sanitizer_freebsd.h"
21 #include "sanitizer_common/sanitizer_libc.h"
22 #include "sanitizer_common/sanitizer_procmaps.h"
25 #include <sys/resource.h>
27 #include <sys/syscall.h>
28 #include <sys/types.h>
37 #include <sys/link_elf.h>
40 #if SANITIZER_ANDROID || SANITIZER_FREEBSD
42 extern "C" void* _DYNAMIC
;
43 #elif SANITIZER_NETBSD
46 extern Elf_Dyn _DYNAMIC
;
48 #include <sys/ucontext.h>
52 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
54 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
55 __FreeBSD_version <= 902001 // v9.2
56 #define ucontext_t xucontext_t
60 ASAN_RT_VERSION_UNDEFINED
= 0,
61 ASAN_RT_VERSION_DYNAMIC
,
62 ASAN_RT_VERSION_STATIC
,
65 // FIXME: perhaps also store abi version here?
67 SANITIZER_INTERFACE_ATTRIBUTE
68 asan_rt_version_t __asan_rt_version
;
73 void InitializePlatformInterceptors() {}
74 void InitializePlatformExceptionHandlers() {}
75 bool IsSystemHeapAddress (uptr addr
) { return false; }
77 void *AsanDoesNotSupportStaticLinkage() {
78 // This will fail to link with -static.
79 return &_DYNAMIC
; // defined in link.h
82 uptr
FindDynamicShadowStart() {
83 UNREACHABLE("FindDynamicShadowStart is not available");
87 void AsanApplyToGlobals(globals_op_fptr op
, const void *needle
) {
92 // FIXME: should we do anything for Android?
93 void AsanCheckDynamicRTPrereqs() {}
94 void AsanCheckIncompatibleRT() {}
96 static int FindFirstDSOCallback(struct dl_phdr_info
*info
, size_t size
,
98 // Continue until the first dynamic library is found
99 if (!info
->dlpi_name
|| info
->dlpi_name
[0] == 0)
103 if (internal_strncmp(info
->dlpi_name
, "linux-", sizeof("linux-") - 1) == 0)
107 // Ignore first entry (the main program)
108 char **p
= (char **)data
;
115 *(const char **)data
= info
->dlpi_name
;
119 static bool IsDynamicRTName(const char *libname
) {
120 return internal_strstr(libname
, "libclang_rt.asan") ||
121 internal_strstr(libname
, "libasan.so");
124 static void ReportIncompatibleRT() {
125 Report("Your application is linked against incompatible ASan runtimes.\n");
129 void AsanCheckDynamicRTPrereqs() {
130 if (!ASAN_DYNAMIC
|| !flags()->verify_asan_link_order
)
133 // Ensure that dynamic RT is the first DSO in the list
134 const char *first_dso_name
= nullptr;
135 dl_iterate_phdr(FindFirstDSOCallback
, &first_dso_name
);
136 if (first_dso_name
&& !IsDynamicRTName(first_dso_name
)) {
137 Report("ASan runtime does not come first in initial library list; "
138 "you should either link runtime to your application or "
139 "manually preload it with LD_PRELOAD.\n");
144 void AsanCheckIncompatibleRT() {
146 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
147 __asan_rt_version
= ASAN_RT_VERSION_DYNAMIC
;
148 } else if (__asan_rt_version
!= ASAN_RT_VERSION_DYNAMIC
) {
149 ReportIncompatibleRT();
152 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
153 // Ensure that dynamic runtime is not present. We should detect it
154 // as early as possible, otherwise ASan interceptors could bind to
155 // the functions in dynamic ASan runtime instead of the functions in
156 // system libraries, causing crashes later in ASan initialization.
157 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
159 MemoryMappedSegment
segment(filename
, sizeof(filename
));
160 while (proc_maps
.Next(&segment
)) {
161 if (IsDynamicRTName(segment
.filename
)) {
162 Report("Your application is linked against "
163 "incompatible ASan runtimes.\n");
167 __asan_rt_version
= ASAN_RT_VERSION_STATIC
;
168 } else if (__asan_rt_version
!= ASAN_RT_VERSION_STATIC
) {
169 ReportIncompatibleRT();
173 #endif // SANITIZER_ANDROID
175 #if !SANITIZER_ANDROID
176 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
177 ucontext_t
*ucp
= (ucontext_t
*)context
;
178 *stack
= (uptr
)ucp
->uc_stack
.ss_sp
;
179 *ssize
= ucp
->uc_stack
.ss_size
;
182 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
187 void *AsanDlSymNext(const char *sym
) {
188 return dlsym(RTLD_NEXT
, sym
);
191 } // namespace __asan
193 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD