1 : // Copyright 2013 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 : // Implementation of the jump table count instrumentation transform.
16 : //
17 : // The purpose of this instrumentation is to count the number of times each jump
18 : // table entry is dereferenced. To do this we redirect each reference in the
19 : // jump tables to the following hook:
20 : // push unique_id_for_this_case
21 : // call jump_table_count.dll!_jump_table_case_counter
22 : // jmp original_reference
23 :
24 : #ifndef SYZYGY_INSTRUMENT_TRANSFORMS_JUMP_TABLE_COUNT_TRANSFORM_H_
25 : #define SYZYGY_INSTRUMENT_TRANSFORMS_JUMP_TABLE_COUNT_TRANSFORM_H_
26 :
27 : #include <string>
28 : #include <utility>
29 : #include <vector>
30 :
31 : #include "syzygy/block_graph/basic_block.h"
32 : #include "syzygy/block_graph/transforms/iterative_transform.h"
33 : #include "syzygy/block_graph/transforms/named_transform.h"
34 : #include "syzygy/instrument/transforms/add_indexed_frequency_data_transform.h"
35 :
36 : namespace instrument {
37 : namespace transforms {
38 :
39 : // An iterative transform that instruments the accesses to the jump/case tables
40 : // to measure the frequency of each case.
41 : class JumpTableCaseCountTransform
42 : : public block_graph::transforms::IterativeTransformImpl<
43 : JumpTableCaseCountTransform> {
44 : public:
45 : // Initialize a new JumpTableCaseCountTransform instance using the default
46 : // module and function names.
47 : JumpTableCaseCountTransform();
48 :
49 : protected:
50 : typedef block_graph::BlockGraph BlockGraph;
51 : typedef block_graph::TransformPolicyInterface TransformPolicyInterface;
52 :
53 : // @name Accessors. For testing.
54 : // @{
55 E : const BlockGraph::Section* thunk_section() const { return thunk_section_; }
56 E : AddIndexedFrequencyDataTransform* add_frequency_data() {
57 E : return &add_frequency_data_;
58 E : }
59 E : BlockGraph::Reference* jump_table_case_counter_hook_ref() {
60 E : return &jump_table_case_counter_hook_ref_;
61 E : }
62 : // @}
63 :
64 : private:
65 : friend NamedBlockGraphTransformImpl<JumpTableCaseCountTransform>;
66 : friend IterativeTransformImpl<JumpTableCaseCountTransform>;
67 :
68 : typedef core::RelativeAddress RelativeAddress;
69 : // A pair containing the address of a jump-table and its size.
70 : typedef std::pair<RelativeAddress, size_t> JumpTableInfo;
71 : typedef std::vector<JumpTableInfo> JumpTableVector;
72 :
73 : // The name of this transform.
74 : static const char kTransformName[];
75 :
76 : // @name IterativeTransformImpl implementation.
77 : // @{
78 : bool PreBlockGraphIteration(const TransformPolicyInterface* policy,
79 : BlockGraph* block_graph,
80 : BlockGraph::Block* header_block);
81 : bool OnBlock(const TransformPolicyInterface* policy,
82 : BlockGraph* block_graph,
83 : BlockGraph::Block* block);
84 : bool PostBlockGraphIteration(const TransformPolicyInterface* policy,
85 : BlockGraph* block_graph,
86 : BlockGraph::Block* header_block);
87 : // @}
88 :
89 : // Creates a single thunk to destination.
90 : // @param block_graph the block-graph being instrumented.
91 : // @param destination the destination reference.
92 : // @returns a pointer to the thunk on success, NULL otherwise.
93 : BlockGraph::Block* CreateOneThunk(BlockGraph* block_graph,
94 : const BlockGraph::Reference& destination);
95 :
96 : // The section we put our thunks in.
97 : BlockGraph::Section* thunk_section_;
98 :
99 : // Adds the jump table frequency data referenced by the jump-table
100 : // instrumentation.
101 : AddIndexedFrequencyDataTransform add_frequency_data_;
102 :
103 : // The entry hook to which jump table entry events are directed.
104 : BlockGraph::Reference jump_table_case_counter_hook_ref_;
105 :
106 : // The instrumentation dll used by this transform.
107 : std::string instrument_dll_name_;
108 :
109 : // The counter used to get a unique ID for each case in a jump table.
110 : size_t jump_table_case_count_;
111 :
112 : // The different jump tables encountered; we store their addresses and sizes.
113 : JumpTableVector jump_table_infos_;
114 :
115 : DISALLOW_COPY_AND_ASSIGN(JumpTableCaseCountTransform);
116 : };
117 :
118 : } // namespace transforms
119 : } // namespace instrument
120 :
121 : #endif // SYZYGY_INSTRUMENT_TRANSFORMS_JUMP_TABLE_COUNT_TRANSFORM_H_
|