Skip to content

Commit

Permalink
Fix assert_called_from_owner_thread debug assertion on SGX platform
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulstrackx committed Nov 15, 2023
1 parent bd3ea0d commit f65f37c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions tokio/src/runtime/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ pub(super) fn budget<R>(f: impl FnOnce(&Cell<coop::Budget>) -> R) -> Result<R, A
cfg_rt! {
use crate::runtime::ThreadId;

#[cfg(target_env = "sgx")]
pub(crate) fn has_thread_id() -> bool {
CONTEXT.try_with(|ctx| {
ctx.thread_id.get().is_some()
}).unwrap_or(false)
}

pub(crate) fn thread_id() -> Result<ThreadId, AccessError> {
CONTEXT.try_with(|ctx| {
match ctx.thread_id.get() {
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/task/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,14 @@ impl LocalState {

#[track_caller]
fn assert_called_from_owner_thread(&self) {
// Even when the thread is being destroyed, local data may still be initialized on the SGX
// platform. So calling `context::thread_id()` will always return a thread id. In line with
// the `debug_assert` below, we ignore such cases on the SGX platform.
#[cfg(target_env = "sgx")]
if !context::has_thread_id() {
return;
}

// FreeBSD has some weirdness around thread-local destruction.
// TODO: remove this hack when thread id is cleaned up
#[cfg(not(any(target_os = "openbsd", target_os = "freebsd")))]
Expand Down
2 changes: 0 additions & 2 deletions tokio/tests/task_local_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ fn join_local_future_elsewhere() {
}

// Tests for <https://github.com/tokio-rs/tokio/issues/4973>
#[cfg(not(target_env = "sgx"))] // Fails on SGX!
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads
#[tokio::test(flavor = "multi_thread")]
async fn localset_in_thread_local() {
Expand Down Expand Up @@ -575,7 +574,6 @@ async fn spawn_wakes_localset() {
}

#[test]
#[cfg(not(target_env = "sgx"))] // Fails on SGX!
fn store_local_set_in_thread_local_with_runtime() {
use tokio::runtime::Runtime;

Expand Down

0 comments on commit f65f37c

Please sign in to comment.