@@ -13,7 +13,7 @@ use crate::runtime::vm::vmcontext::{
13
13
} ;
14
14
use crate :: runtime:: vm:: {
15
15
ExportFunction , ExportGlobal , ExportMemory , ExportTable , GcStore , Imports , ModuleRuntimeInfo ,
16
- SendSyncPtr , VMFunctionBody , VMGcRef , VMStore , WasmFault ,
16
+ SendSyncPtr , VMFunctionBody , VMGcRef , VMStore , VMStoreRawPtr , WasmFault ,
17
17
} ;
18
18
use crate :: store:: { StoreInner , StoreOpaque } ;
19
19
use crate :: { prelude:: * , StoreContextMut } ;
@@ -162,13 +162,7 @@ impl InstanceAndStore {
162
162
/// store).
163
163
#[ inline]
164
164
fn store_ptr ( & self ) -> * mut dyn VMStore {
165
- let ptr = unsafe {
166
- * self
167
- . instance
168
- . vmctx_plus_offset :: < * mut dyn VMStore > ( self . instance . offsets ( ) . ptr . vmctx_store ( ) )
169
- } ;
170
- debug_assert ! ( !ptr. is_null( ) ) ;
171
- ptr
165
+ self . instance . store . unwrap ( ) . 0 . as_ptr ( )
172
166
}
173
167
}
174
168
@@ -281,6 +275,12 @@ pub struct Instance {
281
275
#[ cfg( feature = "wmemcheck" ) ]
282
276
pub ( crate ) wmemcheck_state : Option < Wmemcheck > ,
283
277
278
+ /// Self-pointer back to `Store<T>` and its functions. Not present for
279
+ /// the brief time that `Store<T>` is itself being created. Also not
280
+ /// present for some niche uses that are disconnected from stores (e.g.
281
+ /// cross-thread stuff used in `InstancePre`)
282
+ store : Option < VMStoreRawPtr > ,
283
+
284
284
/// Additional context used by compiled wasm code. This field is last, and
285
285
/// represents a dynamically-sized array that extends beyond the nominal
286
286
/// end of the struct (similar to a flexible array member).
@@ -341,6 +341,7 @@ impl Instance {
341
341
None
342
342
}
343
343
} ,
344
+ store : None ,
344
345
} ,
345
346
) ;
346
347
@@ -584,19 +585,14 @@ impl Instance {
584
585
unsafe { self . vmctx_plus_offset_mut ( self . offsets ( ) . ptr . vmctx_gc_heap_data ( ) ) }
585
586
}
586
587
587
- pub ( crate ) unsafe fn set_store ( & mut self , store : Option < * mut dyn VMStore > ) {
588
- if let Some ( store) = store {
589
- * self . vmctx_plus_offset_mut ( self . offsets ( ) . ptr . vmctx_store ( ) ) = store;
590
- * self . runtime_limits ( ) = ( * store) . vmruntime_limits ( ) ;
591
- * self . epoch_ptr ( ) = ( * store) . engine ( ) . epoch_counter ( ) ;
592
- self . set_gc_heap ( ( * store) . gc_store_mut ( ) . ok ( ) ) ;
588
+ pub ( crate ) unsafe fn set_store ( & mut self , store : Option < NonNull < dyn VMStore > > ) {
589
+ self . store = store. map ( VMStoreRawPtr ) ;
590
+ if let Some ( mut store) = store {
591
+ let store = store. as_mut ( ) ;
592
+ * self . runtime_limits ( ) = store. vmruntime_limits ( ) ;
593
+ * self . epoch_ptr ( ) = store. engine ( ) . epoch_counter ( ) ;
594
+ self . set_gc_heap ( store. gc_store_mut ( ) . ok ( ) ) ;
593
595
} else {
594
- assert_eq ! (
595
- mem:: size_of:: <* mut dyn VMStore >( ) ,
596
- mem:: size_of:: <[ * mut ( ) ; 2 ] >( )
597
- ) ;
598
- * self . vmctx_plus_offset_mut :: < [ * mut ( ) ; 2 ] > ( self . offsets ( ) . ptr . vmctx_store ( ) ) =
599
- [ ptr:: null_mut ( ) , ptr:: null_mut ( ) ] ;
600
596
* self . runtime_limits ( ) = ptr:: null_mut ( ) ;
601
597
* self . epoch_ptr ( ) = ptr:: null_mut ( ) ;
602
598
self . set_gc_heap ( None ) ;
@@ -1607,28 +1603,22 @@ impl InstanceHandle {
1607
1603
/// This should only be used for initializing a vmctx's store pointer. It
1608
1604
/// should never be used to access the store itself. Use `InstanceAndStore`
1609
1605
/// for that instead.
1610
- pub fn traitobj ( & self , store : & StoreOpaque ) -> * mut dyn VMStore {
1606
+ pub fn traitobj ( & self , store : & StoreOpaque ) -> NonNull < dyn VMStore > {
1611
1607
// By requiring a store argument, we are ensuring that callers aren't
1612
1608
// getting this trait object in order to access the store, since they
1613
1609
// already have access. See `InstanceAndStore` and its documentation for
1614
1610
// details about the store access patterns we want to restrict host code
1615
1611
// to.
1616
1612
let _ = store;
1617
1613
1618
- let ptr = unsafe {
1619
- * self
1620
- . instance ( )
1621
- . vmctx_plus_offset :: < * mut dyn VMStore > ( self . instance ( ) . offsets ( ) . ptr . vmctx_store ( ) )
1622
- } ;
1623
- debug_assert ! ( !ptr. is_null( ) ) ;
1624
- ptr
1614
+ self . instance ( ) . store . unwrap ( ) . 0
1625
1615
}
1626
1616
1627
1617
/// Configure the `*mut dyn Store` internal pointer after-the-fact.
1628
1618
///
1629
1619
/// This is provided for the original `Store` itself to configure the first
1630
1620
/// self-pointer after the original `Box` has been initialized.
1631
- pub unsafe fn set_store ( & mut self , store : * mut dyn VMStore ) {
1621
+ pub unsafe fn set_store ( & mut self , store : NonNull < dyn VMStore > ) {
1632
1622
self . instance_mut ( ) . set_store ( Some ( store) ) ;
1633
1623
}
1634
1624
0 commit comments