@@ -346,6 +346,7 @@ pub struct StoreOpaque {
346346 /// `rooted_host_funcs` below. This structure contains pointers which are
347347 /// otherwise kept alive by the `Arc` references in `rooted_host_funcs`.
348348 store_data : ManuallyDrop < StoreData > ,
349+ traitobj : StorePtr ,
349350 default_caller : InstanceHandle ,
350351
351352 /// Used to optimized wasm->host calls when the host function is defined with
@@ -517,67 +518,79 @@ impl<T> Store<T> {
517518 /// tables created to 10,000. This can be overridden with the
518519 /// [`Store::limiter`] configuration method.
519520 pub fn new ( engine : & Engine , data : T ) -> Self {
521+ let store_data = StoreData :: new ( ) ;
522+ log:: trace!( "creating new store {:?}" , store_data. id( ) ) ;
523+
520524 let pkey = engine. allocator ( ) . next_available_pkey ( ) ;
521525
522- let mut inner = Box :: new ( StoreInner {
523- inner : StoreOpaque {
524- _marker : marker:: PhantomPinned ,
525- engine : engine. clone ( ) ,
526- vm_store_context : Default :: default ( ) ,
527- instances : Vec :: new ( ) ,
528- #[ cfg( feature = "component-model" ) ]
529- num_component_instances : 0 ,
530- signal_handler : None ,
531- gc_store : None ,
532- gc_roots : RootSet :: default ( ) ,
533- #[ cfg( feature = "gc" ) ]
534- gc_roots_list : GcRootsList :: default ( ) ,
535- #[ cfg( feature = "gc" ) ]
536- gc_host_alloc_types : Default :: default ( ) ,
537- modules : ModuleRegistry :: default ( ) ,
538- func_refs : FuncRefs :: default ( ) ,
539- host_globals : Vec :: new ( ) ,
540- instance_count : 0 ,
541- instance_limit : crate :: DEFAULT_INSTANCE_LIMIT ,
542- memory_count : 0 ,
543- memory_limit : crate :: DEFAULT_MEMORY_LIMIT ,
544- table_count : 0 ,
545- table_limit : crate :: DEFAULT_TABLE_LIMIT ,
546- #[ cfg( feature = "async" ) ]
547- async_state : AsyncState :: default ( ) ,
548- fuel_reserve : 0 ,
549- fuel_yield_interval : None ,
550- store_data : ManuallyDrop :: new ( StoreData :: new ( ) ) ,
551- default_caller : InstanceHandle :: null ( ) ,
552- hostcall_val_storage : Vec :: new ( ) ,
553- wasm_val_raw_storage : Vec :: new ( ) ,
554- rooted_host_funcs : ManuallyDrop :: new ( Vec :: new ( ) ) ,
555- pkey,
556- #[ cfg( feature = "component-model" ) ]
557- component_host_table : Default :: default ( ) ,
558- #[ cfg( feature = "component-model" ) ]
559- component_calls : Default :: default ( ) ,
560- #[ cfg( feature = "component-model" ) ]
561- host_resource_data : Default :: default ( ) ,
562- #[ cfg( has_host_compiler_backend) ]
563- executor : if cfg ! ( feature = "pulley" ) && engine. target ( ) . is_pulley ( ) {
564- Executor :: Interpreter ( Interpreter :: new ( engine) )
565- } else {
566- Executor :: Native
567- } ,
568- #[ cfg( not( has_host_compiler_backend) ) ]
569- executor : {
570- debug_assert ! ( engine. target( ) . is_pulley( ) ) ;
571- Executor :: Interpreter ( Interpreter :: new ( engine) )
572- } ,
526+ let inner = StoreOpaque {
527+ _marker : marker:: PhantomPinned ,
528+ engine : engine. clone ( ) ,
529+ vm_store_context : Default :: default ( ) ,
530+ instances : Vec :: new ( ) ,
531+ #[ cfg( feature = "component-model" ) ]
532+ num_component_instances : 0 ,
533+ signal_handler : None ,
534+ gc_store : None ,
535+ gc_roots : RootSet :: default ( ) ,
536+ #[ cfg( feature = "gc" ) ]
537+ gc_roots_list : GcRootsList :: default ( ) ,
538+ #[ cfg( feature = "gc" ) ]
539+ gc_host_alloc_types : Default :: default ( ) ,
540+ modules : ModuleRegistry :: default ( ) ,
541+ func_refs : FuncRefs :: default ( ) ,
542+ host_globals : Vec :: new ( ) ,
543+ instance_count : 0 ,
544+ instance_limit : crate :: DEFAULT_INSTANCE_LIMIT ,
545+ memory_count : 0 ,
546+ memory_limit : crate :: DEFAULT_MEMORY_LIMIT ,
547+ table_count : 0 ,
548+ table_limit : crate :: DEFAULT_TABLE_LIMIT ,
549+ #[ cfg( feature = "async" ) ]
550+ async_state : AsyncState :: default ( ) ,
551+ fuel_reserve : 0 ,
552+ fuel_yield_interval : None ,
553+ store_data : ManuallyDrop :: new ( store_data) ,
554+ traitobj : StorePtr :: empty ( ) ,
555+ default_caller : InstanceHandle :: null ( ) ,
556+ hostcall_val_storage : Vec :: new ( ) ,
557+ wasm_val_raw_storage : Vec :: new ( ) ,
558+ rooted_host_funcs : ManuallyDrop :: new ( Vec :: new ( ) ) ,
559+ pkey,
560+ #[ cfg( feature = "component-model" ) ]
561+ component_host_table : Default :: default ( ) ,
562+ #[ cfg( feature = "component-model" ) ]
563+ component_calls : Default :: default ( ) ,
564+ #[ cfg( feature = "component-model" ) ]
565+ host_resource_data : Default :: default ( ) ,
566+ #[ cfg( has_host_compiler_backend) ]
567+ executor : if cfg ! ( feature = "pulley" ) && engine. target ( ) . is_pulley ( ) {
568+ Executor :: Interpreter ( Interpreter :: new ( engine) )
569+ } else {
570+ Executor :: Native
573571 } ,
572+ #[ cfg( not( has_host_compiler_backend) ) ]
573+ executor : {
574+ debug_assert ! ( engine. target( ) . is_pulley( ) ) ;
575+ Executor :: Interpreter ( Interpreter :: new ( engine) )
576+ } ,
577+ } ;
578+ let mut inner = Box :: new ( StoreInner {
579+ inner,
574580 limiter : None ,
575581 call_hook : None ,
576582 #[ cfg( target_has_atomic = "64" ) ]
577583 epoch_deadline_behavior : None ,
578584 data : ManuallyDrop :: new ( data) ,
579585 } ) ;
580586
587+ inner. traitobj = StorePtr :: new ( unsafe {
588+ mem:: transmute :: <
589+ NonNull < dyn crate :: runtime:: vm:: VMStore + ' _ > ,
590+ NonNull < dyn crate :: runtime:: vm:: VMStore + ' static > ,
591+ > ( NonNull :: from ( & mut * inner) )
592+ } ) ;
593+
581594 // Wasmtime uses the callee argument to host functions to learn about
582595 // the original pointer to the `Store` itself, allowing it to
583596 // reconstruct a `StoreContextMut<T>`. When we initially call a `Func`,
@@ -589,9 +602,11 @@ impl<T> Store<T> {
589602 let module = Arc :: new ( wasmtime_environ:: Module :: default ( ) ) ;
590603 let shim = ModuleRuntimeInfo :: bare ( module) ;
591604 let allocator = OnDemandInstanceAllocator :: default ( ) ;
605+
592606 allocator
593607 . validate_module ( shim. env_module ( ) , shim. offsets ( ) )
594608 . unwrap ( ) ;
609+
595610 let mut instance = unsafe {
596611 allocator
597612 . allocate_module ( InstanceAllocationRequest {
@@ -605,19 +620,10 @@ impl<T> Store<T> {
605620 } )
606621 . expect ( "failed to allocate default callee" )
607622 } ;
608-
609- // Note the erasure of the lifetime here into `'static`, so in
610- // general usage of this trait object must be strictly bounded to
611- // the `Store` itself, and this is an invariant that we have to
612- // maintain throughout Wasmtime.
613623 unsafe {
614- let traitobj = mem:: transmute :: <
615- NonNull < dyn crate :: runtime:: vm:: VMStore + ' _ > ,
616- NonNull < dyn crate :: runtime:: vm:: VMStore + ' static > ,
617- > ( NonNull :: from ( & mut * inner) ) ;
618- instance. set_store ( traitobj) ;
619- instance
624+ instance. set_store ( Some ( inner. traitobj ( ) ) ) ;
620625 }
626+ instance
621627 } ;
622628
623629 Self {
@@ -1705,7 +1711,12 @@ impl StoreOpaque {
17051711
17061712 #[ inline]
17071713 pub fn traitobj ( & self ) -> NonNull < dyn crate :: runtime:: vm:: VMStore > {
1708- self . default_caller . traitobj ( self )
1714+ self . traitobj . as_raw ( ) . unwrap ( )
1715+ }
1716+
1717+ #[ inline]
1718+ pub fn traitobj_mut ( & mut self ) -> & mut dyn crate :: runtime:: vm:: VMStore {
1719+ unsafe { self . traitobj ( ) . as_mut ( ) }
17091720 }
17101721
17111722 /// Takes the cached `Vec<Val>` stored internally across hostcalls to get
0 commit comments