xref: /freebsd/contrib/llvm-project/compiler-rt/lib/memprof/memprof_linux.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===-- memprof_linux.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of MemProfiler, a memory profiler.
10 //
11 // Linux-specific details.
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if !SANITIZER_LINUX
16 #error Unsupported OS
17 #endif
18 
19 #include "memprof_interceptors.h"
20 #include "memprof_internal.h"
21 #include "memprof_thread.h"
22 #include "sanitizer_common/sanitizer_flags.h"
23 #include "sanitizer_common/sanitizer_libc.h"
24 #include "sanitizer_common/sanitizer_procmaps.h"
25 
26 #include <dlfcn.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <link.h>
30 #include <pthread.h>
31 #include <stdio.h>
32 #include <sys/mman.h>
33 #include <sys/resource.h>
34 #include <sys/syscall.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/ucontext.h>
38 #include <unistd.h>
39 #include <unwind.h>
40 
41 extern ElfW(Dyn) _DYNAMIC[];
42 
43 typedef enum {
44   MEMPROF_RT_VERSION_UNDEFINED = 0,
45   MEMPROF_RT_VERSION_DYNAMIC,
46   MEMPROF_RT_VERSION_STATIC,
47 } memprof_rt_version_t;
48 
49 // FIXME: perhaps also store abi version here?
50 extern "C" {
51 SANITIZER_INTERFACE_ATTRIBUTE
52 memprof_rt_version_t __memprof_rt_version;
53 }
54 
55 namespace __memprof {
56 
57 void InitializePlatformInterceptors() {}
58 void InitializePlatformExceptionHandlers() {}
59 
60 void *MemprofDoesNotSupportStaticLinkage() {
61   // This will fail to link with -static.
62   return &_DYNAMIC;
63 }
64 
65 uptr FindDynamicShadowStart() {
66   uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
67   return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE,
68                           /*min_shadow_base_alignment*/ 0, kHighMemEnd);
69 }
70 
71 void *MemprofDlSymNext(const char *sym) { return dlsym(RTLD_NEXT, sym); }
72 
73 } // namespace __memprof
74