2022-09-06 19:14:07 -04:00
|
|
|
|
// Copyright 2014 The Crashpad Authors
|
2014-09-03 18:24:29 -04:00
|
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
#include "test/multiprocess_exec.h"
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2015-01-28 14:49:42 -08:00
|
|
|
|
#include <unistd.h>
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
|
|
|
|
#include "base/posix/eintr_wrapper.h"
|
2018-12-20 14:42:17 -08:00
|
|
|
|
#include "build/build_config.h"
|
2014-09-03 18:24:29 -04:00
|
|
|
|
#include "gtest/gtest.h"
|
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
|
|
|
|
#include "test/errors.h"
|
2014-09-03 18:24:29 -04:00
|
|
|
|
#include "util/misc/scoped_forbid_return.h"
|
2014-12-19 16:42:34 -05:00
|
|
|
|
#include "util/posix/close_multiple.h"
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
|
2016-10-31 14:58:30 -04:00
|
|
|
|
#include <stdio_ext.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2018-12-20 11:12:51 -08:00
|
|
|
|
#include "util/mach/task_for_pid.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-09-03 18:24:29 -04:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
MultiprocessExec::MultiprocessExec()
|
|
|
|
|
: Multiprocess(),
|
|
|
|
|
command_(),
|
|
|
|
|
arguments_(),
|
|
|
|
|
argv_() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiprocessExec::SetChildCommand(
|
2017-11-01 10:37:01 -04:00
|
|
|
|
const base::FilePath& command,
|
|
|
|
|
const std::vector<std::string>* arguments) {
|
2014-09-03 18:24:29 -04:00
|
|
|
|
command_ = command;
|
|
|
|
|
if (arguments) {
|
|
|
|
|
arguments_ = *arguments;
|
|
|
|
|
} else {
|
|
|
|
|
arguments_.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MultiprocessExec::~MultiprocessExec() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiprocessExec::PreFork() {
|
2014-10-09 15:08:54 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Multiprocess::PreFork());
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
|
|
|
|
ASSERT_FALSE(command_.empty());
|
|
|
|
|
|
|
|
|
|
// Build up the argv vector. This is done in PreFork() instead of
|
|
|
|
|
// MultiprocessChild() because although the result is only needed in the child
|
|
|
|
|
// process, building it is a hazardous operation in that process.
|
|
|
|
|
ASSERT_TRUE(argv_.empty());
|
|
|
|
|
|
2017-11-01 10:37:01 -04:00
|
|
|
|
argv_.push_back(command_.value().c_str());
|
2014-09-03 18:24:29 -04:00
|
|
|
|
for (const std::string& argument : arguments_) {
|
|
|
|
|
argv_.push_back(argument.c_str());
|
|
|
|
|
}
|
2014-10-14 11:10:45 -04:00
|
|
|
|
argv_.push_back(nullptr);
|
2014-09-03 18:24:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiprocessExec::MultiprocessChild() {
|
|
|
|
|
// Make sure that stdin, stdout, and stderr are FDs 0, 1, and 2, respectively.
|
|
|
|
|
// All FDs above this will be closed.
|
2014-10-01 12:29:01 -07:00
|
|
|
|
static_assert(STDIN_FILENO == 0, "stdin must be fd 0");
|
|
|
|
|
static_assert(STDOUT_FILENO == 1, "stdout must be fd 1");
|
|
|
|
|
static_assert(STDERR_FILENO == 2, "stderr must be fd 2");
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
|
|
|
|
// Move the read pipe to stdin.
|
2015-01-28 14:49:42 -08:00
|
|
|
|
FileHandle read_handle = ReadPipeHandle();
|
|
|
|
|
ASSERT_NE(read_handle, STDIN_FILENO);
|
|
|
|
|
ASSERT_NE(read_handle, STDOUT_FILENO);
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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>
2017-04-04 00:35:21 -04:00
|
|
|
|
ASSERT_EQ(fileno(stdin), STDIN_FILENO);
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
2016-10-31 14:58:30 -04:00
|
|
|
|
int rv;
|
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
|
2016-10-31 14:58:30 -04:00
|
|
|
|
__fpurge(stdin);
|
|
|
|
|
#else
|
|
|
|
|
rv = fpurge(stdin);
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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>
2017-04-04 00:35:21 -04:00
|
|
|
|
ASSERT_EQ(rv, 0) << ErrnoMessage("fpurge");
|
2016-10-31 14:58:30 -04:00
|
|
|
|
#endif
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
2015-01-28 14:49:42 -08:00
|
|
|
|
rv = HANDLE_EINTR(dup2(read_handle, STDIN_FILENO));
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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>
2017-04-04 00:35:21 -04:00
|
|
|
|
ASSERT_EQ(rv, STDIN_FILENO) << ErrnoMessage("dup2");
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
|
|
|
|
// Move the write pipe to stdout.
|
2015-01-28 14:49:42 -08:00
|
|
|
|
FileHandle write_handle = WritePipeHandle();
|
|
|
|
|
ASSERT_NE(write_handle, STDIN_FILENO);
|
|
|
|
|
ASSERT_NE(write_handle, STDOUT_FILENO);
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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>
2017-04-04 00:35:21 -04:00
|
|
|
|
ASSERT_EQ(fileno(stdout), STDOUT_FILENO);
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
|
|
|
|
// Make a copy of the original stdout file descriptor so that in case there’s
|
2020-05-06 20:39:19 -04:00
|
|
|
|
// an execv() failure, the original stdout can be restored so that Google Test
|
2014-09-03 18:24:29 -04:00
|
|
|
|
// messages directed to stdout go to the right place. Mark it as
|
|
|
|
|
// close-on-exec, so that the child won’t see it after a successful exec(),
|
|
|
|
|
// but it will still be available in this process after an unsuccessful
|
|
|
|
|
// exec().
|
|
|
|
|
int dup_orig_stdout_fd = dup(STDOUT_FILENO);
|
|
|
|
|
ASSERT_GE(dup_orig_stdout_fd, 0) << ErrnoMessage("dup");
|
|
|
|
|
|
|
|
|
|
rv = fcntl(dup_orig_stdout_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
ASSERT_NE(rv, -1) << ErrnoMessage("fcntl");
|
|
|
|
|
|
|
|
|
|
rv = HANDLE_EINTR(fflush(stdout));
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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>
2017-04-04 00:35:21 -04:00
|
|
|
|
ASSERT_EQ(rv, 0) << ErrnoMessage("fflush");
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
2015-01-28 14:49:42 -08:00
|
|
|
|
rv = HANDLE_EINTR(dup2(write_handle, STDOUT_FILENO));
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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>
2017-04-04 00:35:21 -04:00
|
|
|
|
ASSERT_EQ(rv, STDOUT_FILENO) << ErrnoMessage("dup2");
|
2014-09-03 18:24:29 -04:00
|
|
|
|
|
|
|
|
|
CloseMultipleNowOrOnExec(STDERR_FILENO + 1, dup_orig_stdout_fd);
|
|
|
|
|
|
|
|
|
|
// Start the new program, replacing this one. execv() has a weird declaration
|
|
|
|
|
// where its argv argument is declared as char* const*. In reality, the
|
|
|
|
|
// implementation behaves as if the argument were const char* const*, and this
|
|
|
|
|
// behavior is required by the standard. See
|
|
|
|
|
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
|
|
|
|
|
// (search for “constant”).
|
|
|
|
|
execv(argv_[0], const_cast<char* const*>(&argv_[0]));
|
|
|
|
|
|
|
|
|
|
// This should not normally be reached. Getting here means that execv()
|
|
|
|
|
// failed.
|
|
|
|
|
|
|
|
|
|
// Be sure not to return until FAIL() is reached.
|
|
|
|
|
ScopedForbidReturn forbid_return;
|
|
|
|
|
|
|
|
|
|
// Put the original stdout back. Close the copy of the write pipe FD that’s
|
|
|
|
|
// currently on stdout first, so that in case the dup2() that restores the
|
|
|
|
|
// original stdout fails, stdout isn’t left attached to the pipe when the
|
|
|
|
|
// FAIL() statement executes.
|
|
|
|
|
HANDLE_EINTR(fflush(stdout));
|
|
|
|
|
IGNORE_EINTR(close(STDOUT_FILENO));
|
|
|
|
|
HANDLE_EINTR(dup2(dup_orig_stdout_fd, STDOUT_FILENO));
|
|
|
|
|
IGNORE_EINTR(close(dup_orig_stdout_fd));
|
|
|
|
|
|
|
|
|
|
forbid_return.Disarm();
|
|
|
|
|
FAIL() << ErrnoMessage("execv") << ": " << argv_[0];
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 13:37:12 -08:00
|
|
|
|
ProcessType MultiprocessExec::ChildProcess() {
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2018-12-20 11:12:51 -08:00
|
|
|
|
return TaskForPID(ChildPID());
|
|
|
|
|
#else
|
2018-01-25 13:37:12 -08:00
|
|
|
|
return ChildPID();
|
2018-12-20 11:12:51 -08:00
|
|
|
|
#endif
|
2018-01-25 13:37:12 -08:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-03 18:24:29 -04:00
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|