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