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> // NOLINT
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/asan_rtl_impl.h"
22 : #include "syzygy/agent/asan/asan_runtime.h"
23 : #include "syzygy/common/logging.h"
24 :
25 : namespace {
26 :
27 : using agent::asan::AsanRuntime;
28 :
29 : // Our AtExit manager required by base.
30 : base::AtExitManager* at_exit = NULL;
31 :
32 : // The asan runtime manager.
33 : AsanRuntime* asan_runtime = NULL;
34 :
35 E : void SetUpAtExitManager() {
36 E : DCHECK(at_exit == NULL);
37 E : at_exit = new base::AtExitManager();
38 E : CHECK(at_exit != NULL);
39 E : }
40 :
41 : void TearDownAtExitManager() {
42 : DCHECK(at_exit != NULL);
43 : delete at_exit;
44 : at_exit = NULL;
45 : }
46 :
47 E : void SetUpAsanRuntime() {
48 E : DCHECK(asan_runtime == NULL);
49 E : asan_runtime = new AsanRuntime();
50 E : CHECK(asan_runtime != NULL);
51 E : std::wstring asan_flags_str;
52 E : if (!AsanRuntime::GetAsanFlagsEnvVar(&asan_flags_str)) {
53 i : LOG(ERROR) << "Error while trying to read Asan command line.";
54 : }
55 E : asan_runtime->SetUp(asan_flags_str);
56 :
57 E : agent::asan::SetUpRtl(asan_runtime);
58 E : }
59 :
60 E : void TearDownAsanRuntime() {
61 E : DCHECK(asan_runtime != NULL);
62 E : asan_runtime->TearDown();
63 E : delete asan_runtime;
64 E : asan_runtime = NULL;
65 E : }
66 :
67 : } // namespace
68 :
69 : extern "C" {
70 :
71 E : BOOL WINAPI DllMain(HMODULE instance, DWORD reason, LPVOID reserved) {
72 E : switch (reason) {
73 : case DLL_PROCESS_ATTACH:
74 : // Create the At-Exit manager.
75 E : SetUpAtExitManager();
76 :
77 : // Disable logging. In the case of Chrome this is running in a sandboxed
78 : // process where logging to file doesn't help us any. In other cases the
79 : // log output will still go to console.
80 E : CommandLine::Init(0, NULL);
81 E : common::InitLoggingForDll(L"asan");
82 :
83 E : SetUpAsanRuntime();
84 :
85 E : break;
86 :
87 : case DLL_THREAD_ATTACH:
88 : // Nothing to do here.
89 i : break;
90 :
91 : case DLL_THREAD_DETACH:
92 : // Nothing to do here.
93 i : break;
94 :
95 : case DLL_PROCESS_DETACH:
96 : // This should be the last thing called in the agent DLL before it
97 : // gets unloaded. Everything should otherwise have been initialized
98 : // and we're now just cleaning it up again.
99 E : agent::asan::TearDownRtl();
100 E : TearDownAsanRuntime();
101 E : break;
102 :
103 : default:
104 i : NOTREACHED();
105 : break;
106 : }
107 :
108 E : return TRUE;
109 E : }
110 :
111 : } // extern "C"
|