Skip to content

Commit

Permalink
fix: don't perform invalid reference cast (#554)
Browse files Browse the repository at this point in the history
Replaces it with a raw pointer. Avoids possible Undefined Behavior.

Stripped-down example of how the current code fails in Miri:
[playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4b7ad1c81275f9213a60443a7edafc4f)
Equivalent using the implementation from this PR:
[playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8e64a9b45aa78b8823f5453b9c54d3a5)
  • Loading branch information
wackbyte authored Jan 31, 2024
1 parent 251fcc0 commit 8e449e8
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions crates/mun_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,6 @@ impl<'name, T: InvokeArgs> InvokeErr<'name, T> {
Output: 'o + ReturnTypeReflection + Marshal<'o>,
'r: 'o,
{
// Safety: The output of `retry_impl` is guaranteed to only contain a shared
// reference.
let runtime = &*runtime;

loop {
self = match unsafe { self.retry_impl(runtime) } {
Ok(output) => return output,
Expand All @@ -687,15 +683,14 @@ impl<'name, T: InvokeArgs> InvokeErr<'name, T> {
///
/// # Safety
///
/// When calling this function, you have to guarantee that `runtime` is mutably
/// borrowed. The `Output` value can only contain a shared borrow of `runtime`.
unsafe fn retry_impl<'r, 'o, Output>(self, runtime: &'r Runtime) -> Result<Output, Self>
/// When calling this function, you have to guarantee that `runtime` can be dereferenced and is
/// valid for `'o`. The `Output` value can only contain a shared borrow of `runtime`.
unsafe fn retry_impl<'o, Output>(self, runtime: *mut Runtime) -> Result<Output, Self>
where
Output: 'o + ReturnTypeReflection + Marshal<'o>,
'r: 'o,
{
#[allow(invalid_reference_casting, invalid_reference_casting)]
let runtime = &mut *(runtime as *const Runtime as *mut Runtime);
// Safety: Guaranteed by the caller to be valid to dereference.
let runtime = &mut *runtime;

eprintln!("{}", self.msg);
while !runtime.update() {
Expand Down

0 comments on commit 8e449e8

Please sign in to comment.