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 : // Declares a simple API for transforming BlockGraphs in situ.
16 :
17 : #ifndef SYZYGY_BLOCK_GRAPH_TRANSFORM_H_
18 : #define SYZYGY_BLOCK_GRAPH_TRANSFORM_H_
19 :
20 : #include "syzygy/block_graph/basic_block_subgraph.h"
21 : #include "syzygy/block_graph/block_graph.h"
22 : #include "syzygy/block_graph/ordered_block_graph.h"
23 : #include "syzygy/block_graph/transform_policy.h"
24 : #include "syzygy/pe/image_layout.h"
25 :
26 : namespace block_graph {
27 :
28 : // A BlockGraphTransform is a pure virtual base class defining the transform
29 : // API.
30 : class BlockGraphTransformInterface {
31 : public:
32 E : virtual ~BlockGraphTransformInterface() { }
33 :
34 : // Gets the name of this transform.
35 : //
36 : // @returns the name of this transform.
37 : virtual const char* name() const = 0;
38 :
39 : // Applies this transform to the provided block graph.
40 : //
41 : // @param policy The policy object restricting how the transform is applied.
42 : // @param block_graph The block graph to transform.
43 : // @param header_block The header block of the block graph to transform.
44 : // @returns true on success, false otherwise.
45 : virtual bool TransformBlockGraph(const TransformPolicyInterface* policy,
46 : BlockGraph* block_graph,
47 : BlockGraph::Block* header_block) = 0;
48 : };
49 :
50 : // This applies the provided BlockGraphTransform and checks that invariant has
51 : // been satisfied; namely, that the header block has not been deleted from the
52 : // block graph.
53 : //
54 : // @param transform The transform to apply.
55 : // @param policy The policy object restricting how the transform is applied.
56 : // @param block_graph The block graph to transform.
57 : // @param header_block The header block from block_graph.
58 : // @returns true on success, false otherwise.
59 : bool ApplyBlockGraphTransform(BlockGraphTransformInterface* transform,
60 : const TransformPolicyInterface* policy,
61 : BlockGraph* block_graph,
62 : BlockGraph::Block* header_block);
63 :
64 : // This applies the provided BlockGraphTransforms in series and checks that
65 : // the invariant has been satisfied; namely, that the header block has not been
66 : // deleted from the block graph.
67 : //
68 : // @param transforms The transforms to apply.
69 : // @param policy The policy object restricting how the transform is applied.
70 : // @param block_graph The block graph to transform.
71 : // @param header_block The header block from block_graph.
72 : // @returns true on success, false otherwise.
73 : bool ApplyBlockGraphTransforms(
74 : const std::vector<BlockGraphTransformInterface*>& transforms,
75 : const TransformPolicyInterface* policy,
76 : BlockGraph* block_graph,
77 : BlockGraph::Block* header_block);
78 :
79 : // A BasicBlockSubGraphTransform is a pure virtual base class defining the
80 : // basic-block transform API.
81 : class BasicBlockSubGraphTransformInterface {
82 : public:
83 E : virtual ~BasicBlockSubGraphTransformInterface() { }
84 :
85 : // Gets the name of this transform.
86 : //
87 : // @returns the name of this transform.
88 : virtual const char* name() const = 0;
89 :
90 : // Applies this transform to the provided block.
91 : //
92 : // @param policy The policy object restricting how the transform is applied.
93 : // @param block_graph the block-graph of which the basic block subgraph
94 : // is a part.
95 : // @param basic_block_subgraph the basic block subgraph to be transformed.
96 : // @returns true on success, false otherwise.
97 : virtual bool TransformBasicBlockSubGraph(
98 : const TransformPolicyInterface* policy,
99 : BlockGraph* block_graph,
100 : BasicBlockSubGraph* basic_block_subgraph) = 0;
101 : };
102 :
103 : // Applies the provided BasicBlockSubGraphTransform to a single block. Takes
104 : // care of basic-block decomposing the block, passes it to the transform, and
105 : // recomposes the block.
106 : //
107 : // @param transform the transform to apply.
108 : // @param policy The policy object restricting how the transform is applied.
109 : // @param block_graph the block containing the block to be transformed.
110 : // @param block the block to be transformed.
111 : // @param new_blocks On success, any newly created blocks will be returned
112 : // here. Note that this parameter may be NULL if you are not interested
113 : // in retrieving the set of new blocks.
114 : // @pre block must be a code block.
115 : // @returns true on success, false otherwise.
116 : bool ApplyBasicBlockSubGraphTransform(
117 : BasicBlockSubGraphTransformInterface* transform,
118 : const TransformPolicyInterface* policy,
119 : BlockGraph* block_graph,
120 : BlockGraph::Block* block,
121 : BlockVector* new_blocks);
122 :
123 : // Applies a series of BasicBlockSubGraphTransform to a single block. Takes
124 : // care of basic-block decomposing the block, passes it to the transform, and
125 : // recomposes the block.
126 : //
127 : // @param transforms the series of transform to apply.
128 : // @param policy The policy object restricting how the transform is applied.
129 : // @param block_graph the block containing the block to be transformed.
130 : // @param block the block to be transformed.
131 : // @param new_blocks On success, any newly created blocks will be returned
132 : // here. Note that this parameter may be NULL if you are not interested
133 : // in retrieving the set of new blocks.
134 : // @pre block must be a code block.
135 : // @returns true on success, false otherwise.
136 : bool ApplyBasicBlockSubGraphTransforms(
137 : const std::vector<BasicBlockSubGraphTransformInterface*>& transforms,
138 : const TransformPolicyInterface* policy,
139 : BlockGraph* block_graph,
140 : BlockGraph::Block* block,
141 : BlockVector* new_blocks);
142 :
143 : // An ImageLayoutTransformInterface is a pure virtual base class defining the
144 : // PE image layout transform API
145 : class ImageLayoutTransformInterface {
146 : public:
147 E : virtual ~ImageLayoutTransformInterface() { }
148 :
149 : // Gets the name of this transform.
150 : //
151 : // @returns the name of this transform
152 : virtual const char* name() const = 0;
153 :
154 : // Applies this layout transform to the provided PE image. Contents of block
155 : // data can be changed in-place, and references may be deleted, created and
156 : // modified. However one cannot add, delete or reorder blocks and/or sections
157 : // nor can the size of blocks or sections be changed by adding / deleting
158 : // data bytes.
159 : //
160 : // @param policy The policy object restricting how the transform is applied.
161 : // @param image_layout The PE image on which to apply the transform.
162 : // @param ordered_block_graph A block graph view of the PE image
163 : // @return true if successful, false otherwise
164 : virtual bool TransformImageLayout(
165 : const TransformPolicyInterface* policy,
166 : const pe::ImageLayout* image_layout,
167 : const OrderedBlockGraph* ordered_block_graph) = 0;
168 : };
169 :
170 : // Applies a single layout transform to a PE image. Checks if the transform
171 : // preserves the number of blocks, the size and order of all blocks in the
172 : // PE image.
173 : //
174 : // @param transform The transform to apply.
175 : // @param policy The policy object restricting how the transform is applied.
176 : // @param image_layout The PE image on which the transform is applied.
177 : // @param ordered_block_graph A block graph view of the PE image.
178 : // @return true if successful, false otherwise.
179 : bool ApplyImageLayoutTransform(
180 : ImageLayoutTransformInterface* transform,
181 : const TransformPolicyInterface* policy,
182 : const pe::ImageLayout* image_layout,
183 : const OrderedBlockGraph* ordered_block_graph);
184 :
185 : // Applies a series of layout transform to a PE image. Checks if the transforms
186 : // preserve the number of blocks, the size and order of all blocks in the
187 : // PE image.
188 : //
189 : // @param transforms The series of transforms to apply.
190 : // @param policy The policy object restricting how the transform is applied.
191 : // @param image_layout The PE image on which the transform is applied.
192 : // @param ordered_block_graph A block graph view of the PE image.
193 : // @return true if successful, false otherwise.
194 : bool ApplyImageLayoutTransforms(
195 : const std::vector<ImageLayoutTransformInterface*>& transforms,
196 : const TransformPolicyInterface* policy,
197 : const pe::ImageLayout* image_layout,
198 : const OrderedBlockGraph* ordered_block_graph);
199 :
200 : } // namespace block_graph
201 :
202 : #endif // SYZYGY_BLOCK_GRAPH_TRANSFORM_H_
|