| 1 | //===-- memprof_shadow_setup.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 | // Set up the shadow memory. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "sanitizer_common/sanitizer_platform.h" |
| 15 | |
| 16 | #include "memprof_internal.h" |
| 17 | #include "memprof_mapping.h" |
| 18 | |
| 19 | namespace __memprof { |
| 20 | |
| 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), boundary: GetPageSizeCached()); |
| 26 | uptr GapShadowEnd = |
| 27 | RoundUpTo(MEM_TO_SHADOW(addr + size), boundary: GetPageSizeCached()) - 1; |
| 28 | if (Verbosity()) |
| 29 | Printf(format: "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(beg: GapShadowBeg, end: GapShadowEnd, |
| 34 | name: "unprotected gap shadow" ); |
| 35 | return; |
| 36 | } |
| 37 | __sanitizer::ProtectGap(addr, size, kZeroBaseShadowStart, |
| 38 | kZeroBaseMaxShadowStart); |
| 39 | } |
| 40 | |
| 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; |
| 45 | |
| 46 | if (kLowShadowBeg) |
| 47 | shadow_start -= GetMmapGranularity(); |
| 48 | |
| 49 | if (Verbosity()) |
| 50 | PrintAddressSpaceLayout(); |
| 51 | |
| 52 | // mmap the low shadow plus at least one page at the left. |
| 53 | if (kLowShadowBeg) |
| 54 | ReserveShadowMemoryRange(beg: shadow_start, kLowShadowEnd, name: "low shadow" ); |
| 55 | // mmap the high shadow. |
| 56 | ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd, name: "high shadow" ); |
| 57 | // protect the gap. |
| 58 | ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1); |
| 59 | CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1); |
| 60 | } |
| 61 | |
| 62 | } // namespace __memprof |
| 63 | |