fuchsia: Update for zx_task_suspend() supporting processes

zx_task_suspend() now supports suspending processes. This is somewhat
more reliable than suspending the constituent threads because after the
call returns and the token is being held, any subsequently started
threads will start in the the suspended state.

However, because the suspend is asynchronous the threads of the process
still need to be iterated to wait for them to assert ZX_THREAD_SUSPENDED
(and that can and does still fail to happen for a number of reasons). So
while improved, this class is still only best-effort.

Additionally, as the version of ScopedTaskSuspend that took a thread
wasn't being used, remove that.

Bug: crashpad:269
Change-Id: Ifb3f8e0d780a5e22af33613f92a61d25459f5139
Reviewed-on: https://chromium-review.googlesource.com/c/1377201
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
This commit is contained in:
Scott Graham 2018-12-14 14:07:56 -08:00 committed by Commit Bot
parent 25048d37f8
commit 922b5750c1
2 changed files with 15 additions and 42 deletions

View File

@ -24,39 +24,20 @@
namespace crashpad {
namespace {
// Returns the suspend token of the suspended thread. This function attempts
// to wait a short time for the thread to actually suspend before returning
// but this is not guaranteed.
zx::suspend_token SuspendThread(const zx::thread& thread) {
zx::suspend_token token;
zx_status_t status = thread.suspend(&token);
if (status != ZX_OK) {
ZX_LOG(ERROR, status) << "zx_task_suspend";
return zx::suspend_token();
}
zx_signals_t observed = 0u;
if (thread.wait_one(ZX_THREAD_SUSPENDED,
zx::deadline_after(zx::msec(50)),
&observed) != ZX_OK) {
LOG(ERROR) << "thread failed to suspend";
}
return token;
}
} // namespace
ScopedTaskSuspend::ScopedTaskSuspend(const zx::process& process) {
DCHECK_NE(process.get(), zx::process::self()->get());
for (const auto& thread : GetThreadHandles(process))
suspend_tokens_.push_back(SuspendThread(thread));
}
ScopedTaskSuspend::ScopedTaskSuspend(const zx::thread& thread) {
DCHECK_NE(thread.get(), zx::thread::self()->get());
suspend_tokens_.push_back(SuspendThread(thread));
zx_status_t status = process.suspend(&suspend_token_);
if (status != ZX_OK) {
ZX_LOG(ERROR, status) << "zx_task_suspend";
} else {
for (const auto& thread : GetThreadHandles(process)) {
zx_signals_t observed = 0u;
status = thread.wait_one(
ZX_THREAD_SUSPENDED, zx::deadline_after(zx::msec(50)), &observed);
ZX_LOG_IF(ERROR, status != ZX_OK, status) << "thread failed to suspend";
}
}
}
} // namespace crashpad

View File

@ -27,27 +27,19 @@ namespace crashpad {
//! \brief Manages the suspension of another task.
//!
//! The underlying API only supports suspending threads (despite its name) not
//! entire tasks. As a result, it's possible some threads may not be correctly
//! suspended/resumed as their creation might race enumeration.
//!
//! Additionally, suspending a thread is asynchronous and may take an
//! arbitrary amount of time.
//!
//! Because of these limitations, this class is limited to being a best-effort,
//! and correct suspension/resumption cannot be relied upon.
//! Suspending a process is asynchronous, and may take an arbitrary amount of
//! time. As a result, this class is limited to being a best-effort, and
//! correct suspension/resumption cannot be relied upon.
//!
//! Callers should not attempt to suspend the current task as obtained via
//! `zx_process_self()`.
class ScopedTaskSuspend {
public:
explicit ScopedTaskSuspend(const zx::process& process);
explicit ScopedTaskSuspend(const zx::thread& thread);
~ScopedTaskSuspend() = default;
private:
// Could be one (for a thread) or many (for every thread in a process).
std::vector<zx::suspend_token> suspend_tokens_;
zx::suspend_token suspend_token_;
DISALLOW_COPY_AND_ASSIGN(ScopedTaskSuspend);
};