1 //===-- memprof_rtl.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of MemProfiler, a memory profiler.
11 // Main file of the MemProf run-time library.
12 //===----------------------------------------------------------------------===//
14 #include "memprof_allocator.h"
15 #include "memprof_interceptors.h"
16 #include "memprof_interface_internal.h"
17 #include "memprof_internal.h"
18 #include "memprof_mapping.h"
19 #include "memprof_stack.h"
20 #include "memprof_stats.h"
21 #include "memprof_thread.h"
22 #include "sanitizer_common/sanitizer_atomic.h"
23 #include "sanitizer_common/sanitizer_flags.h"
24 #include "sanitizer_common/sanitizer_interface_internal.h"
25 #include "sanitizer_common/sanitizer_libc.h"
26 #include "sanitizer_common/sanitizer_symbolizer.h"
30 uptr __memprof_shadow_memory_dynamic_address
; // Global interface symbol.
32 // Allow the user to specify a profile output file via the binary.
33 SANITIZER_WEAK_ATTRIBUTE
char __memprof_profile_filename
[1];
37 static void MemprofDie() {
38 static atomic_uint32_t num_calls
;
39 if (atomic_fetch_add(&num_calls
, 1, memory_order_relaxed
) != 0) {
40 // Don't die twice - run a busy loop.
42 internal_sched_yield();
45 if (common_flags()->print_module_map
>= 1)
47 if (flags()->unmap_shadow_on_exit
) {
49 UnmapOrDie((void *)kLowShadowBeg
, kHighShadowEnd
- kLowShadowBeg
);
53 static void MemprofOnDeadlySignal(int signo
, void *siginfo
, void *context
) {
54 // We call StartReportDeadlySignal not HandleDeadlySignal so we get the
55 // deadly signal message to stderr but no writing to the profile output file
56 StartReportDeadlySignal();
57 __memprof_profile_dump();
61 static void CheckUnwind() {
62 GET_STACK_TRACE(kStackTraceMax
, common_flags()->fast_unwind_on_check
);
66 // -------------------------- Globals --------------------- {{{1
68 int memprof_init_done
;
69 bool memprof_init_is_running
;
70 int memprof_timestamp_inited
;
71 long memprof_init_timestamp_s
;
75 // -------------------------- Run-time entry ------------------- {{{1
78 #define MEMPROF_MEMORY_ACCESS_CALLBACK_BODY() __memprof::RecordAccess(addr);
80 #define MEMPROF_MEMORY_ACCESS_CALLBACK(type) \
81 extern "C" NOINLINE INTERFACE_ATTRIBUTE void __memprof_##type(uptr addr) { \
82 MEMPROF_MEMORY_ACCESS_CALLBACK_BODY() \
85 MEMPROF_MEMORY_ACCESS_CALLBACK(load
)
86 MEMPROF_MEMORY_ACCESS_CALLBACK(store
)
88 // Force the linker to keep the symbols for various MemProf interface
89 // functions. We want to keep those in the executable in order to let the
90 // instrumented dynamic libraries access the symbol even if it is not used by
91 // the executable itself. This should help if the build system is removing dead
93 static NOINLINE
void force_interface_symbols() {
94 volatile int fake_condition
= 0; // prevent dead condition elimination.
96 switch (fake_condition
) {
97 case 1: __memprof_record_access(nullptr); break;
98 case 2: __memprof_record_access_range(nullptr, 0); break;
103 static void memprof_atexit() {
104 Printf("MemProfiler exit stats:\n");
105 __memprof_print_accumulated_stats();
108 static void InitializeHighMemEnd() {
109 kHighMemEnd
= GetMaxUserVirtualAddress();
110 // Increase kHighMemEnd to make sure it's properly
111 // aligned together with kHighMemBeg:
112 kHighMemEnd
|= (GetMmapGranularity() << SHADOW_SCALE
) - 1;
115 void PrintAddressSpaceLayout() {
117 Printf("|| `[%p, %p]` || HighMem ||\n", (void *)kHighMemBeg
,
118 (void *)kHighMemEnd
);
119 Printf("|| `[%p, %p]` || HighShadow ||\n", (void *)kHighShadowBeg
,
120 (void *)kHighShadowEnd
);
122 Printf("|| `[%p, %p]` || ShadowGap ||\n", (void *)kShadowGapBeg
,
123 (void *)kShadowGapEnd
);
125 Printf("|| `[%p, %p]` || LowShadow ||\n", (void *)kLowShadowBeg
,
126 (void *)kLowShadowEnd
);
127 Printf("|| `[%p, %p]` || LowMem ||\n", (void *)kLowMemBeg
,
130 Printf("MemToShadow(shadow): %p %p", (void *)MEM_TO_SHADOW(kLowShadowBeg
),
131 (void *)MEM_TO_SHADOW(kLowShadowEnd
));
133 Printf(" %p %p", (void *)MEM_TO_SHADOW(kHighShadowBeg
),
134 (void *)MEM_TO_SHADOW(kHighShadowEnd
));
137 Printf("malloc_context_size=%zu\n",
138 (uptr
)common_flags()->malloc_context_size
);
140 Printf("SHADOW_SCALE: %d\n", (int)SHADOW_SCALE
);
141 Printf("SHADOW_GRANULARITY: %d\n", (int)SHADOW_GRANULARITY
);
142 Printf("SHADOW_OFFSET: 0x%zx\n", (uptr
)SHADOW_OFFSET
);
143 CHECK(SHADOW_SCALE
>= 3 && SHADOW_SCALE
<= 7);
146 static void MemprofInitInternal() {
147 if (LIKELY(memprof_inited
))
149 SanitizerToolName
= "MemProfiler";
150 CHECK(!memprof_init_is_running
&& "MemProf init calls itself!");
151 memprof_init_is_running
= true;
155 // Initialize flags. This must be done early, because most of the
156 // initialization steps look at flags().
159 AvoidCVE_2016_2143();
161 SetMallocContextSize(common_flags()->malloc_context_size
);
163 InitializeHighMemEnd();
165 // Make sure we are not statically linked.
166 MemprofDoesNotSupportStaticLinkage();
168 // Install tool-specific callbacks in sanitizer_common.
169 AddDieCallback(MemprofDie
);
170 SetCheckUnwindCallback(CheckUnwind
);
172 // Use profile name specified via the binary itself if it exists, and hasn't
173 // been overrriden by a flag at runtime.
174 if (__memprof_profile_filename
[0] != 0 && !common_flags()->log_path
)
175 __sanitizer_set_report_path(__memprof_profile_filename
);
177 __sanitizer_set_report_path(common_flags()->log_path
);
179 __sanitizer::InitializePlatformEarly();
181 // Setup internal allocator callback.
182 SetLowLevelAllocateMinAlignment(SHADOW_GRANULARITY
);
184 InitializeMemprofInterceptors();
187 ReplaceSystemMalloc();
189 DisableCoreDumperIfNecessary();
191 InitializeShadowMemory();
193 TSDInit(PlatformTSDDtor
);
194 InstallDeadlySignalHandlers(MemprofOnDeadlySignal
);
196 InitializeAllocator();
198 // On Linux MemprofThread::ThreadStart() calls malloc() that's why
199 // memprof_inited should be set to 1 prior to initializing the threads.
201 memprof_init_is_running
= false;
204 Atexit(memprof_atexit
);
206 InitializeCoverage(common_flags()->coverage
, common_flags()->coverage_dir
);
211 // Create main thread.
212 MemprofThread
*main_thread
= CreateMainThread();
213 CHECK_EQ(0, main_thread
->tid());
214 force_interface_symbols(); // no-op.
215 SanitizerInitializeUnwinder();
217 Symbolizer::LateInitialize();
219 VReport(1, "MemProfiler Init done\n");
221 memprof_init_done
= 1;
224 void MemprofInitTime() {
225 if (LIKELY(memprof_timestamp_inited
))
228 clock_gettime(CLOCK_REALTIME
, &ts
);
229 memprof_init_timestamp_s
= ts
.tv_sec
;
230 memprof_timestamp_inited
= 1;
233 // Initialize as requested from some part of MemProf runtime library
234 // (interceptors, allocator, etc).
235 void MemprofInitFromRtl() { MemprofInitInternal(); }
238 // Initialize runtime in case it's LD_PRELOAD-ed into uninstrumented executable
239 // (and thus normal initializers from .preinit_array or modules haven't run).
241 class MemprofInitializer
{
243 MemprofInitializer() { MemprofInitFromRtl(); }
246 static MemprofInitializer memprof_initializer
;
247 #endif // MEMPROF_DYNAMIC
249 } // namespace __memprof
251 // ---------------------- Interface ---------------- {{{1
252 using namespace __memprof
;
254 // Initialize as requested from instrumented application code.
255 void __memprof_init() {
257 MemprofInitInternal();
260 void __memprof_preinit() { MemprofInitInternal(); }
262 void __memprof_version_mismatch_check_v1() {}
264 void __memprof_record_access(void const volatile *addr
) {
265 __memprof::RecordAccess((uptr
)addr
);
268 void __memprof_record_access_range(void const volatile *addr
, uptr size
) {
269 for (uptr a
= (uptr
)addr
; a
< (uptr
)addr
+ size
; a
+= kWordSize
)
270 __memprof::RecordAccess(a
);
273 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u16
274 __sanitizer_unaligned_load16(const uu16
*p
) {
275 __memprof_record_access(p
);
279 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u32
280 __sanitizer_unaligned_load32(const uu32
*p
) {
281 __memprof_record_access(p
);
285 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u64
286 __sanitizer_unaligned_load64(const uu64
*p
) {
287 __memprof_record_access(p
);
291 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
void
292 __sanitizer_unaligned_store16(uu16
*p
, u16 x
) {
293 __memprof_record_access(p
);
297 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
void
298 __sanitizer_unaligned_store32(uu32
*p
, u32 x
) {
299 __memprof_record_access(p
);
303 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
void
304 __sanitizer_unaligned_store64(uu64
*p
, u64 x
) {
305 __memprof_record_access(p
);