1 //===-- asan_linux.cc -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Linux-specific details.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
19 #include "asan_interceptors.h"
20 #include "asan_internal.h"
21 #include "asan_premap_shadow.h"
22 #include "asan_thread.h"
23 #include "sanitizer_common/sanitizer_flags.h"
24 #include "sanitizer_common/sanitizer_freebsd.h"
25 #include "sanitizer_common/sanitizer_libc.h"
26 #include "sanitizer_common/sanitizer_procmaps.h"
29 #include <sys/resource.h>
31 #include <sys/syscall.h>
32 #include <sys/types.h>
42 #include <sys/link_elf.h>
49 #if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS
51 extern "C" void* _DYNAMIC;
52 #elif SANITIZER_NETBSD
55 extern Elf_Dyn _DYNAMIC;
57 #include <sys/ucontext.h>
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.
88 return &_DYNAMIC; // defined in link.h
91 static void UnmapFromTo(uptr from, uptr to) {
93 if (to == from) return;
94 uptr res = internal_munmap(reinterpret_cast<void *>(from), to - from);
95 if (UNLIKELY(internal_iserror(res))) {
97 "ERROR: AddresSanitizer failed to unmap 0x%zx (%zd) bytes at address "
99 to - from, to - from, from);
100 CHECK("unable to unmap" && 0);
104 #if ASAN_PREMAP_SHADOW
105 uptr FindPremappedShadowStart() {
106 uptr granularity = GetMmapGranularity();
107 uptr shadow_start = reinterpret_cast<uptr>(&__asan_shadow);
108 uptr premap_shadow_size = PremapShadowSize();
109 uptr shadow_size = RoundUpTo(kHighShadowEnd, granularity);
110 // We may have mapped too much. Release extra memory.
111 UnmapFromTo(shadow_start + shadow_size, shadow_start + premap_shadow_size);
116 uptr FindDynamicShadowStart() {
117 #if ASAN_PREMAP_SHADOW
118 if (!PremapShadowFailed())
119 return FindPremappedShadowStart();
122 uptr granularity = GetMmapGranularity();
123 uptr alignment = granularity * 8;
124 uptr left_padding = granularity;
125 uptr shadow_size = RoundUpTo(kHighShadowEnd, granularity);
126 uptr map_size = shadow_size + left_padding + alignment;
128 uptr map_start = (uptr)MmapNoAccess(map_size);
129 CHECK_NE(map_start, ~(uptr)0);
131 uptr shadow_start = RoundUpTo(map_start + left_padding, alignment);
132 UnmapFromTo(map_start, shadow_start - left_padding);
133 UnmapFromTo(shadow_start + shadow_size, map_start + map_size);
138 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
142 #if SANITIZER_ANDROID
143 // FIXME: should we do anything for Android?
144 void AsanCheckDynamicRTPrereqs() {}
145 void AsanCheckIncompatibleRT() {}
147 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
149 VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n",
150 info->dlpi_name, info->dlpi_addr);
152 // Continue until the first dynamic library is found
153 if (!info->dlpi_name || info->dlpi_name[0] == 0)
157 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
160 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
161 // Ignore first entry (the main program)
162 char **p = (char **)data;
169 #if SANITIZER_SOLARIS
170 // Ignore executable on Solaris
171 if (info->dlpi_addr == 0)
175 *(const char **)data = info->dlpi_name;
179 static bool IsDynamicRTName(const char *libname) {
180 return internal_strstr(libname, "libclang_rt.asan") ||
181 internal_strstr(libname, "libasan.so");
184 static void ReportIncompatibleRT() {
185 Report("Your application is linked against incompatible ASan runtimes.\n");
189 void AsanCheckDynamicRTPrereqs() {
190 if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
193 // Ensure that dynamic RT is the first DSO in the list
194 const char *first_dso_name = nullptr;
195 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
196 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
197 Report("ASan runtime does not come first in initial library list; "
198 "you should either link runtime to your application or "
199 "manually preload it with LD_PRELOAD.\n");
204 void AsanCheckIncompatibleRT() {
206 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
207 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
208 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
209 ReportIncompatibleRT();
212 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
213 // Ensure that dynamic runtime is not present. We should detect it
214 // as early as possible, otherwise ASan interceptors could bind to
215 // the functions in dynamic ASan runtime instead of the functions in
216 // system libraries, causing crashes later in ASan initialization.
217 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
218 char filename[PATH_MAX];
219 MemoryMappedSegment segment(filename, sizeof(filename));
220 while (proc_maps.Next(&segment)) {
221 if (IsDynamicRTName(segment.filename)) {
222 Report("Your application is linked against "
223 "incompatible ASan runtimes.\n");
227 __asan_rt_version = ASAN_RT_VERSION_STATIC;
228 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
229 ReportIncompatibleRT();
233 #endif // SANITIZER_ANDROID
235 #if !SANITIZER_ANDROID
236 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
237 ucontext_t *ucp = (ucontext_t*)context;
238 *stack = (uptr)ucp->uc_stack.ss_sp;
239 *ssize = ucp->uc_stack.ss_size;
242 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
247 void *AsanDlSymNext(const char *sym) {
248 return dlsym(RTLD_NEXT, sym);
251 } // namespace __asan
253 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||