forked from google/binexport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_memory.cc
107 lines (94 loc) · 2.96 KB
/
virtual_memory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2011-2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "third_party/zynamics/binexport/virtual_memory.h"
bool AddressSpace::AddMemoryBlock(Address address, const MemoryBlock& block,
int flags) {
auto it = data_.upper_bound(address);
if (it != data_.end() && it->first < address + block.size()) {
// Intersecting the next block.
return false;
}
if (it != data_.begin()) {
--it;
if (it->first + it->second.size() > address) {
// Intersecting the preceding block.
return false;
}
}
return data_.emplace(address, block).second &&
flags_.emplace(address, flags).second;
}
AddressSpace::Data::const_iterator AddressSpace::GetMemoryBlock(
Address address) const {
auto it = data_.upper_bound(address);
if (it != data_.begin()) {
--it;
if (it->first <= address && it->first + it->second.size() > address) {
return it;
}
}
return data_.end();
}
AddressSpace::Data::iterator AddressSpace::GetMemoryBlock(Address address) {
auto it = data_.upper_bound(address);
if (it != data_.begin()) {
--it;
if (it->first <= address && it->first + it->second.size() > address) {
return it;
}
}
return data_.end();
}
const Byte& AddressSpace::operator[](Address address) const {
auto it = data_.upper_bound(address);
--it;
return it->second[address - it->first];
}
Byte& AddressSpace::operator[](Address address) {
auto it = data_.upper_bound(address);
--it;
return it->second[address - it->first];
}
bool AddressSpace::IsValidAddress(Address address) const {
return GetMemoryBlock(address) != data_.end();
}
bool AddressSpace::IsReadable(Address address) const {
return GetFlags(address) & kRead;
}
bool AddressSpace::IsWritable(Address address) const {
return GetFlags(address) & kWrite;
}
bool AddressSpace::IsExecutable(Address address) const {
return GetFlags(address) & kExecute;
}
int AddressSpace::GetFlags(Address address) const {
auto memory_it = GetMemoryBlock(address);
if (memory_it == data_.end()) {
return 0;
}
auto flags_it = flags_.find(memory_it->first);
if (flags_it != flags_.end()) {
return flags_it->second;
}
return 0;
}
// TODO(soerenme) It is somewhat unexpected to have a size method be O(N). Maybe
// cache the size value in the class?
size_t AddressSpace::size() const {
size_t value = 0;
for (const auto& block : data_) {
value += block.second.size();
}
return value;
}