Chromium Code Reviews| 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 CreateInheritablePipe(ScopedFileHANDLE* read_handle, | |
| 102 ScopedFileHANDLE* write_handle) { | |
| 103 SECURITY_ATTRIBUTES security_attributes = {0}; | |
| 104 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); | |
| 105 security_attributes.bInheritHandle = true; | |
| 106 | |
| 107 HANDLE read, write; | |
| 108 BOOL result = CreatePipe(&read, &write, &security_attributes, 0); | |
| 109 if (!result) { | |
| 110 PLOG(ERROR) << "CreatePipe"; | |
| 111 ADD_FAILURE() << "CreatePipe failed"; | |
| 112 return false; | |
| 113 } | |
| 114 read_handle->reset(read); | |
| 115 write_handle->reset(write); | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 } // namespace | |
| 120 | |
| 121 WinChildProcess::WinChildProcess() { | |
| 122 std::string switch_value; | |
| 123 CHECK(GetSwitch(kIsMultiprocessChild, &switch_value)); | |
| 124 | |
| 125 // Set up the handles we inherited from the parent. These are inherited from | |
| 126 // the parent and so are open and have the same value as in the parent. The | |
| 127 // values are passed to the child on the command line. | |
| 128 std::string left, right; | |
| 129 CHECK(SplitString(switch_value, '|', &left, &right)); | |
| 130 unsigned int write, read; | |
| 131 CHECK(StringToNumber(left, &write)); | |
| 132 CHECK(StringToNumber(right, &read)); | |
| 133 pipe_write_.reset(reinterpret_cast<HANDLE>(write)); | |
| 134 pipe_read_.reset(reinterpret_cast<HANDLE>(read)); | |
| 135 | |
| 136 // Notify the parent that it's OK to proceed. We only need to wait to get to | |
| 137 // the process entry point, but this is the easiest place we can notify. | |
| 138 char c = ' '; | |
| 139 CheckedWriteFile(WritePipeHandle(), &c, sizeof(c)); | |
| 140 } | |
| 141 | |
| 142 // static | |
| 143 bool WinChildProcess::IsChildProcess() { | |
| 144 return GetSwitch(kIsMultiprocessChild, nullptr); | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 scoped_ptr<WinChildProcess::Handles> WinChildProcess::Launch() { | |
| 149 // Make pipes for child-to-parent and parent-to-child communication. Mark them | |
| 150 // as inheritable via the SECURITY_ATTRIBUTES, but use SetHandleInformation to | |
| 151 // ensure that the parent sides are not inherited. | |
| 152 scoped_ptr<Handles> handles_for_parent(new Handles); | |
| 153 ScopedFileHANDLE read_for_child; | |
| 154 ScopedFileHANDLE write_for_child; | |
| 155 | |
| 156 if (!CreateInheritablePipe(&handles_for_parent->read, &write_for_child)) | |
| 157 return scoped_ptr<Handles>(); | |
| 158 | |
| 159 if (!CreateInheritablePipe(&read_for_child, &handles_for_parent->write)) | |
| 160 return scoped_ptr<Handles>(); | |
| 161 | |
| 162 if (!SetHandleInformation( | |
|
scottmg
2015/06/01 21:53:22
Can you move this inside the helper function too?
erikwright (departed)
2015/06/24 20:01:34
Done.
| |
| 163 handles_for_parent->read.get(), HANDLE_FLAG_INHERIT, 0)) { | |
| 164 PLOG(ERROR) << "SetHandleInformation"; | |
| 165 ADD_FAILURE() << "SetHandleInformation"; | |
| 166 return scoped_ptr<Handles>(); | |
| 167 } | |
| 168 if (!SetHandleInformation( | |
| 169 handles_for_parent->write.get(), HANDLE_FLAG_INHERIT, 0)) { | |
| 170 PLOG(ERROR) << "SetHandleInformation"; | |
| 171 ADD_FAILURE() << "SetHandleInformation"; | |
| 172 return scoped_ptr<Handles>(); | |
| 173 } | |
| 174 | |
| 175 // Build a command line for the child process that tells it only to run the | |
| 176 // current test, and to pass down the values of the pipe handles. | |
| 177 const ::testing::TestInfo* const test_info = | |
| 178 ::testing::UnitTest::GetInstance()->current_test_info(); | |
| 179 std::wstring command_line = | |
| 180 Paths::Executable().value() + L" " + | |
| 181 base::UTF8ToUTF16(base::StringPrintf("--gtest_filter=%s.%s %s=0x%x|0x%x", | |
| 182 test_info->test_case_name(), | |
| 183 test_info->name(), | |
| 184 kIsMultiprocessChild, | |
| 185 write_for_child, | |
| 186 read_for_child.get())); | |
| 187 | |
| 188 // Command-line buffer cannot be constant, per CreateProcess signature. | |
| 189 handles_for_parent->process = LaunchCommandLine(&command_line[0]); | |
| 190 if (!handles_for_parent->process.is_valid()) | |
| 191 return scoped_ptr<Handles>(); | |
| 192 | |
| 193 // Block until the child process has launched. CreateProcess() returns | |
| 194 // immediately, and test code expects process initialization to have | |
| 195 // completed so it can, for example, read the process memory. | |
| 196 char c; | |
| 197 if (!LoggingReadFile(handles_for_parent->read.get(), &c, sizeof(c))) { | |
| 198 ADD_FAILURE() << "LoggedReadFile"; | |
| 199 return scoped_ptr<Handles>(); | |
| 200 } | |
| 201 | |
| 202 if (c != ' ') { | |
| 203 ADD_FAILURE() << "invalid data read from child"; | |
| 204 return scoped_ptr<Handles>(); | |
| 205 } | |
| 206 | |
| 207 return handles_for_parent.Pass(); | |
| 208 } | |
| 209 | |
| 210 FileHandle WinChildProcess::ReadPipeHandle() const { | |
| 211 return pipe_read_.get(); | |
| 212 } | |
| 213 | |
| 214 FileHandle WinChildProcess::WritePipeHandle() const { | |
| 215 return pipe_write_.get(); | |
| 216 } | |
| 217 | |
| 218 void WinChildProcess::CloseReadPipe() { | |
| 219 pipe_read_.reset(); | |
| 220 } | |
| 221 | |
| 222 void WinChildProcess::CloseWritePipe() { | |
| 223 pipe_write_.reset(); | |
| 224 } | |
| 225 | |
| 226 } // namespace test | |
| 227 } // namespace crashpad | |
| OLD | NEW |