1+ #![ deny( unsafe_op_in_unsafe_fn) ]
2+
13//! Runtime support for the Component Model Async ABI.
24//!
35//! This module and its submodules provide host runtime support for Component
@@ -2464,17 +2466,19 @@ impl Instance {
24642466 } ;
24652467
24662468 // Queue the call as a "high priority" work item.
2467- self . queue_call (
2468- store. as_context_mut ( ) ,
2469- guest_task,
2470- callee,
2471- param_count,
2472- result_count,
2473- instance_flags,
2474- ( flags & START_FLAG_ASYNC_CALLEE ) != 0 ,
2475- NonNull :: new ( callback) . map ( SendSyncPtr :: new) ,
2476- NonNull :: new ( post_return) . map ( SendSyncPtr :: new) ,
2477- ) ?;
2469+ unsafe {
2470+ self . queue_call (
2471+ store. as_context_mut ( ) ,
2472+ guest_task,
2473+ callee,
2474+ param_count,
2475+ result_count,
2476+ instance_flags,
2477+ ( flags & START_FLAG_ASYNC_CALLEE ) != 0 ,
2478+ NonNull :: new ( callback) . map ( SendSyncPtr :: new) ,
2479+ NonNull :: new ( post_return) . map ( SendSyncPtr :: new) ,
2480+ ) ?;
2481+ }
24782482
24792483 let state = self . concurrent_state_mut ( store. 0 ) ;
24802484
@@ -3490,30 +3494,32 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
34903494 // pointer containing at least `storage_len` items.
34913495 let params = unsafe { std:: slice:: from_raw_parts ( storage, storage_len) } . to_vec ( ) ;
34923496
3493- instance. prepare_call (
3494- StoreContextMut ( self ) ,
3495- start,
3496- return_,
3497- caller_instance,
3498- callee_instance,
3499- task_return_type,
3500- memory,
3501- string_encoding,
3502- match result_count_or_max_if_async {
3503- PREPARE_ASYNC_NO_RESULT => CallerInfo :: Async {
3504- params,
3505- has_result : false ,
3506- } ,
3507- PREPARE_ASYNC_WITH_RESULT => CallerInfo :: Async {
3508- params,
3509- has_result : true ,
3510- } ,
3511- result_count => CallerInfo :: Sync {
3512- params,
3513- result_count,
3497+ unsafe {
3498+ instance. prepare_call (
3499+ StoreContextMut ( self ) ,
3500+ start,
3501+ return_,
3502+ caller_instance,
3503+ callee_instance,
3504+ task_return_type,
3505+ memory,
3506+ string_encoding,
3507+ match result_count_or_max_if_async {
3508+ PREPARE_ASYNC_NO_RESULT => CallerInfo :: Async {
3509+ params,
3510+ has_result : false ,
3511+ } ,
3512+ PREPARE_ASYNC_WITH_RESULT => CallerInfo :: Async {
3513+ params,
3514+ has_result : true ,
3515+ } ,
3516+ result_count => CallerInfo :: Sync {
3517+ params,
3518+ result_count,
3519+ } ,
35143520 } ,
3515- } ,
3516- )
3521+ )
3522+ }
35173523 }
35183524
35193525 unsafe fn sync_start (
@@ -3525,21 +3531,23 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
35253531 storage : * mut MaybeUninit < ValRaw > ,
35263532 storage_len : usize ,
35273533 ) -> Result < ( ) > {
3528- instance
3529- . start_call (
3530- StoreContextMut ( self ) ,
3531- callback,
3532- ptr:: null_mut ( ) ,
3533- callee,
3534- param_count,
3535- 1 ,
3536- START_FLAG_ASYNC_CALLEE ,
3537- // SAFETY: The `wasmtime_cranelift`-generated code that calls
3538- // this method will have ensured that `storage` is a valid
3539- // pointer containing at least `storage_len` items.
3540- Some ( unsafe { std:: slice:: from_raw_parts_mut ( storage, storage_len) } ) ,
3541- )
3542- . map ( drop)
3534+ unsafe {
3535+ instance
3536+ . start_call (
3537+ StoreContextMut ( self ) ,
3538+ callback,
3539+ ptr:: null_mut ( ) ,
3540+ callee,
3541+ param_count,
3542+ 1 ,
3543+ START_FLAG_ASYNC_CALLEE ,
3544+ // SAFETY: The `wasmtime_cranelift`-generated code that calls
3545+ // this method will have ensured that `storage` is a valid
3546+ // pointer containing at least `storage_len` items.
3547+ Some ( std:: slice:: from_raw_parts_mut ( storage, storage_len) ) ,
3548+ )
3549+ . map ( drop)
3550+ }
35433551 }
35443552
35453553 unsafe fn async_start (
@@ -3552,16 +3560,18 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
35523560 result_count : u32 ,
35533561 flags : u32 ,
35543562 ) -> Result < u32 > {
3555- instance. start_call (
3556- StoreContextMut ( self ) ,
3557- callback,
3558- post_return,
3559- callee,
3560- param_count,
3561- result_count,
3562- flags,
3563- None ,
3564- )
3563+ unsafe {
3564+ instance. start_call (
3565+ StoreContextMut ( self ) ,
3566+ callback,
3567+ post_return,
3568+ callee,
3569+ param_count,
3570+ result_count,
3571+ flags,
3572+ None ,
3573+ )
3574+ }
35653575 }
35663576
35673577 unsafe fn future_write (
@@ -4403,11 +4413,13 @@ impl AsyncCx {
44034413 ///
44044414 /// SAFETY: TODO
44054415 unsafe fn poll < U > ( & self , mut future : Pin < & mut ( dyn Future < Output = U > + Send ) > ) -> Poll < U > {
4406- let poll_cx = * self . current_poll_cx ;
4407- let _reset = Reset ( self . current_poll_cx , poll_cx) ;
4408- * self . current_poll_cx = PollContext :: default ( ) ;
4409- assert ! ( !poll_cx. future_context. is_null( ) ) ;
4410- future. as_mut ( ) . poll ( & mut * poll_cx. future_context )
4416+ unsafe {
4417+ let poll_cx = * self . current_poll_cx ;
4418+ let _reset = Reset ( self . current_poll_cx , poll_cx) ;
4419+ * self . current_poll_cx = PollContext :: default ( ) ;
4420+ assert ! ( !poll_cx. future_context. is_null( ) ) ;
4421+ future. as_mut ( ) . poll ( & mut * poll_cx. future_context )
4422+ }
44114423 }
44124424
44134425 /// "Stackfully" poll the specified future by alternately polling it and
@@ -4419,10 +4431,12 @@ impl AsyncCx {
44194431 mut future : Pin < & mut ( dyn Future < Output = U > + Send ) > ,
44204432 ) -> Result < U > {
44214433 loop {
4422- match self . poll ( future. as_mut ( ) ) {
4423- Poll :: Ready ( v) => break Ok ( v) ,
4424- Poll :: Pending => {
4425- self . suspend ( None ) ?;
4434+ unsafe {
4435+ match self . poll ( future. as_mut ( ) ) {
4436+ Poll :: Ready ( v) => break Ok ( v) ,
4437+ Poll :: Pending => {
4438+ self . suspend ( None ) ?;
4439+ }
44264440 }
44274441 }
44284442 }
@@ -4440,7 +4454,7 @@ impl AsyncCx {
44404454 } else {
44414455 ProtectionMask :: all ( )
44424456 } ;
4443- let store = suspend_fiber ( self . current_suspend , self . current_stack_limit , store) ;
4457+ let store = unsafe { suspend_fiber ( self . current_suspend , self . current_stack_limit , store) } ;
44444458 if self . track_pkey_context_switch {
44454459 mpk:: allow ( previous_mask) ;
44464460 }
@@ -4886,17 +4900,19 @@ unsafe fn resume_fiber_raw<'a>(
48864900 }
48874901 }
48884902
4889- let _reset_executor = Reset ( ( * fiber) . executor_ptr , * ( * fiber) . executor_ptr ) ;
4890- * ( * fiber) . executor_ptr = & raw mut ( * fiber) . executor ;
4891- let _reset_suspend = Reset ( ( * fiber) . suspend , * ( * fiber) . suspend ) ;
4892- let _reset_stack_limit = Reset ( ( * fiber) . stack_limit , * ( * fiber) . stack_limit ) ;
4893- let state = Some ( ( * fiber) . state . take ( ) . unwrap ( ) . push ( ) ) ;
4894- let restore = Restore { fiber, state } ;
4895- ( * restore. fiber )
4896- . fiber
4897- . as_ref ( )
4898- . unwrap ( )
4899- . resume ( ( store, result) )
4903+ unsafe {
4904+ let _reset_executor = Reset ( ( * fiber) . executor_ptr , * ( * fiber) . executor_ptr ) ;
4905+ * ( * fiber) . executor_ptr = & raw mut ( * fiber) . executor ;
4906+ let _reset_suspend = Reset ( ( * fiber) . suspend , * ( * fiber) . suspend ) ;
4907+ let _reset_stack_limit = Reset ( ( * fiber) . stack_limit , * ( * fiber) . stack_limit ) ;
4908+ let state = Some ( ( * fiber) . state . take ( ) . unwrap ( ) . push ( ) ) ;
4909+ let restore = Restore { fiber, state } ;
4910+ ( * restore. fiber )
4911+ . fiber
4912+ . as_ref ( )
4913+ . unwrap ( )
4914+ . resume ( ( store, result) )
4915+ }
49004916}
49014917
49024918/// See `resume_fiber_raw`
@@ -4905,7 +4921,9 @@ unsafe fn resume_fiber(
49054921 store : Option < * mut dyn VMStore > ,
49064922 result : Result < ( ) > ,
49074923) -> Result < Result < ( * mut dyn VMStore , Result < ( ) > ) , Option < * mut dyn VMStore > > > {
4908- match resume_fiber_raw ( fiber, store, result) . map ( |( store, result) | ( store. unwrap ( ) , result) ) {
4924+ match unsafe {
4925+ resume_fiber_raw ( fiber, store, result) . map ( |( store, result) | ( store. unwrap ( ) , result) )
4926+ } {
49094927 Ok ( pair) => Ok ( Ok ( pair) ) ,
49104928 Err ( s) => {
49114929 if let Some ( range) = fiber. fiber . as_ref ( ) . unwrap ( ) . stack ( ) . range ( ) {
@@ -4932,10 +4950,12 @@ unsafe fn suspend_fiber(
49324950 stack_limit : * mut usize ,
49334951 store : Option < * mut dyn VMStore > ,
49344952) -> Result < Option < * mut dyn VMStore > > {
4935- let _reset_suspend = Reset ( suspend, * suspend) ;
4936- let _reset_stack_limit = Reset ( stack_limit, * stack_limit) ;
4937- assert ! ( !( * suspend) . is_null( ) ) ;
4938- let ( store, result) = ( * * suspend) . suspend ( store) ;
4953+ let ( store, result) = unsafe {
4954+ let _reset_suspend = Reset ( suspend, * suspend) ;
4955+ let _reset_stack_limit = Reset ( stack_limit, * stack_limit) ;
4956+ assert ! ( !( * suspend) . is_null( ) ) ;
4957+ ( * * suspend) . suspend ( store)
4958+ } ;
49394959 result?;
49404960 Ok ( store)
49414961}
0 commit comments