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 : // The main class of the hot patching Asan runtime library.
16 : //
17 : // A single instance of this class is created by the DllMain of module of
18 : // the hot patching Asan runtime library and can be accessed from anywhere
19 : // via |HotPatchingAsanRuntime::runtime()|.
20 :
21 : #ifndef SYZYGY_AGENT_ASAN_HOT_PATCHING_ASAN_RUNTIME_H_
22 : #define SYZYGY_AGENT_ASAN_HOT_PATCHING_ASAN_RUNTIME_H_
23 :
24 : #include <windows.h>
25 : #include <string>
26 : #include <unordered_set>
27 :
28 : #include "base/logging.h"
29 : #include "base/memory/scoped_ptr.h"
30 : #include "base/memory/singleton.h"
31 : #include "syzygy/agent/common/entry_frame.h"
32 :
33 : namespace agent {
34 : namespace asan {
35 :
36 : class AsanLogger;
37 :
38 : class HotPatchingAsanRuntime {
39 : public:
40 : // Hot patching Asan transform instruments the entry point of the modules so
41 : // that this function is called before each DllMain call of the instrumented
42 : // modules. At this point the code of the hot patching runtime module is
43 : // already loaded so, this is a good place to do hot patching.
44 : // @param entry_frame A frame containing the return address and the parameters
45 : // of the original DllMain function.
46 : static void WINAPI DllMainEntryHook(agent::EntryFrame* entry_frame,
47 : FuncAddr function);
48 :
49 : // Access to the singleton class.
50 : // @returns the hot patching Asan runtime.
51 : static HotPatchingAsanRuntime* GetInstance() {
52 : return base::Singleton<HotPatchingAsanRuntime>::get();
53 : }
54 :
55 : // Activates the hot patching Asan mode on a given module.
56 : // @param instance The handle to the module.
57 : // NOTE: The current implementation of this function is not thread-safe. This
58 : // is not a problem for now, because we call this function under the
59 : // loader lock.
60 : bool HotPatch(HINSTANCE instance);
61 :
62 : // Sets up the hot patching Asan runtime.
63 : void SetUp();
64 :
65 : // Gets the set of modules that have already been hot patched.
66 : // @returns a set containing the handles of the hot patched modules.
67 E : const std::unordered_set<HMODULE>& hot_patched_modules() {
68 E : return hot_patched_modules_;
69 E : }
70 :
71 : // Gets a logger.
72 : AsanLogger* logger() {
73 : DCHECK_NE(static_cast<AsanLogger*>(nullptr), logger_.get());
74 : return logger_.get();
75 : }
76 :
77 : protected:
78 : void SetUpLogger();
79 :
80 : // The shared logger instance that will be used to report errors and runtime
81 : // information.
82 : scoped_ptr<AsanLogger> logger_;
83 :
84 : // Set of modules that have already been hot patched. We don't want to hot
85 : // patch the same module twice.
86 : std::unordered_set<HMODULE> hot_patched_modules_;
87 :
88 : private:
89 : friend struct base::DefaultSingletonTraits<HotPatchingAsanRuntime>;
90 : friend class HotPatchingAsanRuntimeTest;
91 :
92 : HotPatchingAsanRuntime();
93 : ~HotPatchingAsanRuntime();
94 :
95 : DISALLOW_COPY_AND_ASSIGN(HotPatchingAsanRuntime);
96 : };
97 :
98 : } // namespace asan
99 : } // namespace agent
100 :
101 : extern "C" {
102 :
103 : // Exposes the hot patching Asan runtime to the unittests.
104 : // @returns the runtime instance.
105 : agent::asan::HotPatchingAsanRuntime* hp_asan_GetActiveHotPatchingAsanRuntime();
106 :
107 : }
108 :
109 : #endif // SYZYGY_AGENT_ASAN_HOT_PATCHING_ASAN_RUNTIME_H_
|