2017-02-17 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libsanitizer / asan / asan_linux.cc
blob9f058df71a5b604a4414eeb94a88c27d133d0417
1 //===-- asan_linux.cc -----------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
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"
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #include <sys/mman.h>
27 #include <sys/syscall.h>
28 #include <sys/types.h>
29 #include <dlfcn.h>
30 #include <fcntl.h>
31 #include <pthread.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <unwind.h>
36 #if SANITIZER_FREEBSD
37 #include <sys/link_elf.h>
38 #endif
40 #if SANITIZER_ANDROID || SANITIZER_FREEBSD
41 #include <ucontext.h>
42 extern "C" void* _DYNAMIC;
43 #else
44 #include <sys/ucontext.h>
45 #include <link.h>
46 #endif
48 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
49 // 32-bit mode.
50 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
51 __FreeBSD_version <= 902001 // v9.2
52 #define ucontext_t xucontext_t
53 #endif
55 typedef enum {
56 ASAN_RT_VERSION_UNDEFINED = 0,
57 ASAN_RT_VERSION_DYNAMIC,
58 ASAN_RT_VERSION_STATIC,
59 } asan_rt_version_t;
61 // FIXME: perhaps also store abi version here?
62 extern "C" {
63 SANITIZER_INTERFACE_ATTRIBUTE
64 asan_rt_version_t __asan_rt_version;
67 namespace __asan {
69 void InitializePlatformInterceptors() {}
70 void InitializePlatformExceptionHandlers() {}
72 void *AsanDoesNotSupportStaticLinkage() {
73 // This will fail to link with -static.
74 return &_DYNAMIC; // defined in link.h
77 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
78 UNIMPLEMENTED();
81 #if SANITIZER_ANDROID
82 // FIXME: should we do anything for Android?
83 void AsanCheckDynamicRTPrereqs() {}
84 void AsanCheckIncompatibleRT() {}
85 #else
86 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
87 void *data) {
88 // Continue until the first dynamic library is found
89 if (!info->dlpi_name || info->dlpi_name[0] == 0)
90 return 0;
92 // Ignore vDSO
93 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
94 return 0;
96 *(const char **)data = info->dlpi_name;
97 return 1;
100 static bool IsDynamicRTName(const char *libname) {
101 return internal_strstr(libname, "libclang_rt.asan") ||
102 internal_strstr(libname, "libasan.so");
105 static void ReportIncompatibleRT() {
106 Report("Your application is linked against incompatible ASan runtimes.\n");
107 Die();
110 void AsanCheckDynamicRTPrereqs() {
111 if (!ASAN_DYNAMIC)
112 return;
114 // Ensure that dynamic RT is the first DSO in the list
115 const char *first_dso_name = nullptr;
116 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
117 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
118 Report("ASan runtime does not come first in initial library list; "
119 "you should either link runtime to your application or "
120 "manually preload it with LD_PRELOAD.\n");
121 Die();
125 void AsanCheckIncompatibleRT() {
126 if (ASAN_DYNAMIC) {
127 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
128 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
129 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
130 ReportIncompatibleRT();
132 } else {
133 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
134 // Ensure that dynamic runtime is not present. We should detect it
135 // as early as possible, otherwise ASan interceptors could bind to
136 // the functions in dynamic ASan runtime instead of the functions in
137 // system libraries, causing crashes later in ASan initialization.
138 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
139 char filename[128];
140 while (proc_maps.Next(nullptr, nullptr, nullptr, filename,
141 sizeof(filename), nullptr)) {
142 if (IsDynamicRTName(filename)) {
143 Report("Your application is linked against "
144 "incompatible ASan runtimes.\n");
145 Die();
148 __asan_rt_version = ASAN_RT_VERSION_STATIC;
149 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
150 ReportIncompatibleRT();
154 #endif // SANITIZER_ANDROID
156 #if !SANITIZER_ANDROID
157 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
158 ucontext_t *ucp = (ucontext_t*)context;
159 *stack = (uptr)ucp->uc_stack.ss_sp;
160 *ssize = ucp->uc_stack.ss_size;
162 #else
163 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
164 UNIMPLEMENTED();
166 #endif
168 void *AsanDlSymNext(const char *sym) {
169 return dlsym(RTLD_NEXT, sym);
172 } // namespace __asan
174 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX