1*e8d8bef9SDimitry Andric //===-- memprof_shadow_setup.cpp -----------------------------------------===//
2*e8d8bef9SDimitry Andric //
3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e8d8bef9SDimitry Andric //
7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8*e8d8bef9SDimitry Andric //
9*e8d8bef9SDimitry Andric // This file is a part of MemProfiler, a memory profiler.
10*e8d8bef9SDimitry Andric //
11*e8d8bef9SDimitry Andric // Set up the shadow memory.
12*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13*e8d8bef9SDimitry Andric
14*e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_platform.h"
15*e8d8bef9SDimitry Andric
16*e8d8bef9SDimitry Andric #include "memprof_internal.h"
17*e8d8bef9SDimitry Andric #include "memprof_mapping.h"
18*e8d8bef9SDimitry Andric
19*e8d8bef9SDimitry Andric namespace __memprof {
20*e8d8bef9SDimitry Andric
ProtectGap(uptr addr,uptr size)21*e8d8bef9SDimitry Andric static void ProtectGap(uptr addr, uptr size) {
22*e8d8bef9SDimitry Andric if (!flags()->protect_shadow_gap) {
23*e8d8bef9SDimitry Andric // The shadow gap is unprotected, so there is a chance that someone
24*e8d8bef9SDimitry Andric // is actually using this memory. Which means it needs a shadow...
25*e8d8bef9SDimitry Andric uptr GapShadowBeg = RoundDownTo(MEM_TO_SHADOW(addr), GetPageSizeCached());
26*e8d8bef9SDimitry Andric uptr GapShadowEnd =
27*e8d8bef9SDimitry Andric RoundUpTo(MEM_TO_SHADOW(addr + size), GetPageSizeCached()) - 1;
28*e8d8bef9SDimitry Andric if (Verbosity())
29*e8d8bef9SDimitry Andric Printf("protect_shadow_gap=0:"
30*e8d8bef9SDimitry Andric " not protecting shadow gap, allocating gap's shadow\n"
31*e8d8bef9SDimitry Andric "|| `[%p, %p]` || ShadowGap's shadow ||\n",
32*e8d8bef9SDimitry Andric GapShadowBeg, GapShadowEnd);
33*e8d8bef9SDimitry Andric ReserveShadowMemoryRange(GapShadowBeg, GapShadowEnd,
34*e8d8bef9SDimitry Andric "unprotected gap shadow");
35*e8d8bef9SDimitry Andric return;
36*e8d8bef9SDimitry Andric }
37*e8d8bef9SDimitry Andric __sanitizer::ProtectGap(addr, size, kZeroBaseShadowStart,
38*e8d8bef9SDimitry Andric kZeroBaseMaxShadowStart);
39*e8d8bef9SDimitry Andric }
40*e8d8bef9SDimitry Andric
InitializeShadowMemory()41*e8d8bef9SDimitry Andric void InitializeShadowMemory() {
42*e8d8bef9SDimitry Andric uptr shadow_start = FindDynamicShadowStart();
43*e8d8bef9SDimitry Andric // Update the shadow memory address (potentially) used by instrumentation.
44*e8d8bef9SDimitry Andric __memprof_shadow_memory_dynamic_address = shadow_start;
45*e8d8bef9SDimitry Andric
46*e8d8bef9SDimitry Andric if (kLowShadowBeg)
47*e8d8bef9SDimitry Andric shadow_start -= GetMmapGranularity();
48*e8d8bef9SDimitry Andric
49*e8d8bef9SDimitry Andric if (Verbosity())
50*e8d8bef9SDimitry Andric PrintAddressSpaceLayout();
51*e8d8bef9SDimitry Andric
52*e8d8bef9SDimitry Andric // mmap the low shadow plus at least one page at the left.
53*e8d8bef9SDimitry Andric if (kLowShadowBeg)
54*e8d8bef9SDimitry Andric ReserveShadowMemoryRange(shadow_start, kLowShadowEnd, "low shadow");
55*e8d8bef9SDimitry Andric // mmap the high shadow.
56*e8d8bef9SDimitry Andric ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd, "high shadow");
57*e8d8bef9SDimitry Andric // protect the gap.
58*e8d8bef9SDimitry Andric ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
59*e8d8bef9SDimitry Andric CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);
60*e8d8bef9SDimitry Andric }
61*e8d8bef9SDimitry Andric
62*e8d8bef9SDimitry Andric } // namespace __memprof
63