]> git.proxmox.com Git - rustc.git/blame - src/compiler-rt/lib/asan/asan_linux.cc
New upstream version 1.12.0+dfsg1
[rustc.git] / src / compiler-rt / lib / asan / asan_linux.cc
CommitLineData
1a4d82fc
JJ
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"
92a42be0 22#include "sanitizer_common/sanitizer_freebsd.h"
1a4d82fc
JJ
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>
92a42be0 31#include <dlfcn.h>
1a4d82fc
JJ
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>
44extern "C" void* _DYNAMIC;
45#else
46#include <sys/ucontext.h>
1a4d82fc
JJ
47#include <link.h>
48#endif
49
92a42be0
SL
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
1a4d82fc
JJ
55#endif
56
57typedef 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?
64extern "C" {
65SANITIZER_INTERFACE_ATTRIBUTE
66asan_rt_version_t __asan_rt_version;
67}
68
69namespace __asan {
70
92a42be0 71void InitializePlatformInterceptors() {}
5bcae85e 72void InitializePlatformExceptionHandlers() {}
92a42be0 73
1a4d82fc
JJ
74void *AsanDoesNotSupportStaticLinkage() {
75 // This will fail to link with -static.
76 return &_DYNAMIC; // defined in link.h
77}
78
5bcae85e
SL
79void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
80 UNIMPLEMENTED();
81}
82
1a4d82fc
JJ
83#if SANITIZER_ANDROID
84// FIXME: should we do anything for Android?
85void AsanCheckDynamicRTPrereqs() {}
86void AsanCheckIncompatibleRT() {}
87#else
88static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
89 void *data) {
90 // Continue until the first dynamic library is found
91 if (!info->dlpi_name || info->dlpi_name[0] == 0)
92 return 0;
93
92a42be0
SL
94 // Ignore vDSO
95 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
96 return 0;
97
1a4d82fc
JJ
98 *(const char **)data = info->dlpi_name;
99 return 1;
100}
101
102static bool IsDynamicRTName(const char *libname) {
103 return internal_strstr(libname, "libclang_rt.asan") ||
104 internal_strstr(libname, "libasan.so");
105}
106
107static void ReportIncompatibleRT() {
108 Report("Your application is linked against incompatible ASan runtimes.\n");
109 Die();
110}
111
112void AsanCheckDynamicRTPrereqs() {
92a42be0
SL
113 if (!ASAN_DYNAMIC)
114 return;
115
1a4d82fc 116 // Ensure that dynamic RT is the first DSO in the list
92a42be0 117 const char *first_dso_name = nullptr;
1a4d82fc
JJ
118 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
119 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
120 Report("ASan runtime does not come first in initial library list; "
121 "you should either link runtime to your application or "
122 "manually preload it with LD_PRELOAD.\n");
123 Die();
124 }
125}
126
127void AsanCheckIncompatibleRT() {
128 if (ASAN_DYNAMIC) {
129 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
130 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
131 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
132 ReportIncompatibleRT();
133 }
134 } else {
135 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
136 // Ensure that dynamic runtime is not present. We should detect it
137 // as early as possible, otherwise ASan interceptors could bind to
138 // the functions in dynamic ASan runtime instead of the functions in
139 // system libraries, causing crashes later in ASan initialization.
140 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
141 char filename[128];
92a42be0
SL
142 while (proc_maps.Next(nullptr, nullptr, nullptr, filename,
143 sizeof(filename), nullptr)) {
1a4d82fc
JJ
144 if (IsDynamicRTName(filename)) {
145 Report("Your application is linked against "
146 "incompatible ASan runtimes.\n");
147 Die();
148 }
149 }
150 __asan_rt_version = ASAN_RT_VERSION_STATIC;
151 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
152 ReportIncompatibleRT();
153 }
154 }
155}
92a42be0 156#endif // SANITIZER_ANDROID
1a4d82fc
JJ
157
158#if !SANITIZER_ANDROID
159void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
160 ucontext_t *ucp = (ucontext_t*)context;
161 *stack = (uptr)ucp->uc_stack.ss_sp;
162 *ssize = ucp->uc_stack.ss_size;
163}
164#else
165void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
166 UNIMPLEMENTED();
167}
168#endif
169
92a42be0
SL
170void *AsanDlSymNext(const char *sym) {
171 return dlsym(RTLD_NEXT, sym);
172}
173
174} // namespace __asan
1a4d82fc 175
92a42be0 176#endif // SANITIZER_FREEBSD || SANITIZER_LINUX