OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "test/win/win_multiprocess.h" | 15 #include "test/win/win_multiprocess.h" |
16 | 16 |
17 #include <shellapi.h> | 17 #include <shellapi.h> |
18 | 18 |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
20 #include "base/scoped_generic.h" | 20 #include "base/scoped_generic.h" |
21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
23 #include "gtest/gtest.h" | |
24 #include "util/stdlib/string_number_conversion.h" | 23 #include "util/stdlib/string_number_conversion.h" |
25 #include "util/string/split_string.h" | 24 #include "util/string/split_string.h" |
26 #include "test/paths.h" | 25 #include "test/paths.h" |
27 | 26 |
28 namespace crashpad { | 27 namespace crashpad { |
29 namespace test { | 28 namespace test { |
30 | 29 |
31 namespace { | 30 WinMultiprocess::WinMultiprocess() |
32 | 31 : child_handles_(nullptr), child_process_helper_(nullptr) { |
33 const char kIsMultiprocessChild[] = "--is-multiprocess-child"; | |
34 | |
35 struct LocalFreeTraits { | |
36 static HLOCAL InvalidValue() { return nullptr; } | |
37 static void Free(HLOCAL mem) { | |
38 if (LocalFree(mem) != nullptr) | |
39 PLOG(ERROR) << "LocalFree"; | |
40 } | |
41 }; | |
42 | |
43 using ScopedLocalFree = base::ScopedGeneric<HLOCAL, LocalFreeTraits>; | |
44 | |
45 bool GetSwitch(const char* switch_name, std::string* value) { | |
46 int num_args; | |
47 wchar_t** args = CommandLineToArgvW(GetCommandLine(), &num_args); | |
48 ScopedLocalFree scoped_args(args); // Take ownership. | |
49 if (!args) { | |
50 PLOG(ERROR) << "couldn't parse command line"; | |
51 return false; | |
52 } | |
53 | |
54 std::string switch_name_with_equals(switch_name); | |
55 switch_name_with_equals += "="; | |
56 for (size_t i = 1; i < num_args; ++i) { | |
57 const wchar_t* arg = args[i]; | |
58 std::string arg_as_utf8 = base::UTF16ToUTF8(arg); | |
59 if (arg_as_utf8.compare( | |
60 0, switch_name_with_equals.size(), switch_name_with_equals) == 0) { | |
61 *value = arg_as_utf8.substr(switch_name_with_equals.size()); | |
62 return true; | |
63 } | |
64 } | |
65 | |
66 return false; | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 WinMultiprocess::WinMultiprocess() { | |
72 } | |
73 | |
74 void WinMultiprocess::Run() { | |
75 std::string switch_value; | |
76 if (GetSwitch(kIsMultiprocessChild, &switch_value)) { | |
77 // If we're in the child, then set up the handles we inherited from the | |
78 // parent. These are inherited from the parent and so are open and have the | |
79 // same value as in the parent. The values are passed to the child on the | |
80 // command line. | |
81 std::string left, right; | |
82 ASSERT_TRUE(SplitString(switch_value, '|', &left, &right)); | |
83 unsigned int c2p_write, p2c_read; | |
84 ASSERT_TRUE(StringToNumber(left, &c2p_write)); | |
85 ASSERT_TRUE(StringToNumber(right, &p2c_read)); | |
86 pipe_c2p_write_.reset(reinterpret_cast<HANDLE>(c2p_write)); | |
87 pipe_p2c_read_.reset(reinterpret_cast<HANDLE>(p2c_read)); | |
88 | |
89 // Notify the parent that it's OK to proceed. We only need to wait to get to | |
90 // the process entry point, but this is the easiest place we can notify. | |
91 char c = ' '; | |
92 CheckedWriteFile(WritePipeHandle(), &c, sizeof(c)); | |
93 | |
94 // Invoke the child side of the test. | |
95 WinMultiprocessChild(); | |
96 exit(0); | |
97 } else { | |
98 // If we're in the parent, make pipes for child-to-parent and | |
99 // parent-to-child communication. Mark them as inheritable via the | |
100 // SECURITY_ATTRIBUTES, but use SetHandleInformation to ensure that the | |
101 // parent sides are not inherited. | |
102 SECURITY_ATTRIBUTES security_attributes = {0}; | |
103 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); | |
104 security_attributes.bInheritHandle = true; | |
105 | |
106 HANDLE c2p_read, c2p_write; | |
107 PCHECK(CreatePipe(&c2p_read, &c2p_write, &security_attributes, 0)); | |
108 PCHECK(SetHandleInformation(c2p_read, HANDLE_FLAG_INHERIT, 0)); | |
109 pipe_c2p_read_.reset(c2p_read); | |
110 pipe_c2p_write_.reset(c2p_write); | |
111 | |
112 HANDLE p2c_read, p2c_write; | |
113 PCHECK(CreatePipe(&p2c_read, &p2c_write, &security_attributes, 0)); | |
114 PCHECK(SetHandleInformation(p2c_write, HANDLE_FLAG_INHERIT, 0)); | |
115 pipe_p2c_read_.reset(p2c_read); | |
116 pipe_p2c_write_.reset(p2c_write); | |
117 | |
118 // Build a command line for the child process that tells it only to run the | |
119 // current test, and to pass down the values of the pipe handles. | |
120 const ::testing::TestInfo* const test_info = | |
121 ::testing::UnitTest::GetInstance()->current_test_info(); | |
122 std::wstring command_line = Paths::Executable().value() + L" " + | |
123 base::UTF8ToUTF16(base::StringPrintf( | |
124 "--gtest_filter=%s.%s %s=0x%x|0x%x", | |
125 test_info->test_case_name(), | |
126 test_info->name(), | |
127 kIsMultiprocessChild, | |
128 c2p_write, | |
129 p2c_read)); | |
130 STARTUPINFO startup_info = {0}; | |
131 startup_info.cb = sizeof(startup_info); | |
132 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | |
133 startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | |
134 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); | |
135 startup_info.dwFlags = STARTF_USESTDHANDLES; | |
136 PROCESS_INFORMATION process_info; | |
137 PCHECK( | |
138 CreateProcess(Paths::Executable().value().c_str(), | |
139 &command_line[0], // This cannot be constant, per MSDN. | |
140 nullptr, | |
141 nullptr, | |
142 true, // Inherit handles. | |
143 0, | |
144 nullptr, | |
145 nullptr, | |
146 &startup_info, | |
147 &process_info)); | |
148 child_handle_.reset(process_info.hProcess); | |
149 CloseHandle(process_info.hThread); | |
150 | |
151 // Block until the child process has launched. CreateProcess() returns | |
152 // immediately, and test code expects process initialization to have | |
153 // completed so it can, for example, use the process handle. | |
154 char c; | |
155 CheckedReadFile(pipe_c2p_read_.get(), &c, sizeof(c)); | |
156 ASSERT_EQ(' ', c); | |
157 | |
158 // These have been passed to the child, close our side. | |
159 pipe_c2p_write_.reset(); | |
160 pipe_p2c_read_.reset(); | |
161 | |
162 WinMultiprocessParent(); | |
163 } | |
164 } | 32 } |
165 | 33 |
166 WinMultiprocess::~WinMultiprocess() { | 34 WinMultiprocess::~WinMultiprocess() { |
167 } | 35 } |
168 | 36 |
169 FileHandle WinMultiprocess::ReadPipeHandle() const { | 37 FileHandle WinMultiprocess::ReadPipeHandle() const { |
170 FileHandle handle = | 38 if (child_handles_) |
171 child_handle_.get() ? pipe_c2p_read_.get() : pipe_p2c_read_.get(); | 39 return child_handles_->read.get(); |
172 CHECK(handle != nullptr); | 40 CHECK(child_process_helper_); |
173 return handle; | 41 return child_process_helper_->ReadPipeHandleForwarder(); |
174 } | 42 } |
175 | 43 |
176 FileHandle WinMultiprocess::WritePipeHandle() const { | 44 FileHandle WinMultiprocess::WritePipeHandle() const { |
177 FileHandle handle = | 45 if (child_handles_) |
178 child_handle_.get() ? pipe_p2c_write_.get() : pipe_c2p_write_.get(); | 46 return child_handles_->write.get(); |
179 CHECK(handle != nullptr); | 47 CHECK(child_process_helper_); |
180 return handle; | 48 return child_process_helper_->WritePipeHandleForwarder(); |
181 } | 49 } |
182 | 50 |
183 void WinMultiprocess::CloseReadPipe() { | 51 void WinMultiprocess::CloseReadPipe() { |
184 if (child_handle_.get()) | 52 if (child_handles_) { |
185 pipe_c2p_read_.reset(); | 53 child_handles_->read.reset(); |
186 else | 54 } else { |
187 pipe_p2c_read_.reset(); | 55 CHECK(child_process_helper_); |
| 56 child_process_helper_->CloseReadPipeForwarder(); |
| 57 } |
188 } | 58 } |
189 | 59 |
190 void WinMultiprocess::CloseWritePipe() { | 60 void WinMultiprocess::CloseWritePipe() { |
191 if (child_handle_.get()) | 61 if (child_handles_) { |
192 pipe_p2c_write_.reset(); | 62 child_handles_->write.reset(); |
193 else | 63 } else { |
194 pipe_c2p_write_.reset(); | 64 CHECK(child_process_helper_); |
| 65 child_process_helper_->CloseWritePipeForwarder(); |
| 66 } |
195 } | 67 } |
196 | 68 |
197 HANDLE WinMultiprocess::ChildProcess() const { | 69 HANDLE WinMultiprocess::ChildProcess() const { |
198 EXPECT_NE(nullptr, child_handle_.get()); | 70 CHECK(child_handles_); |
199 return child_handle_.get(); | 71 return child_handles_->process.get(); |
200 } | 72 } |
201 | 73 |
202 } // namespace test | 74 } // namespace test |
203 } // namespace crashpad | 75 } // namespace crashpad |
OLD | NEW |