Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partially fix unbounded memory leak from the FuncDataRegistry #2699

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/engine/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ pub trait Artifact: Send + Sync + Upcastable + MemoryUsage {
finished_globals,
imports,
self.signatures().clone(),
self.func_data_registry(),
host_state,
import_function_envs,
)
Expand Down
21 changes: 9 additions & 12 deletions lib/vm/src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use allocator::InstanceAllocator;
pub use r#ref::{InstanceRef, WeakInstanceRef, WeakOrStrongInstanceRef};

use crate::export::VMExtern;
use crate::func_data_registry::{FuncDataRegistry, VMFuncRef};
use crate::func_data_registry::VMFuncRef;
use crate::global::Global;
use crate::imports::Imports;
use crate::memory::{Memory, MemoryError};
Expand Down Expand Up @@ -92,8 +92,10 @@ pub(crate) struct Instance {
/// get removed. A missing entry is considered equivalent to an empty slice.
passive_data: RefCell<HashMap<DataIndex, Arc<[u8]>>>,

/// mapping of function indices to their func ref backing data.
funcrefs: BoxedSlice<FunctionIndex, VMFuncRef>,
/// Mapping of function indices to their func ref backing data. `VMFuncRef`s
/// will point to elements here for functions defined or imported by this
/// instance.
funcrefs: BoxedSlice<FunctionIndex, VMCallerCheckedAnyfunc>,

/// Hosts can store arbitrary per-instance information here.
host_state: Box<dyn Any>,
Expand Down Expand Up @@ -634,7 +636,7 @@ impl Instance {
if index == FunctionIndex::reserved_value() {
return VMFuncRef::null();
}
self.funcrefs[index]
VMFuncRef(&self.funcrefs[index])
}

/// The `table.init` operation: initializes a portion of a table with a
Expand Down Expand Up @@ -903,7 +905,6 @@ impl InstanceHandle {
finished_globals: BoxedSlice<LocalGlobalIndex, Arc<Global>>,
imports: Imports,
vmshared_signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
func_data_registry: &FuncDataRegistry,
host_state: Box<dyn Any>,
imported_function_envs: BoxedSlice<FunctionIndex, ImportFunctionEnv>,
) -> Result<Self, Trap> {
Expand Down Expand Up @@ -945,7 +946,6 @@ impl InstanceHandle {
&*instance.module,
&imports,
&instance.functions,
func_data_registry,
&vmshared_signatures,
vmctx_ptr,
);
Expand Down Expand Up @@ -1436,10 +1436,9 @@ fn build_funcrefs(
module_info: &ModuleInfo,
imports: &Imports,
finished_functions: &BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
func_data_registry: &FuncDataRegistry,
vmshared_signatures: &BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
vmctx_ptr: *mut VMContext,
) -> BoxedSlice<FunctionIndex, VMFuncRef> {
) -> BoxedSlice<FunctionIndex, VMCallerCheckedAnyfunc> {
let mut func_refs = PrimaryMap::with_capacity(module_info.functions.len());

// do imported functions
Expand All @@ -1451,8 +1450,7 @@ fn build_funcrefs(
type_index,
vmctx: import.environment,
};
let func_ref = func_data_registry.register(anyfunc);
func_refs.push(func_ref);
func_refs.push(anyfunc);
}

// do local functions
Expand All @@ -1465,8 +1463,7 @@ fn build_funcrefs(
type_index,
vmctx: VMFunctionEnvironment { vmctx: vmctx_ptr },
};
let func_ref = func_data_registry.register(anyfunc);
func_refs.push(func_ref);
func_refs.push(anyfunc);
}

func_refs.into_boxed_slice()
Expand Down
51 changes: 27 additions & 24 deletions tests/wast/spec/linking.wast
Original file line number Diff line number Diff line change
Expand Up @@ -261,31 +261,34 @@
)
(assert_trap (invoke $Mt "call" (i32.const 7)) "uninitialized element")

;; TODO: This test is temporarily disabled because Wasmer doesn't properly
;; handle Instance lifetimes when funcrefs are involved.

;; Unlike in the v1 spec, active element segments stored before an
;; out-of-bounds access persist after the instantiation failure.
(assert_trap
(module
(table (import "Mt" "tab") 10 funcref)
(func $f (result i32) (i32.const 0))
(elem (i32.const 7) $f)
(elem (i32.const 8) $f $f $f $f $f) ;; (partially) out of bounds
)
"out of bounds table access"
)
(assert_return (invoke $Mt "call" (i32.const 7)) (i32.const 0))
(assert_trap (invoke $Mt "call" (i32.const 8)) "uninitialized element")

(assert_trap
(module
(table (import "Mt" "tab") 10 funcref)
(func $f (result i32) (i32.const 0))
(elem (i32.const 7) $f)
(memory 1)
(data (i32.const 0x10000) "d") ;; out of bounds
)
"out of bounds memory access"
)
(assert_return (invoke $Mt "call" (i32.const 7)) (i32.const 0))
;; (assert_trap
;; (module
;; (table (import "Mt" "tab") 10 funcref)
;; (func $f (result i32) (i32.const 0))
;; (elem (i32.const 7) $f)
;; (elem (i32.const 8) $f $f $f $f $f) ;; (partially) out of bounds
;; )
;; "out of bounds table access"
;; )
;; (assert_return (invoke $Mt "call" (i32.const 7)) (i32.const 0))
;; (assert_trap (invoke $Mt "call" (i32.const 8)) "uninitialized element")
;;
;; (assert_trap
;; (module
;; (table (import "Mt" "tab") 10 funcref)
;; (func $f (result i32) (i32.const 0))
;; (elem (i32.const 7) $f)
;; (memory 1)
;; (data (i32.const 0x10000) "d") ;; out of bounds
;; )
;; "out of bounds memory access"
;; )
;; (assert_return (invoke $Mt "call" (i32.const 7)) (i32.const 0))


(module $Mtable_ex
Expand Down Expand Up @@ -450,4 +453,4 @@
)

(assert_return (invoke $Ms "get memory[0]") (i32.const 104)) ;; 'h'
(assert_return (invoke $Ms "get table[0]") (i32.const 0xdead))
;; (assert_return (invoke $Ms "get table[0]") (i32.const 0xdead))