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 || \
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_premap_shadow.h"
20 #include "asan_thread.h"
21 #include "sanitizer_common/sanitizer_flags.h"
22 #include "sanitizer_common/sanitizer_freebsd.h"
23 #include "sanitizer_common/sanitizer_libc.h"
24 #include "sanitizer_common/sanitizer_procmaps.h"
27 #include <sys/resource.h>
29 #include <sys/syscall.h>
30 #include <sys/types.h>
40 #include <sys/link_elf.h>
47 #if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS
49 extern "C" void* _DYNAMIC
;
50 #elif SANITIZER_NETBSD
53 extern Elf_Dyn _DYNAMIC
;
55 #include <sys/ucontext.h>
59 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
61 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
62 __FreeBSD_version <= 902001 // v9.2
63 #define ucontext_t xucontext_t
67 ASAN_RT_VERSION_UNDEFINED
= 0,
68 ASAN_RT_VERSION_DYNAMIC
,
69 ASAN_RT_VERSION_STATIC
,
72 // FIXME: perhaps also store abi version here?
74 SANITIZER_INTERFACE_ATTRIBUTE
75 asan_rt_version_t __asan_rt_version
;
80 void InitializePlatformInterceptors() {}
81 void InitializePlatformExceptionHandlers() {}
82 bool IsSystemHeapAddress (uptr addr
) { return false; }
84 void *AsanDoesNotSupportStaticLinkage() {
85 // This will fail to link with -static.
86 return &_DYNAMIC
; // defined in link.h
89 static void UnmapFromTo(uptr from
, uptr to
) {
91 if (to
== from
) return;
92 uptr res
= internal_munmap(reinterpret_cast<void *>(from
), to
- from
);
93 if (UNLIKELY(internal_iserror(res
))) {
95 "ERROR: AddresSanitizer failed to unmap 0x%zx (%zd) bytes at address "
97 to
- from
, to
- from
, from
);
98 CHECK("unable to unmap" && 0);
102 #if ASAN_PREMAP_SHADOW
103 uptr
FindPremappedShadowStart() {
104 uptr granularity
= GetMmapGranularity();
105 uptr shadow_start
= reinterpret_cast<uptr
>(&__asan_shadow
);
106 uptr premap_shadow_size
= PremapShadowSize();
107 uptr shadow_size
= RoundUpTo(kHighShadowEnd
, granularity
);
108 // We may have mapped too much. Release extra memory.
109 UnmapFromTo(shadow_start
+ shadow_size
, shadow_start
+ premap_shadow_size
);
114 uptr
FindDynamicShadowStart() {
115 #if ASAN_PREMAP_SHADOW
116 if (!PremapShadowFailed())
117 return FindPremappedShadowStart();
120 uptr granularity
= GetMmapGranularity();
121 uptr alignment
= granularity
* 8;
122 uptr left_padding
= granularity
;
123 uptr shadow_size
= RoundUpTo(kHighShadowEnd
, granularity
);
124 uptr map_size
= shadow_size
+ left_padding
+ alignment
;
126 uptr map_start
= (uptr
)MmapNoAccess(map_size
);
127 CHECK_NE(map_start
, ~(uptr
)0);
129 uptr shadow_start
= RoundUpTo(map_start
+ left_padding
, alignment
);
130 UnmapFromTo(map_start
, shadow_start
- left_padding
);
131 UnmapFromTo(shadow_start
+ shadow_size
, map_start
+ map_size
);
136 void AsanApplyToGlobals(globals_op_fptr op
, const void *needle
) {
140 #if SANITIZER_ANDROID
141 // FIXME: should we do anything for Android?
142 void AsanCheckDynamicRTPrereqs() {}
143 void AsanCheckIncompatibleRT() {}
145 static int FindFirstDSOCallback(struct dl_phdr_info
*info
, size_t size
,
147 VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n",
148 info
->dlpi_name
, info
->dlpi_addr
);
150 // Continue until the first dynamic library is found
151 if (!info
->dlpi_name
|| info
->dlpi_name
[0] == 0)
155 if (internal_strncmp(info
->dlpi_name
, "linux-", sizeof("linux-") - 1) == 0)
158 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
159 // Ignore first entry (the main program)
160 char **p
= (char **)data
;
167 #if SANITIZER_SOLARIS
168 // Ignore executable on Solaris
169 if (info
->dlpi_addr
== 0)
173 *(const char **)data
= info
->dlpi_name
;
177 static bool IsDynamicRTName(const char *libname
) {
178 return internal_strstr(libname
, "libclang_rt.asan") ||
179 internal_strstr(libname
, "libasan.so");
182 static void ReportIncompatibleRT() {
183 Report("Your application is linked against incompatible ASan runtimes.\n");
187 void AsanCheckDynamicRTPrereqs() {
188 if (!ASAN_DYNAMIC
|| !flags()->verify_asan_link_order
)
191 // Ensure that dynamic RT is the first DSO in the list
192 const char *first_dso_name
= nullptr;
193 dl_iterate_phdr(FindFirstDSOCallback
, &first_dso_name
);
194 if (first_dso_name
&& !IsDynamicRTName(first_dso_name
)) {
195 Report("ASan runtime does not come first in initial library list; "
196 "you should either link runtime to your application or "
197 "manually preload it with LD_PRELOAD.\n");
202 void AsanCheckIncompatibleRT() {
204 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
205 __asan_rt_version
= ASAN_RT_VERSION_DYNAMIC
;
206 } else if (__asan_rt_version
!= ASAN_RT_VERSION_DYNAMIC
) {
207 ReportIncompatibleRT();
210 if (__asan_rt_version
== ASAN_RT_VERSION_UNDEFINED
) {
211 // Ensure that dynamic runtime is not present. We should detect it
212 // as early as possible, otherwise ASan interceptors could bind to
213 // the functions in dynamic ASan runtime instead of the functions in
214 // system libraries, causing crashes later in ASan initialization.
215 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
216 char filename
[PATH_MAX
];
217 MemoryMappedSegment
segment(filename
, sizeof(filename
));
218 while (proc_maps
.Next(&segment
)) {
219 if (IsDynamicRTName(segment
.filename
)) {
220 Report("Your application is linked against "
221 "incompatible ASan runtimes.\n");
225 __asan_rt_version
= ASAN_RT_VERSION_STATIC
;
226 } else if (__asan_rt_version
!= ASAN_RT_VERSION_STATIC
) {
227 ReportIncompatibleRT();
231 #endif // SANITIZER_ANDROID
233 #if !SANITIZER_ANDROID
234 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
235 ucontext_t
*ucp
= (ucontext_t
*)context
;
236 *stack
= (uptr
)ucp
->uc_stack
.ss_sp
;
237 *ssize
= ucp
->uc_stack
.ss_size
;
240 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
245 void *AsanDlSymNext(const char *sym
) {
246 return dlsym(RTLD_NEXT
, sym
);
249 } // namespace __asan
251 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||