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 :
52 : // @name Accessors. For testing.
53 : // @{
54 E : const BlockGraph::Section* thunk_section() const { return thunk_section_; }
55 E : AddIndexedFrequencyDataTransform* add_frequency_data() {
56 E : return &add_frequency_data_;
57 E : }
58 E : BlockGraph::Reference* jump_table_case_counter_hook_ref() {
59 E : return &jump_table_case_counter_hook_ref_;
60 E : }
61 : // @}
62 :
63 : private:
64 : friend NamedBlockGraphTransformImpl<JumpTableCaseCountTransform>;
65 : friend IterativeTransformImpl<JumpTableCaseCountTransform>;
66 :
67 : typedef core::RelativeAddress RelativeAddress;
68 : // A pair containing the address of a jump-table and its size.
69 : typedef std::pair<RelativeAddress, size_t> JumpTableInfo;
70 : typedef std::vector<JumpTableInfo> JumpTableVector;
71 :
72 : // The name of this transform.
73 : static const char kTransformName[];
74 :
75 : // @name IterativeTransformImpl implementation.
76 : // @{
77 : bool PreBlockGraphIteration(BlockGraph* block_graph,
78 : BlockGraph::Block* header_block);
79 : bool OnBlock(BlockGraph* block_graph, BlockGraph::Block* block);
80 : bool PostBlockGraphIteration(BlockGraph* block_graph,
81 : BlockGraph::Block* header_block);
82 : // @}
83 :
84 : // Creates a single thunk to destination.
85 : // @param block_graph the block-graph being instrumented.
86 : // @param destination the destination reference.
87 : // @returns a pointer to the thunk on success, NULL otherwise.
88 : BlockGraph::Block* CreateOneThunk(BlockGraph* block_graph,
89 : const BlockGraph::Reference& destination);
90 :
91 : // The section we put our thunks in.
92 : BlockGraph::Section* thunk_section_;
93 :
94 : // Adds the jump table frequency data referenced by the jump-table
95 : // instrumentation.
96 : AddIndexedFrequencyDataTransform add_frequency_data_;
97 :
98 : // The entry hook to which jump table entry events are directed.
99 : BlockGraph::Reference jump_table_case_counter_hook_ref_;
100 :
101 : // The instrumentation dll used by this transform.
102 : std::string instrument_dll_name_;
103 :
104 : // The counter used to get a unique ID for each case in a jump table.
105 : size_t jump_table_case_count_;
106 :
107 : // The different jump tables encountered; we store their addresses and sizes.
108 : JumpTableVector jump_table_infos_;
109 :
110 : DISALLOW_COPY_AND_ASSIGN(JumpTableCaseCountTransform);
111 : };
112 :
113 : } // namespace transforms
114 : } // namespace instrument
115 :
116 : #endif // SYZYGY_INSTRUMENT_TRANSFORMS_JUMP_TABLE_COUNT_TRANSFORM_H_
|