• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- memprof_mapping.h --------------------------------------*- C++ -*-===//
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 // Defines MemProf memory mapping.
12 //===----------------------------------------------------------------------===//
13 #ifndef MEMPROF_MAPPING_H
14 #define MEMPROF_MAPPING_H
15 
16 #include "memprof_internal.h"
17 
18 static const u64 kDefaultShadowScale = 3;
19 #define SHADOW_SCALE kDefaultShadowScale
20 
21 #define SHADOW_OFFSET __memprof_shadow_memory_dynamic_address
22 
23 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
24 #define MEMPROF_ALIGNMENT 32
25 
26 namespace __memprof {
27 
28 extern uptr kHighMemEnd; // Initialized in __memprof_init.
29 
30 } // namespace __memprof
31 
32 #define SHADOW_ENTRY_SIZE 8
33 
34 // Size of memory block mapped to a single shadow location
35 #define MEM_GRANULARITY 64ULL
36 
37 #define SHADOW_MASK ~(MEM_GRANULARITY - 1)
38 
39 #define MEM_TO_SHADOW(mem)                                                     \
40   (((mem & SHADOW_MASK) >> SHADOW_SCALE) + (SHADOW_OFFSET))
41 
42 #define kLowMemBeg 0
43 #define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
44 
45 #define kLowShadowBeg SHADOW_OFFSET
46 #define kLowShadowEnd (MEM_TO_SHADOW(kLowMemEnd) + SHADOW_ENTRY_SIZE - 1)
47 
48 #define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1 + SHADOW_ENTRY_SIZE - 1)
49 
50 #define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
51 #define kHighShadowEnd (MEM_TO_SHADOW(kHighMemEnd) + SHADOW_ENTRY_SIZE - 1)
52 
53 // With the zero shadow base we can not actually map pages starting from 0.
54 // This constant is somewhat arbitrary.
55 #define kZeroBaseShadowStart 0
56 #define kZeroBaseMaxShadowStart (1 << 18)
57 
58 #define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 : kZeroBaseShadowStart)
59 #define kShadowGapEnd (kHighShadowBeg - 1)
60 
61 namespace __memprof {
62 
MemToShadowSize(uptr size)63 inline uptr MemToShadowSize(uptr size) { return size >> SHADOW_SCALE; }
AddrIsInLowMem(uptr a)64 inline bool AddrIsInLowMem(uptr a) { return a <= kLowMemEnd; }
65 
AddrIsInLowShadow(uptr a)66 inline bool AddrIsInLowShadow(uptr a) {
67   return a >= kLowShadowBeg && a <= kLowShadowEnd;
68 }
69 
AddrIsInHighMem(uptr a)70 inline bool AddrIsInHighMem(uptr a) {
71   return kHighMemBeg && a >= kHighMemBeg && a <= kHighMemEnd;
72 }
73 
AddrIsInHighShadow(uptr a)74 inline bool AddrIsInHighShadow(uptr a) {
75   return kHighMemBeg && a >= kHighShadowBeg && a <= kHighShadowEnd;
76 }
77 
AddrIsInShadowGap(uptr a)78 inline bool AddrIsInShadowGap(uptr a) {
79   // In zero-based shadow mode we treat addresses near zero as addresses
80   // in shadow gap as well.
81   if (SHADOW_OFFSET == 0)
82     return a <= kShadowGapEnd;
83   return a >= kShadowGapBeg && a <= kShadowGapEnd;
84 }
85 
AddrIsInMem(uptr a)86 inline bool AddrIsInMem(uptr a) {
87   return AddrIsInLowMem(a) || AddrIsInHighMem(a) ||
88          (flags()->protect_shadow_gap == 0 && AddrIsInShadowGap(a));
89 }
90 
MemToShadow(uptr p)91 inline uptr MemToShadow(uptr p) {
92   CHECK(AddrIsInMem(p));
93   return MEM_TO_SHADOW(p);
94 }
95 
AddrIsInShadow(uptr a)96 inline bool AddrIsInShadow(uptr a) {
97   return AddrIsInLowShadow(a) || AddrIsInHighShadow(a);
98 }
99 
AddrIsAlignedByGranularity(uptr a)100 inline bool AddrIsAlignedByGranularity(uptr a) {
101   return (a & (SHADOW_GRANULARITY - 1)) == 0;
102 }
103 
RecordAccess(uptr a)104 inline void RecordAccess(uptr a) {
105   // If we use a different shadow size then the type below needs adjustment.
106   CHECK_EQ(SHADOW_ENTRY_SIZE, 8);
107   u64 *shadow_address = (u64 *)MEM_TO_SHADOW(a);
108   (*shadow_address)++;
109 }
110 
111 } // namespace __memprof
112 
113 #endif // MEMPROF_MAPPING_H
114