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
18 #include "asan_interceptors.h"
19 #include "asan_internal.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>
39 #include <sys/link_elf.h>
42 #if SANITIZER_ANDROID || SANITIZER_FREEBSD
44 extern "C" void* _DYNAMIC;
46 #include <sys/ucontext.h>
50 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
52 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
53 __FreeBSD_version <= 902001 // v9.2
54 #define ucontext_t xucontext_t
58 ASAN_RT_VERSION_UNDEFINED = 0,
59 ASAN_RT_VERSION_DYNAMIC,
60 ASAN_RT_VERSION_STATIC,
63 // FIXME: perhaps also store abi version here?
65 SANITIZER_INTERFACE_ATTRIBUTE
66 asan_rt_version_t __asan_rt_version;
71 void InitializePlatformInterceptors() {}
73 void DisableReexec() {
74 // No need to re-exec on Linux.
78 // No need to re-exec on Linux.
81 void *AsanDoesNotSupportStaticLinkage() {
82 // This will fail to link with -static.
83 return &_DYNAMIC; // defined in link.h
87 // FIXME: should we do anything for Android?
88 void AsanCheckDynamicRTPrereqs() {}
89 void AsanCheckIncompatibleRT() {}
91 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
93 // Continue until the first dynamic library is found
94 if (!info->dlpi_name || info->dlpi_name[0] == 0)
98 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
101 *(const char **)data = info->dlpi_name;
105 static bool IsDynamicRTName(const char *libname) {
106 return internal_strstr(libname, "libclang_rt.asan") ||
107 internal_strstr(libname, "libasan.so");
110 static void ReportIncompatibleRT() {
111 Report("Your application is linked against incompatible ASan runtimes.\n");
115 void AsanCheckDynamicRTPrereqs() {
119 // Ensure that dynamic RT is the first DSO in the list
120 const char *first_dso_name = 0;
121 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
122 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
123 Report("ASan runtime does not come first in initial library list; "
124 "you should either link runtime to your application or "
125 "manually preload it with LD_PRELOAD.\n");
130 void AsanCheckIncompatibleRT() {
132 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
133 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
134 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
135 ReportIncompatibleRT();
138 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
139 // Ensure that dynamic runtime is not present. We should detect it
140 // as early as possible, otherwise ASan interceptors could bind to
141 // the functions in dynamic ASan runtime instead of the functions in
142 // system libraries, causing crashes later in ASan initialization.
143 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
145 while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) {
146 if (IsDynamicRTName(filename)) {
147 Report("Your application is linked against "
148 "incompatible ASan runtimes.\n");
152 __asan_rt_version = ASAN_RT_VERSION_STATIC;
153 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
154 ReportIncompatibleRT();
158 #endif // SANITIZER_ANDROID
160 void AsanPlatformThreadInit() {
161 // Nothing here for now.
164 #if !SANITIZER_ANDROID
165 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
166 ucontext_t *ucp = (ucontext_t*)context;
167 *stack = (uptr)ucp->uc_stack.ss_sp;
168 *ssize = ucp->uc_stack.ss_size;
171 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
176 void *AsanDlSymNext(const char *sym) {
177 return dlsym(RTLD_NEXT, sym);
180 } // namespace __asan
182 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX