blob: 9580fc7c06d4483e25323a27ddf4750d2f4a4b79 [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- 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//===----------------------------------------------------------------------===//
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000014
15#include "sanitizer_common/sanitizer_platform.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070016#if SANITIZER_FREEBSD || SANITIZER_LINUX
Kostya Serebryany1e172b42011-11-30 01:07:02 +000017
Kostya Serebryanydf499b42012-01-05 00:44:33 +000018#include "asan_interceptors.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000019#include "asan_internal.h"
Kostya Serebryanyc549dd72012-01-05 01:07:27 +000020#include "asan_thread.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070021#include "sanitizer_common/sanitizer_flags.h"
Stephen Hines6a211c52014-07-21 00:49:56 -070022#include "sanitizer_common/sanitizer_freebsd.h"
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000023#include "sanitizer_common/sanitizer_libc.h"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000024#include "sanitizer_common/sanitizer_procmaps.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025
Kostya Serebryanyc549dd72012-01-05 01:07:27 +000026#include <sys/time.h>
27#include <sys/resource.h>
Kostya Serebryany1e172b42011-11-30 01:07:02 +000028#include <sys/mman.h>
29#include <sys/syscall.h>
Kostya Serebryanyde496f42011-12-28 22:58:01 +000030#include <sys/types.h>
Stephen Hines6a211c52014-07-21 00:49:56 -070031#include <dlfcn.h>
Kostya Serebryanyde496f42011-12-28 22:58:01 +000032#include <fcntl.h>
Kostya Serebryanyc549dd72012-01-05 01:07:27 +000033#include <pthread.h>
Kostya Serebryanydf499b42012-01-05 00:44:33 +000034#include <stdio.h>
Kostya Serebryany1e172b42011-11-30 01:07:02 +000035#include <unistd.h>
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +000036#include <unwind.h>
Kostya Serebryany1e172b42011-11-30 01:07:02 +000037
Stephen Hines2d1fdb22014-05-28 23:58:16 -070038#if SANITIZER_FREEBSD
39#include <sys/link_elf.h>
Kostya Serebryany9107c262012-01-06 19:11:09 +000040#endif
41
Stephen Hines2d1fdb22014-05-28 23:58:16 -070042#if SANITIZER_ANDROID || SANITIZER_FREEBSD
43#include <ucontext.h>
Evgeniy Stepanovaa33a502012-03-26 09:48:41 +000044extern "C" void* _DYNAMIC;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070045#else
46#include <sys/ucontext.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070047#include <link.h>
48#endif
49
Stephen Hines6a211c52014-07-21 00:49:56 -070050// 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
Stephen Hines2d1fdb22014-05-28 23:58:16 -070055#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}
Evgeniy Stepanovaa33a502012-03-26 09:48:41 +000068
Kostya Serebryany1e172b42011-11-30 01:07:02 +000069namespace __asan {
70
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070071void InitializePlatformInterceptors() {}
72
Stephen Hines86277eb2015-03-23 12:06:32 -070073void DisableReexec() {
74 // No need to re-exec on Linux.
75}
76
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +000077void MaybeReexec() {
78 // No need to re-exec on Linux.
79}
80
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081void *AsanDoesNotSupportStaticLinkage() {
82 // This will fail to link with -static.
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000083 return &_DYNAMIC; // defined in link.h
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084}
85
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +000086#if SANITIZER_ANDROID
Stephen Hines2d1fdb22014-05-28 23:58:16 -070087// FIXME: should we do anything for Android?
88void AsanCheckDynamicRTPrereqs() {}
89void AsanCheckIncompatibleRT() {}
90#else
91static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
92 void *data) {
93 // Continue until the first dynamic library is found
94 if (!info->dlpi_name || info->dlpi_name[0] == 0)
95 return 0;
96
Stephen Hines6a211c52014-07-21 00:49:56 -070097 // Ignore vDSO
98 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
99 return 0;
100
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700101 *(const char **)data = info->dlpi_name;
102 return 1;
103}
104
105static bool IsDynamicRTName(const char *libname) {
106 return internal_strstr(libname, "libclang_rt.asan") ||
107 internal_strstr(libname, "libasan.so");
108}
109
110static void ReportIncompatibleRT() {
111 Report("Your application is linked against incompatible ASan runtimes.\n");
112 Die();
113}
114
115void AsanCheckDynamicRTPrereqs() {
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700116 if (!ASAN_DYNAMIC)
117 return;
118
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700119 // Ensure that dynamic RT is the first DSO in the list
120 const char *first_dso_name = 0;
121 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
122 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
123 Report("ASan runtime does not come first in initial library list; "
124 "you should either link runtime to your application or "
125 "manually preload it with LD_PRELOAD.\n");
126 Die();
127 }
128}
129
130void AsanCheckIncompatibleRT() {
131 if (ASAN_DYNAMIC) {
132 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
133 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
134 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
135 ReportIncompatibleRT();
136 }
137 } else {
138 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
139 // Ensure that dynamic runtime is not present. We should detect it
140 // as early as possible, otherwise ASan interceptors could bind to
141 // the functions in dynamic ASan runtime instead of the functions in
142 // system libraries, causing crashes later in ASan initialization.
143 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
144 char filename[128];
145 while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) {
146 if (IsDynamicRTName(filename)) {
147 Report("Your application is linked against "
148 "incompatible ASan runtimes.\n");
149 Die();
150 }
151 }
152 __asan_rt_version = ASAN_RT_VERSION_STATIC;
153 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
154 ReportIncompatibleRT();
155 }
156 }
157}
158#endif // SANITIZER_ANDROID
159
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000160void AsanPlatformThreadInit() {
161 // Nothing here for now.
162}
163
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +0000164#if !SANITIZER_ANDROID
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000165void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000166 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000167 *stack = (uptr)ucp->uc_stack.ss_sp;
168 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000169}
170#else
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000171void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovca2849c2013-01-18 09:20:06 +0000172 UNIMPLEMENTED();
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000173}
174#endif
175
Stephen Hines6a211c52014-07-21 00:49:56 -0700176void *AsanDlSymNext(const char *sym) {
177 return dlsym(RTLD_NEXT, sym);
178}
179
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000180} // namespace __asan
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000181
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700182#endif // SANITIZER_FREEBSD || SANITIZER_LINUX