1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <windows.h>
6
7 #include <winternl.h>
8
9 #include <string>
10 #include <utility>
11
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
14 #include "base/scoped_native_library.h"
15 #include "base/test/multiprocess_test.h"
16 #include "base/test/test_timeouts.h"
17 #include "base/win/scoped_handle.h"
18 #include "build/build_config.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/multiprocess_func_list.h"
21
22 namespace base {
23 namespace win {
24
25 namespace {
26
FailureMessage(const std::string & msg)27 std::string FailureMessage(const std::string& msg) {
28 #if defined(NDEBUG) && defined(OFFICIAL_BUILD)
29 // Official release builds strip all fatal messages for saving binary size,
30 // see base/check.h.
31 return "";
32 #else
33 return msg;
34 #endif // defined(NDEBUG) && defined(OFFICIAL_BUILD)
35 }
36
37 } // namespace
38
39 namespace testing {
40 extern "C" bool __declspec(dllexport) RunTest();
41 } // namespace testing
42
43 using ScopedHandleTest = ::testing::Test;
44 using ScopedHandleDeathTest = ::testing::Test;
45
TEST_F(ScopedHandleTest,ScopedHandle)46 TEST_F(ScopedHandleTest, ScopedHandle) {
47 // Any illegal error code will do. We just need to test that it is preserved
48 // by ScopedHandle to avoid https://crbug.com/528394.
49 const DWORD magic_error = 0x12345678;
50
51 HANDLE handle = ::CreateMutex(nullptr, false, nullptr);
52 // Call SetLastError after creating the handle.
53 ::SetLastError(magic_error);
54 base::win::ScopedHandle handle_holder(handle);
55 EXPECT_EQ(magic_error, ::GetLastError());
56
57 // Create a new handle and then set LastError again.
58 handle = ::CreateMutex(nullptr, false, nullptr);
59 ::SetLastError(magic_error);
60 handle_holder.Set(handle);
61 EXPECT_EQ(magic_error, ::GetLastError());
62
63 // Create a new handle and then set LastError again.
64 handle = ::CreateMutex(nullptr, false, nullptr);
65 base::win::ScopedHandle handle_source(handle);
66 ::SetLastError(magic_error);
67 handle_holder = std::move(handle_source);
68 EXPECT_EQ(magic_error, ::GetLastError());
69 }
70
TEST_F(ScopedHandleDeathTest,HandleVerifierTrackedHasBeenClosed)71 TEST_F(ScopedHandleDeathTest, HandleVerifierTrackedHasBeenClosed) {
72 HANDLE handle = ::CreateMutex(nullptr, false, nullptr);
73 ASSERT_NE(HANDLE(nullptr), handle);
74
75 ASSERT_DEATH(
76 {
77 base::win::ScopedHandle handle_holder(handle);
78 ::NtClose(handle);
79 // Destructing a ScopedHandle with an illegally closed handle should
80 // fail.
81 },
82 FailureMessage("CloseHandle failed"));
83 }
84
TEST_F(ScopedHandleDeathTest,HandleVerifierCloseTrackedHandle)85 TEST_F(ScopedHandleDeathTest, HandleVerifierCloseTrackedHandle) {
86 ASSERT_DEATH(
87 {
88 HANDLE handle = ::CreateMutex(nullptr, false, nullptr);
89 ASSERT_NE(HANDLE(nullptr), handle);
90
91 // Start tracking the handle so that closes outside of the checker are
92 // caught.
93 base::win::CheckedScopedHandle handle_holder(handle);
94
95 // Closing a tracked handle using ::CloseHandle should crash due to hook
96 // noticing the illegal close.
97 ::CloseHandle(handle);
98 },
99 // This test must match the CloseHandleHook causing this failure, because
100 // if the hook doesn't crash and instead the handle is double closed by
101 // the `handle_holder` going out of scope, then there is still a crash,
102 // but a different crash and one we are not explicitly testing here. This
103 // other crash is tested in HandleVerifierTrackedHasBeenClosed above.
104 FailureMessage("CloseHandleHook validation failure"));
105 }
106
TEST_F(ScopedHandleDeathTest,HandleVerifierDoubleTracking)107 TEST_F(ScopedHandleDeathTest, HandleVerifierDoubleTracking) {
108 HANDLE handle = ::CreateMutex(nullptr, false, nullptr);
109 ASSERT_NE(HANDLE(nullptr), handle);
110
111 base::win::CheckedScopedHandle handle_holder(handle);
112
113 ASSERT_DEATH({ base::win::CheckedScopedHandle handle_holder2(handle); },
114 FailureMessage("Handle Already Tracked"));
115 }
116
TEST_F(ScopedHandleDeathTest,HandleVerifierWrongOwner)117 TEST_F(ScopedHandleDeathTest, HandleVerifierWrongOwner) {
118 HANDLE handle = ::CreateMutex(nullptr, false, nullptr);
119 ASSERT_NE(HANDLE(nullptr), handle);
120
121 base::win::CheckedScopedHandle handle_holder(handle);
122 ASSERT_DEATH(
123 {
124 base::win::CheckedScopedHandle handle_holder2;
125 handle_holder2.handle_ = handle;
126 },
127 FailureMessage("Closing a handle owned by something else"));
128 ASSERT_TRUE(handle_holder.is_valid());
129 handle_holder.Close();
130 }
131
TEST_F(ScopedHandleDeathTest,HandleVerifierUntrackedHandle)132 TEST_F(ScopedHandleDeathTest, HandleVerifierUntrackedHandle) {
133 HANDLE handle = ::CreateMutex(nullptr, false, nullptr);
134 ASSERT_NE(HANDLE(nullptr), handle);
135
136 ASSERT_DEATH(
137 {
138 base::win::CheckedScopedHandle handle_holder;
139 handle_holder.handle_ = handle;
140 },
141 FailureMessage("Closing an untracked handle"));
142
143 ASSERT_TRUE(::CloseHandle(handle));
144 }
145
146 // Under ASan, the multi-process test crashes during process shutdown for
147 // unknown reasons. Disable it for now. http://crbug.com/685262
148 #if defined(ADDRESS_SANITIZER)
149 #define MAYBE_MultiProcess DISABLED_MultiProcess
150 #else
151 #define MAYBE_MultiProcess MultiProcess
152 #endif
153
TEST_F(ScopedHandleTest,MAYBE_MultiProcess)154 TEST_F(ScopedHandleTest, MAYBE_MultiProcess) {
155 CommandLine command_line(base::GetMultiProcessTestChildBaseCommandLine());
156
157 base::Process test_child_process = base::SpawnMultiProcessTestChild(
158 "HandleVerifierChildProcess", command_line, LaunchOptions());
159
160 int rv = -1;
161 ASSERT_TRUE(test_child_process.WaitForExitWithTimeout(
162 TestTimeouts::action_timeout(), &rv));
163 EXPECT_EQ(0, rv);
164 }
165
MULTIPROCESS_TEST_MAIN(HandleVerifierChildProcess)166 MULTIPROCESS_TEST_MAIN(HandleVerifierChildProcess) {
167 ScopedNativeLibrary module(
168 FilePath(FILE_PATH_LITERAL("scoped_handle_test_dll.dll")));
169
170 if (!module.is_valid())
171 return 1;
172 auto run_test_function = reinterpret_cast<decltype(&testing::RunTest)>(
173 module.GetFunctionPointer("RunTest"));
174 if (!run_test_function)
175 return 1;
176 if (!run_test_function())
177 return 1;
178
179 return 0;
180 }
181
182 } // namespace win
183 } // namespace base
184