Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 // Declaration of the filler instrumentation transform. This instruments a given | |
| 16 // list of functions by injecting NOP fillers at various places. | |
| 17 | |
| 18 #ifndef SYZYGY_INSTRUMENT_TRANSFORMS_FILLER_TRANSFORM_H_ | |
| 19 #define SYZYGY_INSTRUMENT_TRANSFORMS_FILLER_TRANSFORM_H_ | |
| 20 | |
| 21 #include <map> | |
| 22 #include <string> | |
| 23 #include <vector> | |
| 24 | |
| 25 #include "base/macros.h" | |
| 26 #include "base/files/file_path.h" | |
| 27 #include "base/memory/scoped_ptr.h" | |
| 28 #include "syzygy/block_graph/basic_block.h" | |
| 29 #include "syzygy/block_graph/block_graph.h" | |
| 30 #include "syzygy/block_graph/transforms/iterative_transform.h" | |
| 31 #include "syzygy/block_graph/transforms/named_transform.h" | |
| 32 | |
| 33 using block_graph::TransformPolicyInterface; | |
| 34 | |
| 35 namespace instrument { | |
| 36 namespace transforms { | |
| 37 | |
| 38 // A class to transform subgraph by injecting NOP fillers to basic code blocks. | |
| 39 class FillerBasicBlockTransform | |
| 40 : public block_graph::transforms::NamedBasicBlockSubGraphTransformImpl< | |
| 41 FillerBasicBlockTransform> { | |
| 42 public: | |
| 43 typedef block_graph::BasicBlock BasicBlock; | |
| 44 typedef block_graph::BasicBlockSubGraph BasicBlockSubGraph; | |
| 45 typedef block_graph::BasicCodeBlock BasicCodeBlock; | |
| 46 typedef block_graph::BlockGraph BlockGraph; | |
| 47 typedef block_graph::TransformPolicyInterface TransformPolicyInterface; | |
| 48 typedef BlockGraph::Block Block; | |
| 49 | |
| 50 // NOP sizes that can be realized with a single instruction. We disallow | |
| 51 // multi-instruction NOPs to ensure consistent instruction indexes after NOP | |
| 52 // injection. | |
| 53 enum NopSizes { | |
| 54 NOP1 = 1, | |
| 55 NOP2, | |
| 56 NOP3, | |
| 57 NOP4, | |
| 58 NOP5, | |
| 59 NOP6, | |
| 60 NOP7, | |
| 61 NOP8, | |
| 62 NOP9, | |
| 63 NOP10, | |
| 64 NOP11, | |
| 65 }; | |
| 66 | |
| 67 // A map from instruction indices to NOP sizes. For example, | |
| 68 // {1: NOP3, 3: NOP2, 4: NOP5} specifies a transformation that takes | |
| 69 // instruction sequence "ABCDE" to "AXBYZCDE", where "X" is the 3-byte NOP, | |
| 70 // "Y" is the 2-byte NOP, and "Z" is the 5-byte NOP. | |
| 71 typedef const std::map<size_t, NopSizes> NopSpec; | |
| 72 | |
| 73 FillerBasicBlockTransform() | |
| 74 : debug_friendly_(false) { } | |
| 75 virtual ~FillerBasicBlockTransform() { } | |
| 76 | |
| 77 // For NamedBlockGraphTransformImpl. | |
| 78 static const char kTransformName[]; | |
| 79 | |
| 80 // @name Accessors and mutators. | |
| 81 // @{ | |
| 82 bool debug_friendly() const { return debug_friendly_; } | |
| 83 void set_debug_friendly(bool flag) { debug_friendly_ = flag; } | |
| 84 // @} | |
| 85 | |
| 86 // Injects NOP into @p instruction. @p nop_spec specifies post-injection | |
| 87 // instruction indices and sizes of NOPs. We do not inject beyond the last | |
| 88 // instruction. Returns the number of NOPs injected. | |
|
chrisha
2015/06/10 18:07:27
Return statement no longer applicable?
huangs
2015/06/11 14:54:15
Done.
| |
| 89 static void InjectNop(const NopSpec& nop_spec, | |
| 90 bool debug_friendly, | |
| 91 BasicBlock::Instructions* instructions); | |
| 92 | |
| 93 // @name BasicBlockSubGraphTransformInterface implementation. | |
| 94 // @{ | |
| 95 // Applies the filler transform. Specifically, visits every basic code block | |
| 96 // in @p basic_block_subgraph and injects NOP at various places. | |
| 97 bool TransformBasicBlockSubGraph( | |
| 98 const TransformPolicyInterface* policy, | |
| 99 BlockGraph* block_graph, | |
| 100 BasicBlockSubGraph* basic_block_subgraph) override; | |
| 101 // @} | |
| 102 | |
| 103 private: | |
| 104 // Activate the overwriting of source range for created instructions. | |
| 105 bool debug_friendly_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(FillerBasicBlockTransform); | |
| 108 }; | |
| 109 | |
| 110 // A class to apply filler transform, which injects NOPs to basic code blocks | |
| 111 // in a given list of decorated function names. | |
| 112 class FillerTransform | |
| 113 : public block_graph::transforms::IterativeTransformImpl<FillerTransform> { | |
| 114 public: | |
| 115 typedef block_graph::BasicBlockSubGraph BasicBlockSubGraph; | |
| 116 typedef block_graph::BlockGraph BlockGraph; | |
| 117 typedef BlockGraph::Block Block; | |
| 118 | |
| 119 explicit FillerTransform(const std::vector<std::string>& target_list); | |
| 120 virtual ~FillerTransform() { } | |
| 121 | |
| 122 // Accessors | |
| 123 // @{ | |
| 124 size_t num_targets_updated() const { return num_targets_updated_; } | |
| 125 // @} | |
| 126 | |
| 127 // For NamedBlockGraphTransformImpl. | |
| 128 static const char kTransformName[]; | |
| 129 | |
| 130 // @name Accessors and mutators. | |
| 131 // @{ | |
| 132 bool debug_friendly() const { return debug_friendly_; } | |
| 133 void set_debug_friendly(bool flag) { debug_friendly_ = flag; } | |
| 134 // @} | |
| 135 | |
| 136 protected: | |
| 137 // Returns whether @p block is a target. | |
| 138 bool ShouldProcessBlock(Block* block) const; | |
| 139 | |
| 140 // Verifies that all targets were found, and displays warning if not. | |
| 141 void CheckAllTargetFound() const; | |
|
chrisha
2015/06/10 18:07:27
CheckAllTargetsFound
..............^.....
huangs
2015/06/11 14:54:15
Done.
| |
| 142 | |
| 143 // @name IterativeTransformImpl implementation. | |
| 144 // @{ | |
| 145 bool PreBlockGraphIteration(const TransformPolicyInterface* policy, | |
| 146 BlockGraph* block_graph, | |
| 147 Block* header_block); | |
| 148 bool OnBlock(const TransformPolicyInterface* policy, | |
| 149 BlockGraph* block_graph, | |
| 150 Block* block); | |
| 151 bool PostBlockGraphIteration(const TransformPolicyInterface* policy, | |
| 152 BlockGraph* block_graph, | |
| 153 Block* header_block); | |
| 154 // @} | |
| 155 | |
| 156 private: | |
| 157 friend NamedBlockGraphTransformImpl<FillerTransform>; | |
| 158 friend IterativeTransformImpl<FillerTransform>; | |
| 159 | |
| 160 // Activate the overwriting of source range for created instructions. | |
| 161 bool debug_friendly_; | |
| 162 | |
| 163 // Names of targets, maps to whether the target was seen. | |
| 164 std::map<std::string, bool> target_names_; | |
| 165 | |
| 166 // Counters used by CheckAllTargetFound. | |
| 167 // @{ | |
| 168 size_t num_blocks_; | |
| 169 size_t num_code_blocks_; | |
| 170 size_t num_targets_updated_; | |
| 171 // @} | |
| 172 | |
| 173 DISALLOW_COPY_AND_ASSIGN(FillerTransform); | |
| 174 }; | |
| 175 | |
| 176 } // namespace transforms | |
| 177 } // namespace instrument | |
| 178 | |
| 179 #endif // SYZYGY_INSTRUMENT_TRANSFORMS_FILLER_TRANSFORM_H_ | |
| OLD | NEW |