Add the MultiprocessExec test and its test.

TEST=util_test MultiprocessExec.*
R=rsesek@chromium.org

Review URL: https://codereview.chromium.org/531203002
This commit is contained in:
Mark Mentovai 2014-09-03 18:24:29 -04:00
parent d492df404c
commit 52064fdd1b
11 changed files with 452 additions and 8 deletions

View File

@ -0,0 +1,29 @@
// 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.
#ifndef CRASHPAD_UTIL_TEST_EXECUTABLE_PATH_H_
#define CRASHPAD_UTIL_TEST_EXECUTABLE_PATH_H_
#include "base/files/file_path.h"
namespace crashpad {
namespace test {
//! \brief Returns the pathname of the currently-running test executable.
base::FilePath ExecutablePath();
} // namespace test
} // namespace crashpad
#endif // CRASHPAD_UTIL_TEST_EXECUTABLE_PATH_H_

View File

@ -0,0 +1,37 @@
// 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 "util/test/executable_path.h"
#include <mach-o/dyld.h>
#include <stdint.h>
#include "base/logging.h"
namespace crashpad {
namespace test {
base::FilePath ExecutablePath() {
uint32_t executable_length = 0;
_NSGetExecutablePath(NULL, &executable_length);
DCHECK_GT(executable_length, 1u);
std::string executable_path(executable_length, std::string::value_type());
int rv = _NSGetExecutablePath(&executable_path[0], &executable_length);
DCHECK_EQ(rv, 0);
return base::FilePath(executable_path);
}
} // namespace test
} // namespace crashpad

View File

@ -0,0 +1,30 @@
// 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 "util/test/executable_path.h"
#include "base/files/file_path.h"
#include "gtest/gtest.h"
namespace {
using namespace crashpad::test;
TEST(ExecutablePath, ExecutablePath) {
base::FilePath executable_path = ExecutablePath();
base::FilePath executable_name = executable_path.BaseName();
EXPECT_EQ("util_test", executable_name.value());
}
} // namespace

View File

@ -64,7 +64,7 @@ class Multiprocess {
//! \code
//! virtual void PreFork() override {
//! Multiprocess::PreFork();
//! if (testing::Test::HasFatalAssertions()) {
//! if (testing::Test::HasFatalFailure()) {
//! return;
//! }
//!

View File

@ -0,0 +1,145 @@
// 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 "util/test/multiprocess_exec.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include "base/posix/eintr_wrapper.h"
#include "gtest/gtest.h"
#include "util/misc/scoped_forbid_return.h"
#include "util/test/errors.h"
#include "util/test/posix/close_multiple.h"
namespace crashpad {
namespace test {
using namespace testing;
MultiprocessExec::MultiprocessExec()
: Multiprocess(),
command_(),
arguments_(),
argv_() {
}
void MultiprocessExec::SetChildCommand(
const std::string& command, const std::vector<std::string>* arguments) {
command_ = command;
if (arguments) {
arguments_ = *arguments;
} else {
arguments_.clear();
}
}
MultiprocessExec::~MultiprocessExec() {
}
void MultiprocessExec::PreFork() {
Multiprocess::PreFork();
if (testing::Test::HasFatalFailure()) {
return;
}
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());
argv_.push_back(command_.c_str());
for (const std::string& argument : arguments_) {
argv_.push_back(argument.c_str());
}
argv_.push_back(NULL);
}
void MultiprocessExec::MultiprocessChild() {
// Make sure that stdin, stdout, and stderr are FDs 0, 1, and 2, respectively.
// All FDs above this will be closed.
COMPILE_ASSERT(STDIN_FILENO == 0, stdin_must_be_fd_0);
COMPILE_ASSERT(STDOUT_FILENO == 1, stdout_must_be_fd_1);
COMPILE_ASSERT(STDERR_FILENO == 2, stderr_must_be_fd_2);
// Move the read pipe to stdin.
int read_fd = ReadPipeFD();
ASSERT_NE(read_fd, STDIN_FILENO);
ASSERT_NE(read_fd, STDOUT_FILENO);
ASSERT_EQ(STDIN_FILENO, fileno(stdin));
int rv = fpurge(stdin);
ASSERT_EQ(0, rv) << ErrnoMessage("fpurge");
rv = HANDLE_EINTR(dup2(read_fd, STDIN_FILENO));
ASSERT_EQ(STDIN_FILENO, rv) << ErrnoMessage("dup2");
// Move the write pipe to stdout.
int write_fd = WritePipeFD();
ASSERT_NE(write_fd, STDIN_FILENO);
ASSERT_NE(write_fd, STDOUT_FILENO);
ASSERT_EQ(STDOUT_FILENO, fileno(stdout));
// Make a copy of the original stdout file descriptor so that in case theres
// an execv() failure, the original stdout can be restored so that gtest
// messages directed to stdout go to the right place. Mark it as
// close-on-exec, so that the child wont 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));
ASSERT_EQ(0, rv) << ErrnoMessage("fflush");
rv = HANDLE_EINTR(dup2(write_fd, STDOUT_FILENO));
ASSERT_EQ(STDOUT_FILENO, rv) << ErrnoMessage("dup2");
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 thats
// currently on stdout first, so that in case the dup2() that restores the
// original stdout fails, stdout isnt 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];
}
} // namespace test
} // namespace crashpad

View File

@ -0,0 +1,73 @@
// 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.
#ifndef CRASHPAD_UTIL_TEST_MULTIPROCESS_EXEC_H_
#define CRASHPAD_UTIL_TEST_MULTIPROCESS_EXEC_H_
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "util/test/multiprocess.h"
namespace crashpad {
namespace test {
//! \brief Manages an `exec()`-based multiprocess test.
//!
//! These tests are based on `fork()` and `exec()`. The parent process is able
//! to communicate with the child in the same manner as a base-class
//! Multiprocess parent. The read and write pipes appear in the child process on
//! stdin and stdout, respectively.
//!
//! Subclasses are expected to implement the parent in the same was as a
//! base-class Multiprocess parent. The child must be implemented in an
//! executable to be set by SetChildCommand().
class MultiprocessExec : public Multiprocess {
public:
MultiprocessExec();
//! \brief Sets the command to `exec()` in the child.
//!
//! This method must be called before the test can be Run().
//!
//! \param[in] command The executables pathname.
//! \param[in] arguments The command-line arguments to pass to the child
//! process in its `argv[]` vector. This vector must begin at `argv[1]`,
//! as \a command is implicitly used as `argv[0]`. This argument may be
//! `NULL` if no command-line arguments are to be passed.
void SetChildCommand(const std::string& command,
const std::vector<std::string>* arguments);
protected:
~MultiprocessExec();
// Multiprocess:
virtual void PreFork() override;
private:
// Multiprocess:
virtual void MultiprocessChild() override;
std::string command_;
std::vector<std::string> arguments_;
std::vector<const char*> argv_;
DISALLOW_COPY_AND_ASSIGN(MultiprocessExec);
};
} // namespace test
} // namespace crashpad
#endif // CRASHPAD_UTIL_TEST_MULTIPROCESS_EXEC_H_

View File

@ -0,0 +1,61 @@
// 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 "util/test/multiprocess_exec.h"
#include <unistd.h>
#include "base/basictypes.h"
#include "gtest/gtest.h"
#include "util/file/fd_io.h"
#include "util/test/errors.h"
#include "util/test/executable_path.h"
namespace {
using namespace crashpad;
using namespace crashpad::test;
class TestMultiprocessExec final : public MultiprocessExec {
public:
TestMultiprocessExec() : MultiprocessExec() {}
~TestMultiprocessExec() {}
private:
virtual void MultiprocessParent() override {
int write_fd = WritePipeFD();
char c = 'z';
ssize_t rv = WriteFD(write_fd, &c, 1);
ASSERT_EQ(1, rv) << ErrnoMessage("write");
int read_fd = ReadPipeFD();
rv = ReadFD(read_fd, &c, 1);
ASSERT_EQ(1, rv) << ErrnoMessage("read");
EXPECT_EQ('Z', c);
}
DISALLOW_COPY_AND_ASSIGN(TestMultiprocessExec);
};
TEST(MultiprocessExec, MultiprocessExec) {
TestMultiprocessExec multiprocess_exec;
base::FilePath test_executable = ExecutablePath();
std::string child_test_executable =
test_executable.value() + "_multiprocess_exec_test_child";
multiprocess_exec.SetChildCommand(child_test_executable, NULL);
multiprocess_exec.Run();
}
} // namespace

View File

@ -0,0 +1,51 @@
// 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 <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
int main(int argc, char* argv[]) {
// Make sure that theres nothing open at any FD higher than 3. All FDs other
// than stdin, stdout, and stderr should have been closed prior to or at
// exec().
int max_fd = std::max(static_cast<int>(sysconf(_SC_OPEN_MAX)), OPEN_MAX);
max_fd = std::max(max_fd, getdtablesize());
for (int fd = STDERR_FILENO + 1; fd < max_fd; ++fd) {
if (close(fd) == 0 || errno != EBADF) {
abort();
}
}
// Read a byte from stdin, expecting it to be a specific value.
char c;
ssize_t rv = read(STDIN_FILENO, &c, 1);
if (rv != 1 || c != 'z') {
abort();
}
// Write a byte to stdout.
c = 'Z';
rv = write(STDOUT_FILENO, &c, 1);
if (rv != 1) {
abort();
}
return 0;
}

View File

@ -77,7 +77,7 @@ typedef scoped_ptr<DIR, ScopedDIRCloser> ScopedDIR;
// system-specific FD directory to determine which file descriptors are open.
// This is an advantage over looping over all possible file descriptors, because
// no attempt needs to be made to close file descriptors that are not open.
bool CloseMultipleNowOrOnExecUsingFDDir(int fd) {
bool CloseMultipleNowOrOnExecUsingFDDir(int fd, int preserve_fd) {
#if defined(OS_MACOSX)
const char kFDDir[] = "/dev/fd";
#elif defined(OS_LINUX)
@ -120,7 +120,7 @@ bool CloseMultipleNowOrOnExecUsingFDDir(int fd) {
return false;
}
if (entry_fd >= fd && entry_fd != dir_fd) {
if (entry_fd >= fd && entry_fd != preserve_fd && entry_fd != dir_fd) {
CloseNowOrOnExec(entry_fd, false);
}
}
@ -130,8 +130,8 @@ bool CloseMultipleNowOrOnExecUsingFDDir(int fd) {
} // namespace
void CloseMultipleNowOrOnExec(int fd) {
if (CloseMultipleNowOrOnExecUsingFDDir(fd)) {
void CloseMultipleNowOrOnExec(int fd, int preserve_fd) {
if (CloseMultipleNowOrOnExecUsingFDDir(fd, preserve_fd)) {
return;
}
@ -147,8 +147,10 @@ void CloseMultipleNowOrOnExec(int fd) {
max_fd = std::max(max_fd, getdtablesize());
for (int entry_fd = fd; entry_fd < max_fd; ++entry_fd) {
if (entry_fd != preserve_fd) {
CloseNowOrOnExec(entry_fd, true);
}
}
}
} // namespace crashpad

View File

@ -34,7 +34,10 @@ namespace crashpad {
//! protected against `close()`.
//!
//! \param[in] fd The lowest file descriptor to close or set as close-on-exec.
void CloseMultipleNowOrOnExec(int fd);
//! \param[in] preserve_fd A file descriptor to preserve and not close (or set
//! as close-on-exec), even if it is open and its value is greater than \a
//! fd. To not preserve any file descriptor, pass `-1` for this parameter.
void CloseMultipleNowOrOnExec(int fd, int preserve_fd);
} // namespace crashpad

View File

@ -100,12 +100,16 @@
'sources': [
'test/errors.cc',
'test/errors.h',
'test/executable_path.h',
'test/executable_path_mac.cc',
'test/mac/mach_errors.cc',
'test/mac/mach_errors.h',
'test/mac/mach_multiprocess.cc',
'test/mac/mach_multiprocess.h',
'test/multiprocess.cc',
'test/multiprocess.h',
'test/multiprocess_exec.cc',
'test/multiprocess_exec.h',
'test/posix/close_multiple.cc',
'test/posix/close_multiple.h',
],
@ -144,9 +148,18 @@
'posix/process_util_test.cc',
'stdlib/strlcpy_test.cc',
'stdlib/strnlen_test.cc',
'test/executable_path_test.cc',
'test/mac/mach_multiprocess_test.cc',
'test/multiprocess_exec_test.cc',
'test/multiprocess_test.cc',
],
},
{
'target_name': 'util_test_multiprocess_exec_test_child',
'type': 'executable',
'sources': [
'test/multiprocess_exec_test_child.cc',
],
},
],
}