1 : // Copyright 2012 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 <windows.h>
16 :
17 : #include "base/at_exit.h"
18 : #include "base/atomicops.h"
19 : #include "base/command_line.h"
20 : #include "base/logging.h"
21 : #include "syzygy/agent/asan/rtl_impl.h"
22 : #include "syzygy/agent/asan/runtime.h"
23 : #include "syzygy/agent/asan/runtime_util.h"
24 : #include "syzygy/agent/common/agent.h"
25 : #include "syzygy/common/logging.h"
26 :
27 : namespace {
28 :
29 : // Our AtExit manager required by base.
30 : base::AtExitManager* at_exit = nullptr;
31 :
32 : // The asan runtime manager.
33 : agent::asan::AsanRuntime* asan_runtime = nullptr;
34 :
35 E : void SetUpAtExitManager() {
36 E : DCHECK_EQ(static_cast<base::AtExitManager*>(nullptr), at_exit);
37 E : at_exit = new base::AtExitManager();
38 E : CHECK_NE(static_cast<base::AtExitManager*>(nullptr), at_exit);
39 E : }
40 :
41 E : void TearDownAtExitManager() {
42 E : DCHECK_NE(static_cast<base::AtExitManager*>(nullptr), at_exit);
43 E : delete at_exit;
44 E : at_exit = nullptr;
45 E : }
46 :
47 : } // namespace
48 :
49 : extern "C" {
50 :
51 E : BOOL WINAPI DllMain(HMODULE instance, DWORD reason, LPVOID reserved) {
52 E : agent::common::InitializeCrt();
53 :
54 E : switch (reason) {
55 : case DLL_PROCESS_ATTACH: {
56 : // Create the At-Exit manager.
57 E : SetUpAtExitManager();
58 :
59 : // Disable logging. In the case of Chrome this is running in a sandboxed
60 : // process where logging to file doesn't help us any. In other cases the
61 : // log output will still go to console.
62 E : base::CommandLine::Init(0, NULL);
63 E : common::InitLoggingForDll(L"asan");
64 :
65 : // This runtime has no ability to disable instrumentation so can't
66 : // tolerate an initialization failure.
67 E : CHECK(SetUpAsanRuntime(&asan_runtime));
68 E : break;
69 : }
70 :
71 : case DLL_THREAD_ATTACH: {
72 E : agent::asan::AsanRuntime* runtime = agent::asan::AsanRuntime::runtime();
73 E : DCHECK_NE(static_cast<agent::asan::AsanRuntime*>(nullptr), runtime);
74 E : runtime->AddThreadId(::GetCurrentThreadId());
75 E : break;
76 : }
77 :
78 : case DLL_THREAD_DETACH:
79 : // Nothing to do here.
80 i : break;
81 :
82 : case DLL_PROCESS_DETACH: {
83 E : base::CommandLine::Reset();
84 : // This should be the last thing called in the agent DLL before it
85 : // gets unloaded. Everything should otherwise have been initialized
86 : // and we're now just cleaning it up again.
87 E : TearDownAsanRuntime(&asan_runtime);
88 E : TearDownAtExitManager();
89 E : break;
90 : }
91 :
92 : default:
93 i : NOTREACHED();
94 : break;
95 : }
96 :
97 E : return TRUE;
98 E : }
99 :
100 : } // extern "C"
|