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