Fix module initialization with externref element segments (#2392)

This commit fixes an issue with reference-types-using-modules where they
panicked on instantiation if any element segments had an externref null
specified.
This commit is contained in:
Alex Crichton
2020-11-11 11:59:40 -06:00
committed by GitHub
parent 41e87a2f99
commit 59be6dc5ff
2 changed files with 18 additions and 9 deletions

View File

@@ -1270,15 +1270,19 @@ fn initialize_tables(instance: &Instance) -> Result<(), InstantiationError> {
}
for (i, func_idx) in init.elements.iter().enumerate() {
let anyfunc = instance.get_caller_checked_anyfunc(*func_idx).map_or(
ptr::null_mut(),
|f: &VMCallerCheckedAnyfunc| {
f as *const VMCallerCheckedAnyfunc as *mut VMCallerCheckedAnyfunc
},
);
table
.set(u32::try_from(start + i).unwrap(), anyfunc.into())
.unwrap();
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();
}
}