Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: add size check for user-supplied future #6692

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tokio/src/runtime/blocking/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::runtime::blocking::schedule::BlockingSchedule;
use crate::runtime::blocking::{shutdown, BlockingTask};
use crate::runtime::builder::ThreadNameFn;
use crate::runtime::task::{self, JoinHandle};
use crate::runtime::{Builder, Callback, Handle};
use crate::runtime::{Builder, Callback, Handle, MAX_FUTURE_SIZE};
use crate::util::metric_atomics::MetricAtomicUsize;

use std::collections::{HashMap, VecDeque};
Expand Down Expand Up @@ -296,7 +296,7 @@ impl Spawner {
R: Send + 'static,
{
let (join_handle, spawn_result) =
if cfg!(debug_assertions) && std::mem::size_of::<F>() > 2048 {
if cfg!(debug_assertions) && std::mem::size_of::<F>() > MAX_FUTURE_SIZE {
self.spawn_blocking_inner(Box::new(func), Mandatory::NonMandatory, None, rt)
} else {
self.spawn_blocking_inner(func, Mandatory::NonMandatory, None, rt)
Expand All @@ -323,7 +323,7 @@ impl Spawner {
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let (join_handle, spawn_result) = if cfg!(debug_assertions) && std::mem::size_of::<F>() > 2048 {
let (join_handle, spawn_result) = if cfg!(debug_assertions) && std::mem::size_of::<F>() > MAX_FUTURE_SIZE {
self.spawn_blocking_inner(
Box::new(func),
Mandatory::Mandatory,
Expand Down
16 changes: 15 additions & 1 deletion tokio/src/runtime/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Handle {
}

use crate::runtime::task::JoinHandle;
use crate::runtime::MAX_FUTURE_SIZE;
use crate::util::error::{CONTEXT_MISSING_ERROR, THREAD_LOCAL_DESTROYED_ERROR};

use std::future::Future;
Expand Down Expand Up @@ -188,7 +189,11 @@ impl Handle {
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.spawn_named(future, None)
if cfg!(debug_assertions) && std::mem::size_of::<F>() > MAX_FUTURE_SIZE {
self.spawn_named(Box::pin(future), None)
} else {
self.spawn_named(future, None)
}
}

/// Runs the provided function on an executor dedicated to blocking
Expand Down Expand Up @@ -291,6 +296,15 @@ impl Handle {
/// [`tokio::time`]: crate::time
#[track_caller]
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
if cfg!(debug_assertions) && std::mem::size_of::<F>() > MAX_FUTURE_SIZE {
self.block_on_inner(Box::pin(future))
} else {
self.block_on_inner(future)
}
}

#[track_caller]
fn block_on_inner<F: Future>(&self, future: F) -> F::Output {
#[cfg(all(
tokio_unstable,
tokio_taskdump,
Expand Down
4 changes: 4 additions & 0 deletions tokio/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ cfg_rt! {
mod runtime;
pub use runtime::{Runtime, RuntimeFlavor};

/// Boundary value to prevent stack overflow caused by a large-sized
/// Future being placed in the stack.
pub(crate) const MAX_FUTURE_SIZE: usize = 2048;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really a maximum for the future size.

Suggested change
/// Boundary value to prevent stack overflow caused by a large-sized
/// Future being placed in the stack.
pub(crate) const MAX_FUTURE_SIZE: usize = 2048;
/// Boundary value to prevent stack overflow caused by a large-sized
/// Future being placed in the stack.
pub(crate) const BOX_FUTURE_THRESHOLD: usize = 2048;


mod thread_id;
pub(crate) use thread_id::ThreadId;

Expand Down
16 changes: 15 additions & 1 deletion tokio/src/runtime/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::MAX_FUTURE_SIZE;
use crate::runtime::blocking::BlockingPool;
use crate::runtime::scheduler::CurrentThread;
use crate::runtime::{context, EnterGuard, Handle};
Expand Down Expand Up @@ -240,7 +241,11 @@ impl Runtime {
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.handle.spawn(future)
if cfg!(debug_assertions) && std::mem::size_of::<F>() > MAX_FUTURE_SIZE {
self.handle.spawn_named(Box::pin(future), None)
} else {
self.handle.spawn_named(future, None)
}
}

/// Runs the provided function on an executor dedicated to blocking operations.
Expand Down Expand Up @@ -324,6 +329,15 @@ impl Runtime {
/// [handle]: fn@Handle::block_on
#[track_caller]
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
if cfg!(debug_assertions) && std::mem::size_of::<F>() > MAX_FUTURE_SIZE {
self.block_on_inner(Box::pin(future))
} else {
self.block_on_inner(future)
}
}

#[track_caller]
fn block_on_inner<F: Future>(&self, future: F) -> F::Output {
#[cfg(all(
tokio_unstable,
tokio_taskdump,
Expand Down
48 changes: 38 additions & 10 deletions tokio/src/task/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unreachable_pub)]
use crate::{
runtime::Handle,
runtime::{Handle, MAX_FUTURE_SIZE},
task::{JoinHandle, LocalSet},
};
use std::{future::Future, io};
Expand Down Expand Up @@ -88,7 +88,13 @@ impl<'a> Builder<'a> {
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
Ok(super::spawn::spawn_inner(future, self.name))
Ok(
if cfg!(debug_assertions) && std::mem::size_of::<Fut>() > MAX_FUTURE_SIZE {
super::spawn::spawn_inner(Box::pin(future), self.name)
} else {
super::spawn::spawn_inner(future, self.name)
},
)
}

/// Spawn a task with this builder's settings on the provided [runtime
Expand All @@ -104,7 +110,13 @@ impl<'a> Builder<'a> {
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
Ok(handle.spawn_named(future, self.name))
Ok(
if cfg!(debug_assertions) && std::mem::size_of::<Fut>() > MAX_FUTURE_SIZE {
handle.spawn_named(Box::pin(future), self.name)
} else {
handle.spawn_named(future, self.name)
},
)
}

/// Spawns `!Send` a task on the current [`LocalSet`] with this builder's
Expand All @@ -127,7 +139,13 @@ impl<'a> Builder<'a> {
Fut: Future + 'static,
Fut::Output: 'static,
{
Ok(super::local::spawn_local_inner(future, self.name))
Ok(
if cfg!(debug_assertions) && std::mem::size_of::<Fut>() > MAX_FUTURE_SIZE {
super::local::spawn_local_inner(Box::pin(future), self.name)
} else {
super::local::spawn_local_inner(future, self.name)
},
)
}

/// Spawns `!Send` a task on the provided [`LocalSet`] with this builder's
Expand Down Expand Up @@ -188,12 +206,22 @@ impl<'a> Builder<'a> {
Output: Send + 'static,
{
use crate::runtime::Mandatory;
let (join_handle, spawn_result) = handle.inner.blocking_spawner().spawn_blocking_inner(
function,
Mandatory::NonMandatory,
self.name,
handle,
);
let (join_handle, spawn_result) =
if cfg!(debug_assertions) && std::mem::size_of::<Function>() > MAX_FUTURE_SIZE {
handle.inner.blocking_spawner().spawn_blocking_inner(
Box::new(function),
Mandatory::NonMandatory,
self.name,
handle,
)
} else {
handle.inner.blocking_spawner().spawn_blocking_inner(
function,
Mandatory::NonMandatory,
self.name,
handle,
)
};

spawn_result?;
Ok(join_handle)
Expand Down
3 changes: 2 additions & 1 deletion tokio/src/task/spawn.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::runtime::MAX_FUTURE_SIZE;
use crate::task::JoinHandle;

use std::future::Future;
Expand Down Expand Up @@ -168,7 +169,7 @@ cfg_rt! {
{
// preventing stack overflows on debug mode, by quickly sending the
// task to the heap.
if cfg!(debug_assertions) && std::mem::size_of::<F>() > 2048 {
if cfg!(debug_assertions) && std::mem::size_of::<F>() > MAX_FUTURE_SIZE {
spawn_inner(Box::pin(future), None)
} else {
spawn_inner(future, None)
Expand Down
Loading