@@ -346,6 +346,7 @@ pub struct StoreOpaque {
346
346
/// `rooted_host_funcs` below. This structure contains pointers which are
347
347
/// otherwise kept alive by the `Arc` references in `rooted_host_funcs`.
348
348
store_data : ManuallyDrop < StoreData > ,
349
+ traitobj : StorePtr ,
349
350
default_caller : InstanceHandle ,
350
351
351
352
/// Used to optimized wasm->host calls when the host function is defined with
@@ -517,67 +518,83 @@ impl<T> Store<T> {
517
518
/// tables created to 10,000. This can be overridden with the
518
519
/// [`Store::limiter`] configuration method.
519
520
pub fn new ( engine : & Engine , data : T ) -> Self {
521
+ let store_data = StoreData :: new ( ) ;
522
+ log:: trace!( "creating new store {:?}" , store_data. id( ) ) ;
523
+
520
524
let pkey = engine. allocator ( ) . next_available_pkey ( ) ;
521
525
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
573
571
} ,
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,
574
580
limiter : None ,
575
581
call_hook : None ,
576
582
#[ cfg( target_has_atomic = "64" ) ]
577
583
epoch_deadline_behavior : None ,
578
584
data : ManuallyDrop :: new ( data) ,
579
585
} ) ;
580
586
587
+ // Note the erasure of the lifetime here into `'static`, so in general
588
+ // usage of this trait object must be strictly bounded to the `Store`
589
+ // itself, and this is an invariant that we have to maintain throughout
590
+ // Wasmtime.
591
+ inner. traitobj = StorePtr :: new ( unsafe {
592
+ mem:: transmute :: <
593
+ NonNull < dyn crate :: runtime:: vm:: VMStore + ' _ > ,
594
+ NonNull < dyn crate :: runtime:: vm:: VMStore + ' static > ,
595
+ > ( NonNull :: from ( & mut * inner) )
596
+ } ) ;
597
+
581
598
// Wasmtime uses the callee argument to host functions to learn about
582
599
// the original pointer to the `Store` itself, allowing it to
583
600
// reconstruct a `StoreContextMut<T>`. When we initially call a `Func`,
@@ -589,9 +606,11 @@ impl<T> Store<T> {
589
606
let module = Arc :: new ( wasmtime_environ:: Module :: default ( ) ) ;
590
607
let shim = ModuleRuntimeInfo :: bare ( module) ;
591
608
let allocator = OnDemandInstanceAllocator :: default ( ) ;
609
+
592
610
allocator
593
611
. validate_module ( shim. env_module ( ) , shim. offsets ( ) )
594
612
. unwrap ( ) ;
613
+
595
614
let mut instance = unsafe {
596
615
allocator
597
616
. allocate_module ( InstanceAllocationRequest {
@@ -605,19 +624,10 @@ impl<T> Store<T> {
605
624
} )
606
625
. expect ( "failed to allocate default callee" )
607
626
} ;
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.
613
627
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
628
+ instance. set_store ( Some ( inner. traitobj ( ) ) ) ;
620
629
}
630
+ instance
621
631
} ;
622
632
623
633
Self {
@@ -1705,7 +1715,12 @@ impl StoreOpaque {
1705
1715
1706
1716
#[ inline]
1707
1717
pub fn traitobj ( & self ) -> NonNull < dyn crate :: runtime:: vm:: VMStore > {
1708
- self . default_caller . traitobj ( self )
1718
+ self . traitobj . as_raw ( ) . unwrap ( )
1719
+ }
1720
+
1721
+ #[ inline]
1722
+ pub fn traitobj_mut ( & mut self ) -> & mut dyn crate :: runtime:: vm:: VMStore {
1723
+ unsafe { self . traitobj ( ) . as_mut ( ) }
1709
1724
}
1710
1725
1711
1726
/// Takes the cached `Vec<Val>` stored internally across hostcalls to get
0 commit comments