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 :
76 : // Disable logging. In the case of Chrome this is running in a sandboxed
77 : // process where logging to file doesn't help us any. In other cases the
78 : // log output will still go to console.
79 : logging::InitLogging(
80 : NULL,
81 : logging::LOG_NONE,
82 : logging::DONT_LOCK_LOG_FILE,
83 : logging::DELETE_OLD_LOG_FILE,
84 E : logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
85 :
86 E : break;
87 :
88 : case DLL_THREAD_ATTACH:
89 : // Nothing to do here.
90 i : break;
91 :
92 : case DLL_THREAD_DETACH:
93 : // Nothing to do here.
94 i : break;
95 :
96 : case DLL_PROCESS_DETACH:
97 : // This should be the last thing called in the agent DLL before it
98 : // gets unloaded. Everything should otherwise have been initialized
99 : // and we're now just cleaning it up again.
100 E : agent::asan::TearDownRtl();
101 E : TearDownAsanRuntime();
102 E : break;
103 :
104 : default:
105 i : NOTREACHED();
106 : break;
107 : }
108 :
109 E : return TRUE;
110 E : }
111 :
112 : } // extern "C"
|