Path: blob/main/contrib/llvm-project/compiler-rt/lib/memprof/memprof_shadow_setup.cpp
35236 views
//===-- memprof_shadow_setup.cpp -----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of MemProfiler, a memory profiler.9//10// Set up the shadow memory.11//===----------------------------------------------------------------------===//1213#include "sanitizer_common/sanitizer_platform.h"1415#include "memprof_internal.h"16#include "memprof_mapping.h"1718namespace __memprof {1920static void ProtectGap(uptr addr, uptr size) {21if (!flags()->protect_shadow_gap) {22// The shadow gap is unprotected, so there is a chance that someone23// is actually using this memory. Which means it needs a shadow...24uptr GapShadowBeg = RoundDownTo(MEM_TO_SHADOW(addr), GetPageSizeCached());25uptr GapShadowEnd =26RoundUpTo(MEM_TO_SHADOW(addr + size), GetPageSizeCached()) - 1;27if (Verbosity())28Printf("protect_shadow_gap=0:"29" not protecting shadow gap, allocating gap's shadow\n"30"|| `[%p, %p]` || ShadowGap's shadow ||\n",31GapShadowBeg, GapShadowEnd);32ReserveShadowMemoryRange(GapShadowBeg, GapShadowEnd,33"unprotected gap shadow");34return;35}36__sanitizer::ProtectGap(addr, size, kZeroBaseShadowStart,37kZeroBaseMaxShadowStart);38}3940void InitializeShadowMemory() {41uptr shadow_start = FindDynamicShadowStart();42// Update the shadow memory address (potentially) used by instrumentation.43__memprof_shadow_memory_dynamic_address = shadow_start;4445if (kLowShadowBeg)46shadow_start -= GetMmapGranularity();4748if (Verbosity())49PrintAddressSpaceLayout();5051// mmap the low shadow plus at least one page at the left.52if (kLowShadowBeg)53ReserveShadowMemoryRange(shadow_start, kLowShadowEnd, "low shadow");54// mmap the high shadow.55ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd, "high shadow");56// protect the gap.57ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);58CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);59}6061} // namespace __memprof626364