Skip to content

Commit 9661ca8

Browse files
authored
Remove some more panics in concurrent.rs (#12874)
Downgrade some panics to `bail_bug!` or `?` where appropriate by propagating `Result<T>` in a few more locations.
1 parent b8a3e20 commit 9661ca8

4 files changed

Lines changed: 19 additions & 21 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5150,27 +5150,27 @@ impl ConcurrentState {
51505150
///
51515151
/// The `task` is bit-packed as returned by `current_call_context_scope_id`
51525152
/// below.
5153-
pub fn call_context(&mut self, task: u32) -> &mut CallContext {
5153+
pub fn call_context(&mut self, task: u32) -> Result<&mut CallContext> {
51545154
let (task, is_host) = (task >> 1, task & 1 == 1);
51555155
if is_host {
51565156
let task: TableId<HostTask> = TableId::new(task);
5157-
&mut self.get_mut(task).unwrap().call_context
5157+
Ok(&mut self.get_mut(task)?.call_context)
51585158
} else {
51595159
let task: TableId<GuestTask> = TableId::new(task);
5160-
&mut self.get_mut(task).unwrap().call_context
5160+
Ok(&mut self.get_mut(task)?.call_context)
51615161
}
51625162
}
51635163

51645164
/// Used by `ResourceTables` to record the scope of a borrow to get undone
51655165
/// in the future.
5166-
pub fn current_call_context_scope_id(&self) -> u32 {
5166+
pub fn current_call_context_scope_id(&self) -> Result<u32> {
51675167
let (bits, is_host) = match self.current_thread {
51685168
CurrentThread::Guest(id) => (id.task.rep(), false),
51695169
CurrentThread::Host(id) => (id.rep(), true),
5170-
CurrentThread::None => unreachable!(),
5170+
CurrentThread::None => bail_bug!("current thread is not set"),
51715171
};
51725172
assert_eq!((bits << 1) >> 1, bits);
5173-
(bits << 1) | u32::from(is_host)
5173+
Ok((bits << 1) | u32::from(is_host))
51745174
}
51755175

51765176
fn current_guest_thread(&self) -> Result<QualifiedThreadId> {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use wasmtime_environ::component::{CanonicalAbiInfo, InterfaceType};
1111
pub enum ConcurrentState {}
1212

1313
impl ConcurrentState {
14-
pub fn call_context(&mut self, _: u32) -> &mut CallContext {
14+
pub fn call_context(&mut self, _: u32) -> Result<&mut CallContext> {
1515
match *self {}
1616
}
1717

18-
pub fn current_call_context_scope_id(&self) -> u32 {
18+
pub fn current_call_context_scope_id(&self) -> Result<u32> {
1919
match *self {}
2020
}
2121
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,18 +503,16 @@ pub struct ComponentTasksNotConcurrent {
503503
}
504504

505505
impl ComponentTaskState {
506-
pub fn call_context(&mut self, id: u32) -> &mut CallContext {
506+
pub fn call_context(&mut self, id: u32) -> Result<&mut CallContext> {
507507
match self {
508-
ComponentTaskState::NotConcurrent(state) => &mut state.scopes[id as usize],
508+
ComponentTaskState::NotConcurrent(state) => Ok(&mut state.scopes[id as usize]),
509509
ComponentTaskState::Concurrent(state) => state.call_context(id),
510510
}
511511
}
512512

513-
pub fn current_call_context_scope_id(&self) -> u32 {
513+
pub fn current_call_context_scope_id(&self) -> Result<u32> {
514514
match self {
515-
ComponentTaskState::NotConcurrent(state) => {
516-
u32::try_from(state.scopes.len() - 1).unwrap()
517-
}
515+
ComponentTaskState::NotConcurrent(state) => Ok(u32::try_from(state.scopes.len() - 1)?),
518516
ComponentTaskState::Concurrent(state) => state.current_call_context_scope_id(),
519517
}
520518
}

crates/wasmtime/src/runtime/vm/component/resources.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl ResourceTables<'_> {
226226
match self.table_for_index(&index).remove_resource(index)? {
227227
RemovedResource::Own { rep } => Ok(Some(rep)),
228228
RemovedResource::Borrow { scope } => {
229-
self.task_state.call_context(scope).borrow_count -= 1;
229+
self.task_state.call_context(scope)?.borrow_count -= 1;
230230
Ok(None)
231231
}
232232
}
@@ -273,8 +273,8 @@ impl ResourceTables<'_> {
273273
pub fn resource_lift_borrow(&mut self, index: TypedResourceIndex) -> Result<u32> {
274274
let (rep, is_own) = self.table_for_index(&index).resource_lend(index)?;
275275
if is_own {
276-
let scope = self.task_state.current_call_context_scope_id();
277-
self.task_state.call_context(scope).lenders.push(index);
276+
let scope = self.task_state.current_call_context_scope_id()?;
277+
self.task_state.call_context(scope)?.lenders.push(index);
278278
}
279279
Ok(rep)
280280
}
@@ -292,8 +292,8 @@ impl ResourceTables<'_> {
292292
/// `VMComponentContext` which handles the special case of avoiding borrow
293293
/// tracking entirely.
294294
pub fn resource_lower_borrow(&mut self, resource: TypedResource) -> Result<u32> {
295-
let scope = self.task_state.current_call_context_scope_id();
296-
let cx = self.task_state.call_context(scope);
295+
let scope = self.task_state.current_call_context_scope_id()?;
296+
let cx = self.task_state.call_context(scope)?;
297297
cx.borrow_count = cx.borrow_count.checked_add(1).unwrap();
298298
self.table_for_resource(&resource)
299299
.resource_borrow_insert(resource, scope)
@@ -306,8 +306,8 @@ impl ResourceTables<'_> {
306306
/// resources that were originally passed in.
307307
#[inline]
308308
pub fn validate_scope_exit(&mut self) -> Result<()> {
309-
let current = self.task_state.current_call_context_scope_id();
310-
let cx = self.task_state.call_context(current);
309+
let current = self.task_state.current_call_context_scope_id()?;
310+
let cx = self.task_state.call_context(current)?;
311311
if cx.borrow_count > 0 {
312312
bail!("borrow handles still remain at the end of the call")
313313
}

0 commit comments

Comments
 (0)