mirror of
https://github.com/chromium/crashpad.git
synced 2025-03-10 06:36:02 +00:00
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
77d6b17338
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
292 lines
10 KiB
C++
292 lines
10 KiB
C++
// Copyright 2015 The Crashpad Authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include "snapshot/win/exception_snapshot_win.h"
|
|
|
|
#include <string>
|
|
|
|
#include "base/files/file_path.h"
|
|
#include "base/strings/string16.h"
|
|
#include "base/strings/utf_string_conversions.h"
|
|
#include "client/crashpad_client.h"
|
|
#include "gtest/gtest.h"
|
|
#include "snapshot/win/process_snapshot_win.h"
|
|
#include "test/errors.h"
|
|
#include "test/test_paths.h"
|
|
#include "test/win/child_launcher.h"
|
|
#include "util/file/file_io.h"
|
|
#include "util/thread/thread.h"
|
|
#include "util/win/exception_handler_server.h"
|
|
#include "util/win/registration_protocol_win.h"
|
|
#include "util/win/scoped_handle.h"
|
|
#include "util/win/scoped_process_suspend.h"
|
|
|
|
namespace crashpad {
|
|
namespace test {
|
|
namespace {
|
|
|
|
// Runs the ExceptionHandlerServer on a background thread.
|
|
class RunServerThread : public Thread {
|
|
public:
|
|
// Instantiates a thread which will invoke server->Run(delegate);
|
|
RunServerThread(ExceptionHandlerServer* server,
|
|
ExceptionHandlerServer::Delegate* delegate)
|
|
: server_(server), delegate_(delegate) {}
|
|
~RunServerThread() override {}
|
|
|
|
private:
|
|
// Thread:
|
|
void ThreadMain() override { server_->Run(delegate_); }
|
|
|
|
ExceptionHandlerServer* server_;
|
|
ExceptionHandlerServer::Delegate* delegate_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RunServerThread);
|
|
};
|
|
|
|
// During destruction, ensures that the server is stopped and the background
|
|
// thread joined.
|
|
class ScopedStopServerAndJoinThread {
|
|
public:
|
|
ScopedStopServerAndJoinThread(ExceptionHandlerServer* server, Thread* thread)
|
|
: server_(server), thread_(thread) {}
|
|
~ScopedStopServerAndJoinThread() {
|
|
server_->Stop();
|
|
thread_->Join();
|
|
}
|
|
|
|
private:
|
|
ExceptionHandlerServer* server_;
|
|
Thread* thread_;
|
|
DISALLOW_COPY_AND_ASSIGN(ScopedStopServerAndJoinThread);
|
|
};
|
|
|
|
class CrashingDelegate : public ExceptionHandlerServer::Delegate {
|
|
public:
|
|
CrashingDelegate(HANDLE server_ready, HANDLE completed_test_event)
|
|
: server_ready_(server_ready),
|
|
completed_test_event_(completed_test_event),
|
|
break_near_(0) {}
|
|
~CrashingDelegate() override {}
|
|
|
|
void set_break_near(WinVMAddress break_near) { break_near_ = break_near; }
|
|
|
|
void ExceptionHandlerServerStarted() override { SetEvent(server_ready_); }
|
|
|
|
unsigned int ExceptionHandlerServerException(
|
|
HANDLE process,
|
|
WinVMAddress exception_information_address,
|
|
WinVMAddress debug_critical_section_address) override {
|
|
ScopedProcessSuspend suspend(process);
|
|
ProcessSnapshotWin snapshot;
|
|
snapshot.Initialize(process,
|
|
ProcessSuspensionState::kSuspended,
|
|
exception_information_address,
|
|
debug_critical_section_address);
|
|
|
|
// Confirm the exception record was read correctly.
|
|
EXPECT_NE(snapshot.Exception()->ThreadID(), 0u);
|
|
EXPECT_EQ(EXCEPTION_BREAKPOINT, snapshot.Exception()->Exception());
|
|
|
|
// Verify the exception happened at the expected location with a bit of
|
|
// slop space to allow for reading the current PC before the exception
|
|
// happens. See TestCrashingChild().
|
|
const uint64_t kAllowedOffset = 64;
|
|
EXPECT_GT(snapshot.Exception()->ExceptionAddress(), break_near_);
|
|
EXPECT_LT(snapshot.Exception()->ExceptionAddress(),
|
|
break_near_ + kAllowedOffset);
|
|
|
|
SetEvent(completed_test_event_);
|
|
|
|
return snapshot.Exception()->Exception();
|
|
}
|
|
|
|
private:
|
|
HANDLE server_ready_; // weak
|
|
HANDLE completed_test_event_; // weak
|
|
WinVMAddress break_near_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CrashingDelegate);
|
|
};
|
|
|
|
void TestCrashingChild(const base::string16& directory_modification) {
|
|
// Set up the registration server on a background thread.
|
|
ScopedKernelHANDLE server_ready(CreateEvent(nullptr, false, false, nullptr));
|
|
ASSERT_TRUE(server_ready.is_valid()) << ErrorMessage("CreateEvent");
|
|
ScopedKernelHANDLE completed(CreateEvent(nullptr, false, false, nullptr));
|
|
ASSERT_TRUE(completed.is_valid()) << ErrorMessage("CreateEvent");
|
|
CrashingDelegate delegate(server_ready.get(), completed.get());
|
|
|
|
ExceptionHandlerServer exception_handler_server(true);
|
|
std::wstring pipe_name(L"\\\\.\\pipe\\test_name");
|
|
exception_handler_server.SetPipeName(pipe_name);
|
|
RunServerThread server_thread(&exception_handler_server, &delegate);
|
|
server_thread.Start();
|
|
ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread(
|
|
&exception_handler_server, &server_thread);
|
|
|
|
EXPECT_EQ(WaitForSingleObject(server_ready.get(), INFINITE), WAIT_OBJECT_0)
|
|
<< ErrorMessage("WaitForSingleObject");
|
|
|
|
// Spawn a child process, passing it the pipe name to connect to.
|
|
base::FilePath test_executable = TestPaths::Executable();
|
|
std::wstring child_test_executable =
|
|
test_executable.DirName()
|
|
.Append(directory_modification)
|
|
.Append(test_executable.BaseName().RemoveFinalExtension().value() +
|
|
L"_crashing_child.exe")
|
|
.value();
|
|
ChildLauncher child(child_test_executable, pipe_name);
|
|
child.Start();
|
|
|
|
// The child tells us (approximately) where it will crash.
|
|
WinVMAddress break_near_address;
|
|
LoggingReadFileExactly(child.stdout_read_handle(),
|
|
&break_near_address,
|
|
sizeof(break_near_address));
|
|
delegate.set_break_near(break_near_address);
|
|
|
|
// Wait for the child to crash and the exception information to be validated.
|
|
EXPECT_EQ(WaitForSingleObject(completed.get(), INFINITE), WAIT_OBJECT_0)
|
|
<< ErrorMessage("WaitForSingleObject");
|
|
|
|
EXPECT_EQ(child.WaitForExit(), EXCEPTION_BREAKPOINT);
|
|
}
|
|
|
|
TEST(ExceptionSnapshotWinTest, ChildCrash) {
|
|
TestCrashingChild(FILE_PATH_LITERAL("."));
|
|
}
|
|
|
|
#if defined(ARCH_CPU_64_BITS)
|
|
TEST(ExceptionSnapshotWinTest, ChildCrashWOW64) {
|
|
#ifndef NDEBUG
|
|
TestCrashingChild(FILE_PATH_LITERAL("..\\..\\out\\Debug"));
|
|
#else
|
|
TestCrashingChild(FILE_PATH_LITERAL("..\\..\\out\\Release"));
|
|
#endif
|
|
}
|
|
#endif // ARCH_CPU_64_BITS
|
|
|
|
class SimulateDelegate : public ExceptionHandlerServer::Delegate {
|
|
public:
|
|
SimulateDelegate(HANDLE server_ready, HANDLE completed_test_event)
|
|
: server_ready_(server_ready),
|
|
completed_test_event_(completed_test_event),
|
|
dump_near_(0) {}
|
|
~SimulateDelegate() override {}
|
|
|
|
void set_dump_near(WinVMAddress dump_near) { dump_near_ = dump_near; }
|
|
|
|
void ExceptionHandlerServerStarted() override { SetEvent(server_ready_); }
|
|
|
|
unsigned int ExceptionHandlerServerException(
|
|
HANDLE process,
|
|
WinVMAddress exception_information_address,
|
|
WinVMAddress debug_critical_section_address) override {
|
|
ScopedProcessSuspend suspend(process);
|
|
ProcessSnapshotWin snapshot;
|
|
snapshot.Initialize(process,
|
|
ProcessSuspensionState::kSuspended,
|
|
exception_information_address,
|
|
debug_critical_section_address);
|
|
EXPECT_TRUE(snapshot.Exception());
|
|
EXPECT_EQ(snapshot.Exception()->Exception(), 0x517a7ed);
|
|
|
|
// Verify the dump was captured at the expected location with some slop
|
|
// space.
|
|
const uint64_t kAllowedOffset = 64;
|
|
EXPECT_GT(snapshot.Exception()->Context()->InstructionPointer(),
|
|
dump_near_);
|
|
EXPECT_LT(snapshot.Exception()->Context()->InstructionPointer(),
|
|
dump_near_ + kAllowedOffset);
|
|
|
|
EXPECT_EQ(snapshot.Exception()->ExceptionAddress(),
|
|
snapshot.Exception()->Context()->InstructionPointer());
|
|
|
|
SetEvent(completed_test_event_);
|
|
|
|
return 0;
|
|
}
|
|
|
|
private:
|
|
HANDLE server_ready_; // weak
|
|
HANDLE completed_test_event_; // weak
|
|
WinVMAddress dump_near_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SimulateDelegate);
|
|
};
|
|
|
|
void TestDumpWithoutCrashingChild(
|
|
const base::string16& directory_modification) {
|
|
// Set up the registration server on a background thread.
|
|
ScopedKernelHANDLE server_ready(CreateEvent(nullptr, false, false, nullptr));
|
|
ASSERT_TRUE(server_ready.is_valid()) << ErrorMessage("CreateEvent");
|
|
ScopedKernelHANDLE completed(CreateEvent(nullptr, false, false, nullptr));
|
|
ASSERT_TRUE(completed.is_valid()) << ErrorMessage("CreateEvent");
|
|
SimulateDelegate delegate(server_ready.get(), completed.get());
|
|
|
|
ExceptionHandlerServer exception_handler_server(true);
|
|
std::wstring pipe_name(L"\\\\.\\pipe\\test_name");
|
|
exception_handler_server.SetPipeName(pipe_name);
|
|
RunServerThread server_thread(&exception_handler_server, &delegate);
|
|
server_thread.Start();
|
|
ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread(
|
|
&exception_handler_server, &server_thread);
|
|
|
|
EXPECT_EQ(WaitForSingleObject(server_ready.get(), INFINITE), WAIT_OBJECT_0)
|
|
<< ErrorMessage("WaitForSingleObject");
|
|
|
|
// Spawn a child process, passing it the pipe name to connect to.
|
|
base::FilePath test_executable = TestPaths::Executable();
|
|
std::wstring child_test_executable =
|
|
test_executable.DirName()
|
|
.Append(directory_modification)
|
|
.Append(test_executable.BaseName().RemoveFinalExtension().value() +
|
|
L"_dump_without_crashing.exe")
|
|
.value();
|
|
ChildLauncher child(child_test_executable, pipe_name);
|
|
child.Start();
|
|
|
|
// The child tells us (approximately) where it will capture a dump.
|
|
WinVMAddress dump_near_address;
|
|
LoggingReadFileExactly(child.stdout_read_handle(),
|
|
&dump_near_address,
|
|
sizeof(dump_near_address));
|
|
delegate.set_dump_near(dump_near_address);
|
|
|
|
// Wait for the child to crash and the exception information to be validated.
|
|
EXPECT_EQ(WaitForSingleObject(completed.get(), INFINITE), WAIT_OBJECT_0)
|
|
<< ErrorMessage("WaitForSingleObject");
|
|
|
|
EXPECT_EQ(child.WaitForExit(), 0);
|
|
}
|
|
|
|
TEST(SimulateCrash, ChildDumpWithoutCrashing) {
|
|
TestDumpWithoutCrashingChild(FILE_PATH_LITERAL("."));
|
|
}
|
|
|
|
#if defined(ARCH_CPU_64_BITS)
|
|
TEST(SimulateCrash, ChildDumpWithoutCrashingWOW64) {
|
|
#ifndef NDEBUG
|
|
TestDumpWithoutCrashingChild(FILE_PATH_LITERAL("..\\..\\out\\Debug"));
|
|
#else
|
|
TestDumpWithoutCrashingChild(FILE_PATH_LITERAL("..\\..\\out\\Release"));
|
|
#endif
|
|
}
|
|
#endif // ARCH_CPU_64_BITS
|
|
|
|
} // namespace
|
|
} // namespace test
|
|
} // namespace crashpad
|