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_freebsd.h"
24 #include "sanitizer_common/sanitizer_libc.h"
25 #include "sanitizer_common/sanitizer_procmaps.h"
26 
27 #include <dlfcn.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <link.h>
31 #include <pthread.h>
32 #include <stdio.h>
33 #include <sys/mman.h>
34 #include <sys/resource.h>
35 #include <sys/syscall.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <sys/ucontext.h>
39 #include <unistd.h>
40 #include <unwind.h>
41 
42 typedef enum {
43   MEMPROF_RT_VERSION_UNDEFINED = 0,
44   MEMPROF_RT_VERSION_DYNAMIC,
45   MEMPROF_RT_VERSION_STATIC,
46 } memprof_rt_version_t;
47 
48 // FIXME: perhaps also store abi version here?
49 extern "C" {
50 SANITIZER_INTERFACE_ATTRIBUTE
51 memprof_rt_version_t __memprof_rt_version;
52 }
53 
54 namespace __memprof {
55 
InitializePlatformInterceptors()56 void InitializePlatformInterceptors() {}
InitializePlatformExceptionHandlers()57 void InitializePlatformExceptionHandlers() {}
58 
MemprofDoesNotSupportStaticLinkage()59 void *MemprofDoesNotSupportStaticLinkage() {
60   // This will fail to link with -static.
61   return &_DYNAMIC; // defined in link.h
62 }
63 
FindDynamicShadowStart()64 uptr FindDynamicShadowStart() {
65   uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
66   return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE,
67                           /*min_shadow_base_alignment*/ 0, kHighMemEnd);
68 }
69 
ReadContextStack(void * context,uptr * stack,uptr * ssize)70 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
71   ucontext_t *ucp = (ucontext_t *)context;
72   *stack = (uptr)ucp->uc_stack.ss_sp;
73   *ssize = ucp->uc_stack.ss_size;
74 }
75 
MemprofDlSymNext(const char * sym)76 void *MemprofDlSymNext(const char *sym) { return dlsym(RTLD_NEXT, sym); }
77 
78 } // namespace __memprof
79