mirror of
https://github.com/chromium/crashpad.git
synced 2025-03-10 06:36:02 +00:00
This supports multiprocess tests of the non-fork() variety. Also, improve directory finding so that the crashpad_test_test_multiprocess_exec_test_child binary can be located correctly on Fuchsia. Doc ref for launchpad: https://fuchsia.googlesource.com/zircon/+/master/system/ulib/launchpad/include/launchpad/launchpad.h#23 Also, roll mini_chromium to pick up ScopedZxHandle addition. Includes: a19ef08 Merge ScopedZxHandle from Chromium base f21c900 fuchsia: Move zircon libs dep to base, rather than global Bug: crashpad:196 Change-Id: Id01dee43f2d04e682e70c12777aff41f8dd848d6 Reviewed-on: https://chromium-review.googlesource.com/868967 Reviewed-by: Mark Mentovai <mark@chromium.org> Commit-Queue: Scott Graham <scottmg@chromium.org>
156 lines
3.9 KiB
C++
156 lines
3.9 KiB
C++
// Copyright 2018 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_exec.h"
|
|
|
|
#include <launchpad/launchpad.h>
|
|
#include <zircon/process.h>
|
|
#include <zircon/syscalls.h>
|
|
|
|
#include "base/files/scoped_file.h"
|
|
#include "base/fuchsia/fuchsia_logging.h"
|
|
#include "base/fuchsia/scoped_zx_handle.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
namespace crashpad {
|
|
namespace test {
|
|
|
|
namespace internal {
|
|
|
|
struct MultiprocessInfo {
|
|
MultiprocessInfo() {}
|
|
base::ScopedFD stdin_write;
|
|
base::ScopedFD stdout_read;
|
|
base::ScopedZxHandle child;
|
|
};
|
|
|
|
} // namespace internal
|
|
|
|
Multiprocess::Multiprocess()
|
|
: info_(nullptr),
|
|
code_(EXIT_SUCCESS),
|
|
reason_(kTerminationNormal) {
|
|
}
|
|
|
|
void Multiprocess::Run() {
|
|
// Set up and spawn the child process.
|
|
ASSERT_NO_FATAL_FAILURE(PreFork());
|
|
RunChild();
|
|
|
|
// And then run the parent actions in this process.
|
|
RunParent();
|
|
|
|
// Reap the child.
|
|
zx_signals_t signals;
|
|
ASSERT_EQ(
|
|
zx_object_wait_one(
|
|
info_->child.get(), ZX_TASK_TERMINATED, ZX_TIME_INFINITE, &signals),
|
|
ZX_OK);
|
|
ASSERT_EQ(signals, ZX_TASK_TERMINATED);
|
|
}
|
|
|
|
Multiprocess::~Multiprocess() {
|
|
delete info_;
|
|
}
|
|
|
|
FileHandle Multiprocess::ReadPipeHandle() const {
|
|
return info_->stdout_read.get();
|
|
}
|
|
|
|
FileHandle Multiprocess::WritePipeHandle() const {
|
|
return info_->stdin_write.get();
|
|
}
|
|
|
|
void Multiprocess::CloseReadPipe() {
|
|
info_->stdout_read.reset();
|
|
}
|
|
|
|
void Multiprocess::CloseWritePipe() {
|
|
info_->stdin_write.reset();
|
|
}
|
|
|
|
void Multiprocess::RunParent() {
|
|
MultiprocessParent();
|
|
|
|
info_->stdout_read.reset();
|
|
info_->stdin_write.reset();
|
|
}
|
|
|
|
void Multiprocess::RunChild() {
|
|
MultiprocessChild();
|
|
}
|
|
|
|
MultiprocessExec::MultiprocessExec()
|
|
: Multiprocess(), command_(), arguments_(), argv_() {
|
|
}
|
|
|
|
void MultiprocessExec::SetChildCommand(
|
|
const base::FilePath& command,
|
|
const std::vector<std::string>* arguments) {
|
|
command_ = command;
|
|
if (arguments) {
|
|
arguments_ = *arguments;
|
|
} else {
|
|
arguments_.clear();
|
|
}
|
|
}
|
|
|
|
MultiprocessExec::~MultiprocessExec() {}
|
|
|
|
void MultiprocessExec::PreFork() {
|
|
ASSERT_FALSE(command_.empty());
|
|
|
|
ASSERT_TRUE(argv_.empty());
|
|
|
|
argv_.push_back(command_.value().c_str());
|
|
for (const std::string& argument : arguments_) {
|
|
argv_.push_back(argument.c_str());
|
|
}
|
|
|
|
ASSERT_EQ(info(), nullptr);
|
|
set_info(new internal::MultiprocessInfo());
|
|
}
|
|
|
|
void MultiprocessExec::MultiprocessChild() {
|
|
launchpad_t* lp;
|
|
launchpad_create(zx_job_default(), command_.value().c_str(), &lp);
|
|
launchpad_load_from_file(lp, command_.value().c_str());
|
|
launchpad_set_args(lp, argv_.size(), &argv_[0]);
|
|
|
|
// Pass the filesystem namespace, parent environment, and default job to the
|
|
// child, but don't include any other file handles, preferring to set them
|
|
// up explicitly below.
|
|
launchpad_clone(
|
|
lp, LP_CLONE_FDIO_NAMESPACE | LP_CLONE_ENVIRON | LP_CLONE_DEFAULT_JOB);
|
|
|
|
int stdin_parent_side;
|
|
launchpad_add_pipe(lp, &stdin_parent_side, STDIN_FILENO);
|
|
info()->stdin_write.reset(stdin_parent_side);
|
|
|
|
int stdout_parent_side;
|
|
launchpad_add_pipe(lp, &stdout_parent_side, STDOUT_FILENO);
|
|
info()->stdout_read.reset(stdout_parent_side);
|
|
|
|
launchpad_clone_fd(lp, STDERR_FILENO, STDERR_FILENO);
|
|
|
|
const char* error_message;
|
|
zx_handle_t child;
|
|
zx_status_t status = launchpad_go(lp, &child, &error_message);
|
|
ZX_CHECK(status == ZX_OK, status) << "launchpad_go: " << error_message;
|
|
info()->child.reset(child);
|
|
}
|
|
|
|
} // namespace test
|
|
} // namespace crashpad
|