Truly independent web browser
LibWasm: Reject host functions of types the store does not know
Andreas Klingaad9d5fcc05a
diff --git a/Libraries/LibWasm/Engine/src/capi.rs b/Libraries/LibWasm/Engine/src/capi.rsindex 05ceba725cb..cc089900293 100644--- a/Libraries/LibWasm/Engine/src/capi.rs+++ b/Libraries/LibWasm/Engine/src/capi.rs@@ -113,7 +113,7 @@ pub enum WasmHeapKind {Exception,NoException,/// A type the module defines; `type_index` is its index in the module, or its identity in the store for the types- /// of the store's functions and globals.+ /// of the store's functions and globals, and those of a host function.Concrete,}@@ -612,14 +612,10 @@ impl Drop for HostFunctionData {impl HostFunction for HostFunctionData {fn call(&self, store: &mut Store, arguments: &[Value]) -> Result<Vec<Value>, Trap> {- // NB: A host function's concrete types are identities of the store, which the store knows by the time the- // function is called, even its own. An embedder may still pass types of a module that the store does not- // have. Arguments of those types are not traced, which is safe while the call lasts, as the caller keeps- // them alive.let arguments: Vec<WasmValue> = arguments.iter().zip(&self.parameter_types)- .map(|(&value, &ty)| WasmValue::of(value, store.trace_of_known(ty).unwrap_or(TRACE_NONE)))+ .map(|(&value, &ty)| WasmValue::of(value, store.trace_of(ty))).collect();let mut results = vec![WasmValue {@@ -672,7 +668,8 @@ impl HostFunction for HostFunctionData {}/// Add a host function of the given type to the store, and return its cell. The function visits what its user data-/// refers to with `visit`, and releases the user data with `release` when it goes away.+/// refers to with `visit`, and releases the user data with `release` when it goes away. The concrete types among its+/// types must be identities of the store, which is how the collector tells what its values are, or the runtime aborts.////// # Safety///@@ -699,6 +696,13 @@ pub unsafe extern "C" fn wasm_store_allocate_host_function(params: to_types(parameters, parameter_count),results: to_types(results, result_count),};+ assert!(+ ty.params+ .iter()+ .chain(&ty.results)+ .all(|&ty| store_of(store).knows_type(ty)),+ "a host function's concrete types must be identities of the store"+ );let data = Rc::new(HostFunctionData {callback,visit,@@ -940,8 +944,7 @@ pub unsafe extern "C" fn wasm_store_invoke(Ok(values) => {let types = &store.function(entity(function)).ty.results;for (index, (value, &ty)) in values.into_iter().zip(types).take(result_count).enumerate() {- let trace = store.trace_of_known(ty).unwrap_or(TRACE_NONE);- unsafe { *results.add(index) = WasmValue::of(value, trace) };+ unsafe { *results.add(index) = WasmValue::of(value, store.trace_of(ty)) };}WasmStatus::Ok}diff --git a/Libraries/LibWasm/Engine/src/runtime/mod.rs b/Libraries/LibWasm/Engine/src/runtime/mod.rsindex ed3a17b4982..d7e695d54b8 100644--- a/Libraries/LibWasm/Engine/src/runtime/mod.rs+++ b/Libraries/LibWasm/Engine/src/runtime/mod.rs@@ -1141,15 +1141,15 @@ impl Store {gc::trace_of(ty, &self.types)}- /// The same, or None for a concrete type that is not an identity of this store.- pub fn trace_of_known(&self, ty: ValType) -> Option<u8> {- if let ValType::Ref(reference) = ty- && let HeapType::Concrete(identity) = reference.heap- && self.types.get(identity as usize).is_none()- {- return None;+ /// Whether a type is one of this store, which a concrete type is if it names an identity of the store.+ pub fn knows_type(&self, ty: ValType) -> bool {+ match ty {+ ValType::Ref(RefType {+ heap: HeapType::Concrete(identity),+ ..+ }) => (identity as usize) < self.types.len(),+ _ => true,}- Some(self.trace_of(ty))}pub fn function_reference(&self, address: FunctionAddress) -> u64 {diff --git a/Libraries/LibWasm/Engine/tests/execution.rs b/Libraries/LibWasm/Engine/tests/execution.rsindex c6478ad7670..a0982ea2a94 100644--- a/Libraries/LibWasm/Engine/tests/execution.rs+++ b/Libraries/LibWasm/Engine/tests/execution.rs@@ -553,3 +553,27 @@ fn a_change_of_the_word_does_not_end_a_wait() {unsafe { &*(address as *const std::sync::atomic::AtomicU32) }.store(1, std::sync::atomic::Ordering::SeqCst);assert_eq!(waiter.join().unwrap(), Ok(vec![Value::I32(2)]));}++#[test]+fn knows_the_types_of_its_own_identities() {+ use libwasm_engine::types::{FuncType, HeapType, RefType, ValType};+ let mut store = Store::new();+ let reference_to = |identity| {+ ValType::Ref(RefType {+ nullable: true,+ heap: HeapType::Concrete(identity),+ })+ };+ assert!(store.knows_type(ValType::I32));+ assert!(!store.knows_type(reference_to(0)));++ store.allocate_host_function(+ FuncType {+ params: vec![],+ results: vec![],+ },+ std::rc::Rc::new(|_: &mut Store, _: &[Value]| Ok(vec![])),+ );+ assert!(store.knows_type(reference_to(0)));+ assert!(!store.knows_type(reference_to(1)));+}diff --git a/Libraries/LibWasm/Runtime.h b/Libraries/LibWasm/Runtime.hindex 80f0895c330..27fd0fd9769 100644--- a/Libraries/LibWasm/Runtime.h+++ b/Libraries/LibWasm/Runtime.h@@ -226,7 +226,8 @@ public:~Store();// A host function writes one result for each result of its type, or returns an error to trap or throw. The- // function it is made of lives as long as the function is in the store.+ // function it is made of lives as long as the function is in the store. The concrete types of its type must be+ // identities of the store, or the runtime aborts.using HostFunction = GC::Function<ErrorOr<void, HostError>(Store&, ReadonlySpan<Value> arguments, Span<Value> results)>;Entity allocate_host_function(FunctionType const&, GC::Ref<HostFunction>);