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 : // Declaration of the entry call instrumentation transform. This instruments
16 : // individual functions by injecting a call to a transformation import at the
17 : // start of each function.
18 :
19 : #ifndef SYZYGY_INSTRUMENT_TRANSFORMS_ENTRY_CALL_TRANSFORM_H_
20 : #define SYZYGY_INSTRUMENT_TRANSFORMS_ENTRY_CALL_TRANSFORM_H_
21 :
22 : #include <set>
23 : #include <string>
24 :
25 : #include "base/strings/string_piece.h"
26 : #include "syzygy/block_graph/basic_block.h"
27 : #include "syzygy/block_graph/iterate.h"
28 : #include "syzygy/block_graph/transforms/iterative_transform.h"
29 : #include "syzygy/block_graph/transforms/named_transform.h"
30 : #include "syzygy/pe/pe_utils.h"
31 :
32 : namespace instrument {
33 : namespace transforms {
34 :
35 : class EntryCallBasicBlockTransform
36 : : public block_graph::transforms::NamedBasicBlockSubGraphTransformImpl<
37 : EntryCallBasicBlockTransform> {
38 : public:
39 : typedef block_graph::BlockGraph BlockGraph;
40 : typedef block_graph::BasicBlockSubGraph BasicBlockSubGraph;
41 : typedef block_graph::TransformPolicyInterface TransformPolicyInterface;
42 :
43 : EntryCallBasicBlockTransform(
44 : const BlockGraph::Reference& hook_reference,
45 : bool debug_friendly);
46 :
47 : // For NamedBlockGraphTransformImpl.
48 : static const char kTransformName[];
49 :
50 : protected:
51 : // @name BasicBlockSubGraphTransformInterface implementation.
52 : // @{
53 : virtual bool TransformBasicBlockSubGraph(
54 : const TransformPolicyInterface* policy,
55 : BlockGraph* block_graph,
56 : BasicBlockSubGraph* basic_block_subgraph);
57 : // @}
58 :
59 : private:
60 : // Iff true, assigns the first instruction's source range to
61 : // the inserted call.
62 : bool debug_friendly_;
63 : // The hook we call to.
64 : const BlockGraph::Reference hook_reference_;
65 :
66 : DISALLOW_COPY_AND_ASSIGN(EntryCallBasicBlockTransform);
67 : };
68 :
69 : class EntryCallTransform
70 : : public block_graph::transforms::IterativeTransformImpl<
71 : EntryCallTransform> {
72 : public:
73 : typedef block_graph::BlockGraph BlockGraph;
74 : typedef block_graph::TransformPolicyInterface TransformPolicyInterface;
75 :
76 : explicit EntryCallTransform(bool debug_friendly);
77 :
78 : // @name Accessors.
79 : // @{
80 E : bool debug_friendly() const { return debug_friendly_; }
81 E : void set_instrument_dll_name(const base::StringPiece& instrument_dll_name) {
82 E : instrument_dll_name.CopyToString(&instrument_dll_name_);
83 E : }
84 E : const char* instrument_dll_name() const {
85 E : return instrument_dll_name_.c_str();
86 E : }
87 : // @}
88 :
89 : // The name of the import for general entry hooks.
90 : static const char kEntryHookName[];
91 : // The name of the import for DllMain-like function entry hooks.
92 : static const char kDllMainEntryHookName[];
93 : // The name of the import for EXE entry point hook.
94 : static const char kExeMainEntryHookName[];
95 :
96 : // The name of the DLL imported default.
97 : static const char kDefaultInstrumentDll[];
98 :
99 : protected:
100 : typedef std::map<BlockGraph::Offset, BlockGraph::Block*> ThunkBlockMap;
101 :
102 : // @name IterativeTransformImpl implementation.
103 : // @{
104 : bool PreBlockGraphIteration(const TransformPolicyInterface* policy,
105 : BlockGraph* block_graph,
106 : BlockGraph::Block* header_block);
107 : bool OnBlock(const TransformPolicyInterface* policy,
108 : BlockGraph* block_graph,
109 : BlockGraph::Block* block);
110 : bool PostBlockGraphIteration(const TransformPolicyInterface* policy,
111 : BlockGraph* block_graph,
112 : BlockGraph::Block* header_block);
113 : // @}
114 :
115 : private:
116 : friend IterativeTransformImpl<EntryCallTransform>;
117 : friend NamedBlockGraphTransformImpl<EntryCallTransform>;
118 :
119 : bool GetEntryPoints(BlockGraph::Block* header_block);
120 :
121 : // For NamedBlockGraphTransformImpl.
122 : static const char kTransformName[];
123 :
124 : // References to _indirect_penter and _indirect_penter_dllmain import
125 : // entries. Valid after successful PreBlockGraphIteration.
126 : BlockGraph::Reference hook_ref_;
127 : BlockGraph::Reference hook_dllmain_ref_;
128 : BlockGraph::Reference hook_exe_entry_ref_;
129 :
130 : // Iff true, assigns the first instruction's source range to
131 : // inserted calls.
132 : bool debug_friendly_;
133 :
134 : // Name of the instrumentation DLL we import.
135 : // Defaults to "profile_client.dll".
136 : std::string instrument_dll_name_;
137 :
138 : // This contains the set of entrypoints that have DllMain calling conventions.
139 : // These are thunked to the dllmain hook import, instead of the generic
140 : // hook import. Valid after successful call to GetEntryPoints.
141 : pe::EntryPointSet dllmain_entrypoints_;
142 : // If the module being instrumented is an executable, this will hold the
143 : // EXE main entry point. Valid after successful call to GetEntryPoints.
144 : pe::EntryPoint exe_entry_point_;
145 :
146 : DISALLOW_COPY_AND_ASSIGN(EntryCallTransform);
147 : };
148 :
149 : } // namespace transforms
150 : } // namespace instrument
151 :
152 : #endif // SYZYGY_INSTRUMENT_TRANSFORMS_ENTRY_CALL_TRANSFORM_H_
|