Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Migrate more tests to `*.wast` (#15079)

Alex Crichton97bfc0aefc9b
diff --git a/tests/all/component_model/dynamic.rs b/tests/all/component_model/dynamic.rs
index fde137f9b4..5203421474 100644
--- a/tests/all/component_model/dynamic.rs
+++ b/tests/all/component_model/dynamic.rs
@@ -89,22 +89,6 @@ fn primitives() -> Result<()> {
Ok(())
}
-#[test]
-fn strings() -> Result<()> {
- let engine = super::engine();
- let mut store = Store::new(&engine, ());
-
- let component = Component::new(&engine, make_echo_component("string", 8))?;
- let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
- let func = instance.get_func(&mut store, "echo").unwrap();
- let input = Val::String("hello, component!".into());
- let mut output = [Val::Bool(false)];
- func.call(&mut store, &[input.clone()], &mut output)?;
- assert_eq!(input, output[0]);
-
- Ok(())
-}
-
#[test]
fn lists() -> Result<()> {
let engine = super::engine();
@@ -783,127 +767,6 @@ fn flags() -> Result<()> {
Ok(())
}
-#[test]
-fn everything() -> Result<()> {
- // This serves to test both nested types and storing parameters on the heap (i.e. exceeding `MAX_STACK_PARAMS`)
-
- let engine = super::engine();
- let mut store = Store::new(&engine, ());
-
- let component = Component::new(
- &engine,
- make_echo_component_with_params(
- r#"
- (type $b' (enum "a" "b"))
- (export $b "b" (type $b'))
- (type $c' (record (field "D" bool) (field "E" u32)))
- (export $c "c" (type $c'))
- (type $f' (flags "G" "H" "I"))
- (export $f "f" (type $f'))
- (type $m' (record (field "N" bool) (field "O" u32)))
- (export $m "m" (type $m'))
- (type $j' (variant
- (case "K" u32)
- (case "L" float64)
- (case "M" $m)
- ))
- (export $j "j" (type $j'))
-
- (type $Foo' (record
- (field "A" u32)
- (field "B" $b)
- (field "C" $c)
- (field "F" (list $f))
- (field "J" $j)
- (field "P" s8)
- (field "Q" s16)
- (field "R" s32)
- (field "S" s64)
- (field "T" float32)
- (field "U" float64)
- (field "V" string)
- (field "W" char)
- (field "Y" (tuple u32 u32))
- (field "AA" (option u32))
- (field "BB" (result string (error string)))
- ))
- "#,
- &[
- Param(Type::I32, Some(0)),
- Param(Type::U8, Some(4)),
- Param(Type::U8, Some(5)),
- Param(Type::I32, Some(8)),
- Param(Type::I32, Some(12)),
- Param(Type::I32, Some(16)),
- Param(Type::U8, Some(20)),
- Param(Type::I64, Some(28)),
- Param(Type::I32, Some(32)),
- Param(Type::S8, Some(36)),
- Param(Type::S16, Some(38)),
- Param(Type::I32, Some(40)),
- Param(Type::I64, Some(48)),
- Param(Type::F32, Some(56)),
- Param(Type::F64, Some(64)),
- Param(Type::I32, Some(72)),
- Param(Type::I32, Some(76)),
- Param(Type::I32, Some(80)),
- Param(Type::I32, Some(84)),
- Param(Type::I32, Some(88)),
- Param(Type::I64, Some(96)),
- Param(Type::U8, Some(104)),
- Param(Type::I32, Some(108)),
- Param(Type::U8, Some(112)),
- Param(Type::I32, Some(116)),
- Param(Type::I32, Some(120)),
- ],
- ),
- )?;
- let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
- let func = instance.get_func(&mut store, "echo").unwrap();
- let input = Val::Record(vec![
- ("A".into(), Val::U32(32343)),
- ("B".into(), Val::Enum("b".to_string())),
- (
- "C".into(),
- Val::Record(vec![
- ("D".to_string(), Val::Bool(false)),
- ("E".to_string(), Val::U32(2084037802)),
- ]),
- ),
- (
- "F".into(),
- Val::List(vec![Val::Flags(vec!["G".to_string(), "I".to_string()])]),
- ),
- (
- "J".into(),
- Val::Variant("L".to_string(), Some(Box::new(Val::Float64(3.14159265)))),
- ),
- ("P".into(), Val::S8(42)),
- ("Q".into(), Val::S16(4242)),
- ("R".into(), Val::S32(42424242)),
- ("S".into(), Val::S64(424242424242424242)),
- ("T".into(), Val::Float32(3.14159265)),
- ("U".into(), Val::Float64(3.14159265)),
- ("V".into(), Val::String("wow, nice types".to_string())),
- ("W".into(), Val::Char('🦀')),
- ("Y".into(), Val::Tuple(vec![Val::U32(42), Val::U32(24)])),
- (
- "AA".into(),
- Val::Option(Some(Box::new(Val::U32(314159265)))),
- ),
- (
- "BB".into(),
- Val::Result(Ok(Some(Box::new(Val::String("no problem".to_string()))))),
- ),
- ]);
- let mut output = [Val::Bool(false)];
- func.call(&mut store, &[input.clone()], &mut output)?;
-
- assert_eq!(input, output[0]);
-
- Ok(())
-}
-
#[test]
fn introspection() -> Result<()> {
let engine = super::engine();
diff --git a/tests/all/component_model/import.rs b/tests/all/component_model/import.rs
index 285e155ef9..4e458d1078 100644
--- a/tests/all/component_model/import.rs
+++ b/tests/all/component_model/import.rs
@@ -6,82 +6,6 @@ use wasmtime::Result;
use wasmtime::component::*;
use wasmtime::{Config, Engine, Store, StoreContextMut, Trap, WasmBacktrace};
-#[test]
-fn can_compile() -> Result<()> {
- let engine = super::engine();
- let libc = r#"
- (core module $libc
- (memory (export "memory") 1)
- (func (export "realloc") (param i32 i32 i32 i32) (result i32)
- unreachable)
- )
- (core instance $libc (instantiate $libc))
- "#;
- Component::new(
- &engine,
- r#"(component
- (import "a" (func $f))
- (core func (canon lower (func $f)))
- )"#,
- )?;
- Component::new(
- &engine,
- format!(
- r#"(component
- (import "a" (func $f (param "a" string)))
- {libc}
- (core func (canon lower (func $f) (memory (core memory $libc "memory")) (realloc (core func $libc "realloc"))))
- )"#
- ),
- )?;
- Component::new(
- &engine,
- format!(
- r#"(component
- (import "f1" (func $f1 (param "a" string) (result string)))
- {libc}
- (core func (canon lower (func $f1) (memory (core memory $libc "memory")) (realloc (core func $libc "realloc"))))
-
- (import "f2" (func $f2 (param "a" u32) (result (list u8))))
- (core instance $libc2 (instantiate $libc))
- (core func (canon lower (func $f2) (memory (core memory $libc2 "memory")) (realloc (core func $libc2 "realloc"))))
-
- (core func (canon lower (func $f1) (memory (core memory $libc2 "memory")) (realloc (core func $libc2 "realloc"))))
- (core func (canon lower (func $f2) (memory (core memory $libc "memory")) (realloc (core func $libc "realloc"))))
- )"#
- ),
- )?;
- Component::new(
- &engine,
- format!(
- r#"(component
- (import "log" (func $log (param "a" string)))
- {libc}
- (core func $log_lower (canon lower (func $log) (memory (core memory $libc "memory")) (realloc (core func $libc "realloc"))))
-
- (core module $logger
- (import "host" "log" (func $log (param i32 i32)))
- (import "libc" "memory" (memory 1))
-
- (func (export "call")
- i32.const 0
- i32.const 0
- call $log)
- )
- (core instance $logger (instantiate $logger
- (with "host" (instance (export "log" (func $log_lower))))
- (with "libc" (instance $libc))
- ))
-
- (func (export "call")
- (canon lift (core func $logger "call"))
- )
- )"#
- ),
- )?;
- Ok(())
-}
-
#[test]
fn simple() -> Result<()> {
let component = r#"
diff --git a/tests/all/component_model/post_return.rs b/tests/all/component_model/post_return.rs
index 49f896e647..6f084a6d62 100644
--- a/tests/all/component_model/post_return.rs
+++ b/tests/all/component_model/post_return.rs
@@ -200,38 +200,3 @@ fn post_return_string() -> Result<()> {
Ok(())
}
-
-#[test]
-fn trap_in_post_return_poisons_instance() -> Result<()> {
- let component = r#"
- (component
- (core module $m
- (func (export "f"))
- (func (export "post") unreachable)
- )
- (core instance $i (instantiate $m))
- (func (export "f")
- (canon lift
- (core func $i "f")
- (post-return (core func $i "post"))
- )
- )
- )
- "#;
-
- let engine = super::engine();
- let component = Component::new(&engine, component)?;
- let mut store = Store::new(&engine, ());
- let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
- let f = instance.get_typed_func::<(), ()>(&mut store, "f")?;
- let trap = f.call(&mut store, ()).unwrap_err().downcast::<Trap>()?;
- assert_eq!(trap, Trap::UnreachableCodeReached);
- let err = f.call(&mut store, ()).unwrap_err();
- assert_eq!(
- err.downcast_ref(),
- Some(&Trap::CannotEnterComponent),
- "{err}",
- );
-
- Ok(())
-}
diff --git a/tests/all/component_model/resources.rs b/tests/all/component_model/resources.rs
index 6ebef706bd..1db6335d16 100644
--- a/tests/all/component_model/resources.rs
+++ b/tests/all/component_model/resources.rs
@@ -252,59 +252,6 @@ fn mismatch_resource_types() -> Result<()> {
Ok(())
}
-#[test]
-fn drop_in_different_places() -> Result<()> {
- let engine = super::engine();
- let c = Component::new(
- &engine,
- r#"
- (component
- (type $t' (resource (rep i32)))
-
- (export $t "t" (type $t'))
-
- (core func $ctor (canon resource.new $t))
- (func (export "ctor") (param "x" u32) (result (own $t))
- (canon lift (core func $ctor)))
-
- (core func $dtor (canon resource.drop $t))
- (func (export "dtor1") (param "x" (own $t))
- (canon lift (core func $dtor)))
-
- (component $c
- (import "t" (type $t (sub resource)))
- (core func $dtor (canon resource.drop $t))
- (func (export "dtor") (param "x" (own $t))
- (canon lift (core func $dtor)))
- )
- (instance $i1 (instantiate $c (with "t" (type $t))))
- (instance $i2 (instantiate $c (with "t" (type $t))))
-
- (export "dtor2" (func $i1 "dtor"))
- (export "dtor3" (func $i2 "dtor"))
- )
- "#,
- )?;
-
- let mut store = Store::new(&engine, ());
- let i = Linker::new(&engine).instantiate(&mut store, &c)?;
- let ctor = i.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "ctor")?;
- let dtor1 = i.get_typed_func::<(ResourceAny,), ()>(&mut store, "dtor1")?;
- let dtor2 = i.get_typed_func::<(ResourceAny,), ()>(&mut store, "dtor2")?;
- let dtor3 = i.get_typed_func::<(ResourceAny,), ()>(&mut store, "dtor3")?;
-
- let (t,) = ctor.call(&mut store, (100,))?;
- dtor1.call(&mut store, (t,))?;
-
- let (t,) = ctor.call(&mut store, (200,))?;
- dtor2.call(&mut store, (t,))?;
-
- let (t,) = ctor.call(&mut store, (300,))?;
- dtor3.call(&mut store, (t,))?;
-
- Ok(())
-}
-
#[test]
fn drop_guest_twice() -> Result<()> {
let engine = super::engine();
@@ -694,64 +641,6 @@ fn reenter_during_import() -> Result<()> {
Ok(())
}
-#[test]
-fn thread_through_borrow() -> Result<()> {
- let engine = super::engine();
- let c = Component::new(
- &engine,
- r#"
- (component
- (import "t" (type $t (sub resource)))
- (import "f" (func $f (param "x" (borrow $t))))
-
- (core func $f (canon lower (func $f)))
- (core func $drop (canon resource.drop $t))
-
- (core module $m
- (import "" "f" (func $f (param i32)))
- (import "" "drop" (func $drop (param i32)))
- (func (export "f2") (param i32)
- (call $f (local.get 0))
- (call $f (local.get 0))
- (call $drop (local.get 0))
- )
- )
- (core instance $i (instantiate $m
- (with "" (instance
- (export "f" (func $f))
- (export "drop" (func $drop))
- ))
- ))
-
- (func (export "f2") (param "x" (borrow $t))
- (canon lift (core func $i "f2")))
- )
- "#,
- )?;
-
- struct MyType;
-
- let mut store = Store::new(&engine, ());
- let mut linker = Linker::new(&engine);
- linker
- .root()
- .resource("t", ResourceType::host::<MyType>(), |_, _| Ok(()))?;
- linker
- .root()
- .func_wrap("f", |_cx, (r,): (Resource<MyType>,)| {
- assert!(!r.owned());
- assert_eq!(r.rep(), 100);
- Ok(())
- })?;
- let i = linker.instantiate(&mut store, &c)?;
-
- let f = i.get_typed_func::<(&Resource<MyType>,), ()>(&mut store, "f2")?;
-
- let resource = Resource::new_own(100);
- f.call(&mut store, (&resource,))?;
- Ok(())
-}
-
#[test]
fn borrows_must_be_dropped_before_lifting() -> Result<()> {
let engine = super::engine();
@@ -797,59 +686,6 @@ fn borrows_must_be_dropped_before_lifting() -> Result<()> {
Ok(())
}
-#[test]
-fn cannot_use_borrow_for_own() -> Result<()> {
- let engine = super::engine();
- let c = Component::new(
- &engine,
- r#"
- (component
- (import "t" (type $t (sub resource)))
- (import "f" (func $f (param "x" (own $t))))
-
- (core module $m
- (import "" "f" (func $f (param i32)))
- (func (export "f") (param i32)
- local.get 0
- call $f
- )
- )
- (core func $f (canon lower (func $f)))
- (core instance $i (instantiate $m
- (with "" (instance
- (export "f" (func $f))
- ))
- ))
-
- (func (export "f") (param "x" (borrow $t))
- (canon lift (core func $i "f")))
- )
- "#,
- )?;
-
- struct MyType;
-
- let mut store = Store::new(&engine, ());
- let mut linker = Linker::new(&engine);
- linker
- .root()
- .resource("t", ResourceType::host::<MyType>(), |_, _| Ok(()))?;
- linker
- .root()
- .func_wrap("f", |_, _: (Resource<MyType>,)| Ok(()))?;
- let i = linker.instantiate(&mut store, &c)?;
-
- let f = i.get_typed_func::<(&Resource<MyType>,), ()>(&mut store, "f")?;
-
- let resource = Resource::new_own(100);
- let err = f.call(&mut store, (&resource,)).unwrap_err();
- assert!(
- format!("{err:?}").contains("cannot lift own resource from a borrow"),
- "bad error: {err:?}",
- );
- Ok(())
-}
-
#[test]
fn can_use_own_for_borrow() -> Result<()> {
let engine = super::engine();
@@ -1303,79 +1139,6 @@ fn pass_host_borrow_to_guest() -> Result<()> {
Ok(())
}
-#[tokio::test]
-async fn drop_on_owned_resource() -> Result<()> {
- let mut config = wasmtime::Config::new();
- config.wasm_component_model_async(true);
- let engine = &wasmtime::Engine::new(&config)?;
- let c = Component::new(
- &engine,
- r#"
- (component
- (import "t" (type $t (sub resource)))
- (import "[constructor]t" (func $ctor (result (own $t))))
- (import "[method]t.foo" (func $foo async (param "self" (borrow $t))))
-
- (core func $ctor (canon lower (func $ctor)))
- (core func $drop (canon resource.drop $t))
- (core func $foo (canon lower (func $foo) async))
-
- (core module $m
- (import "" "ctor" (func $ctor (result i32)))
- (import "" "foo" (func $foo (param i32) (result i32)))
- (import "" "drop" (func $drop (param i32)))
-
- (func (export "f")
- (local $r i32)
- (local $status i32)
- (local.set $r (call $ctor))
- (local.set $status (call $foo (local.get $r)))
- (if (i32.ne (i32.const 1 (; STARTED ;)) (i32.and (local.get $status) (i32.const 0xf)))
- (then unreachable))
- (call $drop (local.get $r))
- (unreachable)
- )
- )
- (core instance $i (instantiate $m
- (with "" (instance
- (export "ctor" (func $ctor))
- (export "foo" (func $foo))
- (export "drop" (func $drop))
- ))
- ))
- (func (export "f") async (canon lift (core func $i "f")))
- )
- "#,
- )?;
-
- struct MyType;
-
- let mut store = Store::new(&engine, ());
- let mut linker = Linker::new(&engine);
- linker
- .root()
- .resource("t", ResourceType::host::<MyType>(), |_, _| Ok(()))?;
- linker.root().func_wrap("[constructor]t", |_, ()| {
- Ok((Resource::<MyType>::new_own(300),))
- })?;
- linker
- .root()
- .func_wrap_concurrent("[method]t.foo", |_cx, (r,): (Resource<MyType>,)| {
- assert!(!r.owned());
- Box::pin(core::future::pending::<Result<()>>())
- })?;
- let i = linker.instantiate_async(&mut store, &c).await?;
- let f = i.get_typed_func::<(), ()>(&mut store, "f")?;
-
- let err = f.call_async(&mut store, ()).await.unwrap_err();
- assert!(
- format!("{err:?}").contains("cannot remove owned resource while borrowed"),
- "bad error: {err:?}"
- );
-
- Ok(())
-}
-
#[test]
fn guest_different_host_same() -> Result<()> {
let engine = super::engine();
@@ -1642,37 +1405,6 @@ fn resource_dynamic_not_static() -> Result<()> {
Ok(())
}
-#[test]
-fn intrinsic_trampolines() -> Result<()> {
- let engine = super::engine();
- let mut store = Store::new(&engine, ());
- let linker = Linker::new(&engine);
- let c = Component::new(
- &engine,
- r#"
-(component
- (type $r' (resource (rep i32)))
- (export $r "r" (type $r'))
-
- (core func $new (canon resource.new $r))
- (core func $rep (canon resource.rep $r))
-
- (func (export "new") (param "x" u32) (result (own $r))
- (canon lift (core func $new)))
- (func (export "rep") (param "x" (borrow $r)) (result u32)
- (canon lift (core func $rep)))
-)
-"#,
- )?;
- let i = linker.instantiate(&mut store, &c)?;
- let new = i.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "new")?;
- let rep = i.get_typed_func::<(ResourceAny,), (u32,)>(&mut store, "rep")?;
-
- let r = new.call(&mut store, (42,))?.0;
- assert!(rep.call(&mut store, (r,)).is_err());
- Ok(())
-}
-
#[tokio::test]
async fn drop_after_sync_lowered_async_host_function() -> Result<()> {
let mut config = Config::new();
diff --git a/tests/all/component_model/strings.rs b/tests/all/component_model/strings.rs
index 3710d2886f..a4b8e71cb7 100644
--- a/tests/all/component_model/strings.rs
+++ b/tests/all/component_model/strings.rs
@@ -3,9 +3,7 @@
use super::REALLOC_AND_FREE;
use wasmtime::Result;
use wasmtime::component::{Component, Linker};
-use wasmtime::{Config, Engine, PoolingAllocationConfig, Store, StoreContextMut, Trap};
-
-const UTF16_TAG: u32 = 1 << 31;
+use wasmtime::{Config, Engine, PoolingAllocationConfig, Store, StoreContextMut};
// Special cases that this tries to test:
//
@@ -192,391 +190,6 @@ fn test_roundtrip(engine: &Engine, src: &str, dst: &str) -> Result<()> {
Ok(())
}
-#[test]
-fn ptr_out_of_bounds() -> Result<()> {
- let engine = wasmtime_test_util::component::engine();
- for src in ENCODINGS {
- for dst in ENCODINGS {
- test_ptr_out_of_bounds(&engine, src, dst)?;
- }
- }
- Ok(())
-}
-
-fn test_ptr_out_of_bounds(engine: &Engine, src: &str, dst: &str) -> Result<()> {
- let test = |len: u32| -> Result<()> {
- let component = format!(
- r#"
-(component
- (component $c
- (core module $m
- (func (export "") (param i32 i32))
- (func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
- (memory (export "memory") 1)
- )
- (core instance $m (instantiate $m))
- (func (export "a") (param "a" string)
- (canon lift (core func $m "") (realloc (core func $m "realloc")) (memory (core memory $m "memory"))
- string-encoding={dst})
- )
- )
-
- (component $c2
- (import "a" (func $f (param "a" string)))
- (core module $libc
- (memory (export "memory") 1)
- )
- (core instance $libc (instantiate $libc))
- (core func $f (canon lower (func $f) string-encoding={src} (memory (core memory $libc "memory"))))
- (core module $m
- (import "" "" (func $f (param i32 i32)))
-
- (func $start (call $f (i32.const 0x8000_0000) (i32.const {len})))
- (start $start)
- )
- (core instance (instantiate $m (with "" (instance (export "" (func $f))))))
- )
-
- (instance $c (instantiate $c))
- (instance $c2 (instantiate $c2 (with "a" (func $c "a"))))
-)
-"#
- );
- let component = Component::new(engine, &component)?;
- let mut store = Store::new(engine, ());
- let trap = Linker::new(engine)
- .instantiate(&mut store, &component)
- .err()
- .unwrap()
- .downcast::<Trap>()?;
- assert_eq!(trap, Trap::StringOutOfBounds);
- Ok(())
- };
-
- test(0)?;
- test(1)?;
-
- Ok(())
-}
-
-// Test that even if the ptr+len calculation overflows then a trap still
-// happens.
-#[test]
-fn ptr_overflow() -> Result<()> {
- let engine = wasmtime_test_util::component::engine();
- for src in ENCODINGS {
- for dst in ENCODINGS {
- test_ptr_overflow(&engine, src, dst)?;
- }
- }
- Ok(())
-}
-
-fn test_ptr_overflow(engine: &Engine, src: &str, dst: &str) -> Result<()> {
- let component = format!(
- r#"
-(component
- (component $c
- (core module $m
- (func (export "") (param i32 i32))
- (func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
- (memory (export "memory") 1)
- )
- (core instance $m (instantiate $m))
- (func (export "a") (param "a" string)
- (canon lift (core func $m "") (realloc (core func $m "realloc")) (memory (core memory $m "memory"))
- string-encoding={dst})
- )
- )
-
- (component $c2
- (import "a" (func $f (param "a" string)))
- (core module $libc
- (memory (export "memory") 1)
- )
- (core instance $libc (instantiate $libc))
- (core func $f (canon lower (func $f) string-encoding={src} (memory (core memory $libc "memory"))))
- (core module $m
- (import "" "" (func $f (param i32 i32)))
-
- (func (export "f") (param i32) (call $f (i32.const 1000) (local.get 0)))
- )
- (core instance $m (instantiate $m (with "" (instance (export "" (func $f))))))
- (func (export "f") (param "a" u32) (canon lift (core func $m "f")))
- )
-
- (instance $c (instantiate $c))
- (instance $c2 (instantiate $c2 (with "a" (func $c "a"))))
- (export "f" (func $c2 "f"))
-)
-"#
- );
-
- let component = Component::new(engine, &component)?;
-
- let test_overflow = |size: u32| -> Result<()> {
- println!("src={src} dst={dst} size={size:#x}");
- let mut store = Store::new(engine, ());
- let instance = Linker::new(engine).instantiate(&mut store, &component)?;
- let func = instance.get_typed_func::<(u32,), ()>(&mut store, "f")?;
- let trap = func
- .call(&mut store, (size,))
- .unwrap_err()
- .downcast::<Trap>()?;
- assert_eq!(trap, Trap::StringOutOfBounds);
- Ok(())
- };
-
- let max = 1 << 31;
-
- match src {
- "utf8" => {
- // This exceeds MAX_STRING_BYTE_LENGTH
- test_overflow(max)?;
-
- if dst == "utf16" {
- // exceeds MAX_STRING_BYTE_LENGTH when multiplied
- test_overflow(max / 2)?;
-
- // Technically this fails on the first string, not the second.
- // Ideally this would test the overflow check on the second
- // string though.
- test_overflow(max / 2 - 100)?;
- } else {
- // This will point into unmapped memory
- test_overflow(max - 100)?;
- }
- }
-
- "utf16" => {
- test_overflow(max / 2)?;
- test_overflow(max / 2 - 100)?;
- }
-
- "latin1+utf16" => {
- test_overflow((max / 2) | UTF16_TAG)?;
- // tag a utf16 string with the max length and it should overflow.
- test_overflow((max / 2 - 100) | UTF16_TAG)?;
- }
-
- _ => unreachable!(),
- }
-
- Ok(())
-}
-
-// Test that that the pointer returned from `realloc` is bounds-checked.
-#[test]
-fn realloc_oob() -> Result<()> {
- let engine = wasmtime_test_util::component::engine();
- for src in ENCODINGS {
- for dst in ENCODINGS {
- test_realloc_oob(&engine, src, dst)?;
- }
- }
- Ok(())
-}
-
-fn test_realloc_oob(engine: &Engine, src: &str, dst: &str) -> Result<()> {
- let component = format!(
- r#"
-(component
- (component $c
- (core module $m
- (func (export "") (param i32 i32))
- (func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 100_000)
- (memory (export "memory") 1)
- )
- (core instance $m (instantiate $m))
- (func (export "a") (param "a" string)
- (canon lift (core func $m "") (realloc (core func $m "realloc")) (memory (core memory $m "memory"))
- string-encoding={dst})
- )
- )
-
- (component $c2
- (import "a" (func $f (param "a" string)))
- (core module $libc
- (memory (export "memory") 1)
- )
- (core instance $libc (instantiate $libc))
- (core func $f (canon lower (func $f) string-encoding={src} (memory (core memory $libc "memory"))))
- (core module $m
- (import "" "" (func $f (param i32 i32)))
-
- (func (export "f") (call $f (i32.const 1000) (i32.const 10)))
- )
- (core instance $m (instantiate $m (with "" (instance (export "" (func $f))))))
- (func (export "f") (canon lift (core func $m "f")))
- )
-
- (instance $c (instantiate $c))
- (instance $c2 (instantiate $c2 (with "a" (func $c "a"))))
- (export "f" (func $c2 "f"))
-)
-"#
- );
-
- let component = Component::new(engine, &component)?;
- let mut store = Store::new(engine, ());
-
- let instance = Linker::new(engine).instantiate(&mut store, &component)?;
- let func = instance.get_typed_func::<(), ()>(&mut store, "f")?;
- let trap = func.call(&mut store, ()).unwrap_err().downcast::<Trap>()?;
- assert_eq!(trap, Trap::StringOutOfBounds);
- Ok(())
-}
-
-// Test that that the pointer returned from `realloc` is bounds-checked.
-#[test]
-fn raw_string_encodings() -> Result<()> {
- let engine = wasmtime_test_util::component::engine();
- test_invalid_string_encoding(&engine, "utf8", "utf8", &[0xff], 1)?;
- let array = b"valid string until \xffthen valid again";
- test_invalid_string_encoding(&engine, "utf8", "utf8", array, array.len() as u32)?;
- test_invalid_string_encoding(&engine, "utf8", "utf16", array, array.len() as u32)?;
- let array = b"symbol \xce\xa3 until \xffthen valid";
- test_invalid_string_encoding(&engine, "utf8", "utf8", array, array.len() as u32)?;
- test_invalid_string_encoding(&engine, "utf8", "utf16", array, array.len() as u32)?;
- test_invalid_string_encoding(&engine, "utf8", "latin1+utf16", array, array.len() as u32)?;
- test_invalid_string_encoding(&engine, "utf16", "utf8", &[0x01, 0xd8], 1)?;
- test_invalid_string_encoding(&engine, "utf16", "utf16", &[0x01, 0xd8], 1)?;
- test_invalid_string_encoding(
- &engine,
- "utf16",
- "latin1+utf16",
- &[0xff, 0xff, 0x01, 0xd8],
- 2,
- )?;
- test_invalid_string_encoding(
- &engine,
- "latin1+utf16",
- "utf8",
- &[0x01, 0xd8],
- 1 | UTF16_TAG,
- )?;
- test_invalid_string_encoding(
- &engine,
- "latin1+utf16",
- "utf16",
- &[0x01, 0xd8],
- 1 | UTF16_TAG,
- )?;
- test_invalid_string_encoding(
- &engine,
- "latin1+utf16",
- "utf16",
- &[0xff, 0xff, 0x01, 0xd8],
- 2 | UTF16_TAG,
- )?;
- test_invalid_string_encoding(
- &engine,
- "latin1+utf16",
- "latin1+utf16",
- &[0xab, 0x00, 0xff, 0xff, 0x01, 0xd8],
- 3 | UTF16_TAG,
- )?;
-
- // This latin1+utf16 string should get compressed to latin1 across the
- // boundary.
- test_valid_string_encoding(
- &engine,
- "latin1+utf16",
- "latin1+utf16",
- &[0xab, 0x00, 0xff, 0x00],
- 2 | UTF16_TAG,
- )?;
- Ok(())
-}
-
-fn test_invalid_string_encoding(
- engine: &Engine,
- src: &str,
- dst: &str,
- bytes: &[u8],
- len: u32,
-) -> Result<()> {
- let trap = test_raw_when_encoded(engine, src, dst, bytes, len)?.unwrap();
- let src = src.replace("latin1+", "");
- assert!(
- format!("{trap:?}").contains(&format!("invalid {src} encoding")),
- "bad error: {trap:?}",
- );
- Ok(())
-}
-
-fn test_valid_string_encoding(
- engine: &Engine,
- src: &str,
- dst: &str,
- bytes: &[u8],
- len: u32,
-) -> Result<()> {
- let err = test_raw_when_encoded(engine, src, dst, bytes, len)?;
- assert!(err.is_none());
- Ok(())
-}
-
-fn test_raw_when_encoded(
- engine: &Engine,
- src: &str,
- dst: &str,
- bytes: &[u8],
- len: u32,
-) -> Result<Option<wasmtime::Error>> {
- let component = format!(
- r#"
-(component
- (component $c
- (core module $m
- (func (export "") (param i32 i32))
- (func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
- (memory (export "memory") 1)
- )
- (core instance $m (instantiate $m))
- (func (export "a") (param "a" string)
- (canon lift (core func $m "") (realloc (core func $m "realloc")) (memory (core memory $m "memory"))
- string-encoding={dst})
- )
- )
-
- (component $c2
- (import "a" (func $f (param "a" string)))
- (core module $libc
- (memory (export "memory") 1)
- (func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
- )
- (core instance $libc (instantiate $libc))
- (core func $f (canon lower (func $f) string-encoding={src} (memory (core memory $libc "memory"))))
- (core module $m
- (import "" "" (func $f (param i32 i32)))
-
- (func (export "f") (param i32 i32 i32) (call $f (local.get 0) (local.get 2)))
- )
- (core instance $m (instantiate $m (with "" (instance (export "" (func $f))))))
- (func (export "f") (param "a" (list u8)) (param "b" u32) (canon lift (core func $m "f")
- (memory (core memory $libc "memory"))
- (realloc (core func $libc "realloc"))))
- )
-
- (instance $c (instantiate $c))
- (instance $c2 (instantiate $c2 (with "a" (func $c "a"))))
- (export "f" (func $c2 "f"))
-)
-"#
- );
-
- let component = Component::new(engine, &component)?;
- let mut store = Store::new(engine, ());
-
- let instance = Linker::new(engine).instantiate(&mut store, &component)?;
- let func = instance.get_typed_func::<(&[u8], u32), ()>(&mut store, "f")?;
- match func.call(&mut store, (bytes, len)) {
- Ok(_) => Ok(None),
- Err(e) => Ok(Some(e)),
- }
-}
-
#[test]
fn pass_string_on_component_boundary() -> Result<()> {
// Configure an engine such that linear memories are allocated right next
diff --git a/tests/all/component_model/sync_call_inline.rs b/tests/all/component_model/sync_call_inline.rs
index c807db21db..64b1037d2e 100644
--- a/tests/all/component_model/sync_call_inline.rs
+++ b/tests/all/component_model/sync_call_inline.rs
@@ -12,453 +12,6 @@ fn engine() -> Engine {
Engine::new(&config).unwrap()
}
-#[tokio::test]
-async fn host_call_forces_slow_path_preserves_context() -> Result<()> {
- let component = r#"
-(component
- (import "poke" (func $poke))
-
- (component $Inner
- (import "poke" (func $poke))
- (core func $poke' (canon lower (func $poke)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "poke" (func $poke'))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "f'") (param i32) (result i32)
- ;; Freshly entered deferred thread: context starts zeroed.
- (if (i32.ne (call $cget) (i32.const 0)) (then unreachable))
- (call $cset (i32.const 0x5678))
- ;; Force the deferred thread via a guest->host call.
- (call $poke')
- ;; Our context survives the force.
- (if (i32.ne (call $cget) (i32.const 0x5678)) (then unreachable))
- (i32.add (local.get 0) (i32.const 42))))
- (core instance $m (instantiate $M (with "" (instance
- (export "poke" (func $poke'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "f") (param "x" u32) (result u32)
- (canon lift (core func $m "f'"))))
-
- (component $Outer
- (import "f" (func $f (param "x" u32) (result u32)))
- (core func $f' (canon lower (func $f)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $N
- (import "" "f'" (func $f' (param i32) (result i32)))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "g'") (result i32) (local $r i32)
- (call $cset (i32.const 0x1234))
- (local.set $r (call $f' (i32.const 1234)))
- ;; Restored after the callee forced the slow exit path.
- (if (i32.ne (call $cget) (i32.const 0x1234)) (then unreachable))
- (local.get $r)))
- (core instance $n (instantiate $N (with "" (instance
- (export "f'" (func $f'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "g") (result u32)
- (canon lift (core func $n "g'"))))
-
- (instance $inner (instantiate $Inner (with "poke" (func $poke))))
- (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
- (export "g" (func $outer "g"))
-)
- "#;
-
- let engine = engine();
- let component = Component::new(&engine, component)?;
- let mut store = Store::new(&engine, 0u32);
- let mut linker = Linker::new(&engine);
- linker
- .root()
- .func_wrap("poke", |mut cx: StoreContextMut<u32>, (): ()| {
- *cx.data_mut() += 1;
- Ok(())
- })?;
- let instance = linker.instantiate_async(&mut store, &component).await?;
- let g = instance.get_typed_func::<(), (u32,)>(&mut store, "g")?;
-
- let (result,) = g.call_async(&mut store, ()).await?;
- assert_eq!(result, 1276);
- assert_eq!(*store.data(), 1, "host import should have been called once");
- Ok(())
-}
-
-#[tokio::test]
-async fn nested_chain_host_force_preserves_all_contexts() -> Result<()> {
- let component = r#"
-(component
- (import "poke" (func $poke))
-
- (component $Leaf
- (import "poke" (func $poke))
- (core func $poke' (canon lower (func $poke)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "poke" (func $poke'))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "leaf'") (param i32) (result i32)
- (if (i32.ne (call $cget) (i32.const 0)) (then unreachable))
- (call $cset (i32.const 0x0c0ffee0))
- (call $poke')
- (if (i32.ne (call $cget) (i32.const 0x0c0ffee0)) (then unreachable))
- (i32.add (local.get 0) (i32.const 1))))
- (core instance $m (instantiate $M (with "" (instance
- (export "poke" (func $poke'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "leaf") (param "x" u32) (result u32)
- (canon lift (core func $m "leaf'"))))
-
- (component $Mid
- (import "leaf" (func $leaf (param "x" u32) (result u32)))
- (core func $leaf' (canon lower (func $leaf)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "leaf'" (func $leaf' (param i32) (result i32)))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "mid'") (param i32) (result i32) (local $r i32)
- (if (i32.ne (call $cget) (i32.const 0)) (then unreachable))
- (call $cset (i32.const 0x0d00d100))
- (local.set $r (call $leaf' (local.get 0)))
- (if (i32.ne (call $cget) (i32.const 0x0d00d100)) (then unreachable))
- (i32.add (local.get $r) (i32.const 10))))
- (core instance $m (instantiate $M (with "" (instance
- (export "leaf'" (func $leaf'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "mid") (param "x" u32) (result u32)
- (canon lift (core func $m "mid'"))))
-
- (component $Root
- (import "mid" (func $mid (param "x" u32) (result u32)))
- (core func $mid' (canon lower (func $mid)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "mid'" (func $mid' (param i32) (result i32)))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "root'") (result i32) (local $r i32)
- (call $cset (i32.const 0x0badf00d))
- (local.set $r (call $mid' (i32.const 100)))
- (if (i32.ne (call $cget) (i32.const 0x0badf00d)) (then unreachable))
- (i32.add (local.get $r) (i32.const 1000))))
- (core instance $m (instantiate $M (with "" (instance
- (export "mid'" (func $mid'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "root") (result u32)
- (canon lift (core func $m "root'"))))
-
- (instance $leaf (instantiate $Leaf (with "poke" (func $poke))))
- (instance $mid (instantiate $Mid (with "leaf" (func $leaf "leaf"))))
- (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
- (export "root" (func $root "root"))
-)
- "#;
-
- let engine = engine();
- let component = Component::new(&engine, component)?;
- let mut store = Store::new(&engine, 0u32);
- let mut linker = Linker::new(&engine);
- linker
- .root()
- .func_wrap("poke", |mut cx: StoreContextMut<u32>, (): ()| {
- *cx.data_mut() += 1;
- Ok(())
- })?;
- let instance = linker.instantiate_async(&mut store, &component).await?;
- let root = instance.get_typed_func::<(), (u32,)>(&mut store, "root")?;
-
- let (result,) = root.call_async(&mut store, ()).await?;
- assert_eq!(result, 1111);
- assert_eq!(*store.data(), 1);
- Ok(())
-}
-
-#[tokio::test]
-async fn repeated_calls_have_no_state_leak() -> Result<()> {
- let component = r#"
-(component
- (import "poke" (func $poke))
-
- (component $Inner
- (import "poke" (func $poke))
- (core func $poke' (canon lower (func $poke)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "poke" (func $poke'))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "f'") (param i32) (result i32)
- (if (i32.ne (call $cget) (i32.const 0)) (then unreachable))
- (call $cset (local.get 0))
- (call $poke')
- (if (i32.ne (call $cget) (local.get 0)) (then unreachable))
- (i32.add (local.get 0) (i32.const 42))))
- (core instance $m (instantiate $M (with "" (instance
- (export "poke" (func $poke'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "f") (param "x" u32) (result u32)
- (canon lift (core func $m "f'"))))
-
- (component $Outer
- (import "f" (func $f (param "x" u32) (result u32)))
- (core func $f' (canon lower (func $f)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $N
- (import "" "f'" (func $f' (param i32) (result i32)))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "g'") (param i32) (result i32) (local $r i32)
- (call $cset (i32.const 0x4321))
- (local.set $r (call $f' (local.get 0)))
- (if (i32.ne (call $cget) (i32.const 0x4321)) (then unreachable))
- (local.get $r)))
- (core instance $n (instantiate $N (with "" (instance
- (export "f'" (func $f'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "g") (param "x" u32) (result u32)
- (canon lift (core func $n "g'"))))
-
- (instance $inner (instantiate $Inner (with "poke" (func $poke))))
- (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
- (export "g" (func $outer "g"))
-)
- "#;
-
- let engine = engine();
- let component = Component::new(&engine, component)?;
- let mut store = Store::new(&engine, 0u32);
- let mut linker = Linker::new(&engine);
- linker
- .root()
- .func_wrap("poke", |mut cx: StoreContextMut<u32>, (): ()| {
- *cx.data_mut() += 1;
- Ok(())
- })?;
- let instance = linker.instantiate_async(&mut store, &component).await?;
- let g = instance.get_typed_func::<(u32,), (u32,)>(&mut store, "g")?;
-
- for x in [7u32, 100, 0x10000, 1] {
- let (result,) = g.call_async(&mut store, (x,)).await?;
- assert_eq!(result, x + 42);
- }
- assert_eq!(
- *store.data(),
- 4,
- "host import called once per top-level call"
- );
- Ok(())
-}
-
-#[tokio::test]
-async fn transparent_frame_nested_inside_opaque_one() -> Result<()> {
- let component = r#"
-(component
- (import "poke" (func $poke))
-
- (component $Leaf
- (core module $M
- (func (export "leaf'") (param i32) (result i32)
- (i32.add (local.get 0) (i32.const 1))))
- (core instance $m (instantiate $M))
- (func (export "leaf") (param "x" u32) (result u32)
- (canon lift (core func $m "leaf'"))))
-
- (component $Mid
- (import "leaf" (func $leaf (param "x" u32) (result u32)))
- (import "poke" (func $poke))
- (core func $leaf' (canon lower (func $leaf)))
- (core func $poke' (canon lower (func $poke)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "leaf'" (func $leaf' (param i32) (result i32)))
- (import "" "poke" (func $poke'))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "mid'") (param i32) (result i32) (local $r i32)
- ;; Freshly entered deferred thread: context starts zeroed.
- (if (i32.ne (call $cget) (i32.const 0)) (then unreachable))
- (call $cset (i32.const 0x0d00d100))
- ;; Call through the transparent adapter, which pushes no frame.
- (local.set $r (call $leaf' (local.get 0)))
- ;; Only now force the deferred thread, after the omitted frame has
- ;; come and gone.
- (call $poke')
- (if (i32.ne (call $cget) (i32.const 0x0d00d100)) (then unreachable))
- (i32.add (local.get $r) (i32.const 10))))
- (core instance $m (instantiate $M (with "" (instance
- (export "leaf'" (func $leaf'))
- (export "poke" (func $poke'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "mid") (param "x" u32) (result u32)
- (canon lift (core func $m "mid'"))))
-
- (component $Root
- (import "mid" (func $mid (param "x" u32) (result u32)))
- (core func $mid' (canon lower (func $mid)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "mid'" (func $mid' (param i32) (result i32)))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "root'") (result i32) (local $r i32)
- (call $cset (i32.const 0x0badf00d))
- (local.set $r (call $mid' (i32.const 100)))
- (if (i32.ne (call $cget) (i32.const 0x0badf00d)) (then unreachable))
- (i32.add (local.get $r) (i32.const 1000))))
- (core instance $m (instantiate $M (with "" (instance
- (export "mid'" (func $mid'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "root") (result u32)
- (canon lift (core func $m "root'"))))
-
- (instance $leaf (instantiate $Leaf))
- (instance $mid (instantiate $Mid
- (with "leaf" (func $leaf "leaf"))
- (with "poke" (func $poke))))
- (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
- (export "root" (func $root "root"))
-)
- "#;
-
- let engine = engine();
- let component = Component::new(&engine, component)?;
- let mut store = Store::new(&engine, 0u32);
- let mut linker = Linker::new(&engine);
- linker
- .root()
- .func_wrap("poke", |mut cx: StoreContextMut<u32>, (): ()| {
- *cx.data_mut() += 1;
- Ok(())
- })?;
- let instance = linker.instantiate_async(&mut store, &component).await?;
- let root = instance.get_typed_func::<(), (u32,)>(&mut store, "root")?;
-
- // Call more than once so that any state the omitted frame failed to clean
- // up would be visible on a later call.
- for i in 1..=3 {
- let (result,) = root.call_async(&mut store, ()).await?;
- assert_eq!(result, 1111);
- assert_eq!(*store.data(), i);
- }
- Ok(())
-}
-
-#[tokio::test]
-async fn opaque_frame_nested_inside_transparent_one() -> Result<()> {
- let component = r#"
-(component
- (import "poke" (func $poke))
-
- (component $Leaf
- (import "poke" (func $poke))
- (core func $poke' (canon lower (func $poke)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "poke" (func $poke'))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "leaf'") (param i32) (result i32)
- ;; The maintained frame zeroed the context on the way in.
- (if (i32.ne (call $cget) (i32.const 0)) (then unreachable))
- (call $cset (i32.const 0x0feed000))
- ;; Force the deferred thread from the innermost frame.
- (call $poke')
- (if (i32.ne (call $cget) (i32.const 0x0feed000)) (then unreachable))
- (i32.add (local.get 0) (i32.const 1))))
- (core instance $m (instantiate $M (with "" (instance
- (export "poke" (func $poke'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "leaf") (param "x" u32) (result u32)
- (canon lift (core func $m "leaf'"))))
-
- ;; Nothing in here can observe or mutate thread state, so the adapter that
- ;; lifts `mid` is thread-transparent.
- (component $Mid
- (import "leaf" (func $leaf (param "x" u32) (result u32)))
- (core func $leaf' (canon lower (func $leaf)))
- (core module $M
- (import "" "leaf'" (func $leaf' (param i32) (result i32)))
- (func (export "mid'") (param i32) (result i32)
- (i32.add (call $leaf' (local.get 0)) (i32.const 10))))
- (core instance $m (instantiate $M (with "" (instance
- (export "leaf'" (func $leaf'))))))
- (func (export "mid") (param "x" u32) (result u32)
- (canon lift (core func $m "mid'"))))
-
- (component $Root
- (import "mid" (func $mid (param "x" u32) (result u32)))
- (core func $mid' (canon lower (func $mid)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $M
- (import "" "mid'" (func $mid' (param i32) (result i32)))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func (export "root'") (result i32) (local $r i32)
- (call $cset (i32.const 0x0badf00d))
- (local.set $r (call $mid' (i32.const 100)))
- ;; Neither `$Mid` nor `$Leaf` disturbed our slot.
- (if (i32.ne (call $cget) (i32.const 0x0badf00d)) (then unreachable))
- (i32.add (local.get $r) (i32.const 1000))))
- (core instance $m (instantiate $M (with "" (instance
- (export "mid'" (func $mid'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "root") (result u32)
- (canon lift (core func $m "root'"))))
-
- (instance $leaf (instantiate $Leaf (with "poke" (func $poke))))
- (instance $mid (instantiate $Mid (with "leaf" (func $leaf "leaf"))))
- (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
- (export "root" (func $root "root"))
-)
- "#;
-
- let engine = engine();
- let component = Component::new(&engine, component)?;
- let mut store = Store::new(&engine, 0u32);
- let mut linker = Linker::new(&engine);
- linker
- .root()
- .func_wrap("poke", |mut cx: StoreContextMut<u32>, (): ()| {
- *cx.data_mut() += 1;
- Ok(())
- })?;
- let instance = linker.instantiate_async(&mut store, &component).await?;
- let root = instance.get_typed_func::<(), (u32,)>(&mut store, "root")?;
-
- for i in 1..=3 {
- let (result,) = root.call_async(&mut store, ()).await?;
- assert_eq!(result, 1111);
- assert_eq!(*store.data(), i);
- }
- Ok(())
-}
-
#[tokio::test]
async fn trap_unwinds_through_transparent_frames() -> Result<()> {
let component = r#"
@@ -737,167 +290,3 @@ async fn trap_then_instantiate_uses_freed_deferred_thread() -> Result<()> {
}
Ok(())
}
-
-#[tokio::test]
-async fn shared_table_does_not_leak_context() -> Result<()> {
- // The `$Victim`/`$Evil` pair, parameterized over which core module owns the
- // shared table and what else `$Victim` imports.
- fn component(inner: &str) -> String {
- format!(
- r#"
-(component
- (component $Inner {inner})
-
- (component $Outer
- (import "f" (func $f (result u32)))
- (core func $f' (canon lower (func $f)))
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
- (core module $N
- (import "" "f" (func $f' (result i32)))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- ;; What the callee saw in *our* slot.
- (func (export "g") (result i32)
- (call $cset (i32.const 0x1234))
- (call $f'))
- ;; Our slot after the call returns.
- (func (export "h") (result i32)
- (call $cset (i32.const 0x1234))
- (drop (call $f'))
- (call $cget)))
- (core instance $n (instantiate $N (with "" (instance
- (export "f" (func $f'))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
- (func (export "g") (result u32) (canon lift (core func $n "g")))
- (func (export "h") (result u32) (canon lift (core func $n "h"))))
-
- (instance $inner (instantiate $Inner))
- (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
- (export "g" (func $outer "g"))
- (export "h" (func $outer "h"))
-)
- "#
- )
- }
-
- // `$Evil`'s body: read the caller's slot, clobber it, return what was read.
- // The element segment writes into the *imported* table, so this happens
- // during instantiation with no start function or host call involved.
- const EVIL: &str = r#"
- (core module $Evil
- (import "" "t" (table 1 funcref))
- (import "" "cget" (func $cget (result i32)))
- (import "" "cset" (func $cset (param i32)))
- (func $leak (result i32) (local $t i32)
- (local.set $t (call $cget))
- (call $cset (i32.const 0x9999))
- (local.get $t))
- (elem (table 0) (i32.const 0) func $leak))
- "#;
-
- let cases = [
- // The table is defined by a third module that imports nothing at all.
- component(&format!(
- r#"
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
-
- (core module $Shared (table (export "t") 1 funcref))
- (core instance $shared (instantiate $Shared))
-
- {EVIL}
- (core instance $evil (instantiate $Evil (with "" (instance
- (export "t" (table $shared "t"))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
-
- (core module $Victim
- (import "" "t" (table 1 funcref))
- (type $sig (func (result i32)))
- (func (export "f") (result i32)
- (call_indirect (type $sig) (i32.const 0))))
- (core instance $victim (instantiate $Victim (with "" (instance
- (export "t" (table $shared "t"))))))
-
- (func (export "f") (result u32) (canon lift (core func $victim "f")))
- "#
- )),
- // Same thing with only two core modules: the table is defined and
- // exported by `$Victim` itself, which still imports nothing.
- component(&format!(
- r#"
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
-
- (core module $Victim
- (table (export "t") 1 funcref)
- (type $sig (func (result i32)))
- (func (export "f") (result i32)
- (call_indirect (type $sig) (i32.const 0))))
- (core instance $victim (instantiate $Victim))
-
- {EVIL}
- (core instance $evil (instantiate $Evil (with "" (instance
- (export "t" (table $victim "t"))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
-
- (func (export "f") (result u32) (canon lift (core func $victim "f")))
- "#
- )),
- // Control: `$Victim` also directly imports `context.get`, which even a
- // core-instance-granularity analysis would have seen.
- component(&format!(
- r#"
- (core func $cget (canon context.get i32 0))
- (core func $cset (canon context.set i32 0))
-
- (core module $Shared (table (export "t") 1 funcref))
- (core instance $shared (instantiate $Shared))
-
- {EVIL}
- (core instance $evil (instantiate $Evil (with "" (instance
- (export "t" (table $shared "t"))
- (export "cget" (func $cget))
- (export "cset" (func $cset))))))
-
- (core module $Victim
- (import "" "t" (table 1 funcref))
- (import "" "cget" (func (result i32)))
- (type $sig (func (result i32)))
- (func (export "f") (result i32)
- (call_indirect (type $sig) (i32.const 0))))
- (core instance $victim (instantiate $Victim (with "" (instance
- (export "t" (table $shared "t"))
- (export "cget" (func $cget))))))
-
- (func (export "f") (result u32) (canon lift (core func $victim "f")))
- "#
- )),
- ];
-
- let engine = engine();
- for (i, wat) in cases.iter().enumerate() {
- let component = Component::new(&engine, wat)?;
- let mut store = Store::new(&engine, ());
- let linker = Linker::new(&engine);
- let instance = linker.instantiate_async(&mut store, &component).await?;
-
- let g = instance.get_typed_func::<(), (u32,)>(&mut store, "g")?;
- let (leaked,) = g.call_async(&mut store, ()).await?;
- assert_eq!(
- leaked, 0,
- "case {i}: callee read the caller's private context slot: {leaked:#x}",
- );
-
- let h = instance.get_typed_func::<(), (u32,)>(&mut store, "h")?;
- let (after,) = h.call_async(&mut store, ()).await?;
- assert_eq!(
- after, 0x1234,
- "case {i}: callee clobbered the caller's context slot: {after:#x}",
- );
- }
- Ok(())
-}
diff --git a/tests/misc_testsuite/component-model/async/callback-yield-then-exit.wast b/tests/misc_testsuite/component-model/async/callback-yield-then-exit.wast
index 528aa22bfb..4240e2f521 100644
--- a/tests/misc_testsuite/component-model/async/callback-yield-then-exit.wast
+++ b/tests/misc_testsuite/component-model/async/callback-yield-then-exit.wast
@@ -48,3 +48,67 @@
)
(assert_return (invoke "run"))
+
+;; A trap in an early-returning task's callback, while another task's start is
+;; queued behind it.
+;; - `a` calls `task.return`, then makes a future ready and WAITs on a set
+;; holding it, which queues a callback invocation in the event loop.
+;; - `b`'s start is queued behind that callback, which traps, and the trap
+;; surfaces from `b`'s call.
+;; Dropping a typed `call_async` future in this state is tested in Rust by
+;; `drop_call_async_with_queued_start`.
+(component
+ (type $f (future))
+ (core func $future.new (canon future.new $f))
+ (core func $future.read (canon future.read $f async))
+ (core func $future.write (canon future.write $f async))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core func $waitable.join (canon waitable.join))
+ (core func $task.return (canon task.return))
+
+ (core module $m
+ (import "" "future.new" (func $future.new (result i64)))
+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))
+ (import "" "future.write"
+ (func $future.write (param i32 i32) (result i32)))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))
+ (import "" "task.return" (func $task.return))
+
+ (func (export "a") (result i32)
+ (local $t i64) (local $r i32) (local $w i32) (local $s i32)
+ (call $task.return)
+ (local.set $t (call $future.new))
+ (local.set $r (i32.wrap_i64 (local.get $t)))
+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $t) (i64.const 32))))
+ (drop (call $future.read (local.get $r) (i32.const 0)))
+ (local.set $s (call $waitable-set.new))
+ (call $waitable.join (local.get $r) (local.get $s))
+ (drop (call $future.write (local.get $w) (i32.const 0)))
+ ;; WAIT on a set which already has a ready event.
+ (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (local.get $s) (i32.const 4)))
+ )
+ (func (export "callback") (param i32 i32 i32) (result i32) unreachable)
+ (func (export "b") (result i32)
+ (call $task.return)
+ (i32.const 0 (; EXIT ;))
+ )
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "future.new" (func $future.new))
+ (export "future.read" (func $future.read))
+ (export "future.write" (func $future.write))
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "waitable.join" (func $waitable.join))
+ (export "task.return" (func $task.return))
+ ))
+ ))
+
+ (func (export "a") async
+ (canon lift (core func $i "a") async (callback (core func $i "callback"))))
+ (func (export "b") async
+ (canon lift (core func $i "b") async (callback (core func $i "callback"))))
+)
+(assert_return (invoke "a"))
+(assert_trap (invoke "b") "unreachable")
diff --git a/tests/misc_testsuite/component-model/async/sync-call-context.wast b/tests/misc_testsuite/component-model/async/sync-call-context.wast
index 79f7d50c72..3ca8d855a1 100644
--- a/tests/misc_testsuite/component-model/async/sync-call-context.wast
+++ b/tests/misc_testsuite/component-model/async/sync-call-context.wast
@@ -542,3 +542,690 @@
(export "root" (func $root "root"))
)
(assert_return (invoke "root") (u32.const 1111))
+
+;; The tests below force the slow path with a guest-to-host call instead of
+;; backpressure. `host-return-two` is a synchronous host import, so calling it
+;; forces the deferred thread.
+
+;; Single guest-to-guest sync call, forced by a host call.
+;; - $Outer sets its context and calls $Inner.
+;; - $Inner sees a zeroed context, sets its own, then calls the host.
+;; - Both contexts survive the forced slow path.
+(component
+ (import "host-return-two" (func $host (result u32)))
+
+ (component $Inner
+ (import "host" (func $host (result u32)))
+ (core func $host (canon lower (func $host)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "host" (func $host (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "f") (param i32) (result i32)
+ (if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
+ (call $context.set (i32.const 0x5678))
+ (drop (call $host))
+ (if (i32.ne (call $context.get) (i32.const 0x5678))
+ (then unreachable))
+ (i32.add (local.get 0) (i32.const 42))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "host" (func $host))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "f") (param "x" u32) (result u32)
+ (canon lift (core func $m "f")))
+ )
+
+ (component $Outer
+ (import "f" (func $f (param "x" u32) (result u32)))
+ (core func $f (canon lower (func $f)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "f" (func $f (param i32) (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "g") (result i32) (local $r i32)
+ (call $context.set (i32.const 0x1234))
+ (local.set $r (call $f (i32.const 1234)))
+ (if (i32.ne (call $context.get) (i32.const 0x1234))
+ (then unreachable))
+ (local.get $r)
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "f" (func $f))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "g") (result u32)
+ (canon lift (core func $m "g")))
+ )
+
+ (instance $inner (instantiate $Inner (with "host" (func $host))))
+ (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
+ (export "g" (func $outer "g"))
+)
+(assert_return (invoke "g") (u32.const 1276))
+
+;; Repeated calls, each forced by a host call.
+;; - $Inner stores its argument in its context before calling the host.
+;; - A fresh entry must see a zeroed context every time.
+(component
+ (import "host-return-two" (func $host (result u32)))
+
+ (component $Inner
+ (import "host" (func $host (result u32)))
+ (core func $host (canon lower (func $host)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "host" (func $host (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "f") (param i32) (result i32)
+ (if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
+ (call $context.set (local.get 0))
+ (drop (call $host))
+ (if (i32.ne (call $context.get) (local.get 0)) (then unreachable))
+ (i32.add (local.get 0) (i32.const 42))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "host" (func $host))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "f") (param "x" u32) (result u32)
+ (canon lift (core func $m "f")))
+ )
+
+ (component $Outer
+ (import "f" (func $f (param "x" u32) (result u32)))
+ (core func $f (canon lower (func $f)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "f" (func $f (param i32) (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "g") (param i32) (result i32) (local $r i32)
+ (call $context.set (i32.const 0x4321))
+ (local.set $r (call $f (local.get 0)))
+ (if (i32.ne (call $context.get) (i32.const 0x4321))
+ (then unreachable))
+ (local.get $r)
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "f" (func $f))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "g") (param "x" u32) (result u32)
+ (canon lift (core func $m "g")))
+ )
+
+ (instance $inner (instantiate $Inner (with "host" (func $host))))
+ (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
+ (export "g" (func $outer "g"))
+)
+(assert_return (invoke "g" (u32.const 7)) (u32.const 49))
+(assert_return (invoke "g" (u32.const 100)) (u32.const 142))
+(assert_return (invoke "g" (u32.const 0x10000)) (u32.const 0x1002a))
+(assert_return (invoke "g" (u32.const 1)) (u32.const 43))
+
+;; Nested $Root -> $Mid -> $Leaf chain, forced by a host call in $Leaf.
+;; - Each level sets its own context before calling the next.
+;; - Every level's context survives the force.
+(component
+ (import "host-return-two" (func $host (result u32)))
+
+ (component $Leaf
+ (import "host" (func $host (result u32)))
+ (core func $host (canon lower (func $host)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "host" (func $host (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "leaf") (param i32) (result i32)
+ (if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
+ (call $context.set (i32.const 0x0c0ffee0))
+ (drop (call $host))
+ (if (i32.ne (call $context.get) (i32.const 0x0c0ffee0))
+ (then unreachable))
+ (i32.add (local.get 0) (i32.const 1))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "host" (func $host))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "leaf") (param "x" u32) (result u32)
+ (canon lift (core func $m "leaf")))
+ )
+
+ (component $Mid
+ (import "leaf" (func $leaf (param "x" u32) (result u32)))
+ (core func $leaf (canon lower (func $leaf)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "leaf" (func $leaf (param i32) (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "mid") (param i32) (result i32) (local $r i32)
+ (if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
+ (call $context.set (i32.const 0x0d00d100))
+ (local.set $r (call $leaf (local.get 0)))
+ (if (i32.ne (call $context.get) (i32.const 0x0d00d100))
+ (then unreachable))
+ (i32.add (local.get $r) (i32.const 10))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "leaf" (func $leaf))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "mid") (param "x" u32) (result u32)
+ (canon lift (core func $m "mid")))
+ )
+
+ (component $Root
+ (import "mid" (func $mid (param "x" u32) (result u32)))
+ (core func $mid (canon lower (func $mid)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "mid" (func $mid (param i32) (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "root") (result i32) (local $r i32)
+ (call $context.set (i32.const 0x0badf00d))
+ (local.set $r (call $mid (i32.const 100)))
+ (if (i32.ne (call $context.get) (i32.const 0x0badf00d))
+ (then unreachable))
+ (i32.add (local.get $r) (i32.const 1000))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "mid" (func $mid))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "root") (result u32)
+ (canon lift (core func $m "root")))
+ )
+
+ (instance $leaf (instantiate $Leaf (with "host" (func $host))))
+ (instance $mid (instantiate $Mid (with "leaf" (func $leaf "leaf"))))
+ (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
+ (export "root" (func $root "root"))
+)
+(assert_return (invoke "root") (u32.const 1111))
+
+;; A thread-transparent frame nested inside an opaque one.
+;; - $Leaf touches no thread state, so the $Mid -> $Leaf adapter pushes no
+;; frame.
+;; - $Mid calls $Leaf, and only then calls the host to force its thread.
+;; - Repeated calls would expose any state the omitted frame failed to clean
+;; up.
+(component
+ (import "host-return-two" (func $host (result u32)))
+
+ (component $Leaf
+ (core module $M
+ (func (export "leaf") (param i32) (result i32)
+ (i32.add (local.get 0) (i32.const 1))
+ )
+ )
+ (core instance $m (instantiate $M))
+ (func (export "leaf") (param "x" u32) (result u32)
+ (canon lift (core func $m "leaf")))
+ )
+
+ (component $Mid
+ (import "leaf" (func $leaf (param "x" u32) (result u32)))
+ (import "host" (func $host (result u32)))
+ (core func $leaf (canon lower (func $leaf)))
+ (core func $host (canon lower (func $host)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "leaf" (func $leaf (param i32) (result i32)))
+ (import "" "host" (func $host (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "mid") (param i32) (result i32) (local $r i32)
+ (if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
+ (call $context.set (i32.const 0x0d00d100))
+ (local.set $r (call $leaf (local.get 0)))
+ (drop (call $host))
+ (if (i32.ne (call $context.get) (i32.const 0x0d00d100))
+ (then unreachable))
+ (i32.add (local.get $r) (i32.const 10))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "leaf" (func $leaf))
+ (export "host" (func $host))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "mid") (param "x" u32) (result u32)
+ (canon lift (core func $m "mid")))
+ )
+
+ (component $Root
+ (import "mid" (func $mid (param "x" u32) (result u32)))
+ (core func $mid (canon lower (func $mid)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "mid" (func $mid (param i32) (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "root") (result i32) (local $r i32)
+ (call $context.set (i32.const 0x0badf00d))
+ (local.set $r (call $mid (i32.const 100)))
+ (if (i32.ne (call $context.get) (i32.const 0x0badf00d))
+ (then unreachable))
+ (i32.add (local.get $r) (i32.const 1000))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "mid" (func $mid))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "root") (result u32)
+ (canon lift (core func $m "root")))
+ )
+
+ (instance $leaf (instantiate $Leaf))
+ (instance $mid (instantiate $Mid
+ (with "leaf" (func $leaf "leaf"))
+ (with "host" (func $host))
+ ))
+ (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
+ (export "root" (func $root "root"))
+)
+(assert_return (invoke "root") (u32.const 1111))
+(assert_return (invoke "root") (u32.const 1111))
+(assert_return (invoke "root") (u32.const 1111))
+
+;; An opaque frame nested inside a thread-transparent one.
+;; - $Mid touches no thread state, so the $Root -> $Mid adapter is
+;; thread-transparent.
+;; - $Leaf sees a zeroed context, sets its own, then calls the host.
+;; - Neither $Mid nor $Leaf disturbs $Root's context.
+(component
+ (import "host-return-two" (func $host (result u32)))
+
+ (component $Leaf
+ (import "host" (func $host (result u32)))
+ (core func $host (canon lower (func $host)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "host" (func $host (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "leaf") (param i32) (result i32)
+ (if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
+ (call $context.set (i32.const 0x0feed000))
+ (drop (call $host))
+ (if (i32.ne (call $context.get) (i32.const 0x0feed000))
+ (then unreachable))
+ (i32.add (local.get 0) (i32.const 1))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "host" (func $host))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "leaf") (param "x" u32) (result u32)
+ (canon lift (core func $m "leaf")))
+ )
+
+ (component $Mid
+ (import "leaf" (func $leaf (param "x" u32) (result u32)))
+ (core func $leaf (canon lower (func $leaf)))
+ (core module $M
+ (import "" "leaf" (func $leaf (param i32) (result i32)))
+ (func (export "mid") (param i32) (result i32)
+ (i32.add (call $leaf (local.get 0)) (i32.const 10))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "leaf" (func $leaf))
+ ))
+ ))
+ (func (export "mid") (param "x" u32) (result u32)
+ (canon lift (core func $m "mid")))
+ )
+
+ (component $Root
+ (import "mid" (func $mid (param "x" u32) (result u32)))
+ (core func $mid (canon lower (func $mid)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "mid" (func $mid (param i32) (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func (export "root") (result i32) (local $r i32)
+ (call $context.set (i32.const 0x0badf00d))
+ (local.set $r (call $mid (i32.const 100)))
+ (if (i32.ne (call $context.get) (i32.const 0x0badf00d))
+ (then unreachable))
+ (i32.add (local.get $r) (i32.const 1000))
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "mid" (func $mid))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "root") (result u32)
+ (canon lift (core func $m "root")))
+ )
+
+ (instance $leaf (instantiate $Leaf (with "host" (func $host))))
+ (instance $mid (instantiate $Mid (with "leaf" (func $leaf "leaf"))))
+ (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
+ (export "root" (func $root "root"))
+)
+(assert_return (invoke "root") (u32.const 1111))
+(assert_return (invoke "root") (u32.const 1111))
+(assert_return (invoke "root") (u32.const 1111))
+
+;; A table shared between core instances must not let a callee reach the
+;; caller's context through a thread-transparent adapter.
+;; - $Inner's lifted `f` calls through a table that $Evil filled, during
+;; instantiation, with a function that uses `context.{get,set}`.
+;; - $Outer sets its context, then calls `f`.
+;; - The callee must see a zeroed context, and $Outer's must be intact.
+
+;; The table is defined by a third module that imports nothing.
+(component
+ (component $Inner
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+
+ (core module $Shared (table (export "t") 1 funcref))
+ (core instance $shared (instantiate $Shared))
+
+ ;; Reads the caller's slot, clobbers it, and returns what it read.
+ (core module $Evil
+ (import "" "t" (table 1 funcref))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func $leak (result i32) (local $t i32)
+ (local.set $t (call $context.get))
+ (call $context.set (i32.const 0x9999))
+ (local.get $t)
+ )
+ (elem (table 0) (i32.const 0) func $leak)
+ )
+ (core instance $evil (instantiate $Evil
+ (with "" (instance
+ (export "t" (table $shared "t"))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+
+ (core module $Victim
+ (import "" "t" (table 1 funcref))
+ (type $sig (func (result i32)))
+ (func (export "f") (result i32)
+ (call_indirect (type $sig) (i32.const 0))
+ )
+ )
+ (core instance $victim (instantiate $Victim
+ (with "" (instance
+ (export "t" (table $shared "t"))
+ ))
+ ))
+
+ (func (export "f") (result u32) (canon lift (core func $victim "f")))
+ )
+
+ (component $Outer
+ (import "f" (func $f (result u32)))
+ (core func $f (canon lower (func $f)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "f" (func $f (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ ;; Returns what the callee saw in our slot.
+ (func (export "g") (result i32)
+ (call $context.set (i32.const 0x1234))
+ (call $f)
+ )
+ ;; Returns our slot after the call.
+ (func (export "h") (result i32)
+ (call $context.set (i32.const 0x1234))
+ (drop (call $f))
+ (call $context.get)
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "f" (func $f))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "g") (result u32) (canon lift (core func $m "g")))
+ (func (export "h") (result u32) (canon lift (core func $m "h")))
+ )
+
+ (instance $inner (instantiate $Inner))
+ (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
+ (export "g" (func $outer "g"))
+ (export "h" (func $outer "h"))
+)
+(assert_return (invoke "g") (u32.const 0))
+(assert_return (invoke "h") (u32.const 0x1234))
+
+;; The table is defined and exported by $Victim, which imports nothing.
+(component
+ (component $Inner
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+
+ (core module $Victim
+ (table (export "t") 1 funcref)
+ (type $sig (func (result i32)))
+ (func (export "f") (result i32)
+ (call_indirect (type $sig) (i32.const 0))
+ )
+ )
+ (core instance $victim (instantiate $Victim))
+
+ ;; Reads the caller's slot, clobbers it, and returns what it read.
+ (core module $Evil
+ (import "" "t" (table 1 funcref))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func $leak (result i32) (local $t i32)
+ (local.set $t (call $context.get))
+ (call $context.set (i32.const 0x9999))
+ (local.get $t)
+ )
+ (elem (table 0) (i32.const 0) func $leak)
+ )
+ (core instance $evil (instantiate $Evil
+ (with "" (instance
+ (export "t" (table $victim "t"))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+
+ (func (export "f") (result u32) (canon lift (core func $victim "f")))
+ )
+
+ (component $Outer
+ (import "f" (func $f (result u32)))
+ (core func $f (canon lower (func $f)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "f" (func $f (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ ;; Returns what the callee saw in our slot.
+ (func (export "g") (result i32)
+ (call $context.set (i32.const 0x1234))
+ (call $f)
+ )
+ ;; Returns our slot after the call.
+ (func (export "h") (result i32)
+ (call $context.set (i32.const 0x1234))
+ (drop (call $f))
+ (call $context.get)
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "f" (func $f))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "g") (result u32) (canon lift (core func $m "g")))
+ (func (export "h") (result u32) (canon lift (core func $m "h")))
+ )
+
+ (instance $inner (instantiate $Inner))
+ (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
+ (export "g" (func $outer "g"))
+ (export "h" (func $outer "h"))
+)
+(assert_return (invoke "g") (u32.const 0))
+(assert_return (invoke "h") (u32.const 0x1234))
+
+;; Control: $Victim also imports `context.get` directly.
+(component
+ (component $Inner
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+
+ (core module $Shared (table (export "t") 1 funcref))
+ (core instance $shared (instantiate $Shared))
+
+ ;; Reads the caller's slot, clobbers it, and returns what it read.
+ (core module $Evil
+ (import "" "t" (table 1 funcref))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (func $leak (result i32) (local $t i32)
+ (local.set $t (call $context.get))
+ (call $context.set (i32.const 0x9999))
+ (local.get $t)
+ )
+ (elem (table 0) (i32.const 0) func $leak)
+ )
+ (core instance $evil (instantiate $Evil
+ (with "" (instance
+ (export "t" (table $shared "t"))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+
+ (core module $Victim
+ (import "" "t" (table 1 funcref))
+ (import "" "context.get" (func (result i32)))
+ (type $sig (func (result i32)))
+ (func (export "f") (result i32)
+ (call_indirect (type $sig) (i32.const 0))
+ )
+ )
+ (core instance $victim (instantiate $Victim
+ (with "" (instance
+ (export "t" (table $shared "t"))
+ (export "context.get" (func $context.get))
+ ))
+ ))
+
+ (func (export "f") (result u32) (canon lift (core func $victim "f")))
+ )
+
+ (component $Outer
+ (import "f" (func $f (result u32)))
+ (core func $f (canon lower (func $f)))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+ (core module $M
+ (import "" "f" (func $f (result i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ ;; Returns what the callee saw in our slot.
+ (func (export "g") (result i32)
+ (call $context.set (i32.const 0x1234))
+ (call $f)
+ )
+ ;; Returns our slot after the call.
+ (func (export "h") (result i32)
+ (call $context.set (i32.const 0x1234))
+ (drop (call $f))
+ (call $context.get)
+ )
+ )
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "f" (func $f))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))
+ ))
+ (func (export "g") (result u32) (canon lift (core func $m "g")))
+ (func (export "h") (result u32) (canon lift (core func $m "h")))
+ )
+
+ (instance $inner (instantiate $Inner))
+ (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
+ (export "g" (func $outer "g"))
+ (export "h" (func $outer "h"))
+)
+(assert_return (invoke "g") (u32.const 0))
+(assert_return (invoke "h") (u32.const 0x1234))
diff --git a/tests/misc_testsuite/component-model/import.wast b/tests/misc_testsuite/component-model/import.wast
index 20688efe9b..9ae0ef57a3 100644
--- a/tests/misc_testsuite/component-model/import.wast
+++ b/tests/misc_testsuite/component-model/import.wast
@@ -18,3 +18,77 @@
(export "x" (instance))
))
)
+
+;; Lowering imports, with and without memory and realloc options, compiles.
+(component definition
+ (import "a" (func $f))
+ (core func (canon lower (func $f)))
+)
+(component definition
+ (import "a" (func $f (param "a" string)))
+ (core module $libc
+ (memory (export "memory") 1)
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ unreachable)
+ )
+ (core instance $libc (instantiate $libc))
+ (core func (canon lower (func $f)
+ (memory (core memory $libc "memory"))
+ (realloc (core func $libc "realloc"))))
+)
+(component definition
+ (core module $libc
+ (memory (export "memory") 1)
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ unreachable)
+ )
+
+ (import "f1" (func $f1 (param "a" string) (result string)))
+ (core instance $libc (instantiate $libc))
+ (core func (canon lower (func $f1)
+ (memory (core memory $libc "memory"))
+ (realloc (core func $libc "realloc"))))
+
+ (import "f2" (func $f2 (param "a" u32) (result (list u8))))
+ (core instance $libc2 (instantiate $libc))
+ (core func (canon lower (func $f2)
+ (memory (core memory $libc2 "memory"))
+ (realloc (core func $libc2 "realloc"))))
+
+ (core func (canon lower (func $f1)
+ (memory (core memory $libc2 "memory"))
+ (realloc (core func $libc2 "realloc"))))
+ (core func (canon lower (func $f2)
+ (memory (core memory $libc "memory"))
+ (realloc (core func $libc "realloc"))))
+)
+(component definition
+ (import "log" (func $log (param "a" string)))
+ (core module $libc
+ (memory (export "memory") 1)
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ unreachable)
+ )
+ (core instance $libc (instantiate $libc))
+ (core func $log_lower (canon lower (func $log)
+ (memory (core memory $libc "memory"))
+ (realloc (core func $libc "realloc"))))
+
+ (core module $logger
+ (import "host" "log" (func $log (param i32 i32)))
+ (import "libc" "memory" (memory 1))
+
+ (func (export "call")
+ i32.const 0
+ i32.const 0
+ call $log)
+ )
+ (core instance $logger (instantiate $logger
+ (with "host" (instance (export "log" (func $log_lower))))
+ (with "libc" (instance $libc))
+ ))
+
+ (func (export "call")
+ (canon lift (core func $logger "call"))
+ )
+)
diff --git a/tests/misc_testsuite/component-model/post-return.wast b/tests/misc_testsuite/component-model/post-return.wast
new file mode 100644
index 0000000000..33bf2e68de
--- /dev/null
+++ b/tests/misc_testsuite/component-model/post-return.wast
@@ -0,0 +1,74 @@
+;; Each core result type is passed to `post-return` unchanged, which traps if
+;; it sees anything else.
+(component
+ (core module $m
+ (func (export "i32") (result i32) i32.const 1)
+ (func (export "i64") (result i64) i64.const 2)
+ (func (export "f32") (result f32) f32.const 3)
+ (func (export "f64") (result f64) f64.const 4)
+
+ (func (export "post-i32") (param i32)
+ (if (i32.ne (local.get 0) (i32.const 1)) (then unreachable)))
+ (func (export "post-i64") (param i64)
+ (if (i64.ne (local.get 0) (i64.const 2)) (then unreachable)))
+ (func (export "post-f32") (param f32)
+ (if (f32.ne (local.get 0) (f32.const 3)) (then unreachable)))
+ (func (export "post-f64") (param f64)
+ (if (f64.ne (local.get 0) (f64.const 4)) (then unreachable)))
+ )
+ (core instance $i (instantiate $m))
+
+ (func (export "i32") (result u32)
+ (canon lift (core func $i "i32") (post-return (core func $i "post-i32"))))
+ (func (export "i64") (result u64)
+ (canon lift (core func $i "i64") (post-return (core func $i "post-i64"))))
+ (func (export "f32") (result f32)
+ (canon lift (core func $i "f32") (post-return (core func $i "post-f32"))))
+ (func (export "f64") (result f64)
+ (canon lift (core func $i "f64") (post-return (core func $i "post-f64"))))
+)
+
+(assert_return (invoke "i32") (u32.const 1))
+(assert_return (invoke "i64") (u64.const 2))
+(assert_return (invoke "f32") (f32.const 3))
+(assert_return (invoke "f64") (f64.const 4))
+
+;; `post-return` receives the return pointer of a string result.
+(component
+ (core module $m
+ (memory (export "memory") 1)
+ (func (export "get") (result i32)
+ (i32.store offset=0 (i32.const 8) (i32.const 100))
+ (i32.store offset=4 (i32.const 8) (i32.const 11))
+ i32.const 8)
+ (func (export "post") (param i32)
+ (if (i32.ne (local.get 0) (i32.const 8)) (then unreachable)))
+ (data (i32.const 100) "hello world")
+ )
+ (core instance $i (instantiate $m))
+
+ (func (export "get") (result string)
+ (canon lift
+ (core func $i "get")
+ (post-return (core func $i "post"))
+ (memory (core memory $i "memory"))
+ )
+ )
+)
+
+(assert_return (invoke "get") (str.const "hello world"))
+
+;; A trap in `post-return` poisons the instance.
+(component
+ (core module $m
+ (func (export "f"))
+ (func (export "post") unreachable)
+ )
+ (core instance $i (instantiate $m))
+
+ (func (export "f")
+ (canon lift (core func $i "f") (post-return (core func $i "post"))))
+)
+
+(assert_trap (invoke "f") "unreachable")
+(assert_trap (invoke "f") "cannot enter component instance")
diff --git a/tests/misc_testsuite/component-model/resources.wast b/tests/misc_testsuite/component-model/resources.wast
index 36d05845e6..44142160b7 100644
--- a/tests/misc_testsuite/component-model/resources.wast
+++ b/tests/misc_testsuite/component-model/resources.wast
@@ -1124,3 +1124,363 @@
)
(assert_return (invoke "run"))
+
+;; A resource defined by the outer component can be dropped by the outer
+;; component and by two separate nested instances.
+(component
+ (type $t' (resource (rep i32)))
+ (export $t "t" (type $t'))
+
+ (core func $ctor (canon resource.new $t))
+ (func $ctor (param "x" u32) (result (own $t))
+ (canon lift (core func $ctor)))
+ (core func $dtor (canon resource.drop $t))
+ (func $dtor1 (param "x" (own $t)) (canon lift (core func $dtor)))
+
+ (component $C
+ (import "t" (type $t (sub resource)))
+ (core func $dtor (canon resource.drop $t))
+ (func (export "dtor") (param "x" (own $t))
+ (canon lift (core func $dtor)))
+ )
+ (instance $i1 (instantiate $C (with "t" (type $t))))
+ (instance $i2 (instantiate $C (with "t" (type $t))))
+
+ (component $Driver
+ (import "t" (type $t (sub resource)))
+ (import "ctor" (func $ctor (param "x" u32) (result (own $t))))
+ (import "dtor1" (func $dtor1 (param "x" (own $t))))
+ (import "dtor2" (func $dtor2 (param "x" (own $t))))
+ (import "dtor3" (func $dtor3 (param "x" (own $t))))
+
+ (core func $ctor (canon lower (func $ctor)))
+ (core func $dtor1 (canon lower (func $dtor1)))
+ (core func $dtor2 (canon lower (func $dtor2)))
+ (core func $dtor3 (canon lower (func $dtor3)))
+
+ (core module $m
+ (import "" "ctor" (func $ctor (param i32) (result i32)))
+ (import "" "dtor1" (func $dtor1 (param i32)))
+ (import "" "dtor2" (func $dtor2 (param i32)))
+ (import "" "dtor3" (func $dtor3 (param i32)))
+ (func (export "run")
+ (call $dtor1 (call $ctor (i32.const 100)))
+ (call $dtor2 (call $ctor (i32.const 200)))
+ (call $dtor3 (call $ctor (i32.const 300))))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "ctor" (func $ctor))
+ (export "dtor1" (func $dtor1))
+ (export "dtor2" (func $dtor2))
+ (export "dtor3" (func $dtor3))
+ ))
+ ))
+ (func (export "run") (canon lift (core func $i "run")))
+ )
+ (instance $d (instantiate $Driver
+ (with "t" (type $t))
+ (with "ctor" (func $ctor))
+ (with "dtor1" (func $dtor1))
+ (with "dtor2" (func $i1 "dtor"))
+ (with "dtor3" (func $i2 "dtor"))
+ ))
+ (export "run" (func $d "run"))
+)
+
+(assert_return (invoke "run"))
+
+;; A borrow can be passed back to the host multiple times before it's dropped.
+(component definition $C
+ (import "host" (instance $host
+ (export "resource1" (type $r (sub resource)))
+ (export "[constructor]resource1"
+ (func (param "r" u32) (result (own $r))))
+ (export "[method]resource1.simple"
+ (func (param "self" (borrow $r)) (param "rep" u32)))
+ (export "[static]resource1.assert"
+ (func (param "r" (own $r)) (param "rep" u32)))
+ ))
+ (alias export $host "resource1" (type $r))
+ (alias export $host "[constructor]resource1" (func $ctor))
+ (alias export $host "[method]resource1.simple" (func $simple))
+ (alias export $host "[static]resource1.assert" (func $assert))
+
+ ;; `f` passes its borrow to the host twice, then drops it.
+ (component $B
+ (import "r" (type $r (sub resource)))
+ (import "simple"
+ (func $simple (param "self" (borrow $r)) (param "rep" u32)))
+
+ (core func $simple (canon lower (func $simple)))
+ (core func $resource.drop (canon resource.drop $r))
+
+ (core module $m
+ (import "" "simple" (func $simple (param i32 i32)))
+ (import "" "resource.drop" (func $resource.drop (param i32)))
+ (func (export "f") (param i32)
+ (call $simple (local.get 0) (i32.const 100))
+ (call $simple (local.get 0) (i32.const 100))
+ (call $resource.drop (local.get 0)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "simple" (func $simple))
+ (export "resource.drop" (func $resource.drop))
+ ))
+ ))
+ (func (export "f") (param "x" (borrow $r))
+ (canon lift (core func $i "f")))
+ )
+ (instance $b (instantiate $B
+ (with "r" (type $r))
+ (with "simple" (func $simple))
+ ))
+
+ ;; `f` is given a borrow of a host resource and returns it as an `own`.
+ (component $Ret
+ (import "r" (type $r (sub resource)))
+ (core module $m
+ (func (export "f") (param i32) (result i32) local.get 0)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "x" (borrow $r)) (result (own $r))
+ (canon lift (core func $i "f")))
+ )
+ (instance $ret (instantiate $Ret (with "r" (type $r))))
+
+ ;; `f` passes its borrow to a host function which takes an `own`.
+ (component $Own
+ (import "r" (type $r (sub resource)))
+ (import "take-own" (func $take-own (param "r" (own $r)) (param "rep" u32)))
+ (core func $take-own (canon lower (func $take-own)))
+ (core module $m
+ (import "" "take-own" (func $take-own (param i32 i32)))
+ (func (export "f") (param i32)
+ (call $take-own (local.get 0) (i32.const 100)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "take-own" (func $take-own))
+ ))
+ ))
+ (func (export "f") (param "x" (borrow $r))
+ (canon lift (core func $i "f")))
+ )
+ (instance $own (instantiate $Own
+ (with "r" (type $r))
+ (with "take-own" (func $assert))
+ ))
+
+ (core func $ctor (canon lower (func $ctor)))
+ (core func $b (canon lower (func $b "f")))
+ (core func $ret (canon lower (func $ret "f")))
+ (core func $own (canon lower (func $own "f")))
+ (core func $resource.drop (canon resource.drop $r))
+
+ (core module $m
+ (import "" "ctor" (func $ctor (param i32) (result i32)))
+ (import "" "b" (func $b (param i32)))
+ (import "" "ret" (func $ret (param i32) (result i32)))
+ (import "" "own" (func $own (param i32)))
+ (import "" "resource.drop" (func $resource.drop (param i32)))
+ (func (export "thread-through-borrow")
+ (local $r i32)
+ (local.set $r (call $ctor (i32.const 100)))
+ (call $b (local.get $r))
+ (call $resource.drop (local.get $r)))
+ (func (export "return-borrow-as-own")
+ (drop (call $ret (call $ctor (i32.const 100)))))
+ (func (export "pass-borrow-as-own")
+ (call $own (call $ctor (i32.const 100))))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "ctor" (func $ctor))
+ (export "b" (func $b))
+ (export "ret" (func $ret))
+ (export "own" (func $own))
+ (export "resource.drop" (func $resource.drop))
+ ))
+ ))
+ (func (export "thread-through-borrow")
+ (canon lift (core func $i "thread-through-borrow")))
+ (func (export "return-borrow-as-own")
+ (canon lift (core func $i "return-borrow-as-own")))
+ (func (export "pass-borrow-as-own")
+ (canon lift (core func $i "pass-borrow-as-own")))
+)
+
+(component instance $C $C)
+(assert_return (invoke "thread-through-borrow"))
+;; The remaining borrow is reported, not the own/borrow mismatch.
+(component instance $C $C)
+(assert_trap (invoke "return-borrow-as-own")
+ "borrow handles still remain at the end of the call")
+(component instance $C $C)
+(assert_trap (invoke "pass-borrow-as-own")
+ "cannot lift own resource from a borrow")
+
+;; An owned resource can't be dropped while it's lent to a pending async
+;; host call.
+(component
+ (import "host" (instance $host
+ (export "resource1" (type $r (sub resource)))
+ (export "[constructor]resource1"
+ (func (param "r" u32) (result (own $r))))
+ (export "[method]resource1.never-return"
+ (func async (param "self" (borrow $r))))
+ ))
+ (alias export $host "resource1" (type $r))
+ (alias export $host "[constructor]resource1" (func $ctor))
+ (alias export $host "[method]resource1.never-return" (func $never-return))
+
+ (core func $ctor (canon lower (func $ctor)))
+ (core func $never-return (canon lower (func $never-return) async))
+ (core func $resource.drop (canon resource.drop $r))
+
+ (core module $m
+ (import "" "ctor" (func $ctor (param i32) (result i32)))
+ (import "" "never-return" (func $never-return (param i32) (result i32)))
+ (import "" "resource.drop" (func $resource.drop (param i32)))
+ (func (export "f")
+ (local $r i32)
+ (local.set $r (call $ctor (i32.const 300)))
+ ;; Expect the subtask to be STARTED.
+ (if (i32.ne (i32.const 1)
+ (i32.and (call $never-return (local.get $r)) (i32.const 0xf)))
+ (then unreachable))
+ (call $resource.drop (local.get $r))
+ unreachable)
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "ctor" (func $ctor))
+ (export "never-return" (func $never-return))
+ (export "resource.drop" (func $resource.drop))
+ ))
+ ))
+ (func (export "f") async (canon lift (core func $i "f")))
+)
+
+(assert_trap (invoke "f") "cannot remove owned resource while borrowed")
+
+;; `resource.new` and `resource.rep` are lifted directly as exports, so the
+;; host calls their trampolines. `rep` takes a raw handle index here, and 42
+;; (the rep given to `new`) isn't one. A trap in `new` would poison the
+;; instance, and the `assert_trap` would then see a different message.
+(component
+ (type $r' (resource (rep i32)))
+ (export $r "r" (type $r'))
+
+ (core func $resource.new (canon resource.new $r))
+ (core func $resource.rep (canon resource.rep $r))
+
+ (func (export "new") (param "x" u32) (result (own $r))
+ (canon lift (core func $resource.new)))
+ (func (export "rep") (param "x" u32) (result u32)
+ (canon lift (core func $resource.rep)))
+)
+
+(invoke "new" (u32.const 42))
+(assert_trap (invoke "rep" (u32.const 42)) "unknown handle index 42")
+
+;; A task drops a borrow it was given while a subtask it started is blocked in
+;; a sync-lowered async host call. The borrow is tracked by the dropping task,
+;; not the blocked one.
+(component
+ (import "host" (instance $host
+ (export "resource1" (type $r (sub resource)))
+ (export "[constructor]resource1"
+ (func (param "r" u32) (result (own $r))))
+ (export "never-return" (func async))
+ ))
+ (alias export $host "resource1" (type $r))
+ (alias export $host "[constructor]resource1" (func $ctor))
+ (alias export $host "never-return" (func $never-return))
+
+ ;; `call` blocks forever in a sync-lowered call to the host.
+ (component $A
+ (import "never-return" (func $never-return async))
+ (core func $never-return (canon lower (func $never-return)))
+ (core module $m
+ (import "" "never-return" (func $never-return))
+ (func (export "call") (result i32)
+ (call $never-return)
+ i32.const 0 (; EXIT ;))
+ (func (export "callback") (param i32 i32 i32) (result i32)
+ unreachable)
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "never-return" (func $never-return))
+ ))
+ ))
+ (func (export "call") async
+ (canon lift (core func $i "call")
+ async (callback (core func $i "callback"))))
+ )
+ (instance $a (instantiate $A (with "never-return" (func $never-return))))
+
+ ;; `call` starts a subtask in `$A`, then drops its borrow and returns
+ ;; without waiting for the subtask.
+ (component $B
+ (import "r" (type $r (sub resource)))
+ (import "f" (func $f async))
+ (core func $f (canon lower (func $f) async))
+ (core func $task.return (canon task.return))
+ (core func $resource.drop (canon resource.drop $r))
+ (core module $m
+ (import "" "f" (func $f (result i32)))
+ (import "" "task.return" (func $task.return))
+ (import "" "resource.drop" (func $resource.drop (param i32)))
+ (func (export "call") (param i32) (result i32)
+ ;; Expect the subtask to be STARTED.
+ (if (i32.ne (i32.const 1) (i32.and (call $f) (i32.const 0xf)))
+ (then unreachable))
+ (call $resource.drop (local.get 0))
+ (call $task.return)
+ i32.const 0 (; EXIT ;))
+ (func (export "callback") (param i32 i32 i32) (result i32)
+ unreachable)
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "f" (func $f))
+ (export "task.return" (func $task.return))
+ (export "resource.drop" (func $resource.drop))
+ ))
+ ))
+ (func (export "call") async (param "x" (borrow $r))
+ (canon lift (core func $i "call")
+ async (callback (core func $i "callback"))))
+ )
+ (instance $b (instantiate $B
+ (with "r" (type $r))
+ (with "f" (func $a "call"))
+ ))
+
+ (core func $ctor (canon lower (func $ctor)))
+ (core func $call (canon lower (func $b "call")))
+ (core func $resource.drop (canon resource.drop $r))
+ (core module $m
+ (import "" "ctor" (func $ctor (param i32) (result i32)))
+ (import "" "call" (func $call (param i32)))
+ (import "" "resource.drop" (func $resource.drop (param i32)))
+ (func (export "run")
+ (local $r i32)
+ (local.set $r (call $ctor (i32.const 3)))
+ (call $call (local.get $r))
+ (call $resource.drop (local.get $r)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "ctor" (func $ctor))
+ (export "call" (func $call))
+ (export "resource.drop" (func $resource.drop))
+ ))
+ ))
+ (func (export "run") async (canon lift (core func $i "run")))
+)
+
+(assert_return (invoke "run"))
diff --git a/tests/misc_testsuite/component-model/strings.wast b/tests/misc_testsuite/component-model/strings.wast
index 85060d78b9..5596863aea 100644
--- a/tests/misc_testsuite/component-model/strings.wast
+++ b/tests/misc_testsuite/component-model/strings.wast
@@ -377,3 +377,593 @@
(export "f" (func $c2 "f"))
)
(assert_trap (invoke "f") "string content out-of-bounds")
+
+;; The following tests cover transcoding between each pair of string
+;; encodings. `$dst-*` components receive a string with the given encoding
+;; and `$src-*` components pass a string from their own memory with the given
+;; encoding. Each pair is exported as `<src>-to-<dst>`, where `compact` is
+;; latin1+utf16.
+
+;; Strings which are out-of-bounds, or whose `ptr + len` calculation
+;; overflows, trap.
+(component definition $Strings
+ (component $dst-utf8
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 0)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=utf8))
+ )
+ (component $dst-utf16
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 0)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=utf16))
+ )
+ (component $dst-compact
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 0)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=latin1+utf16))
+ )
+ (component $src-utf8
+ (import "f" (func $f (param "a" string)))
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $f (canon lower (func $f) string-encoding=utf8
+ (memory (core memory $libc "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+ (component $src-utf16
+ (import "f" (func $f (param "a" string)))
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $f (canon lower (func $f) string-encoding=utf16
+ (memory (core memory $libc "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+ (component $src-compact
+ (import "f" (func $f (param "a" string)))
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $f (canon lower (func $f) string-encoding=latin1+utf16
+ (memory (core memory $libc "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+
+ (instance $dst-utf8 (instantiate $dst-utf8))
+ (instance $dst-utf16 (instantiate $dst-utf16))
+ (instance $dst-compact (instantiate $dst-compact))
+ (instance $utf8-to-utf8 (instantiate $src-utf8
+ (with "f" (func $dst-utf8 "f"))))
+ (export "utf8-to-utf8" (func $utf8-to-utf8 "f"))
+ (instance $utf8-to-utf16 (instantiate $src-utf8
+ (with "f" (func $dst-utf16 "f"))))
+ (export "utf8-to-utf16" (func $utf8-to-utf16 "f"))
+ (instance $utf8-to-compact (instantiate $src-utf8
+ (with "f" (func $dst-compact "f"))))
+ (export "utf8-to-compact" (func $utf8-to-compact "f"))
+ (instance $utf16-to-utf8 (instantiate $src-utf16
+ (with "f" (func $dst-utf8 "f"))))
+ (export "utf16-to-utf8" (func $utf16-to-utf8 "f"))
+ (instance $utf16-to-utf16 (instantiate $src-utf16
+ (with "f" (func $dst-utf16 "f"))))
+ (export "utf16-to-utf16" (func $utf16-to-utf16 "f"))
+ (instance $utf16-to-compact (instantiate $src-utf16
+ (with "f" (func $dst-compact "f"))))
+ (export "utf16-to-compact" (func $utf16-to-compact "f"))
+ (instance $compact-to-utf8 (instantiate $src-compact
+ (with "f" (func $dst-utf8 "f"))))
+ (export "compact-to-utf8" (func $compact-to-utf8 "f"))
+ (instance $compact-to-utf16 (instantiate $src-compact
+ (with "f" (func $dst-utf16 "f"))))
+ (export "compact-to-utf16" (func $compact-to-utf16 "f"))
+ (instance $compact-to-compact (instantiate $src-compact
+ (with "f" (func $dst-compact "f"))))
+ (export "compact-to-compact" (func $compact-to-compact "f"))
+)
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf8" (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf8" (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf16" (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf16" (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-compact" (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-compact" (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-utf8" (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-utf8" (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-utf16" (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-utf16" (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-compact" (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-compact" (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-utf8" (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-utf8" (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-utf16" (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-utf16" (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-compact"
+ (u32.const 0x80000000) (u32.const 0x0))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-compact"
+ (u32.const 0x80000000) (u32.const 0x1))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf8" (u32.const 0x3e8) (u32.const 0x80000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf8" (u32.const 0x3e8) (u32.const 0x7fffff9c))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf16" (u32.const 0x3e8) (u32.const 0x80000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf16" (u32.const 0x3e8) (u32.const 0x40000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-utf16" (u32.const 0x3e8) (u32.const 0x3fffff9c))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-compact" (u32.const 0x3e8) (u32.const 0x80000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf8-to-compact" (u32.const 0x3e8) (u32.const 0x7fffff9c))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-utf8" (u32.const 0x3e8) (u32.const 0x40000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-utf8" (u32.const 0x3e8) (u32.const 0x3fffff9c))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-utf16" (u32.const 0x3e8) (u32.const 0x40000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-utf16" (u32.const 0x3e8) (u32.const 0x3fffff9c))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-compact"
+ (u32.const 0x3e8) (u32.const 0x40000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "utf16-to-compact"
+ (u32.const 0x3e8) (u32.const 0x3fffff9c))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-utf8" (u32.const 0x3e8) (u32.const 0xc0000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-utf8" (u32.const 0x3e8) (u32.const 0xbfffff9c))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-utf16"
+ (u32.const 0x3e8) (u32.const 0xc0000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-utf16"
+ (u32.const 0x3e8) (u32.const 0xbfffff9c))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-compact"
+ (u32.const 0x3e8) (u32.const 0xc0000000))
+ "string content out-of-bounds")
+(component instance $Strings $Strings)
+(assert_trap (invoke "compact-to-compact"
+ (u32.const 0x3e8) (u32.const 0xbfffff9c))
+ "string content out-of-bounds")
+
+;; The pointer returned from `realloc` is bounds-checked.
+(component definition $ReallocOob
+ (component $dst-utf8
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 100000)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=utf8))
+ )
+ (component $dst-utf16
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 100000)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=utf16))
+ )
+ (component $dst-compact
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 100000)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=latin1+utf16))
+ )
+ (component $src-utf8
+ (import "f" (func $f (param "a" string)))
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $f (canon lower (func $f) string-encoding=utf8
+ (memory (core memory $libc "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+ (component $src-utf16
+ (import "f" (func $f (param "a" string)))
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $f (canon lower (func $f) string-encoding=utf16
+ (memory (core memory $libc "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+ (component $src-compact
+ (import "f" (func $f (param "a" string)))
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $f (canon lower (func $f) string-encoding=latin1+utf16
+ (memory (core memory $libc "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+
+ (instance $dst-utf8 (instantiate $dst-utf8))
+ (instance $dst-utf16 (instantiate $dst-utf16))
+ (instance $dst-compact (instantiate $dst-compact))
+ (instance $utf8-to-utf8 (instantiate $src-utf8
+ (with "f" (func $dst-utf8 "f"))))
+ (export "utf8-to-utf8" (func $utf8-to-utf8 "f"))
+ (instance $utf8-to-utf16 (instantiate $src-utf8
+ (with "f" (func $dst-utf16 "f"))))
+ (export "utf8-to-utf16" (func $utf8-to-utf16 "f"))
+ (instance $utf8-to-compact (instantiate $src-utf8
+ (with "f" (func $dst-compact "f"))))
+ (export "utf8-to-compact" (func $utf8-to-compact "f"))
+ (instance $utf16-to-utf8 (instantiate $src-utf16
+ (with "f" (func $dst-utf8 "f"))))
+ (export "utf16-to-utf8" (func $utf16-to-utf8 "f"))
+ (instance $utf16-to-utf16 (instantiate $src-utf16
+ (with "f" (func $dst-utf16 "f"))))
+ (export "utf16-to-utf16" (func $utf16-to-utf16 "f"))
+ (instance $utf16-to-compact (instantiate $src-utf16
+ (with "f" (func $dst-compact "f"))))
+ (export "utf16-to-compact" (func $utf16-to-compact "f"))
+ (instance $compact-to-utf8 (instantiate $src-compact
+ (with "f" (func $dst-utf8 "f"))))
+ (export "compact-to-utf8" (func $compact-to-utf8 "f"))
+ (instance $compact-to-utf16 (instantiate $src-compact
+ (with "f" (func $dst-utf16 "f"))))
+ (export "compact-to-utf16" (func $compact-to-utf16 "f"))
+ (instance $compact-to-compact (instantiate $src-compact
+ (with "f" (func $dst-compact "f"))))
+ (export "compact-to-compact" (func $compact-to-compact "f"))
+)
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "utf8-to-utf8" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "utf8-to-utf16" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "utf8-to-compact" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "utf16-to-utf8" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "utf16-to-utf16" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "utf16-to-compact" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "compact-to-utf8" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "compact-to-utf16" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+(component instance $ReallocOob $ReallocOob)
+(assert_trap (invoke "compact-to-compact" (u32.const 0x3e8) (u32.const 0xa))
+ "string content out-of-bounds")
+
+;; Strings which are invalid in their source encoding trap when transcoded.
+(component definition $RawBytes
+ ;; The raw bytes passed by the `$src-*` components, at fixed offsets.
+ (core module $data
+ (memory (export "memory") 1)
+ (data (i32.const 0x100) "\ff")
+ (data (i32.const 0x200) "valid string until \ffthen valid again")
+ (data (i32.const 0x300) "symbol \ce\a3 until \ffthen valid")
+ (data (i32.const 0x400) "\01\d8")
+ (data (i32.const 0x500) "\ff\ff\01\d8")
+ (data (i32.const 0x600) "\ab\00\ff\ff\01\d8")
+ (data (i32.const 0x700) "\ab\00\ff\00")
+ )
+
+ (component $dst-utf8
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 0)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=utf8))
+ )
+ (component $dst-utf16
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 0)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=utf16))
+ )
+ (component $dst-compact
+ (core module $m
+ (func (export "f") (param i32 i32))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ i32.const 0)
+ (memory (export "memory") 1)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "a" string)
+ (canon lift (core func $i "f") (realloc (core func $i "realloc"))
+ (memory (core memory $i "memory")) string-encoding=latin1+utf16))
+ )
+ (component $src-utf8
+ (import "f" (func $f (param "a" string)))
+ (alias outer $RawBytes $data (core module $data))
+ (core instance $data (instantiate $data))
+ (core func $f (canon lower (func $f) string-encoding=utf8
+ (memory (core memory $data "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+ (component $src-utf16
+ (import "f" (func $f (param "a" string)))
+ (alias outer $RawBytes $data (core module $data))
+ (core instance $data (instantiate $data))
+ (core func $f (canon lower (func $f) string-encoding=utf16
+ (memory (core memory $data "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+ (component $src-compact
+ (import "f" (func $f (param "a" string)))
+ (alias outer $RawBytes $data (core module $data))
+ (core instance $data (instantiate $data))
+ (core func $f (canon lower (func $f) string-encoding=latin1+utf16
+ (memory (core memory $data "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32)))
+ (func (export "f") (param i32 i32)
+ (call $f (local.get 0) (local.get 1)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "f") (param "ptr" u32) (param "len" u32)
+ (canon lift (core func $i "f")))
+ )
+
+ (instance $dst-utf8 (instantiate $dst-utf8))
+ (instance $dst-utf16 (instantiate $dst-utf16))
+ (instance $dst-compact (instantiate $dst-compact))
+ (instance $utf8-to-utf8 (instantiate $src-utf8
+ (with "f" (func $dst-utf8 "f"))))
+ (export "utf8-to-utf8" (func $utf8-to-utf8 "f"))
+ (instance $utf8-to-utf16 (instantiate $src-utf8
+ (with "f" (func $dst-utf16 "f"))))
+ (export "utf8-to-utf16" (func $utf8-to-utf16 "f"))
+ (instance $utf8-to-compact (instantiate $src-utf8
+ (with "f" (func $dst-compact "f"))))
+ (export "utf8-to-compact" (func $utf8-to-compact "f"))
+ (instance $utf16-to-utf8 (instantiate $src-utf16
+ (with "f" (func $dst-utf8 "f"))))
+ (export "utf16-to-utf8" (func $utf16-to-utf8 "f"))
+ (instance $utf16-to-utf16 (instantiate $src-utf16
+ (with "f" (func $dst-utf16 "f"))))
+ (export "utf16-to-utf16" (func $utf16-to-utf16 "f"))
+ (instance $utf16-to-compact (instantiate $src-utf16
+ (with "f" (func $dst-compact "f"))))
+ (export "utf16-to-compact" (func $utf16-to-compact "f"))
+ (instance $compact-to-utf8 (instantiate $src-compact
+ (with "f" (func $dst-utf8 "f"))))
+ (export "compact-to-utf8" (func $compact-to-utf8 "f"))
+ (instance $compact-to-utf16 (instantiate $src-compact
+ (with "f" (func $dst-utf16 "f"))))
+ (export "compact-to-utf16" (func $compact-to-utf16 "f"))
+ (instance $compact-to-compact (instantiate $src-compact
+ (with "f" (func $dst-compact "f"))))
+ (export "compact-to-compact" (func $compact-to-compact "f"))
+)
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf8-to-utf8" (u32.const 0x100) (u32.const 1))
+ "invalid utf8 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf8-to-utf8" (u32.const 0x200) (u32.const 36))
+ "invalid utf8 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf8-to-utf16" (u32.const 0x200) (u32.const 36))
+ "invalid utf8 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf8-to-utf8" (u32.const 0x300) (u32.const 27))
+ "invalid utf8 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf8-to-utf16" (u32.const 0x300) (u32.const 27))
+ "invalid utf8 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf8-to-compact" (u32.const 0x300) (u32.const 27))
+ "invalid utf8 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf16-to-utf8" (u32.const 0x400) (u32.const 1))
+ "invalid utf16 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf16-to-utf16" (u32.const 0x400) (u32.const 1))
+ "invalid utf16 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "utf16-to-compact" (u32.const 0x500) (u32.const 2))
+ "invalid utf16 encoding")
+
+;; A latin1+utf16 length with the high bit set is a count of utf16 code units.
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "compact-to-utf8" (u32.const 0x400) (u32.const 0x80000001))
+ "invalid utf16 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "compact-to-utf16" (u32.const 0x400) (u32.const 0x80000001))
+ "invalid utf16 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "compact-to-utf16" (u32.const 0x500) (u32.const 0x80000002))
+ "invalid utf16 encoding")
+(component instance $RawBytes $RawBytes)
+(assert_trap
+ (invoke "compact-to-compact" (u32.const 0x600) (u32.const 0x80000003))
+ "invalid utf16 encoding")
+
+;; This latin1+utf16 string is valid and gets compressed to latin1.
+(component instance $RawBytes $RawBytes)
+(assert_return
+ (invoke "compact-to-compact" (u32.const 0x700) (u32.const 0x80000002)))
diff --git a/tests/misc_testsuite/component-model/types.wast b/tests/misc_testsuite/component-model/types.wast
index 8563bc35d0..321cbd483a 100644
--- a/tests/misc_testsuite/component-model/types.wast
+++ b/tests/misc_testsuite/component-model/types.wast
@@ -862,3 +862,130 @@
(tuple.const (option.none) (result.ok))
(tuple.const (option.some (u8.const 2)) (result.err (str.const "hello there")))
(tuple.const (option.some (u8.const 200)) (result.err (str.const "general kenobi")))))
+
+;; Echo a string, which is passed as two flat values and returned via a
+;; return pointer.
+(component
+ (core module $m
+ (memory (export "memory") 1)
+ (global $last (mut i32) (i32.const 8))
+ (func $realloc (export "realloc") (param i32 i32 i32 i32) (result i32)
+ (local $ret i32)
+ (local.set $ret
+ (i32.and
+ (i32.add (global.get $last) (i32.sub (local.get 2) (i32.const 1)))
+ (i32.sub (i32.const 0) (local.get 2))))
+ (global.set $last (i32.add (local.get $ret) (local.get 3)))
+ (local.get $ret))
+ (func (export "echo") (param i32 i32) (result i32)
+ (local $base i32)
+ (local.set $base
+ (call $realloc (i32.const 0) (i32.const 0) (i32.const 4) (i32.const 8)))
+ (i32.store offset=0 (local.get $base) (local.get 0))
+ (i32.store offset=4 (local.get $base) (local.get 1))
+ (local.get $base))
+ )
+ (core instance $i (instantiate $m))
+ (func (export "echo") (param "a" string) (result string)
+ (canon lift (core func $i "echo") (memory (core memory $i "memory"))
+ (realloc (core func $i "realloc"))))
+)
+(assert_return (invoke "echo" (str.const "hello, component!"))
+ (str.const "hello, component!"))
+
+;; Echo a record with many nested types. It has more than 16 flat values, so
+;; it is passed in memory, and the guest returns the same pointer.
+(component
+ (core module $m
+ (memory (export "memory") 1)
+ (global $last (mut i32) (i32.const 8))
+ (func $realloc (export "realloc") (param i32 i32 i32 i32) (result i32)
+ (local $ret i32)
+ (local.set $ret
+ (i32.and
+ (i32.add (global.get $last) (i32.sub (local.get 2) (i32.const 1)))
+ (i32.sub (i32.const 0) (local.get 2))))
+ (global.set $last (i32.add (local.get $ret) (local.get 3)))
+ (local.get $ret))
+ (func (export "echo") (param i32) (result i32) local.get 0)
+ )
+ (core instance $i (instantiate $m))
+
+ (type $b' (enum "a" "b"))
+ (export $b "b" (type $b'))
+ (type $c' (record (field "D" bool) (field "E" u32)))
+ (export $c "c" (type $c'))
+ (type $f' (flags "G" "H" "I"))
+ (export $f "f" (type $f'))
+ (type $m' (record (field "N" bool) (field "O" u32)))
+ (export $m "m" (type $m'))
+ (type $j' (variant
+ (case "K" u32)
+ (case "L" float64)
+ (case "M" $m)
+ ))
+ (export $j "j" (type $j'))
+ (type $Foo' (record
+ (field "A" u32)
+ (field "B" $b)
+ (field "C" $c)
+ (field "F" (list $f))
+ (field "J" $j)
+ (field "P" s8)
+ (field "Q" s16)
+ (field "R" s32)
+ (field "S" s64)
+ (field "T" float32)
+ (field "U" float64)
+ (field "V" string)
+ (field "W" char)
+ (field "Y" (tuple u32 u32))
+ (field "AA" (option u32))
+ (field "BB" (result string (error string)))
+ ))
+ (export $Foo "foo" (type $Foo'))
+
+ (func (export "echo") (param "a" $Foo) (result $Foo)
+ (canon lift (core func $i "echo") (memory (core memory $i "memory"))
+ (realloc (core func $i "realloc"))))
+)
+(assert_return
+ (invoke "echo"
+ (record.const
+ (field "A" u32.const 32343)
+ (field "B" enum.const "b")
+ (field "C" record.const
+ (field "D" bool.const false)
+ (field "E" u32.const 2084037802))
+ (field "F" list.const (flags.const "G" "I"))
+ (field "J" variant.const "L" (f64.const 3.14159265))
+ (field "P" s8.const 42)
+ (field "Q" s16.const 4242)
+ (field "R" s32.const 42424242)
+ (field "S" s64.const 424242424242424242)
+ (field "T" f32.const 3.14159265)
+ (field "U" f64.const 3.14159265)
+ (field "V" str.const "wow, nice types")
+ (field "W" char.const "🦀")
+ (field "Y" tuple.const (u32.const 42) (u32.const 24))
+ (field "AA" option.some (u32.const 314159265))
+ (field "BB" result.ok (str.const "no problem"))))
+ (record.const
+ (field "A" u32.const 32343)
+ (field "B" enum.const "b")
+ (field "C" record.const
+ (field "D" bool.const false)
+ (field "E" u32.const 2084037802))
+ (field "F" list.const (flags.const "G" "I"))
+ (field "J" variant.const "L" (f64.const 3.14159265))
+ (field "P" s8.const 42)
+ (field "Q" s16.const 4242)
+ (field "R" s32.const 42424242)
+ (field "S" s64.const 424242424242424242)
+ (field "T" f32.const 3.14159265)
+ (field "U" f64.const 3.14159265)
+ (field "V" str.const "wow, nice types")
+ (field "W" char.const "🦀")
+ (field "Y" tuple.const (u32.const 42) (u32.const 24))
+ (field "AA" option.some (u32.const 314159265))
+ (field "BB" result.ok (str.const "no problem"))))