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

Avoid pointer casts in IO driver on miri #6859

Merged
merged 7 commits into from
Sep 23, 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
3 changes: 1 addition & 2 deletions tokio/src/runtime/io/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ impl Driver {
self.signal_ready = true;
} else {
let ready = Ready::from_mio(event);
// Use std::ptr::from_exposed_addr when stable
let ptr: *const ScheduledIo = token.0 as *const _;
let ptr = super::EXPOSE_IO.from_exposed_addr(token.0);

// Safety: we ensure that the pointers used as tokens are not freed
// until they are both deregistered from mio **and** we know the I/O
Expand Down
3 changes: 3 additions & 0 deletions tokio/src/runtime/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ use scheduled_io::ScheduledIo;

mod metrics;
use metrics::IoDriverMetrics;

use crate::util::PtrExposeDomain;
static EXPOSE_IO: PtrExposeDomain<ScheduledIo> = PtrExposeDomain::new();
1 change: 1 addition & 0 deletions tokio/src/runtime/io/registration_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl RegistrationSet {
// This function is marked as unsafe, because the caller must make sure that
// `io` is part of the registration set.
pub(super) unsafe fn remove(&self, synced: &mut Synced, io: &ScheduledIo) {
super::EXPOSE_IO.unexpose_provenance(io);
let _ = synced.registrations.remove(io.into());
}
}
Expand Down
3 changes: 1 addition & 2 deletions tokio/src/runtime/io/scheduled_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ impl Default for ScheduledIo {

impl ScheduledIo {
pub(crate) fn token(&self) -> mio::Token {
// use `expose_addr` when stable
mio::Token(self as *const _ as usize)
mio::Token(super::EXPOSE_IO.expose_provenance(self))
}

/// Invoked when the IO driver is shut down; forces this `ScheduledIo` into a
Expand Down
3 changes: 3 additions & 0 deletions tokio/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ cfg_rt! {

mod rc_cell;
pub(crate) use rc_cell::RcCell;

mod ptr_expose;
pub(crate) use ptr_expose::PtrExposeDomain;
}

cfg_rt_multi_thread! {
Expand Down
64 changes: 64 additions & 0 deletions tokio/src/util/ptr_expose.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Utility for helping miri understand our exposed pointers.
//!
//! During normal execution, this module is equivalent to pointer casts. However, when running
//! under miri, pointer casts are replaced with lookups in a hash map. This makes Tokio compatible
//! with strict provenance when running under miri (which comes with a perf cost).

use std::marker::PhantomData;
#[cfg(miri)]
use {std::collections::HashMap, crate::loom::sync::Mutex};

pub(crate) struct PtrExposeDomain<T> {
#[cfg(miri)]
map: Mutex<HashMap<usize, *mut T>>,
_phantom: PhantomData<T>,
}

impl<T> PtrExposeDomain<T> {
pub(crate) const fn new() -> Self {
Self {
#[cfg(miri)]
map: Mutex::new(HashMap::new()),
_phantom: PhantomData,
}
}

#[inline]
pub(crate) fn expose_provenance(&self, ptr: *const T) -> usize {
#[cfg(miri)]
{
// SAFETY: Equivalent to `pointer::addr` which is safe.
let addr: usize = unsafe { std::mem::transmute(ptr) };
self.map.lock().insert(addr, ptr);
addr
}

#[cfg(not(miri))]
{
ptr as usize
}
}

#[inline]
pub(crate) fn from_exposed_addr(&self, addr: usize) -> *const T {
#[cfg(miri)]
{
self.map.lock().get(&addr).expect("Provided address is not exposed.")
}

#[cfg(not(miri))]
{
addr as *const T
}
}

#[inline]
pub(crate) fn unexpose_provenance(&self, _ptr: *const T) {
#[cfg(miri)]
{
// SAFETY: Equivalent to `pointer::addr` which is safe.
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
let addr: usize = unsafe { std::mem::transmute(_ptr) };
self.map.lock().remove(addr).expect("Provided address is not exposed.");
}
}
}
Loading