2014-08-26 17:10:19 -04:00
|
|
|
|
// 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.
|
|
|
|
|
|
test: Move util/test to its own top-level directory, test.
After 9e79ea1da719, it no longer makes sense for crashpad_util_test_lib
to “hide” in util/util_test.gyp. All of util/test is moved to its own
top-level directory, test, which all other test code is allowed to
depend on. test, too, is allowed to depend on all other non-test code.
In a future change, when crashpad_util_test_lib gains a dependency on
crashpad_client, it won’t look so weird for something in util (even
though it’s in util/test) to depend on something in client, because the
thing that needs to depend on client will live in test, not util.
BUG=crashpad:33
R=scottmg@chromium.org
Review URL: https://codereview.chromium.org/1051533002
2015-03-31 17:44:14 -04:00
|
|
|
|
#ifndef CRASHPAD_TEST_MULTIPROCESS_H_
|
|
|
|
|
#define CRASHPAD_TEST_MULTIPROCESS_H_
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
2019-02-25 08:31:11 -08:00
|
|
|
|
#include <stdint.h>
|
2015-01-28 14:49:42 -08:00
|
|
|
|
#include <sys/types.h>
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
2016-01-06 12:22:50 -05:00
|
|
|
|
#include "base/macros.h"
|
2015-01-28 14:49:42 -08:00
|
|
|
|
#include "build/build_config.h"
|
|
|
|
|
#include "util/file/file_io.h"
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
struct MultiprocessInfo;
|
2019-02-11 10:59:14 -05:00
|
|
|
|
} // namespace internal
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
2019-02-25 08:31:11 -08:00
|
|
|
|
#if defined(OS_FUCHSIA)
|
|
|
|
|
using ReturnCodeType = int64_t;
|
|
|
|
|
#else
|
|
|
|
|
using ReturnCodeType = int;
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-08-26 17:10:19 -04:00
|
|
|
|
//! \brief Manages a multiprocess test.
|
|
|
|
|
//!
|
|
|
|
|
//! These tests are `fork()`-based. The parent and child processes are able to
|
|
|
|
|
//! communicate via a pair of POSIX pipes.
|
|
|
|
|
//!
|
|
|
|
|
//! Subclasses are expected to implement the parent and child by overriding the
|
|
|
|
|
//! appropriate methods.
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//!
|
2018-01-17 14:03:39 -08:00
|
|
|
|
//! On Windows and Fuchsia, this class is only an internal implementation
|
|
|
|
|
//! detail of MultiprocessExec and all tests must use that class.
|
2014-08-26 17:10:19 -04:00
|
|
|
|
class Multiprocess {
|
|
|
|
|
public:
|
2014-09-09 17:04:47 -04:00
|
|
|
|
//! \brief The termination type for a child process.
|
|
|
|
|
enum TerminationReason : bool {
|
|
|
|
|
//! \brief The child terminated normally.
|
|
|
|
|
//!
|
|
|
|
|
//! A normal return happens when a test returns from RunChild(), or for
|
|
|
|
|
//! tests that `exec()`, returns from `main()`. This also happens for tests
|
|
|
|
|
//! that call `exit()` or `_exit()`.
|
|
|
|
|
kTerminationNormal = false,
|
|
|
|
|
|
2018-01-25 14:47:28 -08:00
|
|
|
|
#if !defined(OS_FUCHSIA) // There are no signals on Fuchsia.
|
2014-09-09 17:04:47 -04:00
|
|
|
|
//! \brief The child terminated by signal.
|
|
|
|
|
//!
|
|
|
|
|
//! Signal termination happens as a result of a crash, a call to `abort()`,
|
2020-05-06 20:39:19 -04:00
|
|
|
|
//! assertion failure (including Google Test assertions), etc.
|
2014-09-09 17:04:47 -04:00
|
|
|
|
kTerminationSignal,
|
2018-01-25 14:47:28 -08:00
|
|
|
|
#endif // !defined(OS_FUCHSIA)
|
2014-09-09 17:04:47 -04:00
|
|
|
|
};
|
|
|
|
|
|
2014-08-26 17:10:19 -04:00
|
|
|
|
Multiprocess();
|
|
|
|
|
|
|
|
|
|
//! \brief Runs the test.
|
|
|
|
|
//!
|
|
|
|
|
//! This method establishes the proper testing environment by calling
|
|
|
|
|
//! PreFork(), then calls `fork()`. In the parent process, it calls
|
|
|
|
|
//! RunParent(), and in the child process, it calls RunChild().
|
|
|
|
|
//!
|
2020-05-06 20:39:19 -04:00
|
|
|
|
//! This method uses Google Test assertions to validate the testing
|
|
|
|
|
//! environment. If the testing environment cannot be set up properly, it is
|
|
|
|
|
//! possible that MultiprocessParent() or MultiprocessChild() will not be
|
|
|
|
|
//! called. In the parent process, this method also waits for the child
|
|
|
|
|
//! process to exit after MultiprocessParent() returns, and verifies that it
|
|
|
|
|
//! exited in accordance with the expectations set by
|
|
|
|
|
//! SetExpectedChildTermination().
|
2014-08-26 17:10:19 -04:00
|
|
|
|
void Run();
|
|
|
|
|
|
2014-09-09 17:04:47 -04:00
|
|
|
|
//! \brief Sets the expected termination reason and code.
|
|
|
|
|
//!
|
2019-02-25 08:31:11 -08:00
|
|
|
|
//! The default expected termination reason is
|
2014-09-09 17:04:47 -04:00
|
|
|
|
//! TerminationReason::kTerminationNormal, and the default expected
|
|
|
|
|
//! termination code is `EXIT_SUCCESS` (`0`).
|
|
|
|
|
//!
|
mac: Tests that crash intentionally shouldn’t go to ReportCrash
Crashpad has many tests that crash intentionally. Some of these are
gtest death tests, and others arrange for intentional crashes to test
Crashpad’s own crash-catching logic. On macOS, all of the gtest death
tests and some of the other intentional crashes were being logged by
ReportCrash, the system’s crash reporter. Since these reports
corresponded to intentional crashes, they were never useful, and served
only to clutter ~/Library/Logs/DiagnosticReports.
Since Crashpad is adept at handling exceptions on its own, this
introduces the “exception swallowing server”,
crashpad_exception_swallower, which is a Mach exception server that
implements a no-op exception handler routine for all exceptions
received. The exception swallowing server is established as the task
handler for EXC_CRASH and EXC_CORPSE_NOTIFY exceptions during gtest
death tests invoked by {ASSERT,EXPECT}_DEATH_{CHECK,CRASH}, and for all
child processes invoked by the Multiprocess test infrastructure. The
exception swallowing server is not in effect at other times, so
unexpected crashes in test code can still be handled by ReportCrash or
another crash reporter.
With this change in place, no new reports are generated in the
user-level ~/Library/Logs/DiagnosticReports or the system’s
/Library/Logs/DiagnosticReports during a run of Crashpad’s full test
suite on macOS.
Bug: crashpad:33
Change-Id: I13891853a7e25accc30da21fa7ea8bd7d1f3bd2f
Reviewed-on: https://chromium-review.googlesource.com/777859
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-11-20 13:32:26 -05:00
|
|
|
|
//! This method does not need to be called if the default termination
|
|
|
|
|
//! expectation is appropriate, but if this method is called, it must be
|
|
|
|
|
//! called before Run().
|
|
|
|
|
//!
|
2014-09-09 17:04:47 -04:00
|
|
|
|
//! \param[in] reason Whether to expect the child to terminate normally or
|
|
|
|
|
//! as a result of a signal.
|
|
|
|
|
//! \param[in] code If \a reason is TerminationReason::kTerminationNormal,
|
|
|
|
|
//! this is the expected exit status of the child. If \a reason is
|
|
|
|
|
//! TerminationReason::kTerminationSignal, this is the signal that is
|
2018-07-11 08:59:41 -07:00
|
|
|
|
//! expected to kill the child. On Linux platforms, SIG_DFL will be
|
|
|
|
|
//! installed for \a code in the child process.
|
2019-02-25 08:31:11 -08:00
|
|
|
|
void SetExpectedChildTermination(TerminationReason reason,
|
|
|
|
|
ReturnCodeType code);
|
2014-09-09 17:04:47 -04:00
|
|
|
|
|
2018-02-22 12:45:42 -08:00
|
|
|
|
#if !defined(OS_WIN)
|
|
|
|
|
//! \brief Sets termination reason and code appropriately for a child that
|
|
|
|
|
//! terminates via `__builtin_trap()`.
|
|
|
|
|
void SetExpectedChildTerminationBuiltinTrap();
|
|
|
|
|
#endif // !OS_WIN
|
|
|
|
|
|
2014-08-26 17:10:19 -04:00
|
|
|
|
protected:
|
|
|
|
|
~Multiprocess();
|
|
|
|
|
|
|
|
|
|
//! \brief Establishes the proper testing environment prior to forking.
|
|
|
|
|
//!
|
|
|
|
|
//! Subclasses that solely implement a test should not need to override this
|
|
|
|
|
//! method. Subclasses that do not implement tests but instead implement
|
|
|
|
|
//! additional testing features on top of this class may override this method
|
|
|
|
|
//! provided that they call the superclass’ implementation first as follows:
|
|
|
|
|
//!
|
|
|
|
|
//! \code
|
2014-10-14 11:11:57 -04:00
|
|
|
|
//! void PreFork() override {
|
2014-10-09 15:08:54 -04:00
|
|
|
|
//! ASSERT_NO_FATAL_FAILURE(Multiprocess::PreFork());
|
2014-08-26 17:10:19 -04:00
|
|
|
|
//!
|
|
|
|
|
//! // Place subclass-specific pre-fork code here.
|
|
|
|
|
//! }
|
|
|
|
|
//! \endcode
|
|
|
|
|
//!
|
|
|
|
|
//! Subclass implementations may signal failure by raising their own fatal
|
2020-05-06 20:39:19 -04:00
|
|
|
|
//! Google Test assertions.
|
2018-01-17 14:03:39 -08:00
|
|
|
|
virtual void PreFork()
|
|
|
|
|
#if defined(OS_WIN) || defined(OS_FUCHSIA)
|
|
|
|
|
= 0
|
|
|
|
|
#endif // OS_WIN || OS_FUCHSIA
|
|
|
|
|
;
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
2018-01-17 14:03:39 -08:00
|
|
|
|
#if !defined(OS_WIN) && !defined(OS_FUCHSIA)
|
2014-08-26 17:10:19 -04:00
|
|
|
|
//! \brief Returns the child process’ process ID.
|
|
|
|
|
//!
|
|
|
|
|
//! This method may only be called by the parent process.
|
|
|
|
|
pid_t ChildPID() const;
|
2018-01-17 14:03:39 -08:00
|
|
|
|
#endif // !OS_WIN && !OS_FUCHSIA
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//! \brief Returns the read pipe’s file handle.
|
2014-08-26 17:10:19 -04:00
|
|
|
|
//!
|
|
|
|
|
//! This method may be called by either the parent or the child process.
|
|
|
|
|
//! Anything written to the write pipe in the partner process will appear
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//! on this file handle in this process.
|
2014-09-22 13:06:12 -04:00
|
|
|
|
//!
|
|
|
|
|
//! It is an error to call this after CloseReadPipe() has been called.
|
|
|
|
|
//!
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//! \return The read pipe’s file handle.
|
|
|
|
|
FileHandle ReadPipeHandle() const;
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//! \brief Returns the write pipe’s file handle.
|
2014-08-26 17:10:19 -04:00
|
|
|
|
//!
|
|
|
|
|
//! This method may be called by either the parent or the child process.
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//! Anything written to this file handle in this process will appear on
|
2014-08-26 17:10:19 -04:00
|
|
|
|
//! the read pipe in the partner process.
|
2014-09-22 13:06:12 -04:00
|
|
|
|
//!
|
|
|
|
|
//! It is an error to call this after CloseWritePipe() has been called.
|
|
|
|
|
//!
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//! \return The write pipe’s file handle.
|
|
|
|
|
FileHandle WritePipeHandle() const;
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
2014-09-22 13:06:12 -04:00
|
|
|
|
//! \brief Closes the read pipe.
|
|
|
|
|
//!
|
|
|
|
|
//! This method may be called by either the parent or the child process. An
|
|
|
|
|
//! attempt to write to the write pipe in the partner process will fail with
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//! `EPIPE` or `SIGPIPE`. ReadPipeHandle() must not be called after this.
|
2014-09-22 13:06:12 -04:00
|
|
|
|
void CloseReadPipe();
|
|
|
|
|
|
|
|
|
|
//! \brief Closes the write pipe.
|
|
|
|
|
//!
|
|
|
|
|
//! This method may be called by either the parent or the child process. An
|
|
|
|
|
//! attempt to read from the read pipe in the partner process will indicate
|
2015-01-28 14:49:42 -08:00
|
|
|
|
//! end-of-file. WritePipeHandle() must not be called after this.
|
2014-09-22 13:06:12 -04:00
|
|
|
|
void CloseWritePipe();
|
|
|
|
|
|
2015-01-28 14:49:42 -08:00
|
|
|
|
void set_info(internal::MultiprocessInfo* info) { info_ = info; }
|
2015-01-28 15:00:46 -08:00
|
|
|
|
internal::MultiprocessInfo* info() { return info_; }
|
2015-01-28 14:49:42 -08:00
|
|
|
|
|
2014-08-26 17:10:19 -04:00
|
|
|
|
private:
|
|
|
|
|
//! \brief Runs the parent side of the test.
|
|
|
|
|
//!
|
|
|
|
|
//! This method establishes the parent’s environment and calls
|
|
|
|
|
//! MultiprocessParent().
|
|
|
|
|
void RunParent();
|
|
|
|
|
|
|
|
|
|
//! \brief Runs the child side of the test.
|
|
|
|
|
//!
|
|
|
|
|
//! This method establishes the child’s environment, calls
|
2015-03-11 13:59:46 -04:00
|
|
|
|
//! MultiprocessChild(), and exits cleanly by calling `_exit(0)`. However, if
|
2020-05-06 20:39:19 -04:00
|
|
|
|
//! any failure (via fatal or nonfatal Google Test assertion) is detected, the
|
|
|
|
|
//! child will exit with a failure status.
|
2014-08-26 17:10:19 -04:00
|
|
|
|
void RunChild();
|
|
|
|
|
|
|
|
|
|
//! \brief The subclass-provided parent routine.
|
|
|
|
|
//!
|
2020-05-06 20:39:19 -04:00
|
|
|
|
//! Test failures should be reported via Google Test: `EXPECT_*()`,
|
|
|
|
|
//! `ASSERT_*()`, `FAIL()`, etc.
|
2014-08-26 17:10:19 -04:00
|
|
|
|
//!
|
|
|
|
|
//! This method must not use a `wait()`-family system call to wait for the
|
|
|
|
|
//! child process to exit, as this is handled by this class.
|
|
|
|
|
//!
|
|
|
|
|
//! Subclasses must implement this method to define how the parent operates.
|
|
|
|
|
virtual void MultiprocessParent() = 0;
|
|
|
|
|
|
|
|
|
|
//! \brief The subclass-provided child routine.
|
|
|
|
|
//!
|
2020-05-06 20:39:19 -04:00
|
|
|
|
//! Test failures should be reported via Google Test: `EXPECT_*()`,
|
|
|
|
|
//! `ASSERT_*()`, `FAIL()`, etc.
|
2014-08-26 17:10:19 -04:00
|
|
|
|
//!
|
|
|
|
|
//! Subclasses must implement this method to define how the child operates.
|
2015-03-11 13:59:46 -04:00
|
|
|
|
//! Subclasses may exit with a failure status by using `LOG(FATAL)`,
|
|
|
|
|
//! `abort()`, or similar. They may exit cleanly by returning from this method
|
|
|
|
|
//! or by calling `_exit(0)`. Under no circumstances may `exit()` be called
|
|
|
|
|
//! by the child without having the child process `exec()`. Use
|
|
|
|
|
//! MultiprocessExec if the child should call `exec()`.
|
2014-08-26 17:10:19 -04:00
|
|
|
|
virtual void MultiprocessChild() = 0;
|
|
|
|
|
|
|
|
|
|
internal::MultiprocessInfo* info_;
|
2019-02-25 08:31:11 -08:00
|
|
|
|
ReturnCodeType code_;
|
2014-09-09 17:04:47 -04:00
|
|
|
|
TerminationReason reason_;
|
2014-08-26 17:10:19 -04:00
|
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Multiprocess);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|
|
|
|
|
|
test: Move util/test to its own top-level directory, test.
After 9e79ea1da719, it no longer makes sense for crashpad_util_test_lib
to “hide” in util/util_test.gyp. All of util/test is moved to its own
top-level directory, test, which all other test code is allowed to
depend on. test, too, is allowed to depend on all other non-test code.
In a future change, when crashpad_util_test_lib gains a dependency on
crashpad_client, it won’t look so weird for something in util (even
though it’s in util/test) to depend on something in client, because the
thing that needs to depend on client will live in test, not util.
BUG=crashpad:33
R=scottmg@chromium.org
Review URL: https://codereview.chromium.org/1051533002
2015-03-31 17:44:14 -04:00
|
|
|
|
#endif // CRASHPAD_TEST_MULTIPROCESS_H_
|