Skip to content

Commit 8d4e923

Browse files
authored
Resolve unsafe_op_in_unsafe_fn in wasmtime crate (#11432)
Just a few unsafe blocks remain Closes #11180
1 parent 73b3075 commit 8d4e923

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

crates/wasmtime/src/engine.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,9 @@ impl Engine {
845845
memory: NonNull<[u8]>,
846846
expected: ObjectKind,
847847
) -> Result<Arc<crate::CodeMemory>> {
848-
self.load_code(crate::runtime::vm::MmapVec::from_raw(memory)?, expected)
848+
// SAFETY: the contract of this function is the same as that of
849+
// `from_raw`.
850+
unsafe { self.load_code(crate::runtime::vm::MmapVec::from_raw(memory)?, expected) }
849851
}
850852

851853
/// Like `load_code_bytes`, but creates a mmap from a file on disk.
@@ -925,8 +927,11 @@ impl Engine {
925927
assert_eq!(Arc::weak_count(&self.inner), 0);
926928
assert_eq!(Arc::strong_count(&self.inner), 1);
927929

930+
// SAFETY: the contract of this function is the same as `deinit_traps`.
928931
#[cfg(not(miri))]
929-
crate::runtime::vm::deinit_traps();
932+
unsafe {
933+
crate::runtime::vm::deinit_traps();
934+
}
930935
}
931936
}
932937

crates/wasmtime/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@
292292
// and will prevent the doc build from failing.
293293
#![cfg_attr(feature = "default", warn(rustdoc::broken_intra_doc_links))]
294294
#![no_std]
295-
#![expect(unsafe_op_in_unsafe_fn, reason = "crate isn't migrated yet")]
296295

297296
#[cfg(feature = "std")]
298297
#[macro_use]
@@ -352,8 +351,10 @@ macro_rules! map_maybe_uninit {
352351
pub trait MaybeUninitExt<T> {
353352
/// Maps `MaybeUninit<T>` to `MaybeUninit<U>` using the closure provided.
354353
///
355-
/// Note that this is `unsafe` as there is no guarantee that `U` comes from
356-
/// `T`.
354+
/// # Safety
355+
///
356+
/// Requires that `*mut U` is a field projection from `*mut T`. Use
357+
/// `map_maybe_uninit!` above instead.
357358
unsafe fn map<U>(&mut self, f: impl FnOnce(*mut T) -> *mut U)
358359
-> &mut core::mem::MaybeUninit<U>;
359360
}
@@ -364,7 +365,10 @@ impl<T> MaybeUninitExt<T> for core::mem::MaybeUninit<T> {
364365
f: impl FnOnce(*mut T) -> *mut U,
365366
) -> &mut core::mem::MaybeUninit<U> {
366367
let new_ptr = f(self.as_mut_ptr());
367-
core::mem::transmute::<*mut U, &mut core::mem::MaybeUninit<U>>(new_ptr)
368+
// SAFETY: the memory layout of these two types are the same, and
369+
// asserting that it's a safe reference with the same lifetime as `self`
370+
// is a requirement of this function itself.
371+
unsafe { core::mem::transmute::<*mut U, &mut core::mem::MaybeUninit<U>>(new_ptr) }
368372
}
369373
}
370374

crates/wasmtime/src/runtime.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
// explanation of why truncation shouldn't be happening at runtime. This
2626
// situation should be pretty rare though.
2727
#![warn(clippy::cast_possible_truncation)]
28-
#![warn(
29-
unsafe_op_in_unsafe_fn,
30-
reason = "opt-in until the crate opts-in as a whole -- #11180"
31-
)]
3228

3329
#[macro_use]
3430
pub(crate) mod func;

0 commit comments

Comments
 (0)