@@ -248,31 +248,30 @@ impl CachedMemory {
248
248
/// Cached default global variable value.
249
249
#[ derive( Debug ) ]
250
250
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 ,
252
264
}
253
265
254
266
impl Default for CachedGlobal {
255
267
#[ inline]
256
268
fn default ( ) -> Self {
257
269
Self {
258
- data : unsafe { FALLBACK_GLOBAL_VALUE } ,
270
+ data : ptr :: null_mut ( ) ,
259
271
}
260
272
}
261
273
}
262
274
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
-
276
275
impl CachedGlobal {
277
276
/// Create a new [`CachedGlobal`].
278
277
#[ inline]
@@ -294,8 +293,8 @@ impl CachedGlobal {
294
293
///
295
294
/// [`Global`]: crate::Global
296
295
#[ 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 ( )
299
298
}
300
299
301
300
/// Returns the value of the cached global variable.
@@ -307,7 +306,7 @@ impl CachedGlobal {
307
306
pub unsafe fn get ( & self ) -> UntypedVal {
308
307
// SAFETY: This API guarantees to always write to a valid pointer
309
308
// as long as `update` is called when needed by the user.
310
- unsafe { * self . data . as_ref ( ) }
309
+ unsafe { self . data . read ( ) }
311
310
}
312
311
313
312
/// Sets the value of the cached global variable to `new_value`.
@@ -319,6 +318,6 @@ impl CachedGlobal {
319
318
pub unsafe fn set ( & mut self , new_value : UntypedVal ) {
320
319
// SAFETY: This API guarantees to always write to a valid pointer
321
320
// 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 ) } ;
323
322
}
324
323
}
0 commit comments