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