@ -2,7 +2,7 @@ use crate::externref::{ModuleInfoLookup, VMExternRefActivationsTable, EMPTY_MODU
use crate ::imports ::Imports ;
use crate ::instance ::{ Instance , InstanceHandle , ResourceLimiter , RuntimeMemoryCreator } ;
use crate ::memory ::{ DefaultMemoryCreator , Memory } ;
use crate ::table ::{ Table , TableElement } ;
use crate ::table ::Table ;
use crate ::traphandlers ::Trap ;
use crate ::vmcontext ::{
VMBuiltinFunctionsArray , VMCallerCheckedAnyfunc , VMContext , VMFunctionBody , VMFunctionImport ,
@ -19,10 +19,9 @@ use std::rc::Rc;
use std ::slice ;
use std ::sync ::Arc ;
use thiserror ::Error ;
use wasmtime_environ ::entity ::{ packed_option ::ReservedValue , EntityRef , EntitySet , PrimaryMap } ;
use wasmtime_environ ::entity ::{ EntityRef , EntitySet , PrimaryMap } ;
use wasmtime_environ ::wasm ::{
DefinedFuncIndex , DefinedMemoryIndex , DefinedTableIndex , FuncIndex , GlobalInit , SignatureIndex ,
TableElementType , WasmType ,
DefinedFuncIndex , DefinedMemoryIndex , DefinedTableIndex , GlobalInit , SignatureIndex , WasmType ,
} ;
use wasmtime_environ ::{
ir , MemoryInitialization , MemoryInitializer , Module , ModuleType , TableInitializer , VMOffsets ,
@ -212,7 +211,7 @@ impl<'a> From<&'a PrimaryMap<SignatureIndex, VMSharedSignatureIndex>> for Shared
fn get_table_init_start (
init : & TableInitializer ,
instance : & Instance ,
) -> Result < usize , InstantiationError > {
) -> Result < u32 , InstantiationError > {
match init . base {
Some ( base ) = > {
let val = unsafe {
@ -223,7 +222,7 @@ fn get_table_init_start(
}
} ;
init . offset . checked_add ( val as usize ) . ok_or_else ( | | {
init . offset . checked_add ( val ) . ok_or_else ( | | {
InstantiationError ::Link ( LinkError (
"element segment global base overflows" . to_owned ( ) ,
) )
@ -237,6 +236,7 @@ fn check_table_init_bounds(instance: &Instance) -> Result<(), InstantiationError
for init in & instance . module . table_initializers {
let table = instance . get_table ( init . table_index ) ;
let start = get_table_init_start ( init , instance ) ? ;
let start = usize ::try_from ( start ) . unwrap ( ) ;
let end = start . checked_add ( init . elements . len ( ) ) ;
match end {
@ -256,34 +256,15 @@ fn check_table_init_bounds(instance: &Instance) -> Result<(), InstantiationError
fn initialize_tables ( instance : & Instance ) -> Result < ( ) , InstantiationError > {
for init in & instance . module . table_initializers {
let table = instance . get_table ( init . table_index ) ;
let start = get_table_init_start ( init , instance ) ? ;
let end = start . checked_add ( init . elements . len ( ) ) ;
match end {
Some ( end ) if end < = table . size ( ) as usize = > {
for ( i , func_idx ) in init . elements . iter ( ) . enumerate ( ) {
let item = match table . element_type ( ) {
TableElementType ::Func = > instance
. get_caller_checked_anyfunc ( * func_idx )
. map_or ( ptr ::null_mut ( ) , | f : & VMCallerCheckedAnyfunc | {
f as * const VMCallerCheckedAnyfunc as * mut VMCallerCheckedAnyfunc
} )
. into ( ) ,
TableElementType ::Val ( _ ) = > {
assert ! ( * func_idx = = FuncIndex ::reserved_value ( ) ) ;
TableElement ::ExternRef ( None )
}
} ;
table . set ( u32 ::try_from ( start + i ) . unwrap ( ) , item ) . unwrap ( ) ;
}
}
_ = > {
return Err ( InstantiationError ::Trap ( Trap ::wasm (
ir ::TrapCode ::TableOutOfBounds ,
) ) )
}
}
instance
. table_init_segment (
init . table_index ,
& init . elements ,
get_table_init_start ( init , instance ) ? ,
0 ,
init . elements . len ( ) as u32 ,
)
. map_err ( InstantiationError ::Trap ) ? ;
}
Ok ( ( ) )
@ -292,7 +273,7 @@ fn initialize_tables(instance: &Instance) -> Result<(), InstantiationError> {
fn get_memory_init_start (
init : & MemoryInitializer ,
instance : & Instance ,
) -> Result < usize , InstantiationError > {
) -> Result < u32 , InstantiationError > {
match init . base {
Some ( base ) = > {
let val = unsafe {
@ -303,7 +284,7 @@ fn get_memory_init_start(
}
} ;
init . offset . checked_add ( val as usize ) . ok_or_else ( | | {
init . offset . checked_add ( val ) . ok_or_else ( | | {
InstantiationError ::Link ( LinkError ( "data segment global base overflows" . to_owned ( ) ) )
} )
}
@ -311,24 +292,6 @@ fn get_memory_init_start(
}
}
unsafe fn get_memory_slice < 'instance > (
init : & MemoryInitializer ,
instance : & 'instance Instance ,
) -> & 'instance mut [ u8 ] {
let memory = if let Some ( defined_memory_index ) =
instance . module . defined_memory_index ( init . memory_index )
{
instance . memory ( defined_memory_index )
} else {
let import = instance . imported_memory ( init . memory_index ) ;
let foreign_instance = ( & mut * ( import ) . vmctx ) . instance ( ) ;
let foreign_memory = & mut * ( import ) . from ;
let foreign_index = foreign_instance . memory_index ( foreign_memory ) ;
foreign_instance . memory ( foreign_index )
} ;
& mut * ptr ::slice_from_raw_parts_mut ( memory . base , memory . current_length )
}
fn check_memory_init_bounds (
instance : & Instance ,
initializers : & [ MemoryInitializer ] ,
@ -336,6 +299,7 @@ fn check_memory_init_bounds(
for init in initializers {
let memory = instance . get_memory ( init . memory_index ) ;
let start = get_memory_init_start ( init , instance ) ? ;
let start = usize ::try_from ( start ) . unwrap ( ) ;
let end = start . checked_add ( init . data . len ( ) ) ;
match end {
@ -358,21 +322,15 @@ fn initialize_memories(
initializers : & [ MemoryInitializer ] ,
) -> Result < ( ) , InstantiationError > {
for init in initializers {
let memory = instance . get_memory ( init . memory_index ) ;
let start = get_memory_init_start ( init , instance ) ? ;
let end = start . checked_add ( init . data . len ( ) ) ;
match end {
Some ( end ) if end < = memory . current_length = > {
let mem_slice = unsafe { get_memory_slice ( init , instance ) } ;
mem_slice [ start . . end ] . copy_from_slice ( & init . data ) ;
}
_ = > {
return Err ( InstantiationError ::Trap ( Trap ::wasm (
ir ::TrapCode ::HeapOutOfBounds ,
) ) )
}
}
instance
. memory_init_segment (
init . memory_index ,
& init . data ,
get_memory_init_start ( init , instance ) ? ,
0 ,
init . data . len ( ) as u32 ,
)
. map_err ( InstantiationError ::Trap ) ? ;
}
Ok ( ( ) )
@ -496,6 +454,7 @@ unsafe fn initialize_vmcontext(instance: &Instance, req: InstanceAllocationReque
) ;
// Initialize the functions
let mut base = instance . anyfunc_base ( ) ;
for ( index , sig ) in instance . module . functions . iter ( ) {
let type_index = req . shared_signatures . lookup ( * sig ) ;
@ -510,13 +469,14 @@ unsafe fn initialize_vmcontext(instance: &Instance, req: InstanceAllocationReque
} ;
ptr ::write (
instance . anyfunc_ptr ( index ) ,
base ,
VMCallerCheckedAnyfunc {
func_ptr ,
type_index ,
vmctx ,
} ,
) ;
base = base . add ( 1 ) ;
}
// Initialize the defined tables