1 //===-- memprof_shadow_setup.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 // Set up the shadow memory.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
16 #include "memprof_internal.h"
17 #include "memprof_mapping.h"
21 static void ProtectGap(uptr addr
, uptr size
) {
22 if (!flags()->protect_shadow_gap
) {
23 // The shadow gap is unprotected, so there is a chance that someone
24 // is actually using this memory. Which means it needs a shadow...
25 uptr GapShadowBeg
= RoundDownTo(MEM_TO_SHADOW(addr
), GetPageSizeCached());
27 RoundUpTo(MEM_TO_SHADOW(addr
+ size
), GetPageSizeCached()) - 1;
29 Printf("protect_shadow_gap=0:"
30 " not protecting shadow gap, allocating gap's shadow\n"
31 "|| `[%p, %p]` || ShadowGap's shadow ||\n",
32 GapShadowBeg
, GapShadowEnd
);
33 ReserveShadowMemoryRange(GapShadowBeg
, GapShadowEnd
,
34 "unprotected gap shadow");
37 __sanitizer::ProtectGap(addr
, size
, kZeroBaseShadowStart
,
38 kZeroBaseMaxShadowStart
);
41 void InitializeShadowMemory() {
42 uptr shadow_start
= FindDynamicShadowStart();
43 // Update the shadow memory address (potentially) used by instrumentation.
44 __memprof_shadow_memory_dynamic_address
= shadow_start
;
47 shadow_start
-= GetMmapGranularity();
50 PrintAddressSpaceLayout();
52 // mmap the low shadow plus at least one page at the left.
54 ReserveShadowMemoryRange(shadow_start
, kLowShadowEnd
, "low shadow");
55 // mmap the high shadow.
56 ReserveShadowMemoryRange(kHighShadowBeg
, kHighShadowEnd
, "high shadow");
58 ProtectGap(kShadowGapBeg
, kShadowGapEnd
- kShadowGapBeg
+ 1);
59 CHECK_EQ(kShadowGapEnd
, kHighShadowBeg
- 1);
62 } // namespace __memprof