mirror of
https://github.com/chromium/crashpad.git
synced 2024-12-28 07:48:14 +08:00
a45eea40fc
I’m most interested in picking up 1b3eb6ef3462, “Explicitly define copy constructors used in googletest tests.” This also reorganizes files and rewrites text to refer to this project as Google Test and googletest (and Google Mock and googlemock), as it prefers to be known. Some filenames are left at gtest_* following the precedent set by gtest itself. For example, #include "gtest/gtest.h" is still used, so #include "test/gtest_death.h" is retained too. gtest_all_test OutputFileHelpersTest.GetCurrentExecutableName hard-codes the expected executable name as gtest_all_test among other options that do not include googletest_all_test, so test executables retain their names as well. fb19f57880f6 Add GTEST_BRIEF option 3549237957a1 Ensure that gtest/gmock pkgconfig requirements specify version 189299e957bb Merge branch 'master' into quiet-flag 5504ded3ab5c Fix a typo in .travis.yml 6ed4e7168f54 Replace the last instance of `throw()` with `noexcept`. NFC 879fd9b45299 Remove duplicate codes existed in get-nprocessors.sh 644f3a992c28 gtest-unittest-api_test - fix warning in clang build 0b6d567619fe Remove redundant .c_str() be3ac45cf673 fix signed/unsigned comparison issue (on OpenBSD) b51a49e0cb82 Merge pull request #2773 from Quuxplusone:replace-noexcept c2032090f373 Merge pull request #2772 from Quuxplusone:travis 4fe5ac53337e Merge pull request #2756 from Conan-Kudo:fix-pkgconfig-reqs 373d72b6986f Googletest export 4c8e6a9fe1c8 Merge pull request #2810 from ptahmose:master 71d5df6c6b67 Merge pull request #2802 from e-i-n-s:fix_clang_warning dcc92d0ab6c4 Merge pull request #2805 from pepsiman:patch-1 4f002f1e236c VariadicMatcher needs a non-defaulted move constructor for compile-time performance 9d580ea80592 Enable protobuf printing for open-source proto messages 766ac2e1a413 Remove all uses of GTEST_DISALLOW_{MOVE_,}ASSIGN_ 11b3cec177b1 Fix a -Wdeprecated warning 01c0ff5e2373 Fix a -Wdeprecated warning c7d8ec72cc4b Fix a -Wdeprecated warning 1b066f4edfd5 Add -Wdeprecated to the build configuration 4bab55dc54b4 Removed a typo in README.md a67701056425 Googletest export fb5d9b66c5b0 Googletest export 1b3eb6ef3462 Googletest export b0e53e2d64db Merge pull request #2797 from Jyun-Neng:master d7ca9af0049e Googletest export 955552518b4e Googletest export ef25d27d4604 Merge pull request #2815 from Quuxplusone:simple 129329787429 Googletest export b99b421d8d68 Merge pull request #2818 from inazarenko:master 472cd8fd8b1c Merge pull request #2818 from inazarenko:master 3cfb4117f7e5 Googletest export 0eea2e9fc634 Googletest export a9f6c1ed1401 Googletest export 1a9c3e441407 Merge pull request #2830 from keshavgbpecdelhi:patch-1 e589a3371705 Merge pull request #2751 from calumr:quiet-flag Change-Id: Id788a27aa884ef68a21bae6c178cd456f5f6f2b0 Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/2186009 Reviewed-by: Joshua Peraza <jperaza@chromium.org> Commit-Queue: Mark Mentovai <mark@chromium.org>
260 lines
7.3 KiB
C++
260 lines
7.3 KiB
C++
// Copyright 2014 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 "test/multiprocess.h"
|
||
|
||
#include <signal.h>
|
||
#include <stdlib.h>
|
||
#include <sys/wait.h>
|
||
#include <unistd.h>
|
||
|
||
#include <memory>
|
||
#include <string>
|
||
|
||
#include "base/auto_reset.h"
|
||
#include "base/files/scoped_file.h"
|
||
#include "base/logging.h"
|
||
#include "base/posix/eintr_wrapper.h"
|
||
#include "base/strings/stringprintf.h"
|
||
#include "build/build_config.h"
|
||
#include "gtest/gtest.h"
|
||
#include "test/errors.h"
|
||
#include "util/misc/scoped_forbid_return.h"
|
||
#include "util/posix/signals.h"
|
||
|
||
#if defined(OS_MACOSX)
|
||
#include "test/mac/exception_swallower.h"
|
||
#endif
|
||
|
||
namespace crashpad {
|
||
namespace test {
|
||
|
||
namespace internal {
|
||
|
||
struct MultiprocessInfo {
|
||
MultiprocessInfo()
|
||
: pipe_c2p_read(-1),
|
||
pipe_c2p_write(-1),
|
||
pipe_p2c_read(-1),
|
||
pipe_p2c_write(-1),
|
||
child_pid(0) {}
|
||
|
||
base::ScopedFD pipe_c2p_read; // child to parent
|
||
base::ScopedFD pipe_c2p_write; // child to parent
|
||
base::ScopedFD pipe_p2c_read; // parent to child
|
||
base::ScopedFD pipe_p2c_write; // parent to child
|
||
pid_t child_pid; // valid only in parent
|
||
};
|
||
|
||
} // namespace internal
|
||
|
||
Multiprocess::Multiprocess()
|
||
: info_(nullptr),
|
||
code_(EXIT_SUCCESS),
|
||
reason_(kTerminationNormal) {
|
||
}
|
||
|
||
void Multiprocess::Run() {
|
||
ASSERT_EQ(info_, nullptr);
|
||
std::unique_ptr<internal::MultiprocessInfo> info(
|
||
new internal::MultiprocessInfo);
|
||
base::AutoReset<internal::MultiprocessInfo*> reset_info(&info_, info.get());
|
||
|
||
ASSERT_NO_FATAL_FAILURE(PreFork());
|
||
|
||
#if defined(OS_MACOSX)
|
||
// If the child is expected to crash, set up an exception swallower to swallow
|
||
// the exception instead of allowing it to be seen by the system’s crash
|
||
// reporter.
|
||
std::unique_ptr<ExceptionSwallower> exception_swallower;
|
||
if (reason_ == kTerminationSignal && Signals::IsCrashSignal(code_)) {
|
||
exception_swallower.reset(new ExceptionSwallower());
|
||
}
|
||
#endif // OS_MACOSX
|
||
|
||
pid_t pid = fork();
|
||
ASSERT_GE(pid, 0) << ErrnoMessage("fork");
|
||
|
||
if (pid > 0) {
|
||
info_->child_pid = pid;
|
||
|
||
RunParent();
|
||
|
||
// Waiting for the child happens here instead of in RunParent() because even
|
||
// if RunParent() returns early due to a Google Test fatal assertion
|
||
// failure, the child should still be reaped.
|
||
|
||
// This will make the parent hang up on the child as much as would be
|
||
// visible from the child’s perspective. The child’s side of the pipe will
|
||
// be broken, the child’s remote port will become a dead name, and an
|
||
// attempt by the child to look up the service will fail. If this weren’t
|
||
// done, the child might hang while waiting for a parent that has already
|
||
// triggered a fatal assertion failure to do something.
|
||
info.reset();
|
||
info_ = nullptr;
|
||
|
||
int status;
|
||
pid_t wait_pid = HANDLE_EINTR(waitpid(pid, &status, 0));
|
||
ASSERT_EQ(wait_pid, pid) << ErrnoMessage("waitpid");
|
||
|
||
TerminationReason reason;
|
||
int code;
|
||
std::string message;
|
||
if (WIFEXITED(status)) {
|
||
reason = kTerminationNormal;
|
||
code = WEXITSTATUS(status);
|
||
message = base::StringPrintf("Child exited with code %d", code);
|
||
} else if (WIFSIGNALED(status)) {
|
||
reason = kTerminationSignal;
|
||
code = WTERMSIG(status);
|
||
message =
|
||
base::StringPrintf("Child terminated by signal %d (%s)%s",
|
||
code,
|
||
strsignal(code),
|
||
WCOREDUMP(status) ? " (core dumped)" : "");
|
||
} else {
|
||
FAIL() << base::StringPrintf("Unknown termination reason 0x%x", status);
|
||
}
|
||
|
||
if (reason_ == kTerminationNormal) {
|
||
message += base::StringPrintf(", expected exit with code %d", code_);
|
||
} else if (reason_ == kTerminationSignal) {
|
||
message += base::StringPrintf(", expected termination by signal %d (%s)",
|
||
code_,
|
||
strsignal(code_));
|
||
}
|
||
|
||
if (reason != reason_ || code != code_) {
|
||
ADD_FAILURE() << message;
|
||
}
|
||
} else {
|
||
#if defined(OS_MACOSX)
|
||
if (exception_swallower.get()) {
|
||
ExceptionSwallower::SwallowExceptions();
|
||
}
|
||
#elif defined(OS_LINUX) || defined(OS_ANDROID)
|
||
if (reason_ == kTerminationSignal && Signals::IsCrashSignal(code_)) {
|
||
Signals::InstallDefaultHandler(code_);
|
||
}
|
||
#endif // OS_MACOSX
|
||
|
||
RunChild();
|
||
}
|
||
}
|
||
|
||
void Multiprocess::SetExpectedChildTermination(TerminationReason reason,
|
||
ReturnCodeType code) {
|
||
EXPECT_EQ(info_, nullptr)
|
||
<< "SetExpectedChildTermination() must be called before Run()";
|
||
reason_ = reason;
|
||
code_ = code;
|
||
}
|
||
|
||
void Multiprocess::SetExpectedChildTerminationBuiltinTrap() {
|
||
#if defined(ARCH_CPU_ARM64) || defined(ARCH_CPU_MIPS_FAMILY)
|
||
SetExpectedChildTermination(kTerminationSignal, SIGTRAP);
|
||
#else
|
||
SetExpectedChildTermination(kTerminationSignal, SIGILL);
|
||
#endif
|
||
}
|
||
|
||
Multiprocess::~Multiprocess() {
|
||
}
|
||
|
||
void Multiprocess::PreFork() {
|
||
int pipe_fds_c2p[2];
|
||
int rv = pipe(pipe_fds_c2p);
|
||
ASSERT_EQ(rv, 0) << ErrnoMessage("pipe");
|
||
|
||
info_->pipe_c2p_read.reset(pipe_fds_c2p[0]);
|
||
info_->pipe_c2p_write.reset(pipe_fds_c2p[1]);
|
||
|
||
int pipe_fds_p2c[2];
|
||
rv = pipe(pipe_fds_p2c);
|
||
ASSERT_EQ(rv, 0) << ErrnoMessage("pipe");
|
||
|
||
info_->pipe_p2c_read.reset(pipe_fds_p2c[0]);
|
||
info_->pipe_p2c_write.reset(pipe_fds_p2c[1]);
|
||
}
|
||
|
||
pid_t Multiprocess::ChildPID() const {
|
||
EXPECT_NE(info_->child_pid, 0);
|
||
return info_->child_pid;
|
||
}
|
||
|
||
FileHandle Multiprocess::ReadPipeHandle() const {
|
||
int fd = info_->child_pid ? info_->pipe_c2p_read.get()
|
||
: info_->pipe_p2c_read.get();
|
||
CHECK_NE(fd, -1);
|
||
return fd;
|
||
}
|
||
|
||
FileHandle Multiprocess::WritePipeHandle() const {
|
||
int fd = info_->child_pid ? info_->pipe_p2c_write.get()
|
||
: info_->pipe_c2p_write.get();
|
||
CHECK_NE(fd, -1);
|
||
return fd;
|
||
}
|
||
|
||
void Multiprocess::CloseReadPipe() {
|
||
if (info_->child_pid) {
|
||
info_->pipe_c2p_read.reset();
|
||
} else {
|
||
info_->pipe_p2c_read.reset();
|
||
}
|
||
}
|
||
|
||
void Multiprocess::CloseWritePipe() {
|
||
if (info_->child_pid) {
|
||
info_->pipe_p2c_write.reset();
|
||
} else {
|
||
info_->pipe_c2p_write.reset();
|
||
}
|
||
}
|
||
|
||
void Multiprocess::RunParent() {
|
||
// The parent uses the read end of c2p and the write end of p2c.
|
||
info_->pipe_c2p_write.reset();
|
||
info_->pipe_p2c_read.reset();
|
||
|
||
MultiprocessParent();
|
||
|
||
info_->pipe_c2p_read.reset();
|
||
info_->pipe_p2c_write.reset();
|
||
}
|
||
|
||
void Multiprocess::RunChild() {
|
||
ScopedForbidReturn forbid_return;
|
||
|
||
// The child uses the write end of c2p and the read end of p2c.
|
||
info_->pipe_c2p_read.reset();
|
||
info_->pipe_p2c_write.reset();
|
||
|
||
MultiprocessChild();
|
||
|
||
info_->pipe_c2p_write.reset();
|
||
info_->pipe_p2c_read.reset();
|
||
|
||
if (testing::Test::HasFailure()) {
|
||
// Trigger the ScopedForbidReturn destructor.
|
||
return;
|
||
}
|
||
|
||
// In a forked child, exit() is unsafe. Use _exit() instead.
|
||
_exit(EXIT_SUCCESS);
|
||
}
|
||
|
||
} // namespace test
|
||
} // namespace crashpad
|