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
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
;
44 #include <sys/ucontext.h>
48 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
50 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
51 __FreeBSD_version <= 902001 // v9.2
52 #define ucontext_t xucontext_t
56 ASAN_RT_VERSION_UNDEFINED
= 0,
57 ASAN_RT_VERSION_DYNAMIC
,
58 ASAN_RT_VERSION_STATIC
,
61 // FIXME: perhaps also store abi version here?
63 SANITIZER_INTERFACE_ATTRIBUTE
64 asan_rt_version_t __asan_rt_version
;
69 void InitializePlatformInterceptors() {}
71 void DisableReexec() {
72 // No need to re-exec on Linux.
76 // No need to re-exec on Linux.
79 void *AsanDoesNotSupportStaticLinkage() {
80 // This will fail to link with -static.
81 return &_DYNAMIC
; // defined in link.h
85 // FIXME: should we do anything for Android?
86 void AsanCheckDynamicRTPrereqs() {}
87 void AsanCheckIncompatibleRT() {}
89 static int FindFirstDSOCallback(struct dl_phdr_info
*info
, size_t size
,
91 // Continue until the first dynamic library is found
92 if (!info
->dlpi_name
|| info
->dlpi_name
[0] == 0)
96 if (internal_strncmp(info
->dlpi_name
, "linux-", sizeof("linux-") - 1) == 0)
99 *(const char **)data
= info
->dlpi_name
;
103 static bool IsDynamicRTName(const char *libname
) {
104 return internal_strstr(libname
, "libclang_rt.asan") ||
105 internal_strstr(libname
, "libasan.so");
108 static void ReportIncompatibleRT() {
109 Report("Your application is linked against incompatible ASan runtimes.\n");
113 void AsanCheckDynamicRTPrereqs() {
117 // Ensure that dynamic RT is the first DSO in the list
118 const char *first_dso_name
= nullptr;
119 dl_iterate_phdr(FindFirstDSOCallback
, &first_dso_name
);
120 if (first_dso_name
&& !IsDynamicRTName(first_dso_name
)) {
121 Report("ASan runtime does not come first in initial library list; "
122 "you should either link runtime to your application or "
123 "manually preload it with LD_PRELOAD.\n");
128 void AsanCheckIncompatibleRT() {
130 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
131 __asan_rt_version
= ASAN_RT_VERSION_DYNAMIC
;
132 } else if (__asan_rt_version
!= ASAN_RT_VERSION_DYNAMIC
) {
133 ReportIncompatibleRT();
136 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
137 // Ensure that dynamic runtime is not present. We should detect it
138 // as early as possible, otherwise ASan interceptors could bind to
139 // the functions in dynamic ASan runtime instead of the functions in
140 // system libraries, causing crashes later in ASan initialization.
141 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
143 while (proc_maps
.Next(nullptr, nullptr, nullptr, filename
,
144 sizeof(filename
), nullptr)) {
145 if (IsDynamicRTName(filename
)) {
146 Report("Your application is linked against "
147 "incompatible ASan runtimes.\n");
151 __asan_rt_version
= ASAN_RT_VERSION_STATIC
;
152 } else if (__asan_rt_version
!= ASAN_RT_VERSION_STATIC
) {
153 ReportIncompatibleRT();
157 #endif // SANITIZER_ANDROID
159 #if !SANITIZER_ANDROID
160 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
161 ucontext_t
*ucp
= (ucontext_t
*)context
;
162 *stack
= (uptr
)ucp
->uc_stack
.ss_sp
;
163 *ssize
= ucp
->uc_stack
.ss_size
;
166 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
171 void *AsanDlSymNext(const char *sym
) {
172 return dlsym(RTLD_NEXT
, sym
);
175 } // namespace __asan
177 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX