memprof_shadow_setup.cpp 2.2 KB

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