Skip to content

Commit

Permalink
tokio: limit taskdumps to specific, blessed platforms
Browse files Browse the repository at this point in the history
Taskdumps depend on backtrace-rs, whose unwinding behavior is
platform-dependent. Taskdumps should not be enabled on a platform
without first assessing and mitigating platform-dependent behavior.

ref: https://github.com/tokio-rs/tokio/pull/5608/files#r1162746834
  • Loading branch information
jswrenn committed Apr 12, 2023
1 parent 39ab744 commit 02a0c4c
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 15 deletions.
12 changes: 10 additions & 2 deletions examples/dump.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! This example demonstrates tokio's experimental taskdumping functionality.

#[cfg(all(tokio_unstable, target_os = "linux"))]
#[cfg(all(
tokio_unstable,
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
#[tokio::main(flavor = "current_thread")]
async fn main() {
use std::hint::black_box;
Expand Down Expand Up @@ -34,7 +38,11 @@ async fn main() {
}
}

#[cfg(not(all(tokio_unstable, target_os = "linux")))]
#[cfg(not(all(
tokio_unstable,
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
)))]
fn main() {
println!("task dumps are not available")
}
22 changes: 20 additions & 2 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,26 @@ macro_rules! cfg_not_rt_multi_thread {
macro_rules! cfg_taskdump {
($($item:item)*) => {
$(
#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(
target_arch = "aarch64",
target_arch = "i686",
target_arch = "x86_64"
)
))]
#[cfg_attr(docsrs, doc(cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(
target_arch = "aarch64",
target_arch = "i686",
target_arch = "x86_64"
)
))))]
$item
)*
};
Expand Down
18 changes: 16 additions & 2 deletions tokio/src/runtime/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ struct Context {
/// the sheduler
budget: Cell<coop::Budget>,

#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
trace: trace::Context,
}

Expand Down Expand Up @@ -83,7 +88,16 @@ tokio_thread_local! {

budget: Cell::new(coop::Budget::unconstrained()),

#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(
target_arch = "aarch64",
target_arch = "i686",
target_arch = "x86_64"
)
))]
trace: trace::Context::new(),
}
}
Expand Down
14 changes: 12 additions & 2 deletions tokio/src/runtime/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ impl Handle {
/// [`tokio::time`]: crate::time
#[track_caller]
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
let future = super::task::trace::Trace::root(future);

#[cfg(all(tokio_unstable, feature = "tracing"))]
Expand All @@ -277,7 +282,12 @@ impl Handle {
F::Output: Send + 'static,
{
let id = crate::runtime::task::Id::next();
#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
let future = super::task::trace::Trace::root(future);
#[cfg(all(tokio_unstable, feature = "tracing"))]
let future = crate::util::trace::task(future, "task", _name, id.as_u64());
Expand Down
7 changes: 6 additions & 1 deletion tokio/src/runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ impl Runtime {
/// [handle]: fn@Handle::block_on
#[track_caller]
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
let future = super::task::trace::Trace::root(future);

#[cfg(all(tokio_unstable, feature = "tracing"))]
Expand Down
7 changes: 6 additions & 1 deletion tokio/src/runtime/scheduler/current_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,12 @@ impl Handle {
}

/// Capture a snapshot of this runtime's state.
#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
pub(crate) fn dump(&self) -> crate::runtime::Dump {
use crate::runtime::dump;
use task::trace::trace_current_thread;
Expand Down
7 changes: 6 additions & 1 deletion tokio/src/runtime/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,12 @@ impl<S: 'static> Task<S> {
}
}

#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
pub(crate) fn as_raw(&self) -> RawTask {
self.raw
}
Expand Down
7 changes: 6 additions & 1 deletion tokio/src/runtime/task/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,12 @@ impl State {
}

/// Transitions the state to `NOTIFIED`, unconditionally increasing the ref count.
#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
pub(super) fn transition_to_notified_for_tracing(&self) {
self.fetch_update_action(|mut snapshot| {
snapshot.set_notified();
Expand Down
11 changes: 10 additions & 1 deletion tokio/src/task/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,16 @@ cfg_rt! {
T::Output: Send + 'static,
{
use crate::runtime::task;
#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(
target_arch = "aarch64",
target_arch = "i686",
target_arch = "x86_64"
)
))]
let future = task::trace::Trace::root(future);
let id = task::Id::next();
let task = crate::util::trace::task(future, "task", name, id.as_u64());
Expand Down
7 changes: 6 additions & 1 deletion tokio/src/task/yield_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ pub async fn yield_now() {
type Output = ();

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
#[cfg(all(tokio_unstable, feature = "taskdump", target_os = "linux"))]
#[cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]
crate::runtime::task::trace::Trace::leaf();

if self.yielded {
Expand Down
7 changes: 6 additions & 1 deletion tokio/tests/dump_current_thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#![cfg(all(feature = "taskdump", tokio_unstable, target_os = "linux"))]
#![cfg(all(
tokio_unstable,
feature = "taskdump",
target_os = "linux",
any(target_arch = "aarch64", target_arch = "i686", target_arch = "x86_64")
))]

use std::hint::black_box;
use tokio::runtime;
Expand Down

0 comments on commit 02a0c4c

Please sign in to comment.