Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 336c85c

Browse files
authored
Merge pull request #222 from bytecodealliance/dicej/deny-unsafe-op-in-unsafe-fn-concurrent
add `deny(unsafe_op_in_unsafe_fn)` to `concurrent` mod
2 parents 2f89beb + 3e70385 commit 336c85c

File tree

1 file changed

+105
-85
lines changed

1 file changed

+105
-85
lines changed

crates/wasmtime/src/runtime/component/concurrent.rs

Lines changed: 105 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
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
@@ -2481,17 +2483,19 @@ impl Instance {
24812483
};
24822484

24832485
// Queue the call as a "high priority" work item.
2484-
self.queue_call(
2485-
store.as_context_mut(),
2486-
guest_task,
2487-
callee,
2488-
param_count,
2489-
result_count,
2490-
instance_flags,
2491-
(flags & START_FLAG_ASYNC_CALLEE) != 0,
2492-
NonNull::new(callback).map(SendSyncPtr::new),
2493-
NonNull::new(post_return).map(SendSyncPtr::new),
2494-
)?;
2486+
unsafe {
2487+
self.queue_call(
2488+
store.as_context_mut(),
2489+
guest_task,
2490+
callee,
2491+
param_count,
2492+
result_count,
2493+
instance_flags,
2494+
(flags & START_FLAG_ASYNC_CALLEE) != 0,
2495+
NonNull::new(callback).map(SendSyncPtr::new),
2496+
NonNull::new(post_return).map(SendSyncPtr::new),
2497+
)?;
2498+
}
24952499

24962500
let state = self.concurrent_state_mut(store.0);
24972501

@@ -3507,30 +3511,32 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
35073511
// pointer containing at least `storage_len` items.
35083512
let params = unsafe { std::slice::from_raw_parts(storage, storage_len) }.to_vec();
35093513

3510-
instance.prepare_call(
3511-
StoreContextMut(self),
3512-
start,
3513-
return_,
3514-
caller_instance,
3515-
callee_instance,
3516-
task_return_type,
3517-
memory,
3518-
string_encoding,
3519-
match result_count_or_max_if_async {
3520-
PREPARE_ASYNC_NO_RESULT => CallerInfo::Async {
3521-
params,
3522-
has_result: false,
3523-
},
3524-
PREPARE_ASYNC_WITH_RESULT => CallerInfo::Async {
3525-
params,
3526-
has_result: true,
3527-
},
3528-
result_count => CallerInfo::Sync {
3529-
params,
3530-
result_count,
3514+
unsafe {
3515+
instance.prepare_call(
3516+
StoreContextMut(self),
3517+
start,
3518+
return_,
3519+
caller_instance,
3520+
callee_instance,
3521+
task_return_type,
3522+
memory,
3523+
string_encoding,
3524+
match result_count_or_max_if_async {
3525+
PREPARE_ASYNC_NO_RESULT => CallerInfo::Async {
3526+
params,
3527+
has_result: false,
3528+
},
3529+
PREPARE_ASYNC_WITH_RESULT => CallerInfo::Async {
3530+
params,
3531+
has_result: true,
3532+
},
3533+
result_count => CallerInfo::Sync {
3534+
params,
3535+
result_count,
3536+
},
35313537
},
3532-
},
3533-
)
3538+
)
3539+
}
35343540
}
35353541

35363542
unsafe fn sync_start(
@@ -3542,21 +3548,23 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
35423548
storage: *mut MaybeUninit<ValRaw>,
35433549
storage_len: usize,
35443550
) -> Result<()> {
3545-
instance
3546-
.start_call(
3547-
StoreContextMut(self),
3548-
callback,
3549-
ptr::null_mut(),
3550-
callee,
3551-
param_count,
3552-
1,
3553-
START_FLAG_ASYNC_CALLEE,
3554-
// SAFETY: The `wasmtime_cranelift`-generated code that calls
3555-
// this method will have ensured that `storage` is a valid
3556-
// pointer containing at least `storage_len` items.
3557-
Some(unsafe { std::slice::from_raw_parts_mut(storage, storage_len) }),
3558-
)
3559-
.map(drop)
3551+
unsafe {
3552+
instance
3553+
.start_call(
3554+
StoreContextMut(self),
3555+
callback,
3556+
ptr::null_mut(),
3557+
callee,
3558+
param_count,
3559+
1,
3560+
START_FLAG_ASYNC_CALLEE,
3561+
// SAFETY: The `wasmtime_cranelift`-generated code that calls
3562+
// this method will have ensured that `storage` is a valid
3563+
// pointer containing at least `storage_len` items.
3564+
Some(std::slice::from_raw_parts_mut(storage, storage_len)),
3565+
)
3566+
.map(drop)
3567+
}
35603568
}
35613569

35623570
unsafe fn async_start(
@@ -3569,16 +3577,18 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
35693577
result_count: u32,
35703578
flags: u32,
35713579
) -> Result<u32> {
3572-
instance.start_call(
3573-
StoreContextMut(self),
3574-
callback,
3575-
post_return,
3576-
callee,
3577-
param_count,
3578-
result_count,
3579-
flags,
3580-
None,
3581-
)
3580+
unsafe {
3581+
instance.start_call(
3582+
StoreContextMut(self),
3583+
callback,
3584+
post_return,
3585+
callee,
3586+
param_count,
3587+
result_count,
3588+
flags,
3589+
None,
3590+
)
3591+
}
35823592
}
35833593

35843594
unsafe fn future_write(
@@ -4420,11 +4430,13 @@ impl AsyncCx {
44204430
///
44214431
/// SAFETY: TODO
44224432
unsafe fn poll<U>(&self, mut future: Pin<&mut (dyn Future<Output = U> + Send)>) -> Poll<U> {
4423-
let poll_cx = *self.current_poll_cx;
4424-
let _reset = Reset(self.current_poll_cx, poll_cx);
4425-
*self.current_poll_cx = PollContext::default();
4426-
assert!(!poll_cx.future_context.is_null());
4427-
future.as_mut().poll(&mut *poll_cx.future_context)
4433+
unsafe {
4434+
let poll_cx = *self.current_poll_cx;
4435+
let _reset = Reset(self.current_poll_cx, poll_cx);
4436+
*self.current_poll_cx = PollContext::default();
4437+
assert!(!poll_cx.future_context.is_null());
4438+
future.as_mut().poll(&mut *poll_cx.future_context)
4439+
}
44284440
}
44294441

44304442
/// "Stackfully" poll the specified future by alternately polling it and
@@ -4436,10 +4448,12 @@ impl AsyncCx {
44364448
mut future: Pin<&mut (dyn Future<Output = U> + Send)>,
44374449
) -> Result<U> {
44384450
loop {
4439-
match self.poll(future.as_mut()) {
4440-
Poll::Ready(v) => break Ok(v),
4441-
Poll::Pending => {
4442-
self.suspend(None)?;
4451+
unsafe {
4452+
match self.poll(future.as_mut()) {
4453+
Poll::Ready(v) => break Ok(v),
4454+
Poll::Pending => {
4455+
self.suspend(None)?;
4456+
}
44434457
}
44444458
}
44454459
}
@@ -4457,7 +4471,7 @@ impl AsyncCx {
44574471
} else {
44584472
ProtectionMask::all()
44594473
};
4460-
let store = suspend_fiber(self.current_suspend, self.current_stack_limit, store);
4474+
let store = unsafe { suspend_fiber(self.current_suspend, self.current_stack_limit, store) };
44614475
if self.track_pkey_context_switch {
44624476
mpk::allow(previous_mask);
44634477
}
@@ -4903,17 +4917,19 @@ unsafe fn resume_fiber_raw<'a>(
49034917
}
49044918
}
49054919

4906-
let _reset_executor = Reset((*fiber).executor_ptr, *(*fiber).executor_ptr);
4907-
*(*fiber).executor_ptr = &raw mut (*fiber).executor;
4908-
let _reset_suspend = Reset((*fiber).suspend, *(*fiber).suspend);
4909-
let _reset_stack_limit = Reset((*fiber).stack_limit, *(*fiber).stack_limit);
4910-
let state = Some((*fiber).state.take().unwrap().push());
4911-
let restore = Restore { fiber, state };
4912-
(*restore.fiber)
4913-
.fiber
4914-
.as_ref()
4915-
.unwrap()
4916-
.resume((store, result))
4920+
unsafe {
4921+
let _reset_executor = Reset((*fiber).executor_ptr, *(*fiber).executor_ptr);
4922+
*(*fiber).executor_ptr = &raw mut (*fiber).executor;
4923+
let _reset_suspend = Reset((*fiber).suspend, *(*fiber).suspend);
4924+
let _reset_stack_limit = Reset((*fiber).stack_limit, *(*fiber).stack_limit);
4925+
let state = Some((*fiber).state.take().unwrap().push());
4926+
let restore = Restore { fiber, state };
4927+
(*restore.fiber)
4928+
.fiber
4929+
.as_ref()
4930+
.unwrap()
4931+
.resume((store, result))
4932+
}
49174933
}
49184934

49194935
/// See `resume_fiber_raw`
@@ -4922,7 +4938,9 @@ unsafe fn resume_fiber(
49224938
store: Option<*mut dyn VMStore>,
49234939
result: Result<()>,
49244940
) -> Result<Result<(*mut dyn VMStore, Result<()>), Option<*mut dyn VMStore>>> {
4925-
match resume_fiber_raw(fiber, store, result).map(|(store, result)| (store.unwrap(), result)) {
4941+
match unsafe {
4942+
resume_fiber_raw(fiber, store, result).map(|(store, result)| (store.unwrap(), result))
4943+
} {
49264944
Ok(pair) => Ok(Ok(pair)),
49274945
Err(s) => {
49284946
if let Some(range) = fiber.fiber.as_ref().unwrap().stack().range() {
@@ -4949,10 +4967,12 @@ unsafe fn suspend_fiber(
49494967
stack_limit: *mut usize,
49504968
store: Option<*mut dyn VMStore>,
49514969
) -> Result<Option<*mut dyn VMStore>> {
4952-
let _reset_suspend = Reset(suspend, *suspend);
4953-
let _reset_stack_limit = Reset(stack_limit, *stack_limit);
4954-
assert!(!(*suspend).is_null());
4955-
let (store, result) = (**suspend).suspend(store);
4970+
let (store, result) = unsafe {
4971+
let _reset_suspend = Reset(suspend, *suspend);
4972+
let _reset_stack_limit = Reset(stack_limit, *stack_limit);
4973+
assert!(!(*suspend).is_null());
4974+
(**suspend).suspend(store)
4975+
};
49564976
result?;
49574977
Ok(store)
49584978
}

0 commit comments

Comments
 (0)