-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathSection.cpp
334 lines (297 loc) · 12.7 KB
/
Section.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//===- Section.cpp ----------------------------------------------*- C++ -*-===//
//
// Copyright (C) 2020 GrammaTech, Inc.
//
// This code is licensed under the MIT license. See the LICENSE file in the
// project root for license terms.
//
// This project is sponsored by the Office of Naval Research, One Liberty
// Center, 875 N. Randolph Street, Arlington, VA 22203 under contract #
// N68335-17-C-0700. The content of the information does not necessarily
// reflect the position or policy of the Government and no official
// endorsement should be inferred.
//
//===----------------------------------------------------------------------===//
#include "Section.hpp"
#include "IR.hpp"
#include "Serialization.hpp"
using namespace gtirb;
class Section::ByteIntervalObserverImpl : public ByteIntervalObserver {
public:
ByteIntervalObserverImpl(Section* S_) : S(S_) {}
ChangeStatus addCodeBlocks(ByteInterval* BI,
ByteInterval::code_block_range Blocks) override;
ChangeStatus moveCodeBlocks(ByteInterval* BI,
ByteInterval::code_block_range Blocks) override;
ChangeStatus removeCodeBlocks(ByteInterval* BI,
ByteInterval::code_block_range Blocks) override;
ChangeStatus addDataBlocks(ByteInterval* BI,
ByteInterval::data_block_range Blocks) override;
ChangeStatus moveDataBlocks(ByteInterval* BI,
ByteInterval::data_block_range Blocks) override;
ChangeStatus removeDataBlocks(ByteInterval* BI,
ByteInterval::data_block_range Blocks) override;
ChangeStatus
changeExtent(ByteInterval* BI,
std::function<void(ByteInterval*)> Callback) override;
private:
Section* S;
};
Section::Section(Context& C) : Section(C, std::string{}) {}
Section::Section(Context& C, const std::string& N)
: Node(C, Kind::Section), Name(N),
BIO(std::make_unique<ByteIntervalObserverImpl>(this)) {}
Section::Section(Context& C, const std::string& N, const UUID& U)
: Node(C, Kind::Section, U), Name(N),
BIO(std::make_unique<ByteIntervalObserverImpl>(this)) {}
bool Section::operator==(const Section& Other) const {
return this->getAddress() == Other.getAddress() &&
this->getSize() == Other.getSize() && this->Name == Other.Name;
}
bool Section::operator!=(const Section& Other) const {
return !(*this == Other);
}
void Section::toProtobuf(MessageType* Message) const {
nodeUUIDToBytes(this, *Message->mutable_uuid());
Message->set_name(this->Name);
for (auto Flag : flags()) {
Message->add_section_flags(static_cast<proto::SectionFlag>(Flag));
}
for (const auto& Interval : byte_intervals()) {
Interval.toProtobuf(Message->add_byte_intervals());
}
}
ErrorOr<Section*> Section::fromProtobuf(Context& C,
const MessageType& Message) {
UUID Id;
if (!uuidFromBytes(Message.uuid(), Id))
return {IR::load_error::BadUUID, "Could not load section"};
auto* S = Section::Create(C, Message.name(), Id);
for (int I = 0, E = Message.section_flags_size(); I != E; ++I) {
S->addFlag(static_cast<SectionFlag>(Message.section_flags(I)));
}
for (const auto& ProtoInterval : Message.byte_intervals()) {
auto BI = ByteInterval::fromProtobuf(C, ProtoInterval);
if (!BI) {
ErrorInfo err{IR::load_error::CorruptSection,
"Could not load section" + Message.name() + "\n" +
BI.getError().message()};
return err;
}
S->addByteInterval(*BI);
}
return S;
}
// Present for testing purposes only.
void Section::save(std::ostream& Out) const {
MessageType Message;
this->toProtobuf(&Message);
Message.SerializeToOstream(&Out);
}
// Present for testing purposes only.
Section* Section::load(Context& C, std::istream& In) {
MessageType Message;
Message.ParseFromIstream(&In);
auto S = Section::fromProtobuf(C, Message);
if (S)
return *S;
return nullptr;
}
ChangeStatus Section::removeByteInterval(ByteInterval* BI) {
auto& Index = ByteIntervals.get<by_pointer>();
if (auto Iter = Index.find(BI); Iter != Index.end()) {
if (Observer) {
auto Begin = ByteIntervals.project<by_address>(Iter);
auto End = std::next(Begin);
[[maybe_unused]] ChangeStatus Status =
Observer->removeCodeBlocks(this, makeCodeBlockRange(Begin, End));
// None of the known observers reject removals. If that changes, this
// implementation will need to be changed as well. Because
// addByteInterval assumes that removal will not be rejected, it will
// need to be updated.
assert(Status != ChangeStatus::Rejected &&
"recovering from rejected removal is not implemented yet");
}
removeByteIntervalAddrs(BI);
Index.erase(Iter);
BI->setParent(nullptr, nullptr);
[[maybe_unused]] ChangeStatus Status = updateExtent();
assert(Status != ChangeStatus::Rejected &&
"failed to change Section extent after removing ByteInterval");
return ChangeStatus::Accepted;
}
return ChangeStatus::NoChange;
}
ChangeStatus Section::addByteInterval(ByteInterval* BI) {
if (Section* S = BI->getSection()) {
if (S == this) {
return ChangeStatus::NoChange;
}
[[maybe_unused]] ChangeStatus Status = S->removeByteInterval(BI);
assert(Status != ChangeStatus::Rejected &&
"failed to remove node from parent");
}
BI->setParent(this, BIO.get());
auto P = ByteIntervals.emplace(BI);
if (P.second && Observer) {
auto Blocks = makeCodeBlockRange(P.first, std::next(P.first));
[[maybe_unused]] ChangeStatus Status =
Observer->addCodeBlocks(this, Blocks);
// None of the known observers reject insertions. If that changes, this
// implementation must be updated.
assert(Status != ChangeStatus::Rejected &&
"recovering from rejected insertion is unimplemented");
}
insertByteIntervalAddrs(BI);
[[maybe_unused]] ChangeStatus Status = updateExtent();
assert(Status != ChangeStatus::Rejected &&
"failed to change Section extent after adding ByteInterval");
return ChangeStatus::Accepted;
}
void Section::removeByteIntervalAddrs(ByteInterval* BI) {
if (std::optional<AddrRange> OldExtent = addressRange(*BI)) {
ByteIntervalAddrs.subtract(
std::make_pair(ByteIntervalIntMap::interval_type::right_open(
OldExtent->lower(), OldExtent->upper()),
ByteIntervalIntMap::codomain_type({BI})));
}
}
void Section::insertByteIntervalAddrs(ByteInterval* BI) {
if (std::optional<AddrRange> NewExtent = addressRange(*BI)) {
ByteIntervalAddrs.add(
std::make_pair(ByteIntervalIntMap::interval_type::right_open(
NewExtent->lower(), NewExtent->upper()),
ByteIntervalIntMap::codomain_type({BI})));
}
}
ChangeStatus Section::updateExtent() {
std::optional<AddrRange> NewExtent;
if (!ByteIntervals.empty()) {
// Any ByteIntervals without an address will be at the front of the map
// because nullopt sorts lower than any address.
if (std::optional<Addr> Lower = (*ByteIntervals.begin())->getAddress()) {
// All the ByteIntervals have an address, so we can calculate the
// Section's extent. Get the address of the last ByteInterval in case it
// has zero size; ByteIntervalAddrs does not track empty ByteIntervals.
Addr Upper = *(*ByteIntervals.rbegin())->getAddress();
if (!ByteIntervalAddrs.empty()) {
// The last address is the max of the first address in the last
// interval and the last address in intervals with non-zero size.
Upper = std::max(Upper, ByteIntervalAddrs.rbegin()->first.upper());
}
NewExtent = AddrRange{*Lower, static_cast<uint64_t>(Upper - *Lower)};
}
}
if (NewExtent != Extent) {
if (Observer) {
[[maybe_unused]] ChangeStatus Status = Observer->changeExtent(
this, [&NewExtent](Section* S) { S->Extent = NewExtent; });
assert(Status != ChangeStatus::Rejected &&
"recovering from rejected extent changes is unimplemented");
} else {
Extent = NewExtent;
}
return ChangeStatus::Accepted;
}
return ChangeStatus::NoChange;
}
ChangeStatus Section::ByteIntervalObserverImpl::addCodeBlocks(
[[maybe_unused]] ByteInterval* BI, ByteInterval::code_block_range Blocks) {
if (S->Observer) {
[[maybe_unused]] auto& Index = S->ByteIntervals.get<by_pointer>();
assert(Index.find(BI) != Index.end() &&
"byte interval observed by non-owner");
// code_block_iterator takes a range of ranges, so wrap the given block
// range in a one-element array.
std::array<decltype(Blocks), 1> Range{Blocks};
return S->Observer->addCodeBlocks(
S, boost::make_iterator_range(code_block_iterator(Range),
code_block_iterator()));
}
return ChangeStatus::NoChange;
}
ChangeStatus Section::ByteIntervalObserverImpl::moveCodeBlocks(
[[maybe_unused]] ByteInterval* BI, ByteInterval::code_block_range Blocks) {
if (S->Observer) {
[[maybe_unused]] auto& Index = S->ByteIntervals.get<by_pointer>();
assert(Index.find(BI) != Index.end() &&
"byte interval observed by non-owner");
// code_block_iterator takes a range of ranges, so wrap the given block
// range in a one-element array.
std::array<decltype(Blocks), 1> Range{Blocks};
return S->Observer->moveCodeBlocks(
S, boost::make_iterator_range(code_block_iterator(Range),
code_block_iterator()));
}
return ChangeStatus::NoChange;
}
ChangeStatus Section::ByteIntervalObserverImpl::removeCodeBlocks(
[[maybe_unused]] ByteInterval* BI, ByteInterval::code_block_range Blocks) {
if (S->Observer) {
[[maybe_unused]] auto& Index = S->ByteIntervals.get<by_pointer>();
assert(Index.find(BI) != Index.end() &&
"byte interval observed by non-owner");
// code_block_iterator takes a range of ranges, so wrap the given block
// range in a one-element array.
std::array<decltype(Blocks), 1> Range{Blocks};
return S->Observer->removeCodeBlocks(
S, boost::make_iterator_range(code_block_iterator(Range),
code_block_iterator()));
}
return ChangeStatus::NoChange;
}
ChangeStatus Section::ByteIntervalObserverImpl::addDataBlocks(
[[maybe_unused]] ByteInterval* BI, ByteInterval::data_block_range Blocks) {
if (S->Observer) {
[[maybe_unused]] auto& Index = S->ByteIntervals.get<by_pointer>();
assert(Index.find(BI) != Index.end() &&
"byte interval observed by non-owner");
// data_block_iterator takes a range of ranges, so wrap the given block
// range in a one-element array.
std::array<decltype(Blocks), 1> Range{Blocks};
return S->Observer->addDataBlocks(
S, boost::make_iterator_range(data_block_iterator(Range),
data_block_iterator()));
}
return ChangeStatus::NoChange;
}
ChangeStatus Section::ByteIntervalObserverImpl::moveDataBlocks(
[[maybe_unused]] ByteInterval* BI, ByteInterval::data_block_range Blocks) {
if (S->Observer) {
[[maybe_unused]] auto& Index = S->ByteIntervals.get<by_pointer>();
assert(Index.find(BI) != Index.end() &&
"byte interval observed by non-owner");
// data_block_iterator takes a range of ranges, so wrap the given block
// range in a one-element array.
std::array<decltype(Blocks), 1> Range{Blocks};
return S->Observer->moveDataBlocks(
S, boost::make_iterator_range(data_block_iterator(Range),
data_block_iterator()));
}
return ChangeStatus::NoChange;
}
ChangeStatus Section::ByteIntervalObserverImpl::removeDataBlocks(
[[maybe_unused]] ByteInterval* BI, ByteInterval::data_block_range Blocks) {
if (S->Observer) {
[[maybe_unused]] auto& Index = S->ByteIntervals.get<by_pointer>();
assert(Index.find(BI) != Index.end() &&
"byte interval observed by non-owner");
// data_block_iterator takes a range of ranges, so wrap the given block
// range in a one-element array.
std::array<decltype(Blocks), 1> Range{Blocks};
return S->Observer->removeDataBlocks(
S, boost::make_iterator_range(data_block_iterator(Range),
data_block_iterator()));
}
return ChangeStatus::NoChange;
}
ChangeStatus Section::ByteIntervalObserverImpl::changeExtent(
ByteInterval* BI, std::function<void(ByteInterval*)> Callback) {
auto& Index = S->ByteIntervals.get<by_pointer>();
if (auto It = Index.find(BI); It != Index.end()) {
S->removeByteIntervalAddrs(BI);
Index.modify(It, Callback);
S->insertByteIntervalAddrs(BI);
}
return S->updateExtent();
}