1 //===-- memprof_rtl.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 // Main file of the MemProf run-time library.
12 //===----------------------------------------------------------------------===//
13 
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_libc.h"
25 #include "sanitizer_common/sanitizer_symbolizer.h"
26 
27 #include <time.h>
28 
29 uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
30 
31 // Allow the user to specify a profile output file via the binary.
32 SANITIZER_WEAK_ATTRIBUTE char __memprof_profile_filename[1];
33 
34 namespace __memprof {
35 
36 static void MemprofDie() {
37   static atomic_uint32_t num_calls;
38   if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
39     // Don't die twice - run a busy loop.
40     while (1) {
41     }
42   }
43   if (common_flags()->print_module_map >= 1)
44     DumpProcessMap();
45   if (flags()->unmap_shadow_on_exit) {
46     if (kHighShadowEnd)
47       UnmapOrDie((void *)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
48   }
49 }
50 
51 static void CheckUnwind() {
52   GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check);
53   stack.Print();
54 }
55 
56 // -------------------------- Globals --------------------- {{{1
57 int memprof_inited;
58 int memprof_init_done;
59 bool memprof_init_is_running;
60 int memprof_timestamp_inited;
61 long memprof_init_timestamp_s;
62 
63 uptr kHighMemEnd;
64 
65 // -------------------------- Run-time entry ------------------- {{{1
66 // exported functions
67 
68 #define MEMPROF_MEMORY_ACCESS_CALLBACK_BODY() __memprof::RecordAccess(addr);
69 
70 #define MEMPROF_MEMORY_ACCESS_CALLBACK(type)                                   \
71   extern "C" NOINLINE INTERFACE_ATTRIBUTE void __memprof_##type(uptr addr) {   \
72     MEMPROF_MEMORY_ACCESS_CALLBACK_BODY()                                      \
73   }
74 
75 MEMPROF_MEMORY_ACCESS_CALLBACK(load)
76 MEMPROF_MEMORY_ACCESS_CALLBACK(store)
77 
78 // Force the linker to keep the symbols for various MemProf interface
79 // functions. We want to keep those in the executable in order to let the
80 // instrumented dynamic libraries access the symbol even if it is not used by
81 // the executable itself. This should help if the build system is removing dead
82 // code at link time.
83 static NOINLINE void force_interface_symbols() {
84   volatile int fake_condition = 0; // prevent dead condition elimination.
85   // clang-format off
86   switch (fake_condition) {
87     case 1: __memprof_record_access(nullptr); break;
88     case 2: __memprof_record_access_range(nullptr, 0); break;
89   }
90   // clang-format on
91 }
92 
93 static void memprof_atexit() {
94   Printf("MemProfiler exit stats:\n");
95   __memprof_print_accumulated_stats();
96 }
97 
98 static void InitializeHighMemEnd() {
99   kHighMemEnd = GetMaxUserVirtualAddress();
100   // Increase kHighMemEnd to make sure it's properly
101   // aligned together with kHighMemBeg:
102   kHighMemEnd |= (GetMmapGranularity() << SHADOW_SCALE) - 1;
103 }
104 
105 void PrintAddressSpaceLayout() {
106   if (kHighMemBeg) {
107     Printf("|| `[%p, %p]` || HighMem    ||\n", (void *)kHighMemBeg,
108            (void *)kHighMemEnd);
109     Printf("|| `[%p, %p]` || HighShadow ||\n", (void *)kHighShadowBeg,
110            (void *)kHighShadowEnd);
111   }
112   Printf("|| `[%p, %p]` || ShadowGap  ||\n", (void *)kShadowGapBeg,
113          (void *)kShadowGapEnd);
114   if (kLowShadowBeg) {
115     Printf("|| `[%p, %p]` || LowShadow  ||\n", (void *)kLowShadowBeg,
116            (void *)kLowShadowEnd);
117     Printf("|| `[%p, %p]` || LowMem     ||\n", (void *)kLowMemBeg,
118            (void *)kLowMemEnd);
119   }
120   Printf("MemToShadow(shadow): %p %p", (void *)MEM_TO_SHADOW(kLowShadowBeg),
121          (void *)MEM_TO_SHADOW(kLowShadowEnd));
122   if (kHighMemBeg) {
123     Printf(" %p %p", (void *)MEM_TO_SHADOW(kHighShadowBeg),
124            (void *)MEM_TO_SHADOW(kHighShadowEnd));
125   }
126   Printf("\n");
127   Printf("malloc_context_size=%zu\n",
128          (uptr)common_flags()->malloc_context_size);
129 
130   Printf("SHADOW_SCALE: %d\n", (int)SHADOW_SCALE);
131   Printf("SHADOW_GRANULARITY: %d\n", (int)SHADOW_GRANULARITY);
132   Printf("SHADOW_OFFSET: 0x%zx\n", (uptr)SHADOW_OFFSET);
133   CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
134 }
135 
136 static bool UNUSED __local_memprof_dyninit = [] {
137   MaybeStartBackgroudThread();
138   return false;
139 }();
140 
141 static void MemprofInitInternal() {
142   if (LIKELY(memprof_inited))
143     return;
144   SanitizerToolName = "MemProfiler";
145   CHECK(!memprof_init_is_running && "MemProf init calls itself!");
146   memprof_init_is_running = true;
147 
148   CacheBinaryName();
149 
150   // Initialize flags. This must be done early, because most of the
151   // initialization steps look at flags().
152   InitializeFlags();
153 
154   AvoidCVE_2016_2143();
155 
156   SetMallocContextSize(common_flags()->malloc_context_size);
157 
158   InitializeHighMemEnd();
159 
160   // Make sure we are not statically linked.
161   MemprofDoesNotSupportStaticLinkage();
162 
163   // Install tool-specific callbacks in sanitizer_common.
164   AddDieCallback(MemprofDie);
165   SetCheckUnwindCallback(CheckUnwind);
166 
167   // Use profile name specified via the binary itself if it exists, and hasn't
168   // been overrriden by a flag at runtime.
169   if (__memprof_profile_filename[0] != 0 && !common_flags()->log_path)
170     __sanitizer_set_report_path(__memprof_profile_filename);
171   else
172     __sanitizer_set_report_path(common_flags()->log_path);
173 
174   __sanitizer::InitializePlatformEarly();
175 
176   // Re-exec ourselves if we need to set additional env or command line args.
177   MaybeReexec();
178 
179   // Setup internal allocator callback.
180   SetLowLevelAllocateMinAlignment(SHADOW_GRANULARITY);
181 
182   InitializeMemprofInterceptors();
183   CheckASLR();
184 
185   ReplaceSystemMalloc();
186 
187   DisableCoreDumperIfNecessary();
188 
189   InitializeShadowMemory();
190 
191   TSDInit(PlatformTSDDtor);
192 
193   InitializeAllocator();
194 
195   // On Linux MemprofThread::ThreadStart() calls malloc() that's why
196   // memprof_inited should be set to 1 prior to initializing the threads.
197   memprof_inited = 1;
198   memprof_init_is_running = false;
199 
200   if (flags()->atexit)
201     Atexit(memprof_atexit);
202 
203   InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
204 
205   // interceptors
206   InitTlsSize();
207 
208   // Create main thread.
209   MemprofThread *main_thread = CreateMainThread();
210   CHECK_EQ(0, main_thread->tid());
211   force_interface_symbols(); // no-op.
212   SanitizerInitializeUnwinder();
213 
214   Symbolizer::LateInitialize();
215 
216   VReport(1, "MemProfiler Init done\n");
217 
218   memprof_init_done = 1;
219 }
220 
221 void MemprofInitTime() {
222   if (LIKELY(memprof_timestamp_inited))
223     return;
224   timespec ts;
225   clock_gettime(CLOCK_REALTIME, &ts);
226   memprof_init_timestamp_s = ts.tv_sec;
227   memprof_timestamp_inited = 1;
228 }
229 
230 // Initialize as requested from some part of MemProf runtime library
231 // (interceptors, allocator, etc).
232 void MemprofInitFromRtl() { MemprofInitInternal(); }
233 
234 #if MEMPROF_DYNAMIC
235 // Initialize runtime in case it's LD_PRELOAD-ed into uninstrumented executable
236 // (and thus normal initializers from .preinit_array or modules haven't run).
237 
238 class MemprofInitializer {
239 public:
240   MemprofInitializer() { MemprofInitFromRtl(); }
241 };
242 
243 static MemprofInitializer memprof_initializer;
244 #endif // MEMPROF_DYNAMIC
245 
246 } // namespace __memprof
247 
248 // ---------------------- Interface ---------------- {{{1
249 using namespace __memprof;
250 
251 // Initialize as requested from instrumented application code.
252 void __memprof_init() {
253   MemprofInitTime();
254   MemprofInitInternal();
255 }
256 
257 void __memprof_preinit() { MemprofInitInternal(); }
258 
259 void __memprof_version_mismatch_check_v1() {}
260 
261 void __memprof_record_access(void const volatile *addr) {
262   __memprof::RecordAccess((uptr)addr);
263 }
264 
265 void __memprof_record_access_range(void const volatile *addr, uptr size) {
266   for (uptr a = (uptr)addr; a < (uptr)addr + size; a += kWordSize)
267     __memprof::RecordAccess(a);
268 }
269 
270 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u16
271 __sanitizer_unaligned_load16(const uu16 *p) {
272   __memprof_record_access(p);
273   return *p;
274 }
275 
276 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u32
277 __sanitizer_unaligned_load32(const uu32 *p) {
278   __memprof_record_access(p);
279   return *p;
280 }
281 
282 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u64
283 __sanitizer_unaligned_load64(const uu64 *p) {
284   __memprof_record_access(p);
285   return *p;
286 }
287 
288 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
289 __sanitizer_unaligned_store16(uu16 *p, u16 x) {
290   __memprof_record_access(p);
291   *p = x;
292 }
293 
294 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
295 __sanitizer_unaligned_store32(uu32 *p, u32 x) {
296   __memprof_record_access(p);
297   *p = x;
298 }
299 
300 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
301 __sanitizer_unaligned_store64(uu64 *p, u64 x) {
302   __memprof_record_access(p);
303   *p = x;
304 }
305