| 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 #include <stdlib.h> | |
| 19 | 18 |
| 20 #include "base/logging.h" | 19 #include "base/logging.h" |
| 21 #include "base/scoped_generic.h" | 20 #include "base/scoped_generic.h" |
| 22 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 24 #include "gtest/gtest.h" | |
| 25 #include "util/stdlib/string_number_conversion.h" | 23 #include "util/stdlib/string_number_conversion.h" |
| 26 #include "util/string/split_string.h" | 24 #include "util/string/split_string.h" |
| 27 #include "test/paths.h" | 25 #include "test/paths.h" |
| 28 | 26 |
| 29 namespace crashpad { | 27 namespace crashpad { |
| 30 namespace test { | 28 namespace test { |
| 31 | 29 |
| 32 namespace { | 30 WinMultiprocess::WinMultiprocess() |
| 31 : exit_code_(EXIT_SUCCESS), |
| 32 child_handles_(nullptr), |
| 33 child_process_helper_(nullptr) {} |
| 33 | 34 |
| 34 const char kIsMultiprocessChild[] = "--is-multiprocess-child"; | 35 WinMultiprocess::~WinMultiprocess() { |
| 35 | |
| 36 struct LocalFreeTraits { | |
| 37 static HLOCAL InvalidValue() { return nullptr; } | |
| 38 static void Free(HLOCAL mem) { | |
| 39 if (LocalFree(mem) != nullptr) | |
| 40 PLOG(ERROR) << "LocalFree"; | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 using ScopedLocalFree = base::ScopedGeneric<HLOCAL, LocalFreeTraits>; | |
| 45 | |
| 46 bool GetSwitch(const char* switch_name, std::string* value) { | |
| 47 int num_args; | |
| 48 wchar_t** args = CommandLineToArgvW(GetCommandLine(), &num_args); | |
| 49 ScopedLocalFree scoped_args(args); // Take ownership. | |
| 50 if (!args) { | |
| 51 PLOG(ERROR) << "couldn't parse command line"; | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 std::string switch_name_with_equals(switch_name); | |
| 56 switch_name_with_equals += "="; | |
| 57 for (size_t i = 1; i < num_args; ++i) { | |
| 58 const wchar_t* arg = args[i]; | |
| 59 std::string arg_as_utf8 = base::UTF16ToUTF8(arg); | |
| 60 if (arg_as_utf8.compare( | |
| 61 0, switch_name_with_equals.size(), switch_name_with_equals) == 0) { | |
| 62 *value = arg_as_utf8.substr(switch_name_with_equals.size()); | |
| 63 return true; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 WinMultiprocess::WinMultiprocess() | |
| 73 : pipe_c2p_read_(), | |
| 74 pipe_c2p_write_(), | |
| 75 pipe_p2c_read_(), | |
| 76 pipe_p2c_write_(), | |
| 77 child_handle_(), | |
| 78 exit_code_(EXIT_SUCCESS) { | |
| 79 } | |
| 80 | |
| 81 void WinMultiprocess::Run() { | |
| 82 std::string switch_value; | |
| 83 if (GetSwitch(kIsMultiprocessChild, &switch_value)) { | |
| 84 // If we're in the child, then set up the handles we inherited from the | |
| 85 // parent. These are inherited from the parent and so are open and have the | |
| 86 // same value as in the parent. The values are passed to the child on the | |
| 87 // command line. | |
| 88 std::string left, right; | |
| 89 ASSERT_TRUE(SplitString(switch_value, '|', &left, &right)); | |
| 90 unsigned int c2p_write, p2c_read; | |
| 91 ASSERT_TRUE(StringToNumber(left, &c2p_write)); | |
| 92 ASSERT_TRUE(StringToNumber(right, &p2c_read)); | |
| 93 pipe_c2p_write_.reset(reinterpret_cast<HANDLE>(c2p_write)); | |
| 94 pipe_p2c_read_.reset(reinterpret_cast<HANDLE>(p2c_read)); | |
| 95 | |
| 96 // Notify the parent that it's OK to proceed. We only need to wait to get to | |
| 97 // the process entry point, but this is the easiest place we can notify. | |
| 98 char c = ' '; | |
| 99 CheckedWriteFile(WritePipeHandle(), &c, sizeof(c)); | |
| 100 | |
| 101 // Invoke the child side of the test. | |
| 102 WinMultiprocessChild(); | |
| 103 | |
| 104 if (testing::Test::HasFailure()) | |
| 105 exit(255); | |
| 106 exit(EXIT_SUCCESS); | |
| 107 } else { | |
| 108 // If we're in the parent, make pipes for child-to-parent and | |
| 109 // parent-to-child communication. Mark them as inheritable via the | |
| 110 // SECURITY_ATTRIBUTES, but use SetHandleInformation to ensure that the | |
| 111 // parent sides are not inherited. | |
| 112 SECURITY_ATTRIBUTES security_attributes = {0}; | |
| 113 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); | |
| 114 security_attributes.bInheritHandle = true; | |
| 115 | |
| 116 HANDLE c2p_read, c2p_write; | |
| 117 PCHECK(CreatePipe(&c2p_read, &c2p_write, &security_attributes, 0)); | |
| 118 PCHECK(SetHandleInformation(c2p_read, HANDLE_FLAG_INHERIT, 0)); | |
| 119 pipe_c2p_read_.reset(c2p_read); | |
| 120 pipe_c2p_write_.reset(c2p_write); | |
| 121 | |
| 122 HANDLE p2c_read, p2c_write; | |
| 123 PCHECK(CreatePipe(&p2c_read, &p2c_write, &security_attributes, 0)); | |
| 124 PCHECK(SetHandleInformation(p2c_write, HANDLE_FLAG_INHERIT, 0)); | |
| 125 pipe_p2c_read_.reset(p2c_read); | |
| 126 pipe_p2c_write_.reset(p2c_write); | |
| 127 | |
| 128 // Build a command line for the child process that tells it only to run the | |
| 129 // current test, and to pass down the values of the pipe handles. | |
| 130 const ::testing::TestInfo* const test_info = | |
| 131 ::testing::UnitTest::GetInstance()->current_test_info(); | |
| 132 std::wstring command_line = Paths::Executable().value() + L" " + | |
| 133 base::UTF8ToUTF16(base::StringPrintf( | |
| 134 "--gtest_filter=%s.%s %s=0x%x|0x%x", | |
| 135 test_info->test_case_name(), | |
| 136 test_info->name(), | |
| 137 kIsMultiprocessChild, | |
| 138 c2p_write, | |
| 139 p2c_read)); | |
| 140 STARTUPINFO startup_info = {0}; | |
| 141 startup_info.cb = sizeof(startup_info); | |
| 142 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | |
| 143 startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | |
| 144 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); | |
| 145 startup_info.dwFlags = STARTF_USESTDHANDLES; | |
| 146 PROCESS_INFORMATION process_info; | |
| 147 PCHECK( | |
| 148 CreateProcess(Paths::Executable().value().c_str(), | |
| 149 &command_line[0], // This cannot be constant, per MSDN. | |
| 150 nullptr, | |
| 151 nullptr, | |
| 152 true, // Inherit handles. | |
| 153 0, | |
| 154 nullptr, | |
| 155 nullptr, | |
| 156 &startup_info, | |
| 157 &process_info)); | |
| 158 child_handle_.reset(process_info.hProcess); | |
| 159 CloseHandle(process_info.hThread); | |
| 160 | |
| 161 // Block until the child process has launched. CreateProcess() returns | |
| 162 // immediately, and test code expects process initialization to have | |
| 163 // completed so it can, for example, use the process handle. | |
| 164 char c; | |
| 165 CheckedReadFile(pipe_c2p_read_.get(), &c, sizeof(c)); | |
| 166 ASSERT_EQ(' ', c); | |
| 167 | |
| 168 // These have been passed to the child, close our side. | |
| 169 pipe_c2p_write_.reset(); | |
| 170 pipe_p2c_read_.reset(); | |
| 171 | |
| 172 WinMultiprocessParent(); | |
| 173 | |
| 174 // Close our side of the handles now that we're done. The child can | |
| 175 // use this to know when it's safe to complete. | |
| 176 pipe_p2c_write_.reset(); | |
| 177 pipe_c2p_read_.reset(); | |
| 178 | |
| 179 // Wait for the child to complete. | |
| 180 ASSERT_EQ(WAIT_OBJECT_0, | |
| 181 WaitForSingleObject(child_handle_.get(), INFINITE)); | |
| 182 | |
| 183 DWORD exit_code; | |
| 184 ASSERT_TRUE(GetExitCodeProcess(child_handle_.get(), &exit_code)); | |
| 185 ASSERT_EQ(exit_code_, exit_code); | |
| 186 } | |
| 187 } | 36 } |
| 188 | 37 |
| 189 void WinMultiprocess::SetExpectedChildExitCode(unsigned int exit_code) { | 38 void WinMultiprocess::SetExpectedChildExitCode(unsigned int exit_code) { |
| 190 exit_code_ = exit_code; | 39 exit_code_ = exit_code; |
| 191 } | 40 } |
| 192 | 41 |
| 193 WinMultiprocess::~WinMultiprocess() { | |
| 194 } | |
| 195 | |
| 196 FileHandle WinMultiprocess::ReadPipeHandle() const { | 42 FileHandle WinMultiprocess::ReadPipeHandle() const { |
| 197 FileHandle handle = | 43 if (child_handles_) |
| 198 child_handle_.get() ? pipe_c2p_read_.get() : pipe_p2c_read_.get(); | 44 return child_handles_->read.get(); |
| 199 CHECK(handle != nullptr); | 45 CHECK(child_process_helper_); |
| 200 return handle; | 46 return child_process_helper_->ReadPipeHandleForwarder(); |
| 201 } | 47 } |
| 202 | 48 |
| 203 FileHandle WinMultiprocess::WritePipeHandle() const { | 49 FileHandle WinMultiprocess::WritePipeHandle() const { |
| 204 FileHandle handle = | 50 if (child_handles_) |
| 205 child_handle_.get() ? pipe_p2c_write_.get() : pipe_c2p_write_.get(); | 51 return child_handles_->write.get(); |
| 206 CHECK(handle != nullptr); | 52 CHECK(child_process_helper_); |
| 207 return handle; | 53 return child_process_helper_->WritePipeHandleForwarder(); |
| 208 } | 54 } |
| 209 | 55 |
| 210 void WinMultiprocess::CloseReadPipe() { | 56 void WinMultiprocess::CloseReadPipe() { |
| 211 if (child_handle_.get()) | 57 if (child_handles_) { |
| 212 pipe_c2p_read_.reset(); | 58 child_handles_->read.reset(); |
| 213 else | 59 } else { |
| 214 pipe_p2c_read_.reset(); | 60 CHECK(child_process_helper_); |
| 61 child_process_helper_->CloseReadPipeForwarder(); |
| 62 } |
| 215 } | 63 } |
| 216 | 64 |
| 217 void WinMultiprocess::CloseWritePipe() { | 65 void WinMultiprocess::CloseWritePipe() { |
| 218 if (child_handle_.get()) | 66 if (child_handles_) { |
| 219 pipe_p2c_write_.reset(); | 67 child_handles_->write.reset(); |
| 220 else | 68 } else { |
| 221 pipe_c2p_write_.reset(); | 69 CHECK(child_process_helper_); |
| 70 child_process_helper_->CloseWritePipeForwarder(); |
| 71 } |
| 222 } | 72 } |
| 223 | 73 |
| 224 HANDLE WinMultiprocess::ChildProcess() const { | 74 HANDLE WinMultiprocess::ChildProcess() const { |
| 225 EXPECT_NE(nullptr, child_handle_.get()); | 75 CHECK(child_handles_); |
| 226 return child_handle_.get(); | 76 return child_handles_->process.get(); |
| 227 } | 77 } |
| 228 | 78 |
| 229 } // namespace test | 79 } // namespace test |
| 230 } // namespace crashpad | 80 } // namespace crashpad |
| OLD | NEW |