@@ -34,11 +34,12 @@ use core::sync::atomic::AtomicU64;
3434use core:: { mem, ptr} ;
3535#[ cfg( feature = "gc" ) ]
3636use wasmtime_environ:: ModuleInternedTypeIndex ;
37+ use wasmtime_environ:: error:: OutOfMemory ;
3738use wasmtime_environ:: {
3839 DataIndex , DefinedGlobalIndex , DefinedMemoryIndex , DefinedTableIndex , DefinedTagIndex ,
39- ElemIndex , EntityIndex , EntityRef , EntitySet , FuncIndex , GlobalIndex , HostPtr , MemoryIndex ,
40- PrimaryMap , PtrSize , TableIndex , TableInitialValue , TableSegmentElements , TagIndex , Trap ,
41- VMCONTEXT_MAGIC , VMOffsets , VMSharedTypeIndex , packed_option:: ReservedValue ,
40+ ElemIndex , EntityIndex , EntityRef , FuncIndex , GlobalIndex , HostPtr , MemoryIndex , PrimaryMap ,
41+ PtrSize , TableIndex , TableInitialValue , TableSegmentElements , TagIndex , Trap , VMCONTEXT_MAGIC ,
42+ VMOffsets , VMSharedTypeIndex , packed_option:: ReservedValue ,
4243} ;
4344#[ cfg( feature = "wmemcheck" ) ]
4445use wasmtime_wmemcheck:: Wmemcheck ;
@@ -167,11 +168,11 @@ impl Instance {
167168 req : InstanceAllocationRequest ,
168169 memories : PrimaryMap < DefinedMemoryIndex , ( MemoryAllocationIndex , Memory ) > ,
169170 tables : PrimaryMap < DefinedTableIndex , ( TableAllocationIndex , Table ) > ,
170- ) -> InstanceHandle {
171+ ) -> Result < InstanceHandle , OutOfMemory > {
171172 let module = req. runtime_info . env_module ( ) ;
172173 let memory_tys = & module. memories ;
173- let dropped_elements = EntitySet :: with_capacity ( module. passive_elements . len ( ) ) ;
174- let dropped_data = EntitySet :: with_capacity ( module. passive_data_map . len ( ) ) ;
174+ let dropped_elements = EntitySet :: with_capacity ( module. passive_elements . len ( ) ) ? ;
175+ let dropped_data = EntitySet :: with_capacity ( module. passive_data_map . len ( ) ) ? ;
175176
176177 #[ cfg( feature = "wmemcheck" ) ]
177178 let wmemcheck_state = if req. store . engine ( ) . config ( ) . wmemcheck {
@@ -200,14 +201,14 @@ impl Instance {
200201 wmemcheck_state,
201202 store : None ,
202203 vmctx : OwnedVMContext :: new ( ) ,
203- } ) ;
204+ } ) ? ;
204205
205206 // SAFETY: this vmctx was allocated with the same layout above, so it
206207 // should be safe to initialize with the same values here.
207208 unsafe {
208209 ret. get_mut ( ) . initialize_vmctx ( req. store , req. imports ) ;
209210 }
210- ret
211+ Ok ( ret)
211212 }
212213
213214 /// Converts a raw `VMContext` pointer into a raw `Instance` pointer.
@@ -1050,13 +1051,18 @@ impl Instance {
10501051 }
10511052
10521053 /// Drop an element.
1053- pub ( crate ) fn elem_drop ( self : Pin < & mut Self > , elem_index : ElemIndex ) {
1054+ pub ( crate ) fn elem_drop (
1055+ self : Pin < & mut Self > ,
1056+ elem_index : ElemIndex ,
1057+ ) -> Result < ( ) , OutOfMemory > {
10541058 // https://webassembly.github.io/reference-types/core/exec/instructions.html#exec-elem-drop
10551059
1056- self . dropped_elements_mut ( ) . insert ( elem_index) ;
1060+ self . dropped_elements_mut ( ) . insert ( elem_index) ? ;
10571061
10581062 // Note that we don't check that we actually removed a segment because
10591063 // dropping a non-passive segment is a no-op (not a trap).
1064+
1065+ Ok ( ( ) )
10601066 }
10611067
10621068 /// Get a locally-defined memory.
@@ -1218,11 +1224,16 @@ impl Instance {
12181224 }
12191225
12201226 /// Drop the given data segment, truncating its length to zero.
1221- pub ( crate ) fn data_drop ( self : Pin < & mut Self > , data_index : DataIndex ) {
1222- self . dropped_data_mut ( ) . insert ( data_index) ;
1227+ pub ( crate ) fn data_drop (
1228+ self : Pin < & mut Self > ,
1229+ data_index : DataIndex ,
1230+ ) -> Result < ( ) , OutOfMemory > {
1231+ self . dropped_data_mut ( ) . insert ( data_index) ?;
12231232
12241233 // Note that we don't check that we actually removed a segment because
12251234 // dropping a non-passive segment is a no-op (not a trap).
1235+
1236+ Ok ( ( ) )
12261237 }
12271238
12281239 /// Get a table by index regardless of whether it is locally-defined
@@ -1891,7 +1902,7 @@ impl<T: InstanceLayout> OwnedInstance<T> {
18911902 /// Allocates a new `OwnedInstance` and places `instance` inside of it.
18921903 ///
18931904 /// This will `instance`
1894- pub ( super ) fn new ( mut instance : T ) -> OwnedInstance < T > {
1905+ pub ( super ) fn new ( mut instance : T ) -> Result < OwnedInstance < T > , OutOfMemory > {
18951906 let layout = instance. layout ( ) ;
18961907 debug_assert ! ( layout. size( ) >= size_of_val( & instance) ) ;
18971908 debug_assert ! ( layout. align( ) >= align_of_val( & instance) ) ;
@@ -1906,10 +1917,9 @@ impl<T: InstanceLayout> OwnedInstance<T> {
19061917 alloc:: alloc:: alloc ( layout)
19071918 }
19081919 } ;
1909- if ptr. is_null ( ) {
1910- alloc:: alloc:: handle_alloc_error ( layout) ;
1911- }
1912- let instance_ptr = NonNull :: new ( ptr. cast :: < T > ( ) ) . unwrap ( ) ;
1920+ let Some ( instance_ptr) = NonNull :: new ( ptr. cast :: < T > ( ) ) else {
1921+ return Err ( OutOfMemory :: new ( layout. size ( ) ) ) ;
1922+ } ;
19131923
19141924 // SAFETY: it's part of the unsafe contract of `InstanceLayout` that the
19151925 // `add` here is appropriate for the layout allocated.
@@ -1938,7 +1948,7 @@ impl<T: InstanceLayout> OwnedInstance<T> {
19381948 ) ;
19391949 debug_assert_eq ! ( vmctx_self_reference. addr( ) , ret. get( ) . vmctx( ) . addr( ) ) ;
19401950
1941- ret
1951+ Ok ( ret)
19421952 }
19431953
19441954 /// Gets the raw underlying `&Instance` from this handle.
0 commit comments