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 : #include "syzygy/agent/asan/hot_patching_asan_runtime.h"
16 :
17 : #include "base/command_line.h"
18 : #include "base/environment.h"
19 : #include "base/strings/utf_string_conversions.h"
20 : #include "syzygy/agent/asan/logger.h"
21 : #include "syzygy/trace/client/client_utils.h"
22 : #include "syzygy/trace/protocol/call_trace_defs.h"
23 :
24 : namespace agent {
25 : namespace asan {
26 :
27 : HotPatchingAsanRuntime::HotPatchingAsanRuntime() { }
28 :
29 : HotPatchingAsanRuntime::~HotPatchingAsanRuntime() { }
30 :
31 E : bool HotPatchingAsanRuntime::HotPatch(HINSTANCE instance) {
32 : logger_->Write("HPSyzyAsan: Started hot patching. Module: " +
33 : std::to_string(reinterpret_cast<int>(instance)) +
34 : " PID: " +
35 E : std::to_string(GetCurrentProcessId()));
36 :
37 E : if (hot_patched_modules_.count(instance)) {
38 E : logger_->Write("HPSyzyAsan - Already tried to hot patch, exiting.");
39 E : return true;
40 : }
41 i : hot_patched_modules_.insert(instance);
42 :
43 : // TODO(cseri): Do the hot patching.
44 i : logger_->Write("HPSyzyAsan: Hot patching not yet implemented.");
45 :
46 i : return true;
47 E : }
48 :
49 : void HotPatchingAsanRuntime::SetUp() {
50 : SetUpLogger();
51 :
52 : logger_->Write("HPSyzyAsan: Runtime loaded.");
53 : }
54 :
55 : void HotPatchingAsanRuntime::SetUpLogger() {
56 : // Setup variables we're going to use.
57 : scoped_ptr<base::Environment> env(base::Environment::Create());
58 : scoped_ptr<AsanLogger> client(new AsanLogger);
59 : CHECK(env.get() != NULL);
60 : CHECK(client.get() != NULL);
61 :
62 : // Initialize the client.
63 : client->set_instance_id(
64 : base::UTF8ToWide(trace::client::GetInstanceIdForThisModule()));
65 : client->Init();
66 :
67 : // Register the client singleton instance.
68 : logger_.reset(client.release());
69 : }
70 :
71 : void WINAPI HotPatchingAsanRuntime::DllMainEntryHook(
72 : agent::EntryFrame* entry_frame,
73 : FuncAddr function) {
74 : HINSTANCE instance = reinterpret_cast<HINSTANCE>(entry_frame->args[0]);
75 : DWORD reason = entry_frame->args[1];
76 :
77 : switch (reason) {
78 : case DLL_PROCESS_ATTACH: {
79 : HotPatchingAsanRuntime::GetInstance()->HotPatch(instance);
80 : break;
81 : }
82 :
83 : case DLL_THREAD_ATTACH:
84 : // Nothing to do here.
85 : break;
86 :
87 : case DLL_THREAD_DETACH:
88 : // Nothing to do here.
89 : break;
90 :
91 : case DLL_PROCESS_DETACH:
92 : // Nothing to do here.
93 : break;
94 :
95 : default:
96 : NOTREACHED();
97 : break;
98 : }
99 : }
100 :
101 : } // namespace asan
102 : } // namespace agent
103 :
104 : extern "C" {
105 :
106 : agent::asan::HotPatchingAsanRuntime* hp_asan_GetActiveHotPatchingAsanRuntime() {
107 : return agent::asan::HotPatchingAsanRuntime::GetInstance();
108 : }
109 :
110 : }
|