]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_linux.cc
MFV r322232: 8426 mark immutable buffer arguments as such in abd.h
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_linux.cc
1 //===-- asan_linux.cc -----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
11 //
12 // Linux-specific details.
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX
17
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"
25
26 #include <sys/time.h>
27 #include <sys/resource.h>
28 #include <sys/mman.h>
29 #include <sys/syscall.h>
30 #include <sys/types.h>
31 #include <dlfcn.h>
32 #include <fcntl.h>
33 #include <pthread.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <unwind.h>
37
38 #if SANITIZER_FREEBSD
39 #include <sys/link_elf.h>
40 #endif
41
42 #if SANITIZER_ANDROID || SANITIZER_FREEBSD
43 #include <ucontext.h>
44 extern "C" void* _DYNAMIC;
45 #else
46 #include <sys/ucontext.h>
47 #include <link.h>
48 #endif
49
50 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
51 // 32-bit mode.
52 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
53   __FreeBSD_version <= 902001  // v9.2
54 #define ucontext_t xucontext_t
55 #endif
56
57 typedef enum {
58   ASAN_RT_VERSION_UNDEFINED = 0,
59   ASAN_RT_VERSION_DYNAMIC,
60   ASAN_RT_VERSION_STATIC,
61 } asan_rt_version_t;
62
63 // FIXME: perhaps also store abi version here?
64 extern "C" {
65 SANITIZER_INTERFACE_ATTRIBUTE
66 asan_rt_version_t  __asan_rt_version;
67 }
68
69 namespace __asan {
70
71 void InitializePlatformInterceptors() {}
72 void InitializePlatformExceptionHandlers() {}
73 bool IsSystemHeapAddress (uptr addr) { return false; }
74
75 void *AsanDoesNotSupportStaticLinkage() {
76   // This will fail to link with -static.
77   return &_DYNAMIC;  // defined in link.h
78 }
79
80 uptr FindDynamicShadowStart() {
81   UNREACHABLE("FindDynamicShadowStart is not available");
82   return 0;
83 }
84
85 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
86   UNIMPLEMENTED();
87 }
88
89 #if SANITIZER_ANDROID
90 // FIXME: should we do anything for Android?
91 void AsanCheckDynamicRTPrereqs() {}
92 void AsanCheckIncompatibleRT() {}
93 #else
94 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
95                                 void *data) {
96   // Continue until the first dynamic library is found
97   if (!info->dlpi_name || info->dlpi_name[0] == 0)
98     return 0;
99
100   // Ignore vDSO
101   if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
102     return 0;
103
104   *(const char **)data = info->dlpi_name;
105   return 1;
106 }
107
108 static bool IsDynamicRTName(const char *libname) {
109   return internal_strstr(libname, "libclang_rt.asan") ||
110     internal_strstr(libname, "libasan.so");
111 }
112
113 static void ReportIncompatibleRT() {
114   Report("Your application is linked against incompatible ASan runtimes.\n");
115   Die();
116 }
117
118 void AsanCheckDynamicRTPrereqs() {
119   if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
120     return;
121
122   // Ensure that dynamic RT is the first DSO in the list
123   const char *first_dso_name = nullptr;
124   dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
125   if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
126     Report("ASan runtime does not come first in initial library list; "
127            "you should either link runtime to your application or "
128            "manually preload it with LD_PRELOAD.\n");
129     Die();
130   }
131 }
132
133 void AsanCheckIncompatibleRT() {
134   if (ASAN_DYNAMIC) {
135     if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
136       __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
137     } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
138       ReportIncompatibleRT();
139     }
140   } else {
141     if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
142       // Ensure that dynamic runtime is not present. We should detect it
143       // as early as possible, otherwise ASan interceptors could bind to
144       // the functions in dynamic ASan runtime instead of the functions in
145       // system libraries, causing crashes later in ASan initialization.
146       MemoryMappingLayout proc_maps(/*cache_enabled*/true);
147       char filename[128];
148       MemoryMappedSegment segment(filename, sizeof(filename));
149       while (proc_maps.Next(&segment)) {
150         if (IsDynamicRTName(segment.filename)) {
151           Report("Your application is linked against "
152                  "incompatible ASan runtimes.\n");
153           Die();
154         }
155       }
156       __asan_rt_version = ASAN_RT_VERSION_STATIC;
157     } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
158       ReportIncompatibleRT();
159     }
160   }
161 }
162 #endif // SANITIZER_ANDROID
163
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;
169 }
170 #else
171 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
172   UNIMPLEMENTED();
173 }
174 #endif
175
176 void *AsanDlSymNext(const char *sym) {
177   return dlsym(RTLD_NEXT, sym);
178 }
179
180 } // namespace __asan
181
182 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX