OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. 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 "test/win/win_multiprocess.h" |
| 16 |
| 17 #include <shellapi.h> |
| 18 #include <stdlib.h> |
| 19 |
| 20 #include "base/logging.h" |
| 21 #include "base/scoped_generic.h" |
| 22 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" |
| 24 #include "gtest/gtest.h" |
| 25 #include "util/stdlib/string_number_conversion.h" |
| 26 #include "util/string/split_string.h" |
| 27 #include "test/paths.h" |
| 28 |
| 29 namespace crashpad { |
| 30 namespace test { |
| 31 |
| 32 namespace { |
| 33 |
| 34 const char kIsMultiprocessChild[] = "--is-multiprocess-child"; |
| 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 exit(0); |
| 104 } else { |
| 105 // If we're in the parent, make pipes for child-to-parent and |
| 106 // parent-to-child communication. Mark them as inheritable via the |
| 107 // SECURITY_ATTRIBUTES, but use SetHandleInformation to ensure that the |
| 108 // parent sides are not inherited. |
| 109 SECURITY_ATTRIBUTES security_attributes = {0}; |
| 110 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 111 security_attributes.bInheritHandle = true; |
| 112 |
| 113 HANDLE c2p_read, c2p_write; |
| 114 PCHECK(CreatePipe(&c2p_read, &c2p_write, &security_attributes, 0)); |
| 115 PCHECK(SetHandleInformation(c2p_read, HANDLE_FLAG_INHERIT, 0)); |
| 116 pipe_c2p_read_.reset(c2p_read); |
| 117 pipe_c2p_write_.reset(c2p_write); |
| 118 |
| 119 HANDLE p2c_read, p2c_write; |
| 120 PCHECK(CreatePipe(&p2c_read, &p2c_write, &security_attributes, 0)); |
| 121 PCHECK(SetHandleInformation(p2c_write, HANDLE_FLAG_INHERIT, 0)); |
| 122 pipe_p2c_read_.reset(p2c_read); |
| 123 pipe_p2c_write_.reset(p2c_write); |
| 124 |
| 125 // Build a command line for the child process that tells it only to run the |
| 126 // current test, and to pass down the values of the pipe handles. |
| 127 const ::testing::TestInfo* const test_info = |
| 128 ::testing::UnitTest::GetInstance()->current_test_info(); |
| 129 std::wstring command_line = Paths::Executable().value() + L" " + |
| 130 base::UTF8ToUTF16(base::StringPrintf( |
| 131 "--gtest_filter=%s.%s %s=0x%x|0x%x", |
| 132 test_info->test_case_name(), |
| 133 test_info->name(), |
| 134 kIsMultiprocessChild, |
| 135 c2p_write, |
| 136 p2c_read)); |
| 137 STARTUPINFO startup_info = {0}; |
| 138 startup_info.cb = sizeof(startup_info); |
| 139 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 140 startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 141 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 142 startup_info.dwFlags = STARTF_USESTDHANDLES; |
| 143 PROCESS_INFORMATION process_info; |
| 144 PCHECK( |
| 145 CreateProcess(Paths::Executable().value().c_str(), |
| 146 &command_line[0], // This cannot be constant, per MSDN. |
| 147 nullptr, |
| 148 nullptr, |
| 149 true, // Inherit handles. |
| 150 0, |
| 151 nullptr, |
| 152 nullptr, |
| 153 &startup_info, |
| 154 &process_info)); |
| 155 child_handle_.reset(process_info.hProcess); |
| 156 CloseHandle(process_info.hThread); |
| 157 |
| 158 // Block until the child process has launched. CreateProcess() returns |
| 159 // immediately, and test code expects process initialization to have |
| 160 // completed so it can, for example, use the process handle. |
| 161 char c; |
| 162 CheckedReadFile(pipe_c2p_read_.get(), &c, sizeof(c)); |
| 163 ASSERT_EQ(' ', c); |
| 164 |
| 165 // These have been passed to the child, close our side. |
| 166 pipe_c2p_write_.reset(); |
| 167 pipe_p2c_read_.reset(); |
| 168 |
| 169 WinMultiprocessParent(); |
| 170 |
| 171 // Wait for the child to complete. |
| 172 ASSERT_EQ(WAIT_OBJECT_0, |
| 173 WaitForSingleObject(child_handle_.get(), INFINITE)); |
| 174 |
| 175 DWORD exit_code; |
| 176 ASSERT_TRUE(GetExitCodeProcess(child_handle_.get(), &exit_code)); |
| 177 ASSERT_EQ(exit_code_, exit_code); |
| 178 } |
| 179 } |
| 180 |
| 181 void WinMultiprocess::SetExpectedChildExitCode(unsigned int exit_code) { |
| 182 exit_code_ = exit_code; |
| 183 } |
| 184 |
| 185 WinMultiprocess::~WinMultiprocess() { |
| 186 } |
| 187 |
| 188 FileHandle WinMultiprocess::ReadPipeHandle() const { |
| 189 FileHandle handle = |
| 190 child_handle_.get() ? pipe_c2p_read_.get() : pipe_p2c_read_.get(); |
| 191 CHECK(handle != nullptr); |
| 192 return handle; |
| 193 } |
| 194 |
| 195 FileHandle WinMultiprocess::WritePipeHandle() const { |
| 196 FileHandle handle = |
| 197 child_handle_.get() ? pipe_p2c_write_.get() : pipe_c2p_write_.get(); |
| 198 CHECK(handle != nullptr); |
| 199 return handle; |
| 200 } |
| 201 |
| 202 void WinMultiprocess::CloseReadPipe() { |
| 203 if (child_handle_.get()) |
| 204 pipe_c2p_read_.reset(); |
| 205 else |
| 206 pipe_p2c_read_.reset(); |
| 207 } |
| 208 |
| 209 void WinMultiprocess::CloseWritePipe() { |
| 210 if (child_handle_.get()) |
| 211 pipe_p2c_write_.reset(); |
| 212 else |
| 213 pipe_c2p_write_.reset(); |
| 214 } |
| 215 |
| 216 HANDLE WinMultiprocess::ChildProcess() const { |
| 217 EXPECT_NE(nullptr, child_handle_.get()); |
| 218 return child_handle_.get(); |
| 219 } |
| 220 |
| 221 } // namespace test |
| 222 } // namespace crashpad |
OLD | NEW |