A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix a possible panic-after-oom with host resources (#14755)
Alex Crichton6876e3179500
diff --git a/crates/fuzzing/tests/oom/component_resource.rs b/crates/fuzzing/tests/oom/component_resource.rsindex df92c82b3f..ed41dce691 100644--- a/crates/fuzzing/tests/oom/component_resource.rs+++ b/crates/fuzzing/tests/oom/component_resource.rs@@ -1,6 +1,6 @@#![cfg(arc_try_new)]-use wasmtime::component::{Component, Linker, Resource, ResourceAny, ResourceType};+use wasmtime::component::{Component, Linker, Resource, ResourceAny};use wasmtime::{Config, Engine, Result, Store};use wasmtime_fuzzing::oom::OomTest;@@ -79,24 +79,10 @@ fn component_resource_any_try_from_resource() -> Result<()> {let mut config = Config::new();config.concurrency_support(false);let engine = Engine::new(&config)?;- let component = Component::new(- &engine,- r#"- (component- (import "t" (type $t (sub resource)))- )- "#,- )?;- let mut linker = Linker::<()>::new(&engine);- linker- .root()- .resource("t", ResourceType::host::<MyResource>(), |_, _| Ok(()))?;- let instance_pre = linker.instantiate_pre(&component)?;// Error propagation via anyhow allocates after OOM.OomTest::new().allow_alloc_after_oom(true).test(|| {let mut store = Store::try_new(&engine, ())?;- let _instance = instance_pre.instantiate(&mut store)?;let resource = Resource::<MyResource>::new_own(42);let any = ResourceAny::try_from_resource(resource, &mut store)?;let _typed: Resource<MyResource> = any.try_into_resource(&mut store)?;@@ -105,31 +91,26 @@ fn component_resource_any_try_from_resource() -> Result<()> {}#[test]-fn component_resource_any_try_into_resource() -> Result<()> {+fn component_resource_any_try_from_resource_after_oom() -> Result<()> {let mut config = Config::new();config.concurrency_support(false);let engine = Engine::new(&config)?;- let component = Component::new(- &engine,- r#"- (component- (import "t" (type $t (sub resource)))- )- "#,- )?;- let mut linker = Linker::<()>::new(&engine);- linker- .root()- .resource("t", ResourceType::host::<MyResource>(), |_, _| Ok(()))?;- let instance_pre = linker.instantiate_pre(&component)?;- // Error propagation via anyhow allocates after OOM.- OomTest::new().allow_alloc_after_oom(true).test(|| {- let mut store = Store::try_new(&engine, ())?;- let _instance = instance_pre.instantiate(&mut store)?;- let resource = Resource::<MyResource>::new_own(100);- let any = ResourceAny::try_from_resource(resource, &mut store)?;- let _back: Resource<MyResource> = any.try_into_resource(&mut store)?;- Ok(())- })+ OomTest::new()+ .allow_alloc_after_oom(true)+ .alloc_succeeds_after_oom(true)+ .test(|| {+ let mut store = Store::try_new(&engine, ())?;++ // Keep converting after a conversion fails with OOM, to check that+ // the failure left the store usable.+ let mut result = Ok(());+ for rep in 0..10 {+ let resource = Resource::<MyResource>::new_own(rep);+ if let Err(e) = ResourceAny::try_from_resource(resource, &mut store) {+ result = result.and(Err(e));+ }+ }+ result+ })}diff --git a/crates/wasmtime/src/runtime/component/resources/host_tables.rs b/crates/wasmtime/src/runtime/component/resources/host_tables.rsindex 74ca89871f..f120397b4c 100644--- a/crates/wasmtime/src/runtime/component/resources/host_tables.rs+++ b/crates/wasmtime/src/runtime/component/resources/host_tables.rs@@ -152,7 +152,7 @@ impl<'a> HostResourceTables<'a> {instance: Option<RuntimeInstance>,) -> Result<HostResourceIndex> {let idx = self.tables.resource_lower_own(TypedResource::Host(rep))?;- Ok(self.new_host_index(idx, dtor, instance)?)+ self.new_host_index(idx, dtor, instance)}/// See [`HostResourceTables::host_resource_lower_own`].@@ -160,7 +160,7 @@ impl<'a> HostResourceTables<'a> {let idx = self.tables.resource_lower_borrow(TypedResource::Host(rep))?;- Ok(self.new_host_index(idx, None, None)?)+ self.new_host_index(idx, None, None)}/// Validates that `idx` is still valid for the host tables, notably@@ -202,38 +202,45 @@ impl<'a> HostResourceTables<'a> {/// slot provided by `idx`.////// This will register metadata necessary to track the current generation- /// in the returned `HostResourceIndex` as well.+ /// in the returned `HostResourceIndex` as well. If that fails then the+ /// handle at `idx`, which was just inserted, is removed again.fn new_host_index(&mut self,idx: u32,dtor: Option<NonNull<VMFuncRef>>,instance: Option<RuntimeInstance>,) -> Result<HostResourceIndex> {- let list = &mut self.host_resource_data.table_slot_metadata;- let info = TableSlot {- generation: self.host_resource_data.cur_generation,- instance,- dtor: dtor.map(SendSyncPtr::new),- };- match list.get_mut(idx as usize) {- Some(slot) => *slot = info,- None => {- // Resource handles start at 1, not zero, so push two elements- // for the first resource handle.- if list.is_empty() {- assert_eq!(idx, 1);- list.push(TableSlot {- generation: 0,- instance: None,- dtor: None,- })?;+ let result = (|| {+ let list = &mut self.host_resource_data.table_slot_metadata;+ let info = TableSlot {+ generation: self.host_resource_data.cur_generation,+ instance,+ dtor: dtor.map(SendSyncPtr::new),+ };+ match list.get_mut(idx as usize) {+ Some(slot) => *slot = info,+ None => {+ // Resource handles start at 1, not zero, so push two elements+ // for the first resource handle.+ if list.is_empty() {+ assert_eq!(idx, 1);+ list.push(TableSlot {+ generation: 0,+ instance: None,+ dtor: None,+ })?;+ }+ assert_eq!(idx as usize, list.len());+ list.push(info)?;}- assert_eq!(idx as usize, list.len());- list.push(info)?;}- }- Ok(HostResourceIndex::new(idx, info.generation))+ Ok(HostResourceIndex::new(idx, info.generation))+ })();+ if result.is_err() {+ self.tables.resource_drop(TypedResourceIndex::Host(idx))?;+ }+ result}/// Drops a host-owned resource from host tables.