OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/crash/content/app/fallback_crash_handler_launcher_win.h" |
| 6 |
| 7 #include <dbghelp.h> |
| 8 |
| 9 #include "base/base_switches.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/files/file.h" |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/process/process.h" |
| 17 #include "base/process/process_handle.h" |
| 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/test/multiprocess_test.h" |
| 20 #include "base/win/win_util.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "testing/multiprocess_func_list.h" |
| 23 |
| 24 namespace crash_reporter { |
| 25 |
| 26 namespace { |
| 27 |
| 28 const wchar_t kFileName[] = L"crash.dmp"; |
| 29 |
| 30 // This function is called in the fallback handler process instance. |
| 31 MULTIPROCESS_TEST_MAIN(FallbackCrashHandlerLauncherMain) { |
| 32 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 33 |
| 34 // Check for presence of the "process" argument. |
| 35 CHECK(cmd_line->HasSwitch("process")); |
| 36 |
| 37 // Retrieve and check the handle to verify that it was inherited in. |
| 38 unsigned int uint_process = 0; |
| 39 CHECK(base::StringToUint(cmd_line->GetSwitchValueASCII("process"), |
| 40 &uint_process)); |
| 41 base::Process process(base::win::Uint32ToHandle(uint_process)); |
| 42 |
| 43 // Verify that the process handle points to our parent. |
| 44 CHECK_EQ(base::GetParentProcessId(base::GetCurrentProcessHandle()), |
| 45 process.Pid()); |
| 46 |
| 47 // Check the "thread" argument. |
| 48 CHECK(cmd_line->HasSwitch("thread")); |
| 49 |
| 50 // Retrieve the thread id. |
| 51 unsigned int thread_id = 0; |
| 52 CHECK( |
| 53 base::StringToUint(cmd_line->GetSwitchValueASCII("thread"), &thread_id)); |
| 54 |
| 55 // Check the "exception-pointers" argument. |
| 56 CHECK(cmd_line->HasSwitch("exception-pointers")); |
| 57 uint64_t uint_exc_ptrs = 0; |
| 58 CHECK(base::StringToUint64( |
| 59 cmd_line->GetSwitchValueASCII("exception-pointers"), &uint_exc_ptrs)); |
| 60 |
| 61 EXCEPTION_POINTERS* exc_ptrs = reinterpret_cast<EXCEPTION_POINTERS*>( |
| 62 static_cast<uintptr_t>(uint_exc_ptrs)); |
| 63 |
| 64 // Check the "database" argument. |
| 65 CHECK(cmd_line->HasSwitch("database")); |
| 66 base::FilePath database_dir = cmd_line->GetSwitchValuePath("database"); |
| 67 |
| 68 base::FilePath dump_path = database_dir.Append(kFileName); |
| 69 |
| 70 // Create a dump file in the directory. |
| 71 base::File dump(dump_path, base::File::FLAG_CREATE | base::File::FLAG_WRITE); |
| 72 |
| 73 CHECK(dump.IsValid()); |
| 74 |
| 75 const MINIDUMP_TYPE kMinidumpType = static_cast<MINIDUMP_TYPE>( |
| 76 MiniDumpWithHandleData | MiniDumpWithUnloadedModules | |
| 77 MiniDumpWithProcessThreadData | MiniDumpWithThreadInfo | |
| 78 MiniDumpWithTokenInformation); |
| 79 |
| 80 MINIDUMP_EXCEPTION_INFORMATION exc_info = {}; |
| 81 exc_info.ThreadId = thread_id; |
| 82 exc_info.ExceptionPointers = exc_ptrs; |
| 83 exc_info.ClientPointers = TRUE; // ExceptionPointers in client. |
| 84 |
| 85 // Write the minidump as a direct test of the validity and permissions on the |
| 86 // parent process handle. |
| 87 if (!MiniDumpWriteDump(process.Handle(), // Process handle. |
| 88 process.Pid(), // Process Id. |
| 89 dump.GetPlatformFile(), // File handle. |
| 90 kMinidumpType, // Minidump type. |
| 91 &exc_info, // Exception Param |
| 92 nullptr, // UserStreamParam, |
| 93 nullptr)) { // CallbackParam |
| 94 DWORD error = GetLastError(); |
| 95 |
| 96 dump.Close(); |
| 97 CHECK(base::DeleteFile(dump_path, false)); |
| 98 CHECK(false) << "Unable to write dump, error " << error; |
| 99 } |
| 100 |
| 101 return 0; |
| 102 } |
| 103 |
| 104 MULTIPROCESS_TEST_MAIN(TestCrashHandlerLauncherMain) { |
| 105 base::ScopedTempDir database_dir; |
| 106 CHECK(database_dir.CreateUniqueTempDir()); |
| 107 |
| 108 // Construct the base command line that diverts to the main function above. |
| 109 base::CommandLine base_cmdline( |
| 110 base::GetMultiProcessTestChildBaseCommandLine()); |
| 111 |
| 112 base_cmdline.AppendSwitchASCII(switches::kTestChildProcess, |
| 113 "FallbackCrashHandlerLauncherMain"); |
| 114 |
| 115 FallbackCrashHandlerLauncher launcher; |
| 116 CHECK(launcher.Initialize(base_cmdline, database_dir.GetPath())); |
| 117 |
| 118 // Make like an access violation at the current place. |
| 119 EXCEPTION_RECORD exc_record = {}; |
| 120 exc_record.ExceptionCode = EXCEPTION_ACCESS_VIOLATION; |
| 121 CONTEXT ctx = {}; |
| 122 RtlCaptureContext(&ctx); |
| 123 CHECK_NE(0UL, ctx.ContextFlags); |
| 124 |
| 125 EXCEPTION_POINTERS exc_ptrs = {&exc_record, &ctx}; |
| 126 |
| 127 CHECK_EQ(0UL, launcher.LaunchAndWaitForHandler(&exc_ptrs)); |
| 128 |
| 129 // Check that the dump was written, e.g. it's an existing file with non-zero |
| 130 // size. |
| 131 base::File dump(database_dir.GetPath().Append(kFileName), |
| 132 base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 133 CHECK(dump.IsValid()); |
| 134 CHECK_NE(0, dump.GetLength()); |
| 135 return 0; |
| 136 } |
| 137 |
| 138 class FallbackCrashHandlerLauncherTest : public base::MultiProcessTest {}; |
| 139 |
| 140 } // namespace |
| 141 |
| 142 TEST_F(FallbackCrashHandlerLauncherTest, LaunchAndWaitForHandler) { |
| 143 // Because this process is heavily multithreaded it's going to be flaky |
| 144 // and generally fraught with peril to try and grab a minidump of it. |
| 145 // Instead, fire off a sacrificial process to do the testing. |
| 146 base::Process test_process = SpawnChild("TestCrashHandlerLauncherMain"); |
| 147 int exit_code = 0; |
| 148 ASSERT_TRUE(test_process.WaitForExit(&exit_code)); |
| 149 ASSERT_EQ(0, exit_code); |
| 150 } |
| 151 |
| 152 } // namespace crash_reporter |
OLD | NEW |