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_win.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/files/scoped_temp_dir.h" | |
10 #include "base/process/process_handle.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/test/multiprocess_test.h" | |
13 #include "base/threading/platform_thread.h" | |
14 #include "base/win/scoped_handle.h" | |
15 #include "base/win/win_util.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 #include "testing/multiprocess_func_list.h" | |
18 #include "third_party/crashpad/crashpad/client/crash_report_database.h" | |
19 #include "third_party/crashpad/crashpad/client/settings.h" | |
20 #include "third_party/crashpad/crashpad/snapshot/minidump/process_snapshot_minid ump.h" | |
21 #include "third_party/crashpad/crashpad/util/file/file_reader.h" | |
22 #include "third_party/crashpad/crashpad/util/misc/uuid.h" | |
23 | |
24 namespace crash_reporter { | |
25 | |
26 namespace { | |
27 | |
28 // This is the main function for the process to dump. It's unwise to call | |
29 // MinidumpWriteDump on one's own process, as that can hang or flake out in | |
scottmg
2017/01/10 21:33:40
same, and another below
Sigurður Ásgeirsson
2017/01/10 21:56:12
Done.
| |
30 // other ways. | |
31 MULTIPROCESS_TEST_MAIN(FallbackCrashHandlerWinSleeper) { | |
32 // Sleep forever, the parent will kill us. | |
33 Sleep(INFINITE); | |
34 return 0; | |
35 } | |
36 | |
37 class FallbackCrashHandlerWinTest : public base::MultiProcessTest { | |
38 public: | |
39 FallbackCrashHandlerWinTest() : sleeper_handle_(base::kNullProcessHandle) { | |
40 RtlCaptureContext(&context_); | |
41 memset(&exception_, 0, sizeof(exception_)); | |
42 exception_.ExceptionCode = EXCEPTION_ACCESS_VIOLATION; | |
43 | |
44 exc_ptrs_.ExceptionRecord = &exception_; | |
45 exc_ptrs_.ContextRecord = &context_; | |
46 } | |
47 | |
48 void SetUp() override { | |
49 ASSERT_TRUE(database_dir_.CreateUniqueTempDir()); | |
50 | |
51 // Spawn the target sleeper process. | |
52 sleeper_ = SpawnChild("FallbackCrashHandlerWinSleeper"); | |
53 | |
54 // Open a handle to the sleeper process. | |
55 const DWORD kAccessMask = | |
56 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_DUP_HANDLE; | |
57 sleeper_handle_ = OpenProcess(kAccessMask, FALSE, sleeper_.Pid()); | |
58 DWORD err = GetLastError(); | |
59 EXPECT_NE(base::kNullProcessHandle, sleeper_handle_) << "GetLastError: " | |
60 << err; | |
61 } | |
62 | |
63 void TearDown() { | |
64 // Terminate the sleeper, so that it doesn't leak. | |
65 EXPECT_TRUE(sleeper_.Terminate(1, true)); | |
66 | |
67 if (sleeper_handle_ != base::kNullProcessHandle) { | |
68 CloseHandle(sleeper_handle_); | |
69 sleeper_handle_ = base::kNullProcessHandle; | |
70 } | |
71 } | |
72 | |
73 std::string ExcPtrsAsString() const { | |
74 return base::UintToString(reinterpret_cast<uintptr_t>(&exc_ptrs_)); | |
75 }; | |
76 | |
77 std::string SleeperHandleAsString() const { | |
78 return base::UintToString(base::win::HandleToUint32(sleeper_handle_)); | |
79 }; | |
80 | |
81 void CreateDatabase() { | |
82 std::unique_ptr<crashpad::CrashReportDatabase> database = | |
83 crashpad::CrashReportDatabase::InitializeWithoutCreating( | |
84 database_dir_.GetPath()); | |
85 } | |
86 | |
87 protected: | |
88 CONTEXT context_; | |
89 EXCEPTION_RECORD exception_; | |
90 EXCEPTION_POINTERS exc_ptrs_; | |
91 | |
92 base::Process sleeper_; | |
93 base::ProcessHandle sleeper_handle_; | |
94 base::ScopedTempDir database_dir_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(FallbackCrashHandlerWinTest); | |
97 }; | |
98 | |
99 } // namespace | |
100 | |
101 TEST_F(FallbackCrashHandlerWinTest, ParseCommandLine) { | |
102 FallbackCrashHandler handler; | |
103 | |
104 // An empty command line shouldn't work. | |
105 base::CommandLine cmd_line(base::FilePath(L"empty")); | |
106 ASSERT_FALSE(handler.ParseCommandLine(cmd_line)); | |
107 | |
108 cmd_line.AppendSwitchPath("database", database_dir_.GetPath()); | |
109 cmd_line.AppendSwitchASCII("exception-pointers", ExcPtrsAsString()); | |
110 cmd_line.AppendSwitchASCII("process", SleeperHandleAsString()); | |
111 | |
112 // Thread missing, still should fail. | |
113 ASSERT_FALSE(handler.ParseCommandLine(cmd_line)); | |
114 | |
115 cmd_line.AppendSwitchASCII( | |
116 "thread", base::UintToString(base::PlatformThread::CurrentId())); | |
117 | |
118 // Should succeed with a fully populated command line. | |
119 // Because of how handle ownership is guarded, we have to "disown" it before | |
120 // the handler takes it over. | |
121 EXPECT_TRUE(handler.ParseCommandLine(cmd_line)); | |
122 sleeper_handle_ = base::kNullProcessHandle; | |
123 } | |
124 | |
125 TEST_F(FallbackCrashHandlerWinTest, GenerateCrashDump) { | |
126 FallbackCrashHandler handler; | |
127 | |
128 base::CommandLine cmd_line(base::FilePath(L"empty")); | |
129 cmd_line.AppendSwitchPath("database", database_dir_.GetPath()); | |
130 cmd_line.AppendSwitchASCII("exception-pointers", ExcPtrsAsString()); | |
131 | |
132 // TODO(siggi): It's probably safer to spawn a sacrificial process and then | |
133 // terminate it. MinidumpWriteDump is alleged to occasionally hang if used | |
134 // to dump own process. | |
135 cmd_line.AppendSwitchASCII("process", SleeperHandleAsString()); | |
136 cmd_line.AppendSwitchASCII( | |
137 "thread", base::UintToString(base::PlatformThread::CurrentId())); | |
138 | |
139 // Because how handle ownership is guarded, we have to "disown" this before | |
140 // the handler takes it over. | |
141 ASSERT_TRUE(handler.ParseCommandLine(cmd_line)); | |
142 sleeper_handle_ = base::kNullProcessHandle; | |
143 | |
144 const char kProduct[] = "SomeProduct"; | |
145 const char kVersion[] = "1.2.3.6"; | |
146 const char kChannel[] = "canary"; | |
147 const char kProcessType[] = "Test"; | |
148 | |
149 EXPECT_TRUE( | |
150 handler.GenerateCrashDump(kProduct, kVersion, kChannel, kProcessType)); | |
151 | |
152 // Validate that the database contains one valid crash dump. | |
153 std::unique_ptr<crashpad::CrashReportDatabase> database = | |
154 crashpad::CrashReportDatabase::InitializeWithoutCreating( | |
155 database_dir_.GetPath()); | |
156 | |
157 std::vector<crashpad::CrashReportDatabase::Report> reports; | |
158 ASSERT_EQ(crashpad::CrashReportDatabase::kNoError, | |
159 database->GetPendingReports(&reports)); | |
160 | |
161 EXPECT_EQ(1U, reports.size()); | |
162 | |
163 // Validate crashpad can read the produced minidump. | |
164 crashpad::FileReader minidump_file_reader; | |
165 ASSERT_TRUE(minidump_file_reader.Open(reports[0].file_path)); | |
166 | |
167 crashpad::ProcessSnapshotMinidump minidump_process_snapshot; | |
168 ASSERT_TRUE(minidump_process_snapshot.Initialize(&minidump_file_reader)); | |
169 | |
170 crashpad::UUID expected_client_id; | |
171 ASSERT_TRUE(database->GetSettings()->GetClientID(&expected_client_id)); | |
172 | |
173 // Validate that the CrashpadInfo in the report contains the same basic | |
174 // info, as does the database. | |
175 crashpad::UUID client_id; | |
176 minidump_process_snapshot.ClientID(&client_id); | |
177 EXPECT_EQ(expected_client_id, client_id); | |
178 | |
179 crashpad::UUID report_id; | |
180 minidump_process_snapshot.ReportID(&report_id); | |
181 EXPECT_EQ(reports[0].uuid, report_id); | |
182 | |
183 std::map<std::string, std::string> parameters = | |
184 minidump_process_snapshot.AnnotationsSimpleMap(); | |
185 auto it = parameters.find("prod"); | |
186 EXPECT_NE(parameters.end(), it); | |
187 EXPECT_EQ(kProduct, it->second); | |
188 | |
189 it = parameters.find("ver"); | |
190 EXPECT_NE(parameters.end(), it); | |
191 EXPECT_EQ(kVersion, it->second); | |
192 | |
193 it = parameters.find("channel"); | |
194 EXPECT_NE(parameters.end(), it); | |
195 EXPECT_EQ(kChannel, it->second); | |
196 | |
197 it = parameters.find("plat"); | |
198 EXPECT_NE(parameters.end(), it); | |
199 #if defined(ARCH_CPU_64_BITS) | |
200 EXPECT_EQ("Win64", it->second); | |
201 #else | |
202 EXPECT_EQ("Win32", it->second); | |
203 #endif | |
204 | |
205 it = parameters.find("ptype"); | |
206 EXPECT_NE(parameters.end(), it); | |
207 EXPECT_EQ(kProcessType, it->second); | |
208 } | |
209 | |
210 } // namespace crash_reporter | |
OLD | NEW |