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_util.h"
16 :
17 : #include <algorithm>
18 : #include <vector>
19 :
20 : namespace block_graph {
21 :
22 : bool CodeBlockAttributesAreBasicBlockSafe(
23 E : const block_graph::BlockGraph::Block* block) {
24 E : DCHECK(block != NULL);
25 E : DCHECK_EQ(BlockGraph::CODE_BLOCK, block->type());
26 :
27 : // If the block was built by our toolchain it's inherently safe. This
28 : // attribute is used to whitelist a block.
29 E : if (block->attributes() & BlockGraph::BUILT_BY_SYZYGY)
30 E : return true;
31 :
32 : // Any of the following attributes make it unsafe to basic-block
33 : // decompose the code block.
34 : static const BlockGraph::BlockAttributes kInvalidAttributes =
35 : BlockGraph::GAP_BLOCK |
36 : BlockGraph::PADDING_BLOCK |
37 : BlockGraph::HAS_INLINE_ASSEMBLY |
38 : BlockGraph::BUILT_BY_UNSUPPORTED_COMPILER |
39 : BlockGraph::ERRORED_DISASSEMBLY |
40 : BlockGraph::HAS_EXCEPTION_HANDLING |
41 : BlockGraph::DISASSEMBLED_PAST_END;
42 E : if ((block->attributes() & kInvalidAttributes) != 0)
43 E : return false;
44 :
45 E : return true;
46 E : }
47 :
48 : bool GetBasicBlockSourceRange(const BasicCodeBlock& bb,
49 E : BlockGraph::Block::SourceRange* source_range) {
50 E : DCHECK(source_range != NULL);
51 :
52 : typedef BlockGraph::Block::SourceRange SourceRange;
53 E : std::vector<SourceRange> ranges;
54 :
55 : // Collect all the instruction and successor source ranges.
56 E : BasicBlock::Instructions::const_iterator inst_it(bb.instructions().begin());
57 E : for (; inst_it != bb.instructions().end(); ++inst_it) {
58 E : const SourceRange& range = inst_it->source_range();
59 E : if (range.size() > 0)
60 E : ranges.push_back(range);
61 E : }
62 E : BasicBlock::Successors::const_iterator succ_it(bb.successors().begin());
63 E : for (; succ_it != bb.successors().end(); ++succ_it) {
64 E : const SourceRange& range = succ_it->source_range();
65 E : if (range.size() > 0)
66 E : ranges.push_back(range);
67 E : }
68 :
69 E : if (ranges.size() == 0)
70 E : return false;
71 :
72 : // Sort the ranges.
73 E : std::sort(ranges.begin(), ranges.end());
74 :
75 : // Test that they're all contiguous, while computing their total length.
76 E : SourceRange::Size size = ranges[0].size();
77 E : for (size_t i = 0; i < ranges.size() - 1; ++i) {
78 E : size += ranges[i + 1].size();
79 E : if (ranges[i].start() + ranges[i].size() != ranges[i + 1].start())
80 E : return false;
81 E : }
82 E : *source_range = SourceRange(ranges[0].start(), size);
83 :
84 E : return true;
85 E : }
86 :
87 : bool IsUnsafeReference(const BlockGraph::Block* referrer,
88 E : const BlockGraph::Reference& ref) {
89 : // Skip references with a non-zero offset if we're
90 : // not instrumenting unsafe references.
91 E : if (ref.offset() != 0)
92 E : return true;
93 :
94 : BlockGraph::BlockAttributes kUnsafeAttribs =
95 : BlockGraph::HAS_INLINE_ASSEMBLY |
96 E : BlockGraph::BUILT_BY_UNSUPPORTED_COMPILER;
97 :
98 E : bool unsafe_referrer = false;
99 : if (referrer->type() == BlockGraph::CODE_BLOCK &&
100 E : (referrer->attributes() & kUnsafeAttribs) != 0) {
101 E : unsafe_referrer = true;
102 : }
103 :
104 E : DCHECK_EQ(BlockGraph::CODE_BLOCK, ref.referenced()->type());
105 E : bool unsafe_block = (ref.referenced()->attributes() & kUnsafeAttribs) != 0;
106 :
107 : // If both the referrer and the referenced blocks are unsafe, we can't
108 : // safely assume that this reference represents a call semantics,
109 : // e.g. where a return address is at the top of stack at entry.
110 : // Ideally we'd decide this on the basis of a full stack analysis, but
111 : // beggers can't be choosers, plus for hand-coded assembly that's
112 : // the halting problem :).
113 : // For instrumentation that uses return address swizzling, instrumenting
114 : // an unsafe reference leads to crashes, so better to back off and get
115 : // slightly less coverage.
116 E : return unsafe_referrer && unsafe_block;
117 E : }
118 :
119 : } // namespace block_graph
|