2018-11-16 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libsanitizer / asan / asan_linux.cc
blobd92d0596b7cce2009504ba45f2562d7de7f85752
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 || SANITIZER_NETBSD || \
15 SANITIZER_SOLARIS
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"
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 <limits.h>
34 #include <pthread.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <unwind.h>
39 #if SANITIZER_FREEBSD
40 #include <sys/link_elf.h>
41 #endif
43 #if SANITIZER_SOLARIS
44 #include <link.h>
45 #endif
47 #if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS
48 #include <ucontext.h>
49 extern "C" void* _DYNAMIC;
50 #elif SANITIZER_NETBSD
51 #include <link_elf.h>
52 #include <ucontext.h>
53 extern Elf_Dyn _DYNAMIC;
54 #else
55 #include <sys/ucontext.h>
56 #include <link.h>
57 #endif
59 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
60 // 32-bit mode.
61 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
62 __FreeBSD_version <= 902001 // v9.2
63 #define ucontext_t xucontext_t
64 #endif
66 typedef enum {
67 ASAN_RT_VERSION_UNDEFINED = 0,
68 ASAN_RT_VERSION_DYNAMIC,
69 ASAN_RT_VERSION_STATIC,
70 } asan_rt_version_t;
72 // FIXME: perhaps also store abi version here?
73 extern "C" {
74 SANITIZER_INTERFACE_ATTRIBUTE
75 asan_rt_version_t __asan_rt_version;
78 namespace __asan {
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) {
90 CHECK(to >= from);
91 if (to == from) return;
92 uptr res = internal_munmap(reinterpret_cast<void *>(from), to - from);
93 if (UNLIKELY(internal_iserror(res))) {
94 Report(
95 "ERROR: AddresSanitizer failed to unmap 0x%zx (%zd) bytes at address "
96 "%p\n",
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);
110 return shadow_start;
112 #endif
114 uptr FindDynamicShadowStart() {
115 #if ASAN_PREMAP_SHADOW
116 if (!PremapShadowFailed())
117 return FindPremappedShadowStart();
118 #endif
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);
133 return shadow_start;
136 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
137 UNIMPLEMENTED();
140 #if SANITIZER_ANDROID
141 // FIXME: should we do anything for Android?
142 void AsanCheckDynamicRTPrereqs() {}
143 void AsanCheckIncompatibleRT() {}
144 #else
145 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
146 void *data) {
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)
152 return 0;
154 // Ignore vDSO
155 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
156 return 0;
158 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
159 // Ignore first entry (the main program)
160 char **p = (char **)data;
161 if (!(*p)) {
162 *p = (char *)-1;
163 return 0;
165 #endif
167 #if SANITIZER_SOLARIS
168 // Ignore executable on Solaris
169 if (info->dlpi_addr == 0)
170 return 0;
171 #endif
173 *(const char **)data = info->dlpi_name;
174 return 1;
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");
184 Die();
187 void AsanCheckDynamicRTPrereqs() {
188 if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
189 return;
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");
198 Die();
202 void AsanCheckIncompatibleRT() {
203 if (ASAN_DYNAMIC) {
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();
209 } else {
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");
222 Die();
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;
239 #else
240 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
241 UNIMPLEMENTED();
243 #endif
245 void *AsanDlSymNext(const char *sym) {
246 return dlsym(RTLD_NEXT, sym);
249 } // namespace __asan
251 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||
252 // SANITIZER_SOLARIS