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