1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/block_graph/block_graph.h"
16 :
17 : #include <limits>
18 :
19 : #include "base/logging.h"
20 : #include "base/stringprintf.h"
21 :
22 : namespace block_graph {
23 :
24 : namespace {
25 :
26 : COMPILE_ASSERT(BlockGraph::BLOCK_ATTRIBUTES_MAX_BIT < 32,
27 : too_many_block_attributes);
28 :
29 : // A list of printable names corresponding to block types. This needs to
30 : // be kept in sync with the BlockGraph::BlockType enum!
31 : const char* kBlockType[] = {
32 : "CODE_BLOCK", "DATA_BLOCK",
33 : };
34 : COMPILE_ASSERT(arraysize(kBlockType) == BlockGraph::BLOCK_TYPE_MAX,
35 : kBlockType_not_in_sync);
36 :
37 : // Shift all items in an offset -> item map by 'distance', provided the initial
38 : // item offset was >= @p offset.
39 : template<typename ItemType>
40 : void ShiftOffsetItemMap(BlockGraph::Offset offset,
41 : BlockGraph::Offset distance,
42 E : std::map<BlockGraph::Offset, ItemType>* items) {
43 E : DCHECK_GE(offset, 0);
44 E : DCHECK_NE(distance, 0);
45 E : DCHECK(items != NULL);
46 :
47 : typedef std::map<BlockGraph::Offset, ItemType> ItemMap;
48 :
49 : // Get iterators to all of the items that need changing.
50 E : std::vector<ItemMap::iterator> item_its;
51 E : ItemMap::iterator item_it = items->lower_bound(offset);
52 E : while (item_it != items->end()) {
53 E : item_its.push_back(item_it);
54 E : ++item_it;
55 E : }
56 :
57 : // Get the direction and bounds of the iteration. We need to walk through
58 : // the iterators in a different order depending on if we're shifting left
59 : // or right. This is to ensure that earlier shifts don't land on the values
60 : // of later unshifted offsets.
61 E : int start = 0;
62 E : int stop = item_its.size();
63 E : int step = 1;
64 E : if (distance > 0) {
65 E : start = stop - 1;
66 E : stop = -1;
67 E : step = -1;
68 : }
69 :
70 E : for (int i = start; i != stop; i += step) {
71 E : item_it = item_its[i];
72 : items->insert(std::make_pair(item_it->first + distance,
73 E : item_it->second));
74 E : items->erase(item_it);
75 E : }
76 E : }
77 :
78 : void ShiftReferences(BlockGraph::Block* block,
79 : BlockGraph::Offset offset,
80 E : BlockGraph::Offset distance) {
81 : // Make a copy of the reference map for simplicity.
82 E : BlockGraph::Block::ReferenceMap references = block->references();
83 :
84 : // Start by removing all references that have moved.
85 : BlockGraph::Block::ReferenceMap::const_iterator it =
86 E : references.lower_bound(offset);
87 E : for (; it != references.end(); ++it) {
88 E : if (it->first >= offset)
89 E : block->RemoveReference(it->first);
90 E : }
91 :
92 : // Then patch up all existing references.
93 E : it = references.begin();
94 E : for (; it != references.end(); ++it) {
95 E : BlockGraph::Reference ref(it->second);
96 E : BlockGraph::Offset new_offset(it->first);
97 :
98 : // If this is self-referential, fix the destination offset.
99 E : if (ref.referenced() == block && ref.offset() >= offset) {
100 : ref = BlockGraph::Reference(ref.type(),
101 : ref.size(),
102 : ref.referenced(),
103 : ref.offset() + distance,
104 i : ref.base() + distance);
105 : }
106 :
107 : // If its offset is past the change point, fix that.
108 E : if (it->first >= offset)
109 E : new_offset += distance;
110 :
111 : // In many cases this'll be a noop.
112 : // TODO(siggi): Optimize this.
113 E : block->SetReference(new_offset, ref);
114 E : }
115 E : }
116 :
117 : // Shift all referrers beyond @p offset by @p distance.
118 : void ShiftReferrers(BlockGraph::Block* self,
119 : BlockGraph::Offset offset,
120 : BlockGraph::Offset distance,
121 E : BlockGraph::Block::ReferrerSet* referrers) {
122 E : DCHECK_GE(offset, 0);
123 E : DCHECK_NE(distance, 0);
124 E : DCHECK(referrers != NULL);
125 :
126 : typedef BlockGraph::Block::ReferrerSet ReferrerSet;
127 : typedef BlockGraph::Reference Reference;
128 :
129 E : ReferrerSet::iterator ref_it = referrers->begin();
130 E : while (ref_it != referrers->end()) {
131 : // We need to keep around the next iterator as 'ref_it' will be invalidated
132 : // if we need to update the reference. (It will be deleted and then
133 : // recreated.)
134 E : ReferrerSet::iterator next_ref_it = ref_it;
135 E : ++next_ref_it;
136 :
137 E : BlockGraph::Block* ref_block = ref_it->first;
138 : // Our own references will have been moved already.
139 E : if (ref_block != self) {
140 E : BlockGraph::Offset ref_offset = ref_it->second;
141 :
142 E : Reference ref;
143 E : bool ref_found = ref_block->GetReference(ref_offset, &ref);
144 E : DCHECK(ref_found);
145 :
146 : // Shift the reference if need be.
147 E : if (ref.offset() >= offset) {
148 : Reference new_ref(ref.type(),
149 : ref.size(),
150 : ref.referenced(),
151 : ref.offset() + distance,
152 E : ref.base() + distance);
153 E : bool inserted = ref_block->SetReference(ref_offset, new_ref);
154 E : DCHECK(!inserted);
155 : }
156 : }
157 :
158 E : ref_it = next_ref_it;
159 E : }
160 E : }
161 :
162 i : const char* BlockAttributeToString(BlockGraph::BlockAttributeEnum attr) {
163 i : switch (attr) {
164 : #define DEFINE_CASE(name) case BlockGraph::name: return #name;
165 i : BLOCK_ATTRIBUTE_ENUM(DEFINE_CASE)
166 : #undef DEFINE_CASE
167 : default:
168 i : NOTREACHED();
169 i : return NULL;
170 : }
171 i : }
172 :
173 : } // namespace
174 :
175 i : std::string BlockGraph::BlockAttributesToString(BlockAttributes attrs) {
176 i : BlockAttributes attr = 1;
177 i : std::string s;
178 i : for (; attr < BLOCK_ATTRIBUTES_MAX; attr <<= 1) {
179 i : if (attr & attrs) {
180 i : if (!s.empty())
181 i : s.append("|");
182 i : s.append(BlockAttributeToString(static_cast<BlockAttributeEnum>(attr)));
183 : }
184 i : }
185 i : return s;
186 i : }
187 :
188 E : const char* BlockGraph::BlockTypeToString(BlockGraph::BlockType type) {
189 E : DCHECK_LE(BlockGraph::CODE_BLOCK, type);
190 E : DCHECK_GT(BlockGraph::BLOCK_TYPE_MAX, type);
191 E : return kBlockType[type];
192 E : }
193 :
194 : std::string BlockGraph::LabelAttributesToString(
195 E : BlockGraph::LabelAttributes label_attributes) {
196 : static const char* kLabelAttributes[] = {
197 : "Code", "DebugStart", "DebugEnd", "ScopeStart", "ScopeEnd",
198 : "CallSite", "JumpTable", "CaseTable", "Data", "PublicSymbol" };
199 : COMPILE_ASSERT((1 << arraysize(kLabelAttributes)) == LABEL_ATTRIBUTES_MAX,
200 : label_attribute_names_not_in_sync_with_enum);
201 :
202 E : std::string s;
203 E : for (size_t i = 0; i < arraysize(kLabelAttributes); ++i) {
204 E : if (label_attributes & (1 << i)) {
205 E : if (!s.empty())
206 E : s.append("|");
207 E : s.append(kLabelAttributes[i]);
208 : }
209 E : }
210 E : return s;
211 E : }
212 :
213 E : const core::RelativeAddress kInvalidAddress(0xFFFFFFFF);
214 :
215 : const BlockGraph::SectionId BlockGraph::kInvalidSectionId = -1;
216 :
217 : BlockGraph::BlockGraph()
218 : : next_section_id_(0),
219 E : next_block_id_(0) {
220 E : }
221 :
222 E : BlockGraph::~BlockGraph() {
223 E : }
224 :
225 : BlockGraph::Section* BlockGraph::AddSection(const base::StringPiece& name,
226 E : uint32 characteristics) {
227 E : Section new_section(next_section_id_++, name, characteristics);
228 : std::pair<SectionMap::iterator, bool> result = sections_.insert(
229 E : std::make_pair(new_section.id(), new_section));
230 E : DCHECK(result.second);
231 :
232 E : return &result.first->second;
233 E : }
234 :
235 E : BlockGraph::Section* BlockGraph::FindSection(const base::StringPiece& name) {
236 : // This is a linear scan, but thankfully images generally do not have many
237 : // sections and we do not create them very often. Fast lookup by index is
238 : // more important. If this ever becomes an issue, we could keep around a
239 : // second index by name.
240 E : SectionMap::iterator it = sections_.begin();
241 E : for (; it != sections_.end(); ++it) {
242 E : if (it->second.name() == name)
243 E : return &it->second;
244 E : }
245 :
246 E : return NULL;
247 E : }
248 :
249 : BlockGraph::Section* BlockGraph::FindOrAddSection(const base::StringPiece& name,
250 E : uint32 characteristics) {
251 E : Section* section = FindSection(name);
252 E : if (section) {
253 E : section->set_characteristic(characteristics);
254 E : return section;
255 : }
256 E : return AddSection(name, characteristics);
257 E : }
258 :
259 E : bool BlockGraph::RemoveSection(Section* section) {
260 E : DCHECK(section != NULL);
261 :
262 E : SectionMap::iterator it(sections_.find(section->id()));
263 E : if (it == sections_.end() || &it->second != section)
264 i : return false;
265 :
266 E : sections_.erase(it);
267 E : return true;
268 E : }
269 :
270 E : bool BlockGraph::RemoveSectionById(SectionId id) {
271 E : SectionMap::iterator it(sections_.find(id));
272 E : if (it == sections_.end())
273 E : return false;
274 :
275 E : sections_.erase(it);
276 E : return true;
277 E : }
278 :
279 : BlockGraph::Block* BlockGraph::AddBlock(BlockType type,
280 : Size size,
281 E : const base::StringPiece& name) {
282 E : BlockId id = ++next_block_id_;
283 : BlockMap::iterator it = blocks_.insert(
284 E : std::make_pair(id, Block(id, type, size, name))).first;
285 :
286 E : return &it->second;
287 E : }
288 :
289 E : bool BlockGraph::RemoveBlock(Block* block) {
290 E : DCHECK(block != NULL);
291 :
292 E : BlockMap::iterator it(blocks_.find(block->id()));
293 E : if (it == blocks_.end() || &it->second != block)
294 E : return false;
295 :
296 E : return RemoveBlockByIterator(it);
297 E : }
298 :
299 E : bool BlockGraph::RemoveBlockById(BlockId id) {
300 E : BlockMap::iterator it(blocks_.find(id));
301 E : if (it == blocks_.end())
302 E : return false;
303 :
304 E : return RemoveBlockByIterator(it);
305 E : }
306 :
307 E : BlockGraph::Section* BlockGraph::GetSectionById(SectionId id) {
308 E : SectionMap::iterator it(sections_.find(id));
309 :
310 E : if (it == sections_.end())
311 E : return NULL;
312 :
313 E : return &it->second;
314 E : }
315 :
316 E : const BlockGraph::Section* BlockGraph::GetSectionById(SectionId id) const {
317 E : SectionMap::const_iterator it(sections_.find(id));
318 :
319 E : if (it == sections_.end())
320 E : return NULL;
321 :
322 E : return &it->second;
323 E : }
324 :
325 E : BlockGraph::Block* BlockGraph::GetBlockById(BlockId id) {
326 E : BlockMap::iterator it(blocks_.find(id));
327 :
328 E : if (it == blocks_.end())
329 E : return NULL;
330 :
331 E : return &it->second;
332 E : }
333 :
334 : const BlockGraph::Block* BlockGraph::GetBlockById(BlockId id) const {
335 : BlockMap::const_iterator it(blocks_.find(id));
336 :
337 : if (it == blocks_.end())
338 : return NULL;
339 :
340 : return &it->second;
341 : }
342 :
343 E : bool BlockGraph::RemoveBlockByIterator(BlockMap::iterator it) {
344 E : DCHECK(it != blocks_.end());
345 :
346 : // Verify this block is fully disconnected.
347 E : if (it->second.referrers().size() > 0 || it->second.references().size() > 0)
348 E : return false;
349 :
350 E : blocks_.erase(it);
351 :
352 E : return true;
353 E : }
354 :
355 : BlockGraph::AddressSpace::AddressSpace(BlockGraph* graph)
356 E : : graph_(graph) {
357 E : DCHECK(graph != NULL);
358 E : }
359 :
360 : BlockGraph::Block* BlockGraph::AddressSpace::AddBlock(
361 : BlockType type, RelativeAddress addr, Size size,
362 E : const base::StringPiece& name) {
363 : // First check to see that the range is clear.
364 E : AddressSpaceImpl::Range range(addr, size);
365 : AddressSpaceImpl::RangeMap::iterator it =
366 E : address_space_.FindFirstIntersection(range);
367 E : if (it != address_space_.ranges().end())
368 E : return NULL;
369 :
370 E : BlockGraph::Block* block = graph_->AddBlock(type, size, name);
371 E : DCHECK(block != NULL);
372 E : bool inserted = InsertImpl(addr, block);
373 E : DCHECK(inserted);
374 :
375 E : return block;
376 E : }
377 :
378 E : bool BlockGraph::AddressSpace::InsertBlock(RelativeAddress addr, Block* block) {
379 E : return InsertImpl(addr, block);
380 E : }
381 :
382 : BlockGraph::Block* BlockGraph::AddressSpace::GetBlockByAddress(
383 E : RelativeAddress addr) const {
384 E : return GetContainingBlock(addr, 1);
385 E : }
386 :
387 : BlockGraph::Block* BlockGraph::AddressSpace::GetContainingBlock(
388 E : RelativeAddress addr, Size size) const {
389 E : AddressSpaceImpl::Range range(addr, size);
390 : AddressSpaceImpl::RangeMap::const_iterator it =
391 E : address_space_.FindContaining(range);
392 E : if (it == address_space_.ranges().end())
393 E : return NULL;
394 :
395 E : return it->second;
396 E : }
397 :
398 : BlockGraph::Block* BlockGraph::AddressSpace::GetFirstIntersectingBlock(
399 E : RelativeAddress addr, Size size) {
400 E : AddressSpaceImpl::Range range(addr, size);
401 : AddressSpaceImpl::RangeMap::iterator it =
402 E : address_space_.FindFirstIntersection(range);
403 E : if (it == address_space_.ranges().end())
404 E : return NULL;
405 :
406 E : return it->second;
407 E : }
408 :
409 : BlockGraph::AddressSpace::RangeMapConstIterPair
410 : BlockGraph::AddressSpace::GetIntersectingBlocks(RelativeAddress address,
411 E : Size size) const {
412 E : return address_space_.FindIntersecting(Range(address, size));
413 E : }
414 :
415 : BlockGraph::AddressSpace::RangeMapIterPair
416 : BlockGraph::AddressSpace::GetIntersectingBlocks(RelativeAddress address,
417 E : Size size) {
418 E : return address_space_.FindIntersecting(Range(address, size));
419 E : }
420 :
421 : bool BlockGraph::AddressSpace::GetAddressOf(const Block* block,
422 E : RelativeAddress* addr) const {
423 E : DCHECK(block != NULL);
424 E : DCHECK(addr != NULL);
425 :
426 E : BlockAddressMap::const_iterator it(block_addresses_.find(block));
427 E : if (it == block_addresses_.end())
428 E : return false;
429 :
430 E : *addr = it->second;
431 E : return true;
432 E : }
433 :
434 E : bool BlockGraph::AddressSpace::InsertImpl(RelativeAddress addr, Block* block) {
435 E : Range range(addr, block->size());
436 E : bool inserted = address_space_.Insert(range, block);
437 E : if (!inserted)
438 E : return false;
439 :
440 E : inserted = block_addresses_.insert(std::make_pair(block, addr)).second;
441 E : DCHECK(inserted);
442 : // Update the address stored in the block.
443 E : block->set_addr(addr);
444 :
445 E : return true;
446 E : }
447 :
448 E : bool BlockGraph::AddressSpace::ContainsBlock(const Block* block) {
449 E : DCHECK(block != NULL);
450 E : return block_addresses_.count(block) != 0;
451 E : }
452 :
453 : BlockGraph::Block* BlockGraph::AddressSpace::MergeIntersectingBlocks(
454 E : const Range& range) {
455 : typedef std::vector<std::pair<RelativeAddress, BlockGraph::Block*>>
456 : BlockAddressVector;
457 :
458 : // Find all the blocks that intersect the range, keep them and their
459 : // addresses. Start by finding the first intersection, then iterate
460 : // from there until we find a block that doesn't intersect with range.
461 : AddressSpaceImpl::RangeMap::iterator address_start =
462 E : address_space_.FindFirstIntersection(range);
463 E : AddressSpaceImpl::RangeMap::iterator address_it(address_start);
464 :
465 E : BlockAddressVector intersecting;
466 : for (; address_it != address_space_.ranges().end() &&
467 E : address_it->first.Intersects(range); ++address_it) {
468 : intersecting.push_back(std::make_pair(address_it->first.start(),
469 E : address_it->second));
470 E : }
471 :
472 : // Bail if the intersection doesn't cover at least two blocks.
473 E : if (intersecting.empty())
474 i : return NULL;
475 :
476 : // In case of single-block intersection, we're done.
477 E : if (intersecting.size() == 1)
478 i : return intersecting[0].second;
479 :
480 E : DCHECK(!intersecting.empty());
481 :
482 : // Calculate the start and end addresses of the new block.
483 E : BlockGraph::Block* first_block = intersecting[0].second;
484 E : BlockGraph::Block* last_block = intersecting[intersecting.size() - 1].second;
485 E : DCHECK(first_block != NULL && last_block != NULL);
486 :
487 E : RelativeAddress begin = std::min(range.start(), intersecting[0].first);
488 : RelativeAddress end = std::max(range.start() + range.size(),
489 E : intersecting[intersecting.size() - 1].first + last_block->size());
490 :
491 E : DCHECK(begin <= range.start());
492 E : DCHECK(end >= range.start() + range.size());
493 :
494 E : base::StringPiece block_name = first_block->name();
495 E : BlockType block_type = first_block->type();
496 E : size_t section_id = first_block->section();
497 E : size_t alignment = first_block->alignment();
498 E : BlockAttributes attributes = 0;
499 :
500 E : BlockGraph::Block::SourceRanges source_ranges;
501 :
502 : // Remove the found blocks from the address space, and make sure they're all
503 : // of the same type and from the same section as the first block. Merge the
504 : // data from all the blocks as we go along, as well as the attributes and
505 : // source ranges.
506 E : std::vector<uint8> merged_data(end - begin);
507 E : bool have_data = false;
508 E : for (size_t i = 0; i < intersecting.size(); ++i) {
509 E : RelativeAddress addr = intersecting[i].first;
510 E : BlockGraph::Block* block = intersecting[i].second;
511 E : DCHECK_EQ(block_type, block->type());
512 E : DCHECK_EQ(section_id, block->section());
513 :
514 E : if (block->data() != NULL) {
515 E : have_data = true;
516 E : memcpy(&merged_data.at(addr - begin), block->data(), block->data_size());
517 : }
518 E : attributes |= block->attributes();
519 :
520 : // Merge in the source ranges from each block.
521 E : BlockGraph::Offset block_offset = addr - begin;
522 : BlockGraph::Block::SourceRanges::RangePairs::const_iterator src_it =
523 E : block->source_ranges().range_pairs().begin();
524 E : for (; src_it != block->source_ranges().range_pairs().end(); ++src_it) {
525 : // The data range is wrt to the containing block, wo we have to translate
526 : // each individual block's offset to an offset in the merged block.
527 E : BlockGraph::Offset merged_offset = block_offset + src_it->first.start();
528 : bool pushed = source_ranges.Push(
529 : BlockGraph::Block::DataRange(merged_offset, src_it->first.size()),
530 E : src_it->second);
531 E : DCHECK(pushed);
532 E : }
533 :
534 E : bool removed = address_space_.Remove(Range(addr, block->size()));
535 E : DCHECK(removed);
536 E : size_t num_removed = block_addresses_.erase(intersecting[i].second);
537 E : DCHECK_EQ(1U, num_removed);
538 E : }
539 :
540 : // Create the new block.
541 : BlockGraph::Block* new_block = AddBlock(block_type,
542 : begin, end - begin,
543 E : block_name);
544 E : DCHECK(new_block != NULL);
545 :
546 : // Set the rest of the properties for the new block.
547 E : new_block->source_ranges() = source_ranges;
548 E : new_block->set_section(section_id);
549 E : new_block->set_alignment(alignment);
550 E : new_block->set_attributes(attributes);
551 E : if (have_data) {
552 E : uint8* data = new_block->CopyData(merged_data.size(), &merged_data.at(0));
553 E : if (data == NULL) {
554 i : LOG(ERROR) << "Unable to copy merged data";
555 i : return NULL;
556 : }
557 : }
558 :
559 : // Now move all labels and references to the new block.
560 E : for (size_t i = 0; i < intersecting.size(); ++i) {
561 E : RelativeAddress addr = intersecting[i].first;
562 E : BlockGraph::Block* block = intersecting[i].second;
563 E : BlockGraph::Offset start_offset = addr - begin;
564 :
565 : // If the destination block is not a code block, preserve the old block
566 : // names as labels for debugging. We also need to make sure the label is
567 : // not empty, as that is verboten.
568 E : if (block_type != BlockGraph::CODE_BLOCK && !block->name().empty()) {
569 : new_block->SetLabel(start_offset,
570 : block->name(),
571 E : BlockGraph::DATA_LABEL);
572 : }
573 :
574 : // Move labels.
575 : BlockGraph::Block::LabelMap::const_iterator
576 E : label_it(block->labels().begin());
577 E : for (; label_it != block->labels().end(); ++label_it) {
578 : new_block->SetLabel(start_offset + label_it->first,
579 E : label_it->second);
580 E : }
581 :
582 : // Copy the reference map since we mutate the original.
583 E : BlockGraph::Block::ReferenceMap refs(block->references());
584 E : BlockGraph::Block::ReferenceMap::const_iterator ref_it(refs.begin());
585 E : for (; ref_it != refs.end(); ++ref_it) {
586 E : block->RemoveReference(ref_it->first);
587 E : new_block->SetReference(start_offset + ref_it->first, ref_it->second);
588 E : }
589 :
590 : // Redirect all referrers to the new block.
591 E : block->TransferReferrers(start_offset, new_block);
592 :
593 : // Check that we've removed all references and
594 : // referrers from the original block.
595 E : DCHECK(block->references().empty());
596 E : DCHECK(block->referrers().empty());
597 :
598 : // Remove the original block.
599 E : bool removed = graph_->RemoveBlock(block);
600 E : DCHECK(removed);
601 E : }
602 :
603 E : return new_block;
604 E : }
605 :
606 E : bool BlockGraph::Section::set_name(const base::StringPiece& name) {
607 E : if (name == NULL)
608 i : return false;
609 :
610 E : if (name.empty())
611 i : return false;
612 :
613 E : name.CopyToString(&name_);
614 E : return true;
615 E : }
616 :
617 E : bool BlockGraph::Section::Save(core::OutArchive* out_archive) const {
618 E : DCHECK(out_archive != NULL);
619 : return out_archive->Save(id_) && out_archive->Save(name_) &&
620 E : out_archive->Save(characteristics_);
621 E : }
622 :
623 E : bool BlockGraph::Section::Load(core::InArchive* in_archive) {
624 E : DCHECK(in_archive != NULL);
625 : return in_archive->Load(&id_) && in_archive->Load(&name_) &&
626 E : in_archive->Load(&characteristics_);
627 E : }
628 :
629 E : std::string BlockGraph::Label::ToString() const {
630 : return base::StringPrintf("%s (%s)",
631 : name_.c_str(),
632 E : LabelAttributesToString(attributes_).c_str());
633 E : }
634 :
635 E : bool BlockGraph::Label::IsValid() const {
636 E : return AreValidAttributes(attributes_);
637 E : }
638 :
639 E : bool BlockGraph::Label::AreValidAttributes(LabelAttributes attributes) {
640 : // A label needs to have at least one attribute.
641 E : if (attributes == 0)
642 E : return false;
643 :
644 : // TODO(chrisha): Once we make the switch to VS2010 determine where call
645 : // site labels may land. Are they at the beginning of the call
646 : // instruction (in which case they may coincide with *_START_LABEL,
647 : // *_END_LABEL and CODE_LABEL), or do they point at the address of the
648 : // call (in which case they must be completely on their own)? For now, we
649 : // simply ignore them entirely from consideration.
650 E : attributes &= ~CALL_SITE_LABEL;
651 :
652 : // Public symbols can coincide with anything, so we can basically ignore
653 : // them.
654 E : attributes &= ~PUBLIC_SYMBOL_LABEL;
655 :
656 : // A code label can coincide with a debug and scope labels. (It can coincide
657 : // with *_END_LABEL labels because of 1-byte instructions, like RET or INT.)
658 : const LabelAttributes kCodeDebugScopeLabels =
659 : CODE_LABEL | DEBUG_START_LABEL | DEBUG_END_LABEL | SCOPE_START_LABEL |
660 E : SCOPE_END_LABEL;
661 : if ((attributes & CODE_LABEL) != 0 &&
662 E : (attributes & ~kCodeDebugScopeLabels) != 0) {
663 E : return false;
664 : }
665 :
666 : // A jump table must be paired with a data label. It may also be paired
667 : // with a debug-end label if tail-call optimization has been applied by
668 : // the compiler/linker.
669 : const LabelAttributes kJumpDataLabelAttributes =
670 E : JUMP_TABLE_LABEL | DATA_LABEL;
671 E : if (attributes & JUMP_TABLE_LABEL) {
672 E : if ((attributes & kJumpDataLabelAttributes) != kJumpDataLabelAttributes)
673 E : return false;
674 : // Filter out the debug-end label if present and check that nothing else
675 : // is set.
676 E : attributes &= ~DEBUG_END_LABEL;
677 E : if ((attributes & ~kJumpDataLabelAttributes) != 0)
678 i : return false;
679 E : return true;
680 : }
681 :
682 : // A case table must be paired with a data label and nothing else.
683 : const LabelAttributes kCaseDataLabelAttributes =
684 E : CASE_TABLE_LABEL | DATA_LABEL;
685 E : if (attributes & CASE_TABLE_LABEL) {
686 E : if ((attributes & kCaseDataLabelAttributes) != kCaseDataLabelAttributes)
687 E : return false;
688 E : if ((attributes & ~kCaseDataLabelAttributes) != 0)
689 i : return false;
690 E : return true;
691 : }
692 :
693 : // If there is no case or jump label, then a data label must be on its own.
694 E : if ((attributes & DATA_LABEL) != 0 && (attributes & ~DATA_LABEL) != 0)
695 i : return false;
696 :
697 E : return true;
698 E : }
699 :
700 : BlockGraph::Block::Block()
701 : : id_(0),
702 : type_(BlockGraph::CODE_BLOCK),
703 : size_(0),
704 : alignment_(1),
705 : addr_(kInvalidAddress),
706 : section_(kInvalidSectionId),
707 : attributes_(0),
708 : owns_data_(false),
709 : data_(NULL),
710 E : data_size_(0) {
711 E : }
712 :
713 : BlockGraph::Block::Block(BlockId id,
714 : BlockType type,
715 : Size size,
716 : const base::StringPiece& name)
717 : : id_(id),
718 : type_(type),
719 : size_(size),
720 : alignment_(1),
721 : name_(name.begin(), name.end()),
722 : addr_(kInvalidAddress),
723 : section_(kInvalidSectionId),
724 : attributes_(0),
725 : owns_data_(false),
726 : data_(NULL),
727 E : data_size_(0) {
728 E : }
729 :
730 E : BlockGraph::Block::~Block() {
731 E : if (owns_data_)
732 E : delete [] data_;
733 E : }
734 :
735 E : uint8* BlockGraph::Block::AllocateRawData(size_t data_size) {
736 E : DCHECK_GT(data_size, 0u);
737 E : DCHECK_LE(data_size, size_);
738 :
739 E : uint8* new_data = new uint8[data_size];
740 E : if (!new_data)
741 i : return NULL;
742 :
743 E : if (owns_data()) {
744 i : DCHECK(data_ != NULL);
745 i : delete [] data_;
746 : }
747 :
748 E : data_ = new_data;
749 E : data_size_ = data_size;
750 E : owns_data_ = true;
751 :
752 E : return new_data;
753 E : }
754 :
755 : void BlockGraph::Block::InsertData(Offset offset,
756 : Size size,
757 E : bool always_allocate_data) {
758 E : DCHECK_GE(offset, 0);
759 E : DCHECK_LE(offset, static_cast<Offset>(size_));
760 :
761 E : if (size > 0) {
762 : // Patch up the block.
763 E : size_ += size;
764 E : ShiftOffsetItemMap(offset, size, &labels_);
765 E : ShiftReferences(this, offset, size);
766 E : ShiftReferrers(this, offset, size, &referrers_);
767 E : source_ranges_.InsertUnmappedRange(DataRange(offset, size));
768 :
769 : // Does this affect already allocated data?
770 E : if (static_cast<Size>(offset) < data_size_) {
771 : // Reallocate, shift the old data to the end, and zero out the new data.
772 E : size_t old_data_size = data_size_;
773 E : size_t bytes_to_shift = data_size_ - offset;
774 E : ResizeData(data_size_ + size);
775 E : uint8* new_data = GetMutableData();
776 E : memmove(new_data + offset + size, new_data + offset, bytes_to_shift);
777 E : memset(new_data + offset, 0, size);
778 : }
779 : }
780 :
781 : // If we've been asked to, at least make sure that the data is allocated.
782 E : if (always_allocate_data && data_size_ < offset + size)
783 E : ResizeData(offset + size);
784 :
785 : return;
786 E : }
787 :
788 E : bool BlockGraph::Block::RemoveData(Offset offset, Size size) {
789 E : DCHECK_GE(offset, 0);
790 E : DCHECK_LE(offset, static_cast<Offset>(size_));
791 :
792 E : if (size == 0)
793 i : return true;
794 :
795 : // Ensure there are no labels in this range.
796 E : if (labels_.lower_bound(offset) != labels_.lower_bound(offset + size))
797 E : return false;
798 :
799 : // Ensure that there are no references intersecting this range.
800 E : ReferenceMap::const_iterator refc_it = references_.begin();
801 E : for (; refc_it != references_.end(); ++refc_it) {
802 E : if (refc_it->first >= static_cast<Offset>(offset + size))
803 E : break;
804 E : if (static_cast<Offset>(refc_it->first + refc_it->second.size()) > offset)
805 E : return false;
806 E : }
807 :
808 : // Ensure there are no referrers pointing to the data we want to remove.
809 E : ReferrerSet::const_iterator refr_it = referrers_.begin();
810 E : for (; refr_it != referrers_.end(); ++refr_it) {
811 E : Reference ref;
812 E : if (!refr_it->first->GetReference(refr_it->second, &ref)) {
813 i : LOG(ERROR) << "Unable to get reference from referrer.";
814 i : return false;
815 : }
816 : if (ref.offset() < static_cast<Offset>(offset + size) &&
817 E : static_cast<Offset>(ref.offset() + ref.size()) > offset) {
818 E : return false;
819 : }
820 E : }
821 :
822 : // Patch up the block.
823 E : size_ -= size;
824 E : ShiftOffsetItemMap(offset + size, -static_cast<int>(size), &labels_);
825 E : ShiftReferences(this, offset + size, -static_cast<int>(size));
826 E : ShiftReferrers(this, offset + size, -static_cast<int>(size), &referrers_);
827 E : source_ranges_.RemoveMappedRange(DataRange(offset, size));
828 :
829 : // Does this affect already allocated data?
830 E : if (static_cast<Size>(offset) < data_size_) {
831 E : size_t new_data_size = data_size_ - size;
832 : // Is there data beyond the section to delete?
833 E : if (static_cast<Size>(offset + size) < data_size_) {
834 : // Shift tail data to left.
835 E : uint8* data = GetMutableData();
836 E : size_t bytes_to_shift = data_size_ - offset - size;
837 E : size_t old_data_size = data_size_;
838 : memmove(data + new_data_size - bytes_to_shift,
839 : data + old_data_size - bytes_to_shift,
840 E : bytes_to_shift);
841 E : } else {
842 E : new_data_size = offset;
843 : }
844 E : ResizeData(new_data_size);
845 : }
846 :
847 E : return true;
848 E : }
849 :
850 : bool BlockGraph::Block::InsertOrRemoveData(Offset offset,
851 : Size current_size,
852 : Size new_size,
853 E : bool always_allocate_data) {
854 E : DCHECK_GE(offset, 0);
855 E : DCHECK_LE(offset, static_cast<Offset>(size_));
856 :
857 : // If we're growing use InsertData.
858 E : if (new_size > current_size) {
859 E : Offset insert_offset = offset + current_size;
860 E : Size insert_size = new_size - current_size;
861 E : InsertData(insert_offset, insert_size, always_allocate_data);
862 E : return true;
863 : }
864 :
865 : // If we're shrinking we'll need to use RemoveData.
866 E : if (new_size < current_size) {
867 E : Offset remove_offset = offset + new_size;
868 E : Size remove_size = current_size - new_size;
869 E : if (!RemoveData(remove_offset, remove_size))
870 i : return false;
871 : // We fall through so that 'always_allocate_data' can be respected.
872 : }
873 :
874 : // If we've been asked to, at least make sure that the data is allocated.
875 E : if (always_allocate_data && data_size_ < offset + new_size)
876 E : ResizeData(offset + new_size);
877 :
878 E : return true;
879 E : }
880 :
881 E : void BlockGraph::Block::SetData(const uint8* data, size_t data_size) {
882 : DCHECK((data_size == 0 && data == NULL) ||
883 E : (data_size != 0 && data != NULL));
884 E : DCHECK(data_size <= size_);
885 :
886 E : if (owns_data_)
887 E : delete [] data_;
888 :
889 E : owns_data_ = false;
890 E : data_ = data;
891 E : data_size_ = data_size;
892 E : }
893 :
894 E : uint8* BlockGraph::Block::AllocateData(size_t size) {
895 E : uint8* new_data = AllocateRawData(size);
896 E : if (new_data == NULL)
897 i : return NULL;
898 :
899 E : ::memset(new_data, 0, size);
900 E : return new_data;
901 E : }
902 :
903 E : uint8* BlockGraph::Block::CopyData(size_t size, const void* data) {
904 E : uint8* new_data = AllocateRawData(size);
905 E : if (new_data == NULL)
906 i : return NULL;
907 :
908 E : memcpy(new_data, data, size);
909 E : return new_data;
910 E : }
911 :
912 E : const uint8* BlockGraph::Block::ResizeData(size_t new_size) {
913 E : if (new_size == data_size_)
914 E : return data_;
915 :
916 E : if (!owns_data() && new_size < data_size_) {
917 : // Not in our ownership and shrinking. We only need to adjust our length.
918 E : data_size_ = new_size;
919 E : } else {
920 : // Either our own data, or it's growing (or both). We need to reallocate.
921 E : uint8* new_data = new uint8[new_size];
922 E : if (new_data == NULL)
923 i : return NULL;
924 :
925 : // Copy the (head of the) old data.
926 E : memcpy(new_data, data_, std::min(data_size_, new_size));
927 E : if (new_size > data_size_) {
928 : // Zero the tail.
929 E : memset(new_data + data_size_, 0, new_size - data_size_);
930 : }
931 :
932 E : if (owns_data())
933 E : delete [] data_;
934 :
935 E : owns_data_ = true;
936 E : data_ = new_data;
937 E : data_size_ = new_size;
938 : }
939 :
940 E : return data_;
941 E : }
942 :
943 E : uint8* BlockGraph::Block::GetMutableData() {
944 E : DCHECK(data_size_ != 0);
945 E : DCHECK(data_ != NULL);
946 :
947 : // Make a copy if we don't already own the data.
948 E : if (!owns_data()) {
949 E : uint8* new_data = new uint8[data_size_];
950 E : if (new_data == NULL)
951 i : return NULL;
952 E : memcpy(new_data, data_, data_size_);
953 E : data_ = new_data;
954 E : owns_data_ = true;
955 : }
956 E : DCHECK(owns_data_);
957 :
958 E : return const_cast<uint8*>(data_);
959 E : }
960 :
961 E : bool BlockGraph::Block::HasExternalReferrers() const {
962 E : ReferrerSet::const_iterator it = referrers().begin();
963 E : for (; it != referrers().end(); ++it) {
964 E : if (it->first != this)
965 E : return true;
966 E : }
967 E : return false;
968 E : }
969 :
970 E : bool BlockGraph::Block::SetReference(Offset offset, const Reference& ref) {
971 E : DCHECK(ref.referenced() != NULL);
972 :
973 : // Non-code blocks can be referred to by pointers that lie outside of their
974 : // extent (due to loop induction, arrays indexed with an implicit offset,
975 : // etc). Code blocks can not be referred to in this manner, because references
976 : // in code blocks must be places where the flow of execution actually lands.
977 E : if (ref.referenced()->type() == CODE_BLOCK) {
978 : DCHECK(ref.offset() >= 0 &&
979 E : static_cast<size_t>(ref.offset()) <= ref.referenced()->size());
980 E : DCHECK(offset + ref.size() <= size());
981 : }
982 :
983 : #if defined(DEBUG) || !defined(NDEBUG)
984 : {
985 : // NOTE: It might be worthwhile making SetReference return true on success,
986 : // and false on failure as it is possible for references to conflict.
987 : // For now we simply check for conflicts in debug builds and die an
988 : // unglorious death if we find any.
989 :
990 E : if (!ref.IsValid())
991 i : NOTREACHED() << "Trying to insert invalid reference.";
992 :
993 : // Examine references before us that could possibly conflict with us.
994 E : Offset offset_begin = offset - Reference::kMaximumSize + 1;
995 : ReferenceMap::const_iterator it =
996 E : references_.lower_bound(offset_begin);
997 E : for (; it != references_.end() && it->first < offset; ++it) {
998 E : if (static_cast<Offset>(it->first + it->second.size()) > offset)
999 i : NOTREACHED() << "Trying to insert conflicting reference.";
1000 E : }
1001 :
1002 : // Examine the reference at the same offset if there is one. We expect it to
1003 : // have the same size and type.
1004 E : if (it != references_.end() && it->first == offset) {
1005 E : if (it->second.size() != ref.size() || it->second.type() != ref.type()) {
1006 : }
1007 E : ++it;
1008 : }
1009 :
1010 : // This is the first reference after our offset. Check to see if it lands
1011 : // within the range we want to occupy.
1012 : if (it != references_.end() &&
1013 E : it->first < static_cast<Offset>(offset + ref.size())) {
1014 i : NOTREACHED() << "Trying to insert conflicting reference.";
1015 : }
1016 : }
1017 : #endif
1018 :
1019 : // Did we have an earlier reference at this location?
1020 E : ReferenceMap::iterator it(references_.find(offset));
1021 E : bool inserted = false;
1022 E : if (it != references_.end()) {
1023 : // Erase the back reference.
1024 E : BlockGraph::Block* referenced = it->second.referenced();
1025 E : Referrer referrer(this, offset);
1026 E : size_t removed = referenced->referrers_.erase(referrer);
1027 E : DCHECK_EQ(1U, removed);
1028 :
1029 : // Lastly switch the reference.
1030 E : it->second = ref;
1031 E : } else {
1032 : // It's a new reference, insert it.
1033 E : inserted = references_.insert(std::make_pair(offset, ref)).second;
1034 E : DCHECK(inserted);
1035 : }
1036 :
1037 : // Record the back-reference.
1038 E : ref.referenced()->referrers_.insert(std::make_pair(this, offset));
1039 :
1040 E : return inserted;
1041 E : }
1042 :
1043 : bool BlockGraph::Block::GetReference(Offset offset,
1044 E : Reference* reference) const {
1045 E : DCHECK(reference != NULL);
1046 E : ReferenceMap::const_iterator it(references_.find(offset));
1047 E : if (it == references_.end())
1048 E : return false;
1049 :
1050 E : *reference = it->second;
1051 E : return true;
1052 E : }
1053 :
1054 E : bool BlockGraph::Block::RemoveReference(Offset offset) {
1055 : // Do we have reference at this location?
1056 E : ReferenceMap::iterator it(references_.find(offset));
1057 E : if (it == references_.end())
1058 i : return false;
1059 :
1060 E : BlockGraph::Block* referenced = it->second.referenced();
1061 E : Referrer referrer(this, offset);
1062 E : size_t removed = referenced->referrers_.erase(referrer);
1063 E : DCHECK_EQ(1U, removed);
1064 E : references_.erase(it);
1065 :
1066 E : return true;
1067 E : }
1068 :
1069 E : bool BlockGraph::Block::RemoveAllReferences() {
1070 E : ReferenceMap::iterator it = references_.begin();
1071 E : while (it != references_.end()) {
1072 E : ReferenceMap::iterator to_remove = it;
1073 E : ++it;
1074 :
1075 : // TODO(rogerm): As an optimization, we don't need to drop intra-block
1076 : // references when disconnecting from the block_graph. Consider having
1077 : // BlockGraph::RemoveBlockByIterator() check that the block has no
1078 : // external referrers before calling this function and erasing the
1079 : // block.
1080 :
1081 : // Unregister this reference from the referred block then erase it.
1082 E : BlockGraph::Block* referenced = to_remove->second.referenced();
1083 E : Referrer referrer(this, to_remove->first);
1084 E : size_t removed = referenced->referrers_.erase(referrer);
1085 E : DCHECK_EQ(1U, removed);
1086 E : references_.erase(to_remove);
1087 E : }
1088 :
1089 E : return true;
1090 E : }
1091 :
1092 E : bool BlockGraph::Block::SetLabel(Offset offset, const Label& label) {
1093 E : DCHECK_LE(0, offset);
1094 E : DCHECK_LE(static_cast<size_t>(offset), size_);
1095 :
1096 E : VLOG(2) << name() << ": adding "
1097 : << LabelAttributesToString(label.attributes()) << " label '"
1098 : << label.name() << "' at offset " << offset << ".";
1099 :
1100 : // Try inserting the label into the label map.
1101 : std::pair<LabelMap::iterator, bool> result(
1102 E : labels_.insert(std::make_pair(offset, label)));
1103 :
1104 : // If it was freshly inserted then we're done.
1105 E : if (result.second)
1106 E : return true;
1107 :
1108 E : return false;
1109 E : }
1110 :
1111 E : bool BlockGraph::Block::GetLabel(Offset offset, Label* label) const {
1112 E : DCHECK(offset >= 0 && static_cast<size_t>(offset) <= size_);
1113 E : DCHECK(label != NULL);
1114 :
1115 E : LabelMap::const_iterator it = labels_.find(offset);
1116 E : if (it == labels_.end())
1117 E : return false;
1118 :
1119 E : *label = it->second;
1120 E : return true;
1121 E : }
1122 :
1123 E : bool BlockGraph::Block::RemoveLabel(Offset offset) {
1124 E : DCHECK(offset >= 0 && static_cast<size_t>(offset) <= size_);
1125 :
1126 E : return labels_.erase(offset) == 1;
1127 E : }
1128 :
1129 E : bool BlockGraph::Block::HasLabel(Offset offset) const {
1130 E : DCHECK(offset >= 0 && static_cast<size_t>(offset) <= size_);
1131 :
1132 E : return labels_.find(offset) != labels_.end();
1133 E : }
1134 :
1135 : bool BlockGraph::Block::TransferReferrers(Offset offset,
1136 E : Block* new_block) {
1137 : // Redirect all referrers to the new block, we copy the referrer set
1138 : // because it is otherwise mutated during iteration.
1139 E : BlockGraph::Block::ReferrerSet referrers = referrers_;
1140 E : BlockGraph::Block::ReferrerSet::const_iterator referrer_it(referrers.begin());
1141 :
1142 E : for (; referrer_it != referrers.end(); ++referrer_it) {
1143 : // Get the original reference.
1144 E : BlockGraph::Block::Referrer referrer = *referrer_it;
1145 : BlockGraph::Block::ReferenceMap::const_iterator found_ref(
1146 E : referrer.first->references().find(referrer.second));
1147 E : DCHECK(found_ref != referrer.first->references().end());
1148 E : BlockGraph::Reference ref(found_ref->second);
1149 :
1150 E : Offset new_offset = ref.offset() + offset;
1151 E : Offset new_base = ref.base() + offset;
1152 :
1153 : // Same thing as in SetReferrer, references to non-code blocks may lie
1154 : // outside the extent of the block.
1155 E : if (type_ == CODE_BLOCK) {
1156 : if (new_offset < 0 ||
1157 E : static_cast<size_t>(new_offset) > new_block->size()) {
1158 E : LOG(ERROR) << "Transferred reference lies outside of code block.";
1159 E : return false;
1160 : }
1161 : }
1162 :
1163 : // Redirect the reference to the new block with the adjusted offset.
1164 : BlockGraph::Reference new_ref(ref.type(),
1165 : ref.size(),
1166 : new_block,
1167 : new_offset,
1168 E : new_base);
1169 E : referrer.first->SetReference(referrer.second, new_ref);
1170 E : }
1171 :
1172 E : return true;
1173 E : }
1174 :
1175 : // Returns true if this block contains the given range of bytes.
1176 E : bool BlockGraph::Block::Contains(RelativeAddress address, size_t size) const {
1177 E : return (address >= addr_ && address + size <= addr_ + size_);
1178 E : }
1179 :
1180 E : bool BlockGraph::Reference::IsValid() const {
1181 : // We can't reference a NULL block.
1182 E : if (referenced_ == NULL)
1183 E : return false;
1184 :
1185 : // First see if the base address is valid for the referenced block.
1186 E : if (base_ < 0 || static_cast<size_t>(base_) >= referenced_->size())
1187 i : return false;
1188 :
1189 E : if (!IsValidTypeSize(type_, size_))
1190 i : return false;
1191 :
1192 E : return true;
1193 E : }
1194 :
1195 E : bool BlockGraph::Reference::IsValidTypeSize(ReferenceType type, Size size) {
1196 E : switch (type) {
1197 : // We see 8- and 32-bit relative JMPs.
1198 : case PC_RELATIVE_REF:
1199 E : return size == 1 || size == 4;
1200 :
1201 : // These guys are all pointer sized.
1202 : case ABSOLUTE_REF:
1203 : case RELATIVE_REF:
1204 : case FILE_OFFSET_REF:
1205 E : return size == 4;
1206 :
1207 : default:
1208 i : NOTREACHED() << "Unknown ReferenceType.";
1209 : }
1210 :
1211 i : return false;
1212 E : }
1213 :
1214 : } // namespace block_graph
|