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_child_process.h" |
| 16 |
| 17 #include <windows.h> |
| 18 #include <shellapi.h> |
| 19 |
| 20 #include <string> |
| 21 |
| 22 #include "base/logging.h" |
| 23 #include "base/strings/stringprintf.h" |
| 24 #include "base/strings/utf_string_conversions.h" |
| 25 #include "gtest/gtest.h" |
| 26 #include "util/stdlib/string_number_conversion.h" |
| 27 #include "util/string/split_string.h" |
| 28 #include "test/paths.h" |
| 29 |
| 30 namespace crashpad { |
| 31 namespace test { |
| 32 |
| 33 namespace { |
| 34 |
| 35 const char kIsMultiprocessChild[] = "--is-multiprocess-child"; |
| 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(FATAL) << "CommandLineToArgvW"; |
| 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 if (value) |
| 63 *value = arg_as_utf8.substr(switch_name_with_equals.size()); |
| 64 return true; |
| 65 } |
| 66 } |
| 67 |
| 68 return false; |
| 69 } |
| 70 |
| 71 ScopedKernelHANDLE LaunchCommandLine(wchar_t* command_line) { |
| 72 STARTUPINFO startup_info = {0}; |
| 73 startup_info.cb = sizeof(startup_info); |
| 74 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 75 startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 76 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 77 startup_info.dwFlags = STARTF_USESTDHANDLES; |
| 78 PROCESS_INFORMATION process_info; |
| 79 if (!CreateProcess(Paths::Executable().value().c_str(), |
| 80 &command_line[0], // This cannot be constant, per MSDN. |
| 81 nullptr, |
| 82 nullptr, |
| 83 true, // Inherit handles. |
| 84 0, |
| 85 nullptr, |
| 86 nullptr, |
| 87 &startup_info, |
| 88 &process_info)) { |
| 89 PLOG(ERROR) << "CreateProcess"; |
| 90 return ScopedKernelHANDLE(); |
| 91 } |
| 92 if (!CloseHandle(process_info.hThread)) { |
| 93 PLOG(ERROR) << "CloseHandle"; |
| 94 if (!CloseHandle(process_info.hProcess)) |
| 95 PLOG(ERROR) << "CloseHandle"; |
| 96 return ScopedKernelHANDLE(); |
| 97 } |
| 98 return ScopedKernelHANDLE(process_info.hProcess); |
| 99 } |
| 100 |
| 101 bool UnsetHandleInheritance(HANDLE handle) { |
| 102 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0)) { |
| 103 PLOG(ERROR) << "SetHandleInformation"; |
| 104 ADD_FAILURE() << "SetHandleInformation"; |
| 105 return false; |
| 106 } |
| 107 return true; |
| 108 } |
| 109 |
| 110 bool CreateInheritablePipe(ScopedFileHANDLE* read_handle, |
| 111 bool read_inheritable, |
| 112 ScopedFileHANDLE* write_handle, |
| 113 bool write_inheritable) { |
| 114 // Mark both sides as inheritable via the SECURITY_ATTRIBUTES and use |
| 115 // SetHandleInformation as necessary to restrict inheritance of either side. |
| 116 SECURITY_ATTRIBUTES security_attributes = {0}; |
| 117 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 118 security_attributes.bInheritHandle = true; |
| 119 |
| 120 HANDLE read, write; |
| 121 BOOL result = CreatePipe(&read, &write, &security_attributes, 0); |
| 122 if (!result) { |
| 123 PLOG(ERROR) << "CreatePipe"; |
| 124 ADD_FAILURE() << "CreatePipe failed"; |
| 125 return false; |
| 126 } |
| 127 ScopedFileHANDLE temp_read(read); |
| 128 ScopedFileHANDLE temp_write(write); |
| 129 |
| 130 if (!read_inheritable && !UnsetHandleInheritance(temp_read.get())) |
| 131 return false; |
| 132 if (!write_inheritable && !UnsetHandleInheritance(temp_write.get())) |
| 133 return false; |
| 134 |
| 135 *read_handle = temp_read.Pass(); |
| 136 *write_handle = temp_write.Pass(); |
| 137 |
| 138 return true; |
| 139 } |
| 140 |
| 141 } // namespace |
| 142 |
| 143 WinChildProcess::WinChildProcess() { |
| 144 std::string switch_value; |
| 145 CHECK(GetSwitch(kIsMultiprocessChild, &switch_value)); |
| 146 |
| 147 // Set up the handles we inherited from the parent. These are inherited from |
| 148 // the parent and so are open and have the same value as in the parent. The |
| 149 // values are passed to the child on the command line. |
| 150 std::string left, right; |
| 151 CHECK(SplitString(switch_value, '|', &left, &right)); |
| 152 unsigned int write, read; |
| 153 CHECK(StringToNumber(left, &write)); |
| 154 CHECK(StringToNumber(right, &read)); |
| 155 pipe_write_.reset(reinterpret_cast<HANDLE>(write)); |
| 156 pipe_read_.reset(reinterpret_cast<HANDLE>(read)); |
| 157 |
| 158 // Notify the parent that it's OK to proceed. We only need to wait to get to |
| 159 // the process entry point, but this is the easiest place we can notify. |
| 160 char c = ' '; |
| 161 CheckedWriteFile(WritePipeHandle(), &c, sizeof(c)); |
| 162 } |
| 163 |
| 164 // static |
| 165 bool WinChildProcess::IsChildProcess() { |
| 166 return GetSwitch(kIsMultiprocessChild, nullptr); |
| 167 } |
| 168 |
| 169 // static |
| 170 scoped_ptr<WinChildProcess::Handles> WinChildProcess::Launch() { |
| 171 // Make pipes for child-to-parent and parent-to-child communication. |
| 172 scoped_ptr<Handles> handles_for_parent(new Handles); |
| 173 ScopedFileHANDLE read_for_child; |
| 174 ScopedFileHANDLE write_for_child; |
| 175 |
| 176 if (!CreateInheritablePipe( |
| 177 &handles_for_parent->read, false, &write_for_child, true)) { |
| 178 return scoped_ptr<Handles>(); |
| 179 } |
| 180 |
| 181 if (!CreateInheritablePipe( |
| 182 &read_for_child, true, &handles_for_parent->write, false)) { |
| 183 return scoped_ptr<Handles>(); |
| 184 } |
| 185 |
| 186 // Build a command line for the child process that tells it only to run the |
| 187 // current test, and to pass down the values of the pipe handles. |
| 188 const ::testing::TestInfo* const test_info = |
| 189 ::testing::UnitTest::GetInstance()->current_test_info(); |
| 190 std::wstring command_line = |
| 191 Paths::Executable().value() + L" " + |
| 192 base::UTF8ToUTF16(base::StringPrintf("--gtest_filter=%s.%s %s=0x%x|0x%x", |
| 193 test_info->test_case_name(), |
| 194 test_info->name(), |
| 195 kIsMultiprocessChild, |
| 196 write_for_child, |
| 197 read_for_child.get())); |
| 198 |
| 199 // Command-line buffer cannot be constant, per CreateProcess signature. |
| 200 handles_for_parent->process = LaunchCommandLine(&command_line[0]); |
| 201 if (!handles_for_parent->process.is_valid()) |
| 202 return scoped_ptr<Handles>(); |
| 203 |
| 204 // Block until the child process has launched. CreateProcess() returns |
| 205 // immediately, and test code expects process initialization to have |
| 206 // completed so it can, for example, read the process memory. |
| 207 char c; |
| 208 if (!LoggingReadFile(handles_for_parent->read.get(), &c, sizeof(c))) { |
| 209 ADD_FAILURE() << "LoggedReadFile"; |
| 210 return scoped_ptr<Handles>(); |
| 211 } |
| 212 |
| 213 if (c != ' ') { |
| 214 ADD_FAILURE() << "invalid data read from child"; |
| 215 return scoped_ptr<Handles>(); |
| 216 } |
| 217 |
| 218 return handles_for_parent.Pass(); |
| 219 } |
| 220 |
| 221 FileHandle WinChildProcess::ReadPipeHandle() const { |
| 222 return pipe_read_.get(); |
| 223 } |
| 224 |
| 225 FileHandle WinChildProcess::WritePipeHandle() const { |
| 226 return pipe_write_.get(); |
| 227 } |
| 228 |
| 229 void WinChildProcess::CloseReadPipe() { |
| 230 pipe_read_.reset(); |
| 231 } |
| 232 |
| 233 void WinChildProcess::CloseWritePipe() { |
| 234 pipe_write_.reset(); |
| 235 } |
| 236 |
| 237 } // namespace test |
| 238 } // namespace crashpad |
OLD | NEW |