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

Use a drop guard to prevent leaving a dangling reference in internals::serialize::id::run_as_context #203

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 19 additions & 13 deletions src/internals/serialize/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,40 @@ thread_local! {

/// Runs the provided function with this canon as context for Entity (de)serialization.
pub fn run_as_context<F: FnOnce() -> R, R>(context: &dyn EntitySerializer, f: F) -> R {
struct SerializerGuard<'a> {
cell: &'a RefCell<Option<&'static dyn EntitySerializer>>,
prev: Option<&'static dyn EntitySerializer>,
}

impl Drop for SerializerGuard<'_> {
fn drop(&mut self) {
// swap context back out of TLS, putting back what we took out.
let mut existing = self.cell.borrow_mut();
*existing = self.prev;
}
}

SERIALIZER.with(|cell| {
// swap context into TLS
let prev = {
let _serializer_guard = {
let mut existing = cell.borrow_mut();
// Safety? Hmm
// This &'static reference is only stored in the TLS for the duration of this function,
// meaning it will be valid for all TLS uses as long as it's not copied anywhere else.
// Can't express this lifetime in a TLS-static, so that's why this unsafe block needs to exist.
//
// If `f` panics, we could end up with a dangling reference in the TLS block.
// A catch_unwind block could fix this problem
// If `f` unwinds, the SerializerGuard destructor will restore the previous value of
// SERIALIZER, so there's no risk of leaving a dangling reference.
let hacked_context = unsafe {
core::mem::transmute::<&dyn EntitySerializer, &'static dyn EntitySerializer>(
context,
)
};
std::mem::replace(&mut *existing, Some(hacked_context))
let prev = std::mem::replace(&mut *existing, Some(hacked_context));
SerializerGuard { cell, prev }
};

let result = (f)();

// swap context back out of LTS, putting back what we took out.
{
let mut existing = cell.borrow_mut();
*existing = prev;
}

result
(f)()
})
}

Expand Down
15 changes: 15 additions & 0 deletions src/internals/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,19 @@ mod test {

assert_eq!(8, world.len());
}

#[test]
fn run_as_context_panic() {
std::panic::catch_unwind(|| {
let registry = Registry::<i32>::default();

super::WorldSerializer::with_entity_serializer(&registry, &mut |canon| {
super::id::run_as_context(canon, || panic!());
});
})
.unwrap_err();

// run the serialize_bincode test again
serialize_bincode();
}
}