* gcc-interface/trans.c (Subprogram_Body_to_gnu): Initialize locus.
[official-gcc.git] / libsanitizer / asan / asan_linux.cc
bloba43243e3b4302cf22fcc2dadfdd920342e80a092
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
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 #elif SANITIZER_NETBSD
44 #include <link_elf.h>
45 #include <ucontext.h>
46 extern Elf_Dyn _DYNAMIC;
47 #else
48 #include <sys/ucontext.h>
49 #include <link.h>
50 #endif
52 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
53 // 32-bit mode.
54 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
55 __FreeBSD_version <= 902001 // v9.2
56 #define ucontext_t xucontext_t
57 #endif
59 typedef enum {
60 ASAN_RT_VERSION_UNDEFINED = 0,
61 ASAN_RT_VERSION_DYNAMIC,
62 ASAN_RT_VERSION_STATIC,
63 } asan_rt_version_t;
65 // FIXME: perhaps also store abi version here?
66 extern "C" {
67 SANITIZER_INTERFACE_ATTRIBUTE
68 asan_rt_version_t __asan_rt_version;
71 namespace __asan {
73 void InitializePlatformInterceptors() {}
74 void InitializePlatformExceptionHandlers() {}
75 bool IsSystemHeapAddress (uptr addr) { return false; }
77 void *AsanDoesNotSupportStaticLinkage() {
78 // This will fail to link with -static.
79 return &_DYNAMIC; // defined in link.h
82 uptr FindDynamicShadowStart() {
83 UNREACHABLE("FindDynamicShadowStart is not available");
84 return 0;
87 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
88 UNIMPLEMENTED();
91 #if SANITIZER_ANDROID
92 // FIXME: should we do anything for Android?
93 void AsanCheckDynamicRTPrereqs() {}
94 void AsanCheckIncompatibleRT() {}
95 #else
96 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
97 void *data) {
98 // Continue until the first dynamic library is found
99 if (!info->dlpi_name || info->dlpi_name[0] == 0)
100 return 0;
102 // Ignore vDSO
103 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
104 return 0;
106 #if SANITIZER_NETBSD
107 // Ignore first entry (the main program)
108 char **p = (char **)data;
109 if (!(*p)) {
110 *p = (char *)-1;
111 return 0;
113 #endif
115 *(const char **)data = info->dlpi_name;
116 return 1;
119 static bool IsDynamicRTName(const char *libname) {
120 return internal_strstr(libname, "libclang_rt.asan") ||
121 internal_strstr(libname, "libasan.so");
124 static void ReportIncompatibleRT() {
125 Report("Your application is linked against incompatible ASan runtimes.\n");
126 Die();
129 void AsanCheckDynamicRTPrereqs() {
130 if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
131 return;
133 // Ensure that dynamic RT is the first DSO in the list
134 const char *first_dso_name = nullptr;
135 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
136 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
137 Report("ASan runtime does not come first in initial library list; "
138 "you should either link runtime to your application or "
139 "manually preload it with LD_PRELOAD.\n");
140 Die();
144 void AsanCheckIncompatibleRT() {
145 if (ASAN_DYNAMIC) {
146 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
147 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
148 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
149 ReportIncompatibleRT();
151 } else {
152 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
153 // Ensure that dynamic runtime is not present. We should detect it
154 // as early as possible, otherwise ASan interceptors could bind to
155 // the functions in dynamic ASan runtime instead of the functions in
156 // system libraries, causing crashes later in ASan initialization.
157 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
158 char filename[128];
159 MemoryMappedSegment segment(filename, sizeof(filename));
160 while (proc_maps.Next(&segment)) {
161 if (IsDynamicRTName(segment.filename)) {
162 Report("Your application is linked against "
163 "incompatible ASan runtimes.\n");
164 Die();
167 __asan_rt_version = ASAN_RT_VERSION_STATIC;
168 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
169 ReportIncompatibleRT();
173 #endif // SANITIZER_ANDROID
175 #if !SANITIZER_ANDROID
176 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
177 ucontext_t *ucp = (ucontext_t*)context;
178 *stack = (uptr)ucp->uc_stack.ss_sp;
179 *ssize = ucp->uc_stack.ss_size;
181 #else
182 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
183 UNIMPLEMENTED();
185 #endif
187 void *AsanDlSymNext(const char *sym) {
188 return dlsym(RTLD_NEXT, sym);
191 } // namespace __asan
193 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD