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 : // Utilities for dealing with block-graphs and blocks.
16 :
17 : #ifndef SYZYGY_BLOCK_GRAPH_BLOCK_UTIL_H_
18 : #define SYZYGY_BLOCK_GRAPH_BLOCK_UTIL_H_
19 :
20 : #include "syzygy/block_graph/basic_block.h"
21 : #include "syzygy/block_graph/basic_block_subgraph.h"
22 : #include "syzygy/block_graph/block_graph.h"
23 :
24 m : namespace block_graph {
25 :
26 : // Determines whether @p block's attributes preclude basic-block
27 : // decomposition.
28 : // @param block the code block whose attributes are to be inspected.
29 : // @returns true if the block attributes are safe for decomposition to basic-
30 : // blocks, false otherwise.
31 : // @pre block has type CODE_BLOCK.
32 m : bool CodeBlockAttributesAreBasicBlockSafe(const BlockGraph::Block* block);
33 :
34 : // Determines whether @p bb's instructions and successors comprise a contiguous
35 : // source range, and return it if so.
36 : // @param bb the basic block to inspect.
37 : // @param source_range returns @p bb's source range on success.
38 : // @returns true iff @p bb's instructions and successors comprise a contiguous
39 : // source range.
40 : // @note @p bb's source range is deemed contiguous if at least one instruction
41 : // or successor has a source range, and if all the source ranges constitute
42 : // a single contiguous range, irrespective order. This means that this
43 : // function may succeed even if instructions in @p bb have been added,
44 : // reordered or mutated.
45 m : bool GetBasicBlockSourceRange(const BasicCodeBlock& bb,
46 m : BlockGraph::Block::SourceRange* source_range);
47 :
48 : // Returns true if the given references @p ref from @p referrer may not safely
49 : // be redirected. If both the referrer and the referenced blocks are irregular
50 : // in any way (i.e., inline assembly, not generated by cl.exe, etc) we cannot
51 : // safely assume that @p ref has call semantics, i.e., where a return address
52 : // is at the top of stack at entry. Ideally we would decide this on the basis
53 : // of a full stack analysis, but beggars can't be choosers; plus, for
54 : // hand-coded assembly that's the halting problem :).
55 : // For any instrumentation or manipulation that uses return address swizzling,
56 : // instrumenting an unsafe reference generally leads to crashes.
57 m : bool IsUnsafeReference(const BlockGraph::Block* referrer,
58 m : const BlockGraph::Reference& ref);
59 :
60 : // Returns true if there are any instructions manipulating the stack frame
61 : // pointer in an unexpected way. We expect the compiler to produce a standard
62 : // stack frame with two pointers (base EBP, top ESP). Any writes to EBP inside
63 : // this scope is reported as unexpected by this function.
64 : // @param subgraph The subgraph to inspect.
65 m : bool HasUnexpectedStackFrameManipulation(BasicBlockSubGraph* subgraph);
66 :
67 : // Calculates the number of entries in a given jump table. A jump table is a run
68 : // of contiguous 32-bit references terminating when there is no next reference,
69 : // at the next data label or the end of the block, whichever comes first.
70 : // @param block The block containing this jump table.
71 : // @param jump_table_label An iterator to the jump table label in this block.
72 : // @param table_size Will receive the size of the jump table.
73 : // @returns true on success, false otherwise.
74 m : bool GetJumpTableSize(const block_graph::BlockGraph::Block* block,
75 m : block_graph::BlockGraph::Block::LabelMap::const_iterator jump_table_label,
76 m : size_t* table_size);
77 :
78 m : } // namespace block_graph
79 :
80 : #endif // SYZYGY_BLOCK_GRAPH_BLOCK_UTIL_H_
|