Skip to content

Commit 0df1722

Browse files
authored
Fix compilation error for Rust versions <1.78 (#1093)
fix compilation error for Rust versions <1.78
1 parent 47add87 commit 0df1722

File tree

1 file changed

+18
-19
lines changed
  • crates/wasmi/src/engine/executor

1 file changed

+18
-19
lines changed

crates/wasmi/src/engine/executor/cache.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -248,31 +248,30 @@ impl CachedMemory {
248248
/// Cached default global variable value.
249249
#[derive(Debug)]
250250
pub struct CachedGlobal {
251-
data: NonNull<UntypedVal>,
251+
// Dev. Note: we cannot use `NonNull<UntypedVal>` here, yet.
252+
//
253+
// The advantage is that we could safely use a static fallback value
254+
// which would be safer than using a null pointer since it would
255+
// only read or overwrite the fallback value instead of reading or
256+
// writing a null pointer which is UB.
257+
//
258+
// We cannot use `NonNull<UntypedVal>` because it requires pointers
259+
// to mutable statics which have just been allowed in Rust 1.78 but
260+
// not in Rust 1.77 which is Wasmi's MSRV.
261+
//
262+
// We can and should use `NonNull<UntypedVal>` here once we bump the MSRV.
263+
data: *mut UntypedVal,
252264
}
253265

254266
impl Default for CachedGlobal {
255267
#[inline]
256268
fn default() -> Self {
257269
Self {
258-
data: unsafe { FALLBACK_GLOBAL_VALUE },
270+
data: ptr::null_mut(),
259271
}
260272
}
261273
}
262274

263-
/// Static fallback value for when an [`Instance`] does not define a global variable.
264-
///
265-
/// # Dev. Note
266-
///
267-
/// If the Wasm inputs are valid and the Wasmi translation and executor work correctly
268-
/// this fallback global value is never read from or written to. Doing so indicates a bug
269-
/// or an invalid Wasm input.
270-
static mut FALLBACK_GLOBAL_VALUE: NonNull<UntypedVal> = {
271-
static mut ZERO_CELL: UntypedVal = UntypedVal::from_bits(0_u64);
272-
273-
unsafe { NonNull::new_unchecked(ptr::addr_of_mut!(ZERO_CELL)) }
274-
};
275-
276275
impl CachedGlobal {
277276
/// Create a new [`CachedGlobal`].
278277
#[inline]
@@ -294,8 +293,8 @@ impl CachedGlobal {
294293
///
295294
/// [`Global`]: crate::Global
296295
#[inline]
297-
fn load_global(ctx: &mut StoreInner, global: &Global) -> NonNull<UntypedVal> {
298-
ctx.resolve_global_mut(global).get_untyped_ptr()
296+
fn load_global(ctx: &mut StoreInner, global: &Global) -> *mut UntypedVal {
297+
ctx.resolve_global_mut(global).get_untyped_ptr().as_ptr()
299298
}
300299

301300
/// Returns the value of the cached global variable.
@@ -307,7 +306,7 @@ impl CachedGlobal {
307306
pub unsafe fn get(&self) -> UntypedVal {
308307
// SAFETY: This API guarantees to always write to a valid pointer
309308
// as long as `update` is called when needed by the user.
310-
unsafe { *self.data.as_ref() }
309+
unsafe { self.data.read() }
311310
}
312311

313312
/// Sets the value of the cached global variable to `new_value`.
@@ -319,6 +318,6 @@ impl CachedGlobal {
319318
pub unsafe fn set(&mut self, new_value: UntypedVal) {
320319
// SAFETY: This API guarantees to always write to a valid pointer
321320
// as long as `update` is called when needed by the user.
322-
*unsafe { self.data.as_mut() } = new_value;
321+
unsafe { self.data.write(new_value) };
323322
}
324323
}

0 commit comments

Comments
 (0)