A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix and optimize type check when spawning threads (#14610)
Alex Crichtonaa899d8ed618
diff --git a/crates/environ/src/component/dfg.rs b/crates/environ/src/component/dfg.rsindex 7b26558672..83b4a39326 100644--- a/crates/environ/src/component/dfg.rs+++ b/crates/environ/src/component/dfg.rs@@ -478,7 +478,7 @@ pub enum Trampoline {},ThreadNewIndirect {instance: RuntimeComponentInstanceIndex,- start_func_ty_idx: ComponentTypeIndex,+ start_func_ty_idx: ModuleInternedTypeIndex,start_func_table_id: TableId,},ThreadResumeLater {diff --git a/crates/environ/src/component/info.rs b/crates/environ/src/component/info.rsindex 9d2f623fae..0e5de10953 100644--- a/crates/environ/src/component/info.rs+++ b/crates/environ/src/component/info.rs@@ -1106,8 +1106,9 @@ pub enum Trampoline {ThreadNewIndirect {/// The specific component instance which is calling the intrinsic.instance: RuntimeComponentInstanceIndex,- /// The type index for the start function of the thread.- start_func_ty_idx: ComponentTypeIndex,+ /// The core function type that the thread's start function must have,+ /// as declared by the `thread.new-indirect` built-in.+ start_func_ty_idx: ModuleInternedTypeIndex,/// The index of the table that stores the start function.start_func_table_idx: RuntimeTableIndex,},diff --git a/crates/environ/src/component/translate.rs b/crates/environ/src/component/translate.rsindex 88d2933fe4..8654925ec1 100644--- a/crates/environ/src/component/translate.rs+++ b/crates/environ/src/component/translate.rs@@ -312,7 +312,7 @@ enum LocalInitializer<'data> {},ThreadNewIndirect {func: ModuleInternedTypeIndex,- start_func_ty: ComponentTypeIndex,+ start_func_ty: ModuleInternedTypeIndex,start_func_table_index: TableIndex,},ThreadResumeLater {@@ -893,8 +893,32 @@ impl<'a, 'data> Translator<'a, 'data> {component_type_index += 1;}}++ // Core types defined at the component level, such as the type of a+ // `thread.new-indirect` start function, are interned as they're+ // defined, just like in a core module. This ensures that all rec+ // groups that a rec group refers to are interned before it is.Payload::CoreTypeSection(s) => {+ let mut index = self+ .validator+ .types(0)+ .unwrap()+ .core_type_count_in_component();self.validator.core_type_section(&s)?;+ let types = self.validator.types(0).unwrap();+ for ty in s {+ match ty? {+ wasmparser::CoreType::Rec(group) => {+ let id = types.core_type_at_in_component(index).unwrap_sub();+ let group_id = types.rec_group_id_of(id);+ self.types+ .module_types_builder()+ .intern_rec_group(types, group_id)?;+ index += u32::try_from(group.types().len()).unwrap();+ }+ wasmparser::CoreType::Module(_) => index += 1,+ }+ }}// Processing the import section at this point is relatively simple@@ -1276,9 +1300,16 @@ impl<'a, 'data> Translator<'a, 'data> {} => {let func = self.core_func_signature(core_func_index)?;core_func_index += 1;+ let types = self.validator.types(0).unwrap();+ let start_func_ty =+ types.core_type_at_in_component(func_ty_index).unwrap_sub();+ let start_func_ty = self+ .types+ .module_types_builder()+ .intern_type(types, start_func_ty)?;LocalInitializer::ThreadNewIndirect {func,- start_func_ty: ComponentTypeIndex::from_u32(func_ty_index),+ start_func_ty,start_func_table_index: TableIndex::from_u32(table_index),}}diff --git a/crates/environ/src/trap_encoding.rs b/crates/environ/src/trap_encoding.rsindex a72ef5b4bb..2346cfef04 100644--- a/crates/environ/src/trap_encoding.rs+++ b/crates/environ/src/trap_encoding.rs@@ -246,7 +246,7 @@ generate_trap_type! {SubtaskDropNotResolved = "cannot drop a subtask which has not yet resolved",/// Start function does not match the expected type.- ThreadNewIndirectInvalidType = "start function does not match expected type (currently only `(i32) -> ()` is supported)",+ ThreadNewIndirectInvalidType = "start function does not match expected type",/// The start function index points to an uninitialized function.ThreadNewIndirectUninitialized = "the start function index points to an uninitialized function",diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex eb22762eb3..85572b8bea 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -67,9 +67,7 @@ use crate::vm::component::{CallContext, ComponentInstance, CurrentScope, Instancuse crate::vm::{AlwaysMut, SendSyncPtr, UncaughtException, VMFuncRef, VMLazyThread, VMMemoryDefinition, VMStore,};-use crate::{- AsContext, AsContextMut, FuncType, Result, StoreContext, StoreContextMut, ValRaw, ValType, bail,-};+use crate::{AsContext, AsContextMut, Result, StoreContext, StoreContextMut, ValRaw, bail};use crate::{Instance as ModuleInstance, bail_bug};use alloc::borrow::ToOwned;use alloc::collections::{BTreeMap, BTreeSet, VecDeque};@@ -93,10 +91,10 @@ use wasmtime_environ::component::{MAX_FLAT_RESULTS, OptionsIndex, PREPARE_ASYNC_NO_RESULT, PREPARE_ASYNC_WITH_RESULT,RuntimeComponentInstanceIndex, RuntimeTableIndex, StringEncoding,TypeComponentGlobalErrorContextTableIndex, TypeComponentLocalErrorContextTableIndex,- TypeFuncIndex, TypeFutureTableIndex, TypeStreamTableIndex, TypeTupleIndex,+ TypeFutureTableIndex, TypeStreamTableIndex, TypeTupleIndex,};use wasmtime_environ::packed_option::ReservedValue;-use wasmtime_environ::{NUM_COMPONENT_CONTEXT_SLOTS, Trap};+use wasmtime_environ::{ModuleInternedTypeIndex, NUM_COMPONENT_CONTEXT_SLOTS, Trap};#[cfg(feature = "gc")]use wasmtime_unwinder::Unwind;@@ -4091,19 +4089,22 @@ impl Instance {self,mut store: StoreContextMut<T>,runtime_instance: RuntimeComponentInstanceIndex,- _func_ty_idx: TypeFuncIndex, // currently unused+ start_func_ty: ModuleInternedTypeIndex,start_func_table_idx: RuntimeTableIndex,start_func_idx: u32,context: i32,) -> Result<u32> {log::trace!("creating new thread");- let start_func_ty = FuncType::new(store.engine(), [ValType::I32], []);let (instance, registry) = self.id().get_mut_and_registry(store.0);+ let Some(start_func_ty) = instance.component().signatures().shared_type(start_func_ty)+ else {+ bail_bug!("thread.new-indirect start function type should be registered");+ };let callee = instance.index_runtime_func_table(registry, start_func_table_idx, start_func_idx as u64)?.ok_or_else(|| Trap::ThreadNewIndirectUninitialized)?;- if callee.type_index(store.0) != start_func_ty.type_index() {+ if callee.type_index(store.0) != start_func_ty {bail!(Trap::ThreadNewIndirectInvalidType);}@@ -4795,7 +4796,7 @@ pub trait VMComponentAsyncStore {&mut self,instance: Instance,caller: RuntimeComponentInstanceIndex,- func_ty_idx: TypeFuncIndex,+ func_ty_idx: ModuleInternedTypeIndex,start_func_table_idx: RuntimeTableIndex,start_func_idx: u32,context: i32,@@ -5073,7 +5074,7 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {&mut self,instance: Instance,caller: RuntimeComponentInstanceIndex,- func_ty_idx: TypeFuncIndex,+ func_ty_idx: ModuleInternedTypeIndex,start_func_table_idx: RuntimeTableIndex,start_func_idx: u32,context: i32,diff --git a/crates/wasmtime/src/runtime/vm/component/libcalls.rs b/crates/wasmtime/src/runtime/vm/component/libcalls.rsindex 863f2e59b3..83f8e53405 100644--- a/crates/wasmtime/src/runtime/vm/component/libcalls.rs+++ b/crates/wasmtime/src/runtime/vm/component/libcalls.rs@@ -1296,7 +1296,7 @@ fn thread_new_indirect(store.component_async_store().thread_new_indirect(instance,RuntimeComponentInstanceIndex::from_u32(caller),- TypeFuncIndex::from_u32(func_ty_id),+ wasmtime_environ::ModuleInternedTypeIndex::from_u32(func_ty_id),RuntimeTableIndex::from_u32(func_table_idx),func_idx,context as i32,diff --git a/tests/misc_testsuite/component-model-threading/thread-new-indirect-declared-type.wast b/tests/misc_testsuite/component-model-threading/thread-new-indirect-declared-type.wastnew file mode 100644index 0000000000..874c4c91ed--- /dev/null+++ b/tests/misc_testsuite/component-model-threading/thread-new-indirect-declared-type.wast@@ -0,0 +1,222 @@+;;! component_model_async = true+;;! component_model_threading = true+;;! reference_types = true+;;! function_references = true+;;! gc = true++;; `thread.new-indirect` traps if the start function's type isn't exactly the+;; core function type named by the canonical built-in, using core wasm's type+;; canonicalization rules. Validation only requires that type to be+;; structurally `[i32] -> []`, so it may be non-final, have a supertype, or be+;; part of a larger rec group.+;;+;; Each `spawn-*` export below uses a `thread.new-indirect` with a different+;; declared type, and spawns the start function at the given table index. Only+;; the function whose type is the same as the declared type should be accepted.++(component definition $C+ (core module $libc+ (table (export "__indirect_function_table") 7 funcref))+ (core instance $libc (instantiate $libc))+ (alias core export $libc "__indirect_function_table" (core table $table))++ (core type $final (func (param i32)))+ (core type $non-final (sub (func (param i32))))+ (core rec (type $rec (func (param i32))) (type (struct)))+ (core type $super (sub (func (param i32))))+ (core type $sub (sub $super (func (param i32))))+ (core type $s (struct))+ (core rec+ (type $rec-outer (func (param i32)))+ (type (struct (field (ref $s)))))++ (core func $thread.new-indirect-final+ (canon thread.new-indirect $final (core table $table)))+ (core func $thread.new-indirect-non-final+ (canon thread.new-indirect $non-final (core table $table)))+ (core func $thread.new-indirect-rec+ (canon thread.new-indirect $rec (core table $table)))+ (core func $thread.new-indirect-sub+ (canon thread.new-indirect $sub (core table $table)))+ (core func $thread.new-indirect-rec-outer+ (canon thread.new-indirect $rec-outer (core table $table)))+ (core func $thread.yield-then-resume (canon thread.yield-then-resume))++ (core module $m+ (type $final (func (param i32)))+ (type $non-final (sub (func (param i32))))+ (rec (type $rec (func (param i32))) (type (struct)))+ (rec (type $rec-other (func (param i32))) (type (array i8)))+ (type $super (sub (func (param i32))))+ (type $sub (sub $super (func (param i32))))+ (type $s (struct))+ (rec+ (type $rec-outer (func (param i32)))+ (type (struct (field (ref $s)))))++ (import "" "thread.new-indirect-final"+ (func $thread.new-indirect-final (param i32 i32) (result i32)))+ (import "" "thread.new-indirect-non-final"+ (func $thread.new-indirect-non-final (param i32 i32) (result i32)))+ (import "" "thread.new-indirect-rec"+ (func $thread.new-indirect-rec (param i32 i32) (result i32)))+ (import "" "thread.new-indirect-sub"+ (func $thread.new-indirect-sub (param i32 i32) (result i32)))+ (import "" "thread.new-indirect-rec-outer"+ (func $thread.new-indirect-rec-outer (param i32 i32) (result i32)))+ (import "" "thread.yield-then-resume"+ (func $thread.yield-then-resume (param i32) (result i32)))+ (import "libc" "__indirect_function_table" (table $table 7 funcref))++ (global $ran (mut i32) (i32.const 0))++ (func $start-final (type $final) (param i32)+ (global.set $ran (local.get 0)))+ (func $start-non-final (type $non-final) (param i32)+ (global.set $ran (local.get 0)))+ (func $start-rec (type $rec) (param i32)+ (global.set $ran (local.get 0)))+ (func $start-rec-other (type $rec-other) (param i32)+ (global.set $ran (local.get 0)))+ (func $start-super (type $super) (param i32)+ (global.set $ran (local.get 0)))+ (func $start-sub (type $sub) (param i32)+ (global.set $ran (local.get 0)))+ (func $start-rec-outer (type $rec-outer) (param i32)+ (global.set $ran (local.get 0)))+ (elem (table $table) (i32.const 0) func+ $start-final ;; 0+ $start-non-final ;; 1+ $start-rec ;; 2+ $start-rec-other ;; 3+ $start-super ;; 4+ $start-sub ;; 5+ $start-rec-outer) ;; 6++ ;; Runs the new thread to completion and returns what it stored in `$ran`.+ (func $run (param $thread i32) (result i32)+ (global.set $ran (i32.const 0))+ (drop (call $thread.yield-then-resume (local.get $thread)))+ (global.get $ran))++ (func (export "spawn-final") (param i32) (result i32)+ (call $run+ (call $thread.new-indirect-final (local.get 0) (i32.const 42))))+ (func (export "spawn-non-final") (param i32) (result i32)+ (call $run+ (call $thread.new-indirect-non-final (local.get 0) (i32.const 42))))+ (func (export "spawn-rec") (param i32) (result i32)+ (call $run+ (call $thread.new-indirect-rec (local.get 0) (i32.const 42))))+ (func (export "spawn-sub") (param i32) (result i32)+ (call $run+ (call $thread.new-indirect-sub (local.get 0) (i32.const 42))))+ (func (export "spawn-rec-outer") (param i32) (result i32)+ (call $run+ (call $thread.new-indirect-rec-outer (local.get 0) (i32.const 42)))))++ (core instance $i (instantiate $m+ (with "" (instance+ (export "thread.new-indirect-final" (func $thread.new-indirect-final))+ (export "thread.new-indirect-non-final"+ (func $thread.new-indirect-non-final))+ (export "thread.new-indirect-rec" (func $thread.new-indirect-rec))+ (export "thread.new-indirect-sub" (func $thread.new-indirect-sub))+ (export "thread.new-indirect-rec-outer"+ (func $thread.new-indirect-rec-outer))+ (export "thread.yield-then-resume" (func $thread.yield-then-resume))))+ (with "libc" (instance $libc))))++ (func (export "spawn-final") async (param "i" u32) (result u32)+ (canon lift (core func $i "spawn-final")))+ (func (export "spawn-non-final") async (param "i" u32) (result u32)+ (canon lift (core func $i "spawn-non-final")))+ (func (export "spawn-rec") async (param "i" u32) (result u32)+ (canon lift (core func $i "spawn-rec")))+ (func (export "spawn-sub") async (param "i" u32) (result u32)+ (canon lift (core func $i "spawn-sub")))+ (func (export "spawn-rec-outer") async (param "i" u32) (result u32)+ (canon lift (core func $i "spawn-rec-outer")))+)++;; A final `(func (param i32))` matches only itself.+(component instance $C $C)+(assert_return (invoke "spawn-final" (u32.const 0)) (u32.const 42))+(component instance $C $C)+(assert_trap (invoke "spawn-final" (u32.const 1)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-final" (u32.const 2)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-final" (u32.const 3)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-final" (u32.const 4)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-final" (u32.const 5)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-final" (u32.const 6)) "does not match")++;; A non-final type with no supertype matches the identical `$super`, but not a+;; subtype of it.+(component instance $C $C)+(assert_trap (invoke "spawn-non-final" (u32.const 0)) "does not match")+(component instance $C $C)+(assert_return (invoke "spawn-non-final" (u32.const 1)) (u32.const 42))+(component instance $C $C)+(assert_trap (invoke "spawn-non-final" (u32.const 2)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-non-final" (u32.const 3)) "does not match")+(component instance $C $C)+(assert_return (invoke "spawn-non-final" (u32.const 4)) (u32.const 42))+(component instance $C $C)+(assert_trap (invoke "spawn-non-final" (u32.const 5)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-non-final" (u32.const 6)) "does not match")++;; A rec group member matches the same member of a structurally identical rec+;; group, but not one of a different rec group.+(component instance $C $C)+(assert_trap (invoke "spawn-rec" (u32.const 0)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec" (u32.const 1)) "does not match")+(component instance $C $C)+(assert_return (invoke "spawn-rec" (u32.const 2)) (u32.const 42))+(component instance $C $C)+(assert_trap (invoke "spawn-rec" (u32.const 3)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec" (u32.const 4)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec" (u32.const 5)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec" (u32.const 6)) "does not match")++;; A type with a supertype matches only the same subtype.+(component instance $C $C)+(assert_trap (invoke "spawn-sub" (u32.const 0)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-sub" (u32.const 1)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-sub" (u32.const 2)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-sub" (u32.const 3)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-sub" (u32.const 4)) "does not match")+(component instance $C $C)+(assert_return (invoke "spawn-sub" (u32.const 5)) (u32.const 42))+(component instance $C $C)+(assert_trap (invoke "spawn-sub" (u32.const 6)) "does not match")++;; A rec group member whose group refers to a type outside of the group.+(component instance $C $C)+(assert_trap (invoke "spawn-rec-outer" (u32.const 0)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec-outer" (u32.const 1)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec-outer" (u32.const 2)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec-outer" (u32.const 3)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec-outer" (u32.const 4)) "does not match")+(component instance $C $C)+(assert_trap (invoke "spawn-rec-outer" (u32.const 5)) "does not match")+(component instance $C $C)+(assert_return (invoke "spawn-rec-outer" (u32.const 6)) (u32.const 42))