A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Migrate much of component_model/func.rs to `*.wast` (#15044)
Alex Crichtondeeae74b90a0
diff --git a/crates/wast/src/wast.rs b/crates/wast/src/wast.rsindex b242539722..01a30af742 100644--- a/crates/wast/src/wast.rs+++ b/crates/wast/src/wast.rs@@ -2,6 +2,8 @@use crate::component;use crate::core;use crate::spectest::*;+#[cfg(feature = "component-model")]+use json_from_wast::CoreConst;use json_from_wast::{Action, Command, Const, WasmFile, WasmFileType};use std::collections::HashMap;use std::path::{Path, PathBuf};@@ -342,6 +344,12 @@ impl WastContext {.iter().map(|v| match v {Const::Component(v) => component::val(v),+ Const::Core(CoreConst::F32 { value }) => {+ Ok(component::Val::Float32(f32::from_bits(value.to_bits())))+ }+ Const::Core(CoreConst::F64 { value }) => {+ Ok(component::Val::Float64(f64::from_bits(value.to_bits())))+ }_ => bail!("expected component function, found other argument {v:?}"),}).collect::<Result<Vec<_>>>()?;@@ -527,12 +535,17 @@ impl WastContext {bail!("expected {} results found {}", results.len(), values.len());}for (i, (v, e)) in values.iter().zip(results).enumerate() {- let e = match e {- Const::Component(val) => val,+ match (e, v) {+ (Const::Component(e), v) => component::match_val(e, v),+ (Const::Core(CoreConst::F32 { value }), component::Val::Float32(v)) => {+ core::match_f32(v.to_bits(), value)+ }+ (Const::Core(CoreConst::F64 { value }), component::Val::Float64(v)) => {+ core::match_f64(v.to_bits(), value)+ }_ => bail!("expected component value found other value {e:?}"),- };- component::match_val(e, v)- .with_context(|| format!("result {i} didn't match"))?;+ }+ .with_context(|| format!("result {i} didn't match"))?;}}}diff --git a/tests/all/component_model/func.rs b/tests/all/component_model/func.rsindex 45ce954f92..4d3f4c7ac1 100644--- a/tests/all/component_model/func.rs+++ b/tests/all/component_model/func.rs@@ -11,41 +11,6 @@ use wasmtime::component::*;use wasmtime::{Config, Engine, Store, StoreContextMut, Trap};const CANON_32BIT_NAN: u32 = 0b01111111110000000000000000000000;-const CANON_64BIT_NAN: u64 = 0b0111111111111000000000000000000000000000000000000000000000000000;--#[test]-fn thunks() -> Result<()> {- let component = r#"- (component- (core module $m- (func (export "thunk"))- (func (export "thunk-trap") unreachable)- )- (core instance $i (instantiate $m))- (func (export "thunk")- (canon lift (core func $i "thunk"))- )- (func (export "thunk-trap")- (canon lift (core func $i "thunk-trap"))- )- )- "#;-- 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)?;- instance- .get_typed_func::<(), ()>(&mut store, "thunk")?- .call(&mut store, ())?;- let err = instance- .get_typed_func::<(), ()>(&mut store, "thunk-trap")?- .call(&mut store, ())- .unwrap_err();- assert_eq!(err.downcast::<Trap>()?, Trap::UnreachableCodeReached);-- Ok(())-}#[test]fn typecheck() -> Result<()> {@@ -126,299 +91,6 @@ fn typecheck() -> Result<()> {Ok(())}-#[test]-fn integers() -> Result<()> {- let component = r#"- (component- (core module $m- (func (export "take-i32-100") (param i32)- local.get 0- i32.const 100- i32.eq- br_if 0- unreachable- )- (func (export "take-i64-100") (param i64)- local.get 0- i64.const 100- i64.eq- br_if 0- unreachable- )- (func (export "ret-i32-0") (result i32) i32.const 0)- (func (export "ret-i64-0") (result i64) i64.const 0)- (func (export "ret-i32-minus-1") (result i32) i32.const -1)- (func (export "ret-i64-minus-1") (result i64) i64.const -1)- (func (export "ret-i32-100000") (result i32) i32.const 100000)- )- (core instance $i (instantiate (module $m)))- (func (export "take-u8") (param "a" u8) (canon lift (core func $i "take-i32-100")))- (func (export "take-s8") (param "a" s8) (canon lift (core func $i "take-i32-100")))- (func (export "take-u16") (param "a" u16) (canon lift (core func $i "take-i32-100")))- (func (export "take-s16") (param "a" s16) (canon lift (core func $i "take-i32-100")))- (func (export "take-u32") (param "a" u32) (canon lift (core func $i "take-i32-100")))- (func (export "take-s32") (param "a" s32) (canon lift (core func $i "take-i32-100")))- (func (export "take-u64") (param "a" u64) (canon lift (core func $i "take-i64-100")))- (func (export "take-s64") (param "a" s64) (canon lift (core func $i "take-i64-100")))-- (func (export "ret-u8") (result u8) (canon lift (core func $i "ret-i32-0")))- (func (export "ret-s8") (result s8) (canon lift (core func $i "ret-i32-0")))- (func (export "ret-u16") (result u16) (canon lift (core func $i "ret-i32-0")))- (func (export "ret-s16") (result s16) (canon lift (core func $i "ret-i32-0")))- (func (export "ret-u32") (result u32) (canon lift (core func $i "ret-i32-0")))- (func (export "ret-s32") (result s32) (canon lift (core func $i "ret-i32-0")))- (func (export "ret-u64") (result u64) (canon lift (core func $i "ret-i64-0")))- (func (export "ret-s64") (result s64) (canon lift (core func $i "ret-i64-0")))-- (func (export "retm1-u8") (result u8) (canon lift (core func $i "ret-i32-minus-1")))- (func (export "retm1-s8") (result s8) (canon lift (core func $i "ret-i32-minus-1")))- (func (export "retm1-u16") (result u16) (canon lift (core func $i "ret-i32-minus-1")))- (func (export "retm1-s16") (result s16) (canon lift (core func $i "ret-i32-minus-1")))- (func (export "retm1-u32") (result u32) (canon lift (core func $i "ret-i32-minus-1")))- (func (export "retm1-s32") (result s32) (canon lift (core func $i "ret-i32-minus-1")))- (func (export "retm1-u64") (result u64) (canon lift (core func $i "ret-i64-minus-1")))- (func (export "retm1-s64") (result s64) (canon lift (core func $i "ret-i64-minus-1")))-- (func (export "retbig-u8") (result u8) (canon lift (core func $i "ret-i32-100000")))- (func (export "retbig-s8") (result s8) (canon lift (core func $i "ret-i32-100000")))- (func (export "retbig-u16") (result u16) (canon lift (core func $i "ret-i32-100000")))- (func (export "retbig-s16") (result s16) (canon lift (core func $i "ret-i32-100000")))- (func (export "retbig-u32") (result u32) (canon lift (core func $i "ret-i32-100000")))- (func (export "retbig-s32") (result s32) (canon lift (core func $i "ret-i32-100000")))- )- "#;-- 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)?;-- // Passing in 100 is valid for all primitives- instance- .get_typed_func::<(u8,), ()>(&mut store, "take-u8")?- .call(&mut store, (100,))?;- instance- .get_typed_func::<(i8,), ()>(&mut store, "take-s8")?- .call(&mut store, (100,))?;- instance- .get_typed_func::<(u16,), ()>(&mut store, "take-u16")?- .call(&mut store, (100,))?;- instance- .get_typed_func::<(i16,), ()>(&mut store, "take-s16")?- .call(&mut store, (100,))?;- instance- .get_typed_func::<(u32,), ()>(&mut store, "take-u32")?- .call(&mut store, (100,))?;- instance- .get_typed_func::<(i32,), ()>(&mut store, "take-s32")?- .call(&mut store, (100,))?;- instance- .get_typed_func::<(u64,), ()>(&mut store, "take-u64")?- .call(&mut store, (100,))?;- instance- .get_typed_func::<(i64,), ()>(&mut store, "take-s64")?- .call(&mut store, (100,))?;-- // This specific wasm instance traps if any value other than 100 is passed- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(u8,), ()>(&mut *store, "take-u8")?- .call(store, (101,))- .unwrap_err()- .downcast::<Trap>()- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(i8,), ()>(&mut *store, "take-s8")?- .call(store, (101,))- .unwrap_err()- .downcast::<Trap>()- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(u16,), ()>(&mut *store, "take-u16")?- .call(store, (101,))- .unwrap_err()- .downcast::<Trap>()- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(i16,), ()>(&mut *store, "take-s16")?- .call(store, (101,))- .unwrap_err()- .downcast::<Trap>()- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(u32,), ()>(&mut *store, "take-u32")?- .call(store, (101,))- .unwrap_err()- .downcast::<Trap>()- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(i32,), ()>(&mut *store, "take-s32")?- .call(store, (101,))- .unwrap_err()- .downcast::<Trap>()- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(u64,), ()>(&mut *store, "take-u64")?- .call(store, (101,))- .unwrap_err()- .downcast::<Trap>()- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(i64,), ()>(&mut *store, "take-s64")?- .call(store, (101,))- .unwrap_err()- .downcast::<Trap>()- })?;-- // Zero can be returned as any integer- assert_eq!(- instance- .get_typed_func::<(), (u8,)>(&mut store, "ret-u8")?- .call(&mut store, ())?,- (0,)- );- assert_eq!(- instance- .get_typed_func::<(), (i8,)>(&mut store, "ret-s8")?- .call(&mut store, ())?,- (0,)- );- assert_eq!(- instance- .get_typed_func::<(), (u16,)>(&mut store, "ret-u16")?- .call(&mut store, ())?,- (0,)- );- assert_eq!(- instance- .get_typed_func::<(), (i16,)>(&mut store, "ret-s16")?- .call(&mut store, ())?,- (0,)- );- assert_eq!(- instance- .get_typed_func::<(), (u32,)>(&mut store, "ret-u32")?- .call(&mut store, ())?,- (0,)- );- assert_eq!(- instance- .get_typed_func::<(), (i32,)>(&mut store, "ret-s32")?- .call(&mut store, ())?,- (0,)- );- assert_eq!(- instance- .get_typed_func::<(), (u64,)>(&mut store, "ret-u64")?- .call(&mut store, ())?,- (0,)- );- assert_eq!(- instance- .get_typed_func::<(), (i64,)>(&mut store, "ret-s64")?- .call(&mut store, ())?,- (0,)- );-- // Returning -1 should reinterpret the bytes as defined by each type.- assert_eq!(- instance- .get_typed_func::<(), (u8,)>(&mut store, "retm1-u8")?- .call(&mut store, ())?,- (0xff,)- );- assert_eq!(- instance- .get_typed_func::<(), (i8,)>(&mut store, "retm1-s8")?- .call(&mut store, ())?,- (-1,)- );- assert_eq!(- instance- .get_typed_func::<(), (u16,)>(&mut store, "retm1-u16")?- .call(&mut store, ())?,- (0xffff,)- );- assert_eq!(- instance- .get_typed_func::<(), (i16,)>(&mut store, "retm1-s16")?- .call(&mut store, ())?,- (-1,)- );- assert_eq!(- instance- .get_typed_func::<(), (u32,)>(&mut store, "retm1-u32")?- .call(&mut store, ())?,- (0xffffffff,)- );- assert_eq!(- instance- .get_typed_func::<(), (i32,)>(&mut store, "retm1-s32")?- .call(&mut store, ())?,- (-1,)- );- assert_eq!(- instance- .get_typed_func::<(), (u64,)>(&mut store, "retm1-u64")?- .call(&mut store, ())?,- (0xffffffff_ffffffff,)- );- assert_eq!(- instance- .get_typed_func::<(), (i64,)>(&mut store, "retm1-s64")?- .call(&mut store, ())?,- (-1,)- );-- // Returning 100000 should chop off bytes as necessary- let ret: u32 = 100000;- assert_eq!(- instance- .get_typed_func::<(), (u8,)>(&mut store, "retbig-u8")?- .call(&mut store, ())?,- (ret as u8,),- );- assert_eq!(- instance- .get_typed_func::<(), (i8,)>(&mut store, "retbig-s8")?- .call(&mut store, ())?,- (ret as i8,),- );- assert_eq!(- instance- .get_typed_func::<(), (u16,)>(&mut store, "retbig-u16")?- .call(&mut store, ())?,- (ret as u16,),- );- assert_eq!(- instance- .get_typed_func::<(), (i16,)>(&mut store, "retbig-s16")?- .call(&mut store, ())?,- (ret as i16,),- );- assert_eq!(- instance- .get_typed_func::<(), (u32,)>(&mut store, "retbig-u32")?- .call(&mut store, ())?,- (ret,),- );- assert_eq!(- instance- .get_typed_func::<(), (i32,)>(&mut store, "retbig-s32")?- .call(&mut store, ())?,- (ret as i32,),- );-- Ok(())-}-#[test]fn type_layers() -> Result<()> {let component = r#"@@ -458,223 +130,6 @@ fn type_layers() -> Result<()> {Ok(())}-#[test]-fn floats() -> Result<()> {- let component = r#"- (component- (core module $m- (func (export "i32.reinterpret_f32") (param f32) (result i32)- local.get 0- i32.reinterpret_f32- )- (func (export "i64.reinterpret_f64") (param f64) (result i64)- local.get 0- i64.reinterpret_f64- )- (func (export "f32.reinterpret_i32") (param i32) (result f32)- local.get 0- f32.reinterpret_i32- )- (func (export "f64.reinterpret_i64") (param i64) (result f64)- local.get 0- f64.reinterpret_i64- )- )- (core instance $i (instantiate $m))-- (func (export "f32-to-u32") (param "a" float32) (result u32)- (canon lift (core func $i "i32.reinterpret_f32"))- )- (func (export "f64-to-u64") (param "a" float64) (result u64)- (canon lift (core func $i "i64.reinterpret_f64"))- )- (func (export "u32-to-f32") (param "a" u32) (result float32)- (canon lift (core func $i "f32.reinterpret_i32"))- )- (func (export "u64-to-f64") (param "a" u64) (result float64)- (canon lift (core func $i "f64.reinterpret_i64"))- )- )- "#;-- 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 f32_to_u32 = instance.get_typed_func::<(f32,), (u32,)>(&mut store, "f32-to-u32")?;- let f64_to_u64 = instance.get_typed_func::<(f64,), (u64,)>(&mut store, "f64-to-u64")?;- let u32_to_f32 = instance.get_typed_func::<(u32,), (f32,)>(&mut store, "u32-to-f32")?;- let u64_to_f64 = instance.get_typed_func::<(u64,), (f64,)>(&mut store, "u64-to-f64")?;-- assert_eq!(f32_to_u32.call(&mut store, (1.0,))?, (1.0f32.to_bits(),));- assert_eq!(f64_to_u64.call(&mut store, (2.0,))?, (2.0f64.to_bits(),));- assert_eq!(u32_to_f32.call(&mut store, (3.0f32.to_bits(),))?, (3.0,));- assert_eq!(u64_to_f64.call(&mut store, (4.0f64.to_bits(),))?, (4.0,));-- assert_eq!(- u32_to_f32- .call(&mut store, (CANON_32BIT_NAN | 1,))?- .0- .to_bits(),- CANON_32BIT_NAN | 1- );- assert_eq!(- u64_to_f64- .call(&mut store, (CANON_64BIT_NAN | 1,))?- .0- .to_bits(),- CANON_64BIT_NAN | 1,- );-- assert_eq!(- f32_to_u32.call(&mut store, (f32::from_bits(CANON_32BIT_NAN | 1),))?,- (CANON_32BIT_NAN | 1,)- );- assert_eq!(- f64_to_u64.call(&mut store, (f64::from_bits(CANON_64BIT_NAN | 1),))?,- (CANON_64BIT_NAN | 1,)- );-- Ok(())-}--#[test]-fn chars() -> Result<()> {- let component = r#"- (component- (core module $m- (func (export "pass") (param i32) (result i32) local.get 0)- )- (core instance $i (instantiate $m))-- (func (export "u32-to-char") (param "a" u32) (result char)- (canon lift (core func $i "pass"))- )- (func (export "char-to-u32") (param "a" char) (result u32)- (canon lift (core func $i "pass"))- )- )- "#;-- 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 u32_to_char = instance.get_typed_func::<(u32,), (char,)>(&mut store, "u32-to-char")?;- let char_to_u32 = instance.get_typed_func::<(char,), (u32,)>(&mut store, "char-to-u32")?;-- let mut roundtrip = |x: char| -> Result<()> {- assert_eq!(char_to_u32.call(&mut store, (x,))?, (x as u32,));- assert_eq!(u32_to_char.call(&mut store, (x as u32,))?, (x,));- Ok(())- };-- roundtrip('x')?;- roundtrip('a')?;- roundtrip('\0')?;- roundtrip('\n')?;- roundtrip('π')?;- }-- let u32_to_char = |store: &mut Store<()>| {- Linker::new(&engine)- .instantiate(&mut *store, &component)?- .get_typed_func::<(u32,), (char,)>(&mut *store, "u32-to-char")- };-- {- let mut store = Store::new(&engine, ());- let err = u32_to_char(&mut store)?- .call(&mut store, (0xd800,))- .unwrap_err();- assert!(err.to_string().contains("integer out of range"), "{}", err);- }-- {- let mut store = Store::new(&engine, ());- let err = u32_to_char(&mut store)?- .call(&mut store, (0xdfff,))- .unwrap_err();- assert!(err.to_string().contains("integer out of range"), "{}", err);- }-- {- let mut store = Store::new(&engine, ());- let err = u32_to_char(&mut store)?- .call(&mut store, (0x110000,))- .unwrap_err();- assert!(err.to_string().contains("integer out of range"), "{}", err);- }-- {- let mut store = Store::new(&engine, ());- let err = u32_to_char(&mut store)?- .call(&mut store, (u32::MAX,))- .unwrap_err();- assert!(err.to_string().contains("integer out of range"), "{}", err);- }-- Ok(())-}--#[test]-fn tuple_result() -> Result<()> {- let component = r#"- (component- (core module $m- (memory (export "memory") 1)- (func (export "foo") (param i32 i32 f32 f64) (result i32)- (local $base i32)- (local.set $base (i32.const 8))- (i32.store8 offset=0 (local.get $base) (local.get 0))- (i32.store16 offset=2 (local.get $base) (local.get 1))- (f32.store offset=4 (local.get $base) (local.get 2))- (f64.store offset=8 (local.get $base) (local.get 3))- local.get $base- )-- (func (export "invalid") (result i32)- i32.const -8- )- )- (core instance $i (instantiate $m))-- (type $result (tuple s8 u16 float32 float64))- (func (export "tuple")- (param "a" s8) (param "b" u16) (param "c" float32) (param "d" float64) (result $result)- (canon lift (core func $i "foo") (memory (core memory $i "memory")))- )- (func (export "invalid") (result $result)- (canon lift (core func $i "invalid") (memory (core memory $i "memory")))- )- )- "#;-- 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 input = (-1, 100, 3.0, 100.0);- let output = instance- .get_typed_func::<(i8, u16, f32, f64), ((i8, u16, f32, f64),)>(&mut store, "tuple")?- .call(&mut store, input)?;- assert_eq!((input,), output);-- let invalid_func =- instance.get_typed_func::<(), ((i8, u16, f32, f64),)>(&mut store, "invalid")?;- let err = invalid_func.call(&mut store, ()).err().unwrap();- assert!(- err.to_string().contains("pointer out of bounds of memory"),- "{}",- err- );-- Ok(())-}-#[test]fn strings() -> Result<()> {let component = format!(@@ -809,11 +264,6 @@ async fn many_parameters_concurrent() -> Result<()> {test_many_parameters(false, true).await}-#[tokio::test]-async fn many_parameters_dynamic() -> Result<()> {- test_many_parameters(true, false).await-}-#[tokio::test]async fn many_parameters_dynamic_concurrent() -> Result<()> {test_many_parameters(true, true).await@@ -1094,11 +544,6 @@ async fn many_results_concurrent() -> Result<()> {test_many_results(false, true).await}-#[tokio::test]-async fn many_results_dynamic() -> Result<()> {- test_many_results(true, false).await-}-#[tokio::test]async fn many_results_dynamic_concurrent() -> Result<()> {test_many_results(true, true).await@@ -1531,869 +976,12 @@ async fn test_many_results(dynamic: bool, concurrent: bool) -> Result<()> {Ok(())}-#[test]-fn some_traps() -> Result<()> {- let middle_of_memory = (i32::MAX / 2) & (!0xff);- let component = format!(- r#"(component- (core module $m- (memory (export "memory") 1)- (func (export "take-many") (param i32))- (func (export "take-list") (param i32 i32))+trait SliceExt<'a> {+ fn take_n<const N: usize>(&mut self) -> &'a [u8; N];- (func (export "realloc") (param i32 i32 i32 i32) (result i32)- unreachable)- )- (core instance $i (instantiate $m))-- (func (export "take-list-unreachable") (param "a" (list u8))- (canon lift (core func $i "take-list") (memory (core memory $i "memory")) (realloc (core func $i "realloc")))- )- (func (export "take-string-unreachable") (param "a" string)- (canon lift (core func $i "take-list") (memory (core memory $i "memory")) (realloc (core func $i "realloc")))- )-- (type $t (func- (param "s1" string)- (param "s2" string)- (param "s3" string)- (param "s4" string)- (param "s5" string)- (param "s6" string)- (param "s7" string)- (param "s8" string)- (param "s9" string)- (param "s10" string)- ))- (func (export "take-many-unreachable") (type $t)- (canon lift (core func $i "take-many") (memory (core memory $i "memory")) (realloc (core func $i "realloc")))- )-- (core module $m2- (memory (export "memory") 1)- (func (export "take-many") (param i32))- (func (export "take-list") (param i32 i32))-- (func (export "realloc") (param i32 i32 i32 i32) (result i32)- i32.const {middle_of_memory})- )- (core instance $i2 (instantiate $m2))-- (func (export "take-list-base-oob") (param "a" (list u8))- (canon lift (core func $i2 "take-list") (memory (core memory $i2 "memory")) (realloc (core func $i2 "realloc")))- )- (func (export "take-string-base-oob") (param "a" string)- (canon lift (core func $i2 "take-list") (memory (core memory $i2 "memory")) (realloc (core func $i2 "realloc")))- )- (func (export "take-many-base-oob") (type $t)- (canon lift (core func $i2 "take-many") (memory (core memory $i2 "memory")) (realloc (core func $i2 "realloc")))- )-- (core module $m3- (memory (export "memory") 1)- (func (export "take-many") (param i32))- (func (export "take-list") (param i32 i32))-- (func (export "realloc") (param i32 i32 i32 i32) (result i32)- i32.const 65532)- )- (core instance $i3 (instantiate $m3))-- (func (export "take-list-end-oob") (param "a" (list u8))- (canon lift (core func $i3 "take-list") (memory (core memory $i3 "memory")) (realloc (core func $i3 "realloc")))- )- (func (export "take-string-end-oob") (param "a" string)- (canon lift (core func $i3 "take-list") (memory (core memory $i3 "memory")) (realloc (core func $i3 "realloc")))- )- (func (export "take-many-end-oob") (type $t)- (canon lift (core func $i3 "take-many") (memory (core memory $i3 "memory")) (realloc (core func $i3 "realloc")))- )-- (core module $m4- (memory (export "memory") 1)- (func (export "take-many") (param i32))-- (global $cnt (mut i32) (i32.const 0))- (func (export "realloc") (param i32 i32 i32 i32) (result i32)- global.get $cnt- if (result i32)- i32.const 100000- else- i32.const 1- global.set $cnt- i32.const 0- end- )- )- (core instance $i4 (instantiate $m4))-- (func (export "take-many-second-oob") (type $t)- (canon lift (core func $i4 "take-many") (memory (core memory $i4 "memory")) (realloc (core func $i4 "realloc")))- )- )"#- );-- let engine = super::engine();- let component = Component::new(&engine, component)?;-- // This should fail when calling the allocator function for the argument- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&[u8],), ()>(&mut *store, "take-list-unreachable")?- .call(store, (&[],))- .unwrap_err()- .downcast::<Trap>()- })?;- assert_eq!(err, Trap::UnreachableCodeReached);-- // This should fail when calling the allocator function for the argument- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str,), ()>(&mut *store, "take-string-unreachable")?- .call(store, ("",))- .unwrap_err()- .downcast::<Trap>()- })?;- assert_eq!(err, Trap::UnreachableCodeReached);-- // This should fail when calling the allocator function for the space- // to store the arguments (before arguments are even lowered)- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str, &str, &str, &str, &str, &str, &str, &str, &str, &str), ()>(- &mut *store,- "take-many-unreachable",- )?- .call(store, ("", "", "", "", "", "", "", "", "", ""))- .unwrap_err()- .downcast::<Trap>()- })?;- assert_eq!(err, Trap::UnreachableCodeReached);-- // Assert that when the base pointer returned by malloc is out of bounds- // that errors are reported as such. Both empty and lists with contents- // should all be invalid here.- //- // FIXME(WebAssembly/component-model#32) confirm the semantics here are- // what's desired.- #[track_caller]- fn assert_oob(err: &wasmtime::Error) {- assert!(- err.to_string()- .contains("realloc return: beyond end of memory"),- "{err:?}",- );- }- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&[u8],), ()>(&mut *store, "take-list-base-oob")?- .call(store, (&[],))- })- .unwrap_err();- assert_oob(&err);- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&[u8],), ()>(&mut *store, "take-list-base-oob")?- .call(store, (&[1],))- })- .unwrap_err();- assert_oob(&err);- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str,), ()>(&mut *store, "take-string-base-oob")?- .call(store, ("",))- })- .unwrap_err();- assert_oob(&err);- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str,), ()>(&mut *store, "take-string-base-oob")?- .call(store, ("x",))- })- .unwrap_err();- assert_oob(&err);- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str, &str, &str, &str, &str, &str, &str, &str, &str, &str), ()>(- &mut *store,- "take-many-base-oob",- )?- .call(store, ("", "", "", "", "", "", "", "", "", ""))- })- .unwrap_err();- assert_oob(&err);-- // Test here that when the returned pointer from malloc is one byte from the- // end of memory that empty things are fine, but larger things are not.-- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&[u8],), ()>(&mut *store, "take-list-end-oob")?- .call(store, (&[],))- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&[u8],), ()>(&mut *store, "take-list-end-oob")?- .call(store, (&[1, 2, 3, 4],))- })?;- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&[u8],), ()>(&mut *store, "take-list-end-oob")?- .call(store, (&[1, 2, 3, 4, 5],))- })- .unwrap_err();- assert_oob(&err);- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str,), ()>(&mut *store, "take-string-end-oob")?- .call(store, ("",))- })?;- with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str,), ()>(&mut *store, "take-string-end-oob")?- .call(store, ("abcd",))- })?;- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str,), ()>(&mut *store, "take-string-end-oob")?- .call(store, ("abcde",))- })- .unwrap_err();- assert_oob(&err);- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str, &str, &str, &str, &str, &str, &str, &str, &str, &str), ()>(- &mut *store,- "take-many-end-oob",- )?- .call(store, ("", "", "", "", "", "", "", "", "", ""))- })- .unwrap_err();- assert_oob(&err);-- // For this function the first allocation, the space to store all the- // arguments, is in-bounds but then all further allocations, such as for- // each individual string, are all out of bounds.- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str, &str, &str, &str, &str, &str, &str, &str, &str, &str), ()>(- &mut *store,- "take-many-second-oob",- )?- .call(store, ("", "", "", "", "", "", "", "", "", ""))- })- .unwrap_err();- assert_oob(&err);- let err = with_new_instance(&engine, &component, |store, instance| {- instance- .get_typed_func::<(&str, &str, &str, &str, &str, &str, &str, &str, &str, &str), ()>(- &mut *store,- "take-many-second-oob",- )?- .call(store, ("", "", "", "", "", "", "", "", "", "x"))- })- .unwrap_err();- assert_oob(&err);- Ok(())-}--#[test]-fn char_bool_memory() -> Result<()> {- let component = format!(- r#"(component- (core module $m- (memory (export "memory") 1)- (func (export "ret-tuple") (param i32 i32) (result i32)- (local $base i32)-- ;; Allocate space for the return- (local.set $base- (call $realloc- (i32.const 0)- (i32.const 0)- (i32.const 4)- (i32.const 8)))-- ;; store the boolean- (i32.store offset=0- (local.get $base)- (local.get 0))-- ;; store the char- (i32.store offset=4- (local.get $base)- (local.get 1))-- (local.get $base)- )-- {REALLOC_AND_FREE}- )- (core instance $i (instantiate $m))-- (func (export "ret-tuple") (param "a" u32) (param "b" u32) (result (tuple bool char))- (canon lift (core func $i "ret-tuple")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc")))- )- )"#- );-- 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 func = instance.get_typed_func::<(u32, u32), ((bool, char),)>(&mut store, "ret-tuple")?;-- let (ret,) = func.call(&mut store, (0, 'a' as u32))?;- assert_eq!(ret, (false, 'a'));-- let (ret,) = func.call(&mut store, (1, 'π°' as u32))?;- assert_eq!(ret, (true, 'π°'));-- let (ret,) = func.call(&mut store, (2, 'a' as u32))?;- assert_eq!(ret, (true, 'a'));-- assert!(func.call(&mut store, (0, 0xd800)).is_err());-- Ok(())-}--#[test]-fn string_list_oob() -> Result<()> {- let component = format!(- r#"(component- (core module $m- (memory (export "memory") 1)- (func (export "ret-list") (result i32)- (local $base i32)-- ;; Allocate space for the return- (local.set $base- (call $realloc- (i32.const 0)- (i32.const 0)- (i32.const 4)- (i32.const 8)))-- (i32.store offset=0- (local.get $base)- (i32.const 100000))- (i32.store offset=4- (local.get $base)- (i32.const 1))-- (local.get $base)- )-- {REALLOC_AND_FREE}- )- (core instance $i (instantiate $m))-- (func (export "ret-list-u8") (result (list u8))- (canon lift (core func $i "ret-list")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- (func (export "ret-string") (result string)- (canon lift (core func $i "ret-list")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- )"#- );-- let engine = super::engine();- let component = Component::new(&engine, component)?;-- {- let mut store = Store::new(&engine, ());- let ret_list_u8 = Linker::new(&engine)- .instantiate(&mut store, &component)?- .get_typed_func::<(), (WasmList<u8>,)>(&mut store, "ret-list-u8")?;-- let err = ret_list_u8.call(&mut store, ()).err().unwrap();- assert!(err.to_string().contains("out of bounds"), "{}", err);- }-- {- let mut store = Store::new(&engine, ());- let ret_string = Linker::new(&engine)- .instantiate(&mut store, &component)?- .get_typed_func::<(), (WasmStr,)>(&mut store, "ret-string")?;-- let err = ret_string.call(&mut store, ()).err().unwrap();- assert!(err.to_string().contains("out of bounds"), "{}", err);- }-- Ok(())-}--#[test]-fn tuples() -> Result<()> {- let component = format!(- r#"(component- (core module $m- (memory (export "memory") 1)- (func (export "foo")- (param i32 f64 i32)- (result i32)-- local.get 0- i32.const 0- i32.ne- if unreachable end-- local.get 1- f64.const 1- f64.ne- if unreachable end-- local.get 2- i32.const 2- i32.ne- if unreachable end-- i32.const 3- )- )- (core instance $i (instantiate $m))-- (func (export "foo")- (param "a" (tuple s32 float64))- (param "b" (tuple s8))- (result (tuple u16))- (canon lift (core func $i "foo"))- )- )"#- );-- 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 foo = instance.get_typed_func::<((i32, f64), (i8,)), ((u16,),)>(&mut store, "foo")?;- assert_eq!(foo.call(&mut store, ((0, 1.0), (2,)))?, ((3,),));-- Ok(())-}--#[test]-fn option() -> Result<()> {- let component = format!(- r#"(component- (core module $m- (memory (export "memory") 1)- (func (export "pass1") (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)- )- (func (export "pass2") (param i32 i32 i32) (result i32)- (local $base i32)- (local.set $base- (call $realloc- (i32.const 0)- (i32.const 0)- (i32.const 4)- (i32.const 12)))-- (i32.store offset=0- (local.get $base)- (local.get 0))- (i32.store offset=4- (local.get $base)- (local.get 1))- (i32.store offset=8- (local.get $base)- (local.get 2))-- (local.get $base)- )-- {REALLOC_AND_FREE}- )- (core instance $i (instantiate $m))-- (func (export "option-u8-to-tuple") (param "a" (option u8)) (result (tuple u32 u32))- (canon lift (core func $i "pass1") (memory (core memory $i "memory")))- )- (func (export "option-u32-to-tuple") (param "a" (option u32)) (result (tuple u32 u32))- (canon lift (core func $i "pass1") (memory (core memory $i "memory")))- )- (func (export "option-string-to-tuple") (param "a" (option string)) (result (tuple u32 string))- (canon lift- (core func $i "pass2")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- (func (export "to-option-u8") (param "a" u32) (param "b" u32) (result (option u8))- (canon lift (core func $i "pass1") (memory (core memory $i "memory")))- )- (func (export "to-option-u32") (param "a" u32) (param "b" u32) (result (option u32))- (canon lift- (core func $i "pass1")- (memory (core memory $i "memory"))- )- )- (func (export "to-option-string") (param "a" u32) (param "b" string) (result (option string))- (canon lift- (core func $i "pass2")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- )"#- );-- let engine = super::engine();- let component = Component::new(&engine, component)?;- let linker = Linker::new(&engine);-- {- let mut store = Store::new(&engine, ());- let instance = linker.instantiate(&mut store, &component)?;-- let option_u8_to_tuple = instance- .get_typed_func::<(Option<u8>,), ((u32, u32),)>(&mut store, "option-u8-to-tuple")?;- assert_eq!(option_u8_to_tuple.call(&mut store, (None,))?, ((0, 0),));- assert_eq!(option_u8_to_tuple.call(&mut store, (Some(0),))?, ((1, 0),));- assert_eq!(- option_u8_to_tuple.call(&mut store, (Some(100),))?,- ((1, 100),)- );-- let option_u32_to_tuple = instance- .get_typed_func::<(Option<u32>,), ((u32, u32),)>(&mut store, "option-u32-to-tuple")?;- assert_eq!(option_u32_to_tuple.call(&mut store, (None,))?, ((0, 0),));- assert_eq!(option_u32_to_tuple.call(&mut store, (Some(0),))?, ((1, 0),));- assert_eq!(- option_u32_to_tuple.call(&mut store, (Some(100),))?,- ((1, 100),)- );-- let option_string_to_tuple = instance- .get_typed_func::<(Option<&str>,), ((u32, WasmStr),)>(- &mut store,- "option-string-to-tuple",- )?;- let ((a, b),) = option_string_to_tuple.call(&mut store, (None,))?;- assert_eq!(a, 0);- assert_eq!(b.to_str(&store)?, "");- let ((a, b),) = option_string_to_tuple.call(&mut store, (Some(""),))?;- assert_eq!(a, 1);- assert_eq!(b.to_str(&store)?, "");- let ((a, b),) = option_string_to_tuple.call(&mut store, (Some("hello"),))?;- assert_eq!(a, 1);- assert_eq!(b.to_str(&store)?, "hello");-- let instance = linker.instantiate(&mut store, &component)?;- let to_option_u8 =- instance.get_typed_func::<(u32, u32), (Option<u8>,)>(&mut store, "to-option-u8")?;- assert_eq!(to_option_u8.call(&mut store, (0x00_00, 0))?, (None,));- assert_eq!(to_option_u8.call(&mut store, (0x00_01, 0))?, (Some(0),));- assert_eq!(to_option_u8.call(&mut store, (0xfd_01, 0))?, (Some(0xfd),));- assert!(to_option_u8.call(&mut store, (0x00_02, 0)).is_err());- }-- {- let mut store = Store::new(&engine, ());- let instance = linker.instantiate(&mut store, &component)?;- let to_option_u32 =- instance.get_typed_func::<(u32, u32), (Option<u32>,)>(&mut store, "to-option-u32")?;- assert_eq!(to_option_u32.call(&mut store, (0, 0))?, (None,));- assert_eq!(to_option_u32.call(&mut store, (1, 0))?, (Some(0),));- assert_eq!(- to_option_u32.call(&mut store, (1, 0x1234fead))?,- (Some(0x1234fead),)- );- assert!(to_option_u32.call(&mut store, (2, 0)).is_err());- }-- {- let mut store = Store::new(&engine, ());- let instance = linker.instantiate(&mut store, &component)?;- let to_option_string = instance- .get_typed_func::<(u32, &str), (Option<WasmStr>,)>(&mut store, "to-option-string")?;- let ret = to_option_string.call(&mut store, (0, ""))?.0;- assert!(ret.is_none());- let ret = to_option_string.call(&mut store, (1, ""))?.0;- assert_eq!(ret.unwrap().to_str(&store)?, "");- let ret = to_option_string.call(&mut store, (1, "cheesecake"))?.0;- assert_eq!(ret.unwrap().to_str(&store)?, "cheesecake");- assert!(to_option_string.call(&mut store, (2, "")).is_err());- }-- Ok(())-}--#[test]-fn expected() -> Result<()> {- let component = format!(- r#"(component- (core module $m- (memory (export "memory") 1)- (func (export "pass0") (param i32) (result i32)- local.get 0- )- (func (export "pass1") (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)- )- (func (export "pass2") (param i32 i32 i32) (result i32)- (local $base i32)- (local.set $base- (call $realloc- (i32.const 0)- (i32.const 0)- (i32.const 4)- (i32.const 12)))-- (i32.store offset=0- (local.get $base)- (local.get 0))- (i32.store offset=4- (local.get $base)- (local.get 1))- (i32.store offset=8- (local.get $base)- (local.get 2))-- (local.get $base)- )-- {REALLOC_AND_FREE}- )- (core instance $i (instantiate $m))-- (func (export "take-expected-unit") (param "a" (result)) (result u32)- (canon lift (core func $i "pass0"))- )- (func (export "take-expected-u8-f32") (param "a" (result u8 (error float32))) (result (tuple u32 u32))- (canon lift (core func $i "pass1") (memory (core memory $i "memory")))- )- (type $list (list u8))- (func (export "take-expected-string") (param "a" (result string (error $list))) (result (tuple u32 string))- (canon lift- (core func $i "pass2")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- (func (export "to-expected-unit") (param "a" u32) (result (result))- (canon lift (core func $i "pass0"))- )- (func (export "to-expected-s16-f32") (param "a" u32) (param "b" u32) (result (result s16 (error float32)))- (canon lift- (core func $i "pass1")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- )"#- );-- let engine = super::engine();- let component = Component::new(&engine, component)?;- let linker = Linker::new(&engine);-- {- let mut store = Store::new(&engine, ());- let instance = linker.instantiate(&mut store, &component)?;- let take_expected_unit = instance- .get_typed_func::<(Result<(), ()>,), (u32,)>(&mut store, "take-expected-unit")?;- assert_eq!(take_expected_unit.call(&mut store, (Ok(()),))?, (0,));- assert_eq!(take_expected_unit.call(&mut store, (Err(()),))?, (1,));-- let take_expected_u8_f32 = instance.get_typed_func::<(Result<u8, f32>,), ((u32, u32),)>(- &mut store,- "take-expected-u8-f32",- )?;- assert_eq!(take_expected_u8_f32.call(&mut store, (Ok(1),))?, ((0, 1),));- assert_eq!(- take_expected_u8_f32.call(&mut store, (Err(2.0),))?,- ((1, 2.0f32.to_bits()),)- );-- let take_expected_string = instance- .get_typed_func::<(Result<&str, &[u8]>,), ((u32, WasmStr),)>(- &mut store,- "take-expected-string",- )?;- let ((a, b),) = take_expected_string.call(&mut store, (Ok("hello"),))?;- assert_eq!(a, 0);- assert_eq!(b.to_str(&store)?, "hello");- let ((a, b),) = take_expected_string.call(&mut store, (Err(b"goodbye"),))?;- assert_eq!(a, 1);- assert_eq!(b.to_str(&store)?, "goodbye");-- let instance = linker.instantiate(&mut store, &component)?;- let to_expected_unit =- instance.get_typed_func::<(u32,), (Result<(), ()>,)>(&mut store, "to-expected-unit")?;- assert_eq!(to_expected_unit.call(&mut store, (0,))?, (Ok(()),));- assert_eq!(to_expected_unit.call(&mut store, (1,))?, (Err(()),));- let err = to_expected_unit.call(&mut store, (2,)).unwrap_err();- assert!(err.to_string().contains("invalid expected"), "{}", err);- }-- {- let mut store = Store::new(&engine, ());- let instance = linker.instantiate(&mut store, &component)?;- let to_expected_s16_f32 = instance- .get_typed_func::<(u32, u32), (Result<i16, f32>,)>(&mut store, "to-expected-s16-f32")?;- assert_eq!(to_expected_s16_f32.call(&mut store, (0, 0))?, (Ok(0),));- assert_eq!(to_expected_s16_f32.call(&mut store, (0, 100))?, (Ok(100),));- assert_eq!(- to_expected_s16_f32.call(&mut store, (1, 1.0f32.to_bits()))?,- (Err(1.0),)- );- let ret = to_expected_s16_f32- .call(&mut store, (1, CANON_32BIT_NAN | 1))?- .0;- assert_eq!(ret.unwrap_err().to_bits(), CANON_32BIT_NAN | 1);- assert!(to_expected_s16_f32.call(&mut store, (2, 0)).is_err());- }-- Ok(())-}--#[test]-fn fancy_list() -> Result<()> {- let component = format!(- r#"(component- (core module $m- (memory (export "memory") 1)- (func (export "take") (param i32 i32) (result i32)- (local $base i32)- (local.set $base- (call $realloc- (i32.const 0)- (i32.const 0)- (i32.const 4)- (i32.const 16)))-- (i32.store offset=0- (local.get $base)- (local.get 0))- (i32.store offset=4- (local.get $base)- (local.get 1))- (i32.store offset=8- (local.get $base)- (i32.const 0))- (i32.store offset=12- (local.get $base)- (i32.mul- (memory.size)- (i32.const 65536)))-- (local.get $base)- )-- {REALLOC_AND_FREE}- )- (core instance $i (instantiate $m))-- (type $a (option u8))- (type $b (result (error string)))- (type $input (list (tuple $a $b)))- (func (export "take")- (param "a" $input)- (result (tuple u32 u32 (list u8)))- (canon lift- (core func $i "take")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- )"#- );-- 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 func = instance- .get_typed_func::<(&[(Option<u8>, Result<(), &str>)],), ((u32, u32, WasmList<u8>),)>(- &mut store, "take",- )?;-- let input = [- (None, Ok(())),- (Some(2), Err("hello there")),- (Some(200), Err("general kenobi")),- ];- let ((ptr, len, list),) = func.call(&mut store, (&input,))?;- let memory = list.as_le_slice(&store);- let ptr = usize::try_from(ptr).unwrap();- let len = usize::try_from(len).unwrap();- let mut array = &memory[ptr..][..len * 16];-- for (a, b) in input.iter() {- match a {- Some(val) => {- assert_eq!(*array.take_n::<2>(), [1, *val]);- }- None => {- assert_eq!(*array.take_n::<1>(), [0]);- array.skip::<1>();- }- }- array.skip::<2>();- match b {- Ok(()) => {- assert_eq!(*array.take_n::<1>(), [0]);- array.skip::<11>();- }- Err(s) => {- assert_eq!(*array.take_n::<1>(), [1]);- array.skip::<3>();- assert_eq!(array.ptr_len(memory, 1), s.as_bytes());- }- }- }- assert!(array.is_empty());-- Ok(())-}--trait SliceExt<'a> {- fn take_n<const N: usize>(&mut self) -> &'a [u8; N];-- fn skip<const N: usize>(&mut self) {- self.take_n::<N>();- }+ fn skip<const N: usize>(&mut self) {+ self.take_n::<N>();+ }fn ptr_len<'b>(&mut self, all_memory: &'b [u8], size: usize) -> &'b [u8] {let ptr = u32::from_le_bytes(*self.take_n::<4>());@@ -2412,113 +1000,6 @@ impl<'a> SliceExt<'a> for &'a [u8] {}}-#[test]-fn invalid_alignment() -> Result<()> {- let component = format!(- r#"(component- (core module $m- (memory (export "memory") 1)- (func (export "realloc") (param i32 i32 i32 i32) (result i32)- i32.const 1)-- (func (export "take-i32") (param i32))- (func (export "ret-1") (result i32) i32.const 1)- (func (export "ret-unaligned-list") (result i32)- (i32.store offset=0 (i32.const 8) (i32.const 1))- (i32.store offset=4 (i32.const 8) (i32.const 1))- i32.const 8)- )- (core instance $i (instantiate $m))-- (func (export "many-params")- (param "s1" string) (param "s2" string) (param "s3" string) (param "s4" string)- (param "s5" string) (param "s6" string) (param "s7" string) (param "s8" string)- (param "s9" string) (param "s10" string) (param "s11" string) (param "s12" string)- (canon lift- (core func $i "take-i32")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- (func (export "string-ret") (result string)- (canon lift- (core func $i "ret-1")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- (func (export "list-u32-ret") (result (list u32))- (canon lift- (core func $i "ret-unaligned-list")- (memory (core memory $i "memory"))- (realloc (core func $i "realloc"))- )- )- )"#- );-- let engine = super::engine();- let component = Component::new(&engine, component)?;- let instance = |store: &mut Store<()>| Linker::new(&engine).instantiate(store, &component);-- {- let mut store = Store::new(&engine, ());- let err = instance(&mut store)?- .get_typed_func::<(- &str,- &str,- &str,- &str,- &str,- &str,- &str,- &str,- &str,- &str,- &str,- &str,- ), ()>(&mut store, "many-params")?- .call(&mut store, ("", "", "", "", "", "", "", "", "", "", "", ""))- .unwrap_err();- assert!(- err.to_string()- .contains("realloc return: result not aligned"),- "{}",- err- );- }-- {- let mut store = Store::new(&engine, ());- let err = instance(&mut store)?- .get_typed_func::<(), (WasmStr,)>(&mut store, "string-ret")?- .call(&mut store, ())- .err()- .unwrap();- assert!(- err.to_string().contains("return pointer not aligned"),- "{}",- err- );- }-- {- let mut store = Store::new(&engine, ());- let err = instance(&mut store)?- .get_typed_func::<(), (WasmList<u32>,)>(&mut store, "list-u32-ret")?- .call(&mut store, ())- .err()- .unwrap();- assert!(- err.to_string().contains("list pointer is not aligned"),- "{}",- err- );- }-- Ok(())-}-#[test]fn drop_component_still_works() -> Result<()> {let component = r#"@@ -2862,6 +1343,174 @@ fn lower_then_lift() -> Result<()> {Ok(())}+// The dynamic `Val` API is covered by wast tests, but `TypedFunc` has its own+// lifting and lowering for options, results, strings, and lists.+#[test]+fn typed_lift_errors() -> Result<()> {+ let component = r#"+(component+ (import "host-option" (func $host (param "a" (option u8))))+ (core func $host (canon lower (func $host)))+ (core module $m+ (import "" "host" (func $host (param i32 i32)))+ (memory (export "memory") 1)+ (func (export "id") (param i32) (result i32) local.get 0)+ (func (export "second") (param i32 i32) (result i32) local.get 1)+ (func (export "store") (param i32 i32) (result i32)+ (i32.store (i32.const 8) (local.get 0))+ (i32.store (i32.const 12) (local.get 1))+ i32.const 8)+ (func (export "call-host") (param i32)+ (call $host (local.get 0) (i32.const 3)))+ )+ (core instance $i (instantiate $m+ (with "" (instance (export "host" (func $host))))))+ (alias core export $i "memory" (core memory $mem))++ (func (export "call-host") (param "a" u32)+ (canon lift (core func $i "call-host")))+ (func (export "result-unit") (param "a" u32) (result (result))+ (canon lift (core func $i "id")))+ (func (export "option-u32") (param "a" u32) (param "b" u32)+ (result (option u32))+ (canon lift (core func $i "store") (memory $mem)))+ (func (export "result-s16-f32") (param "a" u32) (param "b" u32)+ (result (result s16 (error f32)))+ (canon lift (core func $i "store") (memory $mem)))+ (func (export "list-u8") (param "a" u32) (param "b" u32)+ (result (list u8))+ (canon lift (core func $i "store") (memory $mem)))+ (func (export "list-u32") (param "a" u32) (param "b" u32)+ (result (list u32))+ (canon lift (core func $i "store") (memory $mem)))+ (func (export "string") (param "a" u32) (param "b" u32) (result string)+ (canon lift (core func $i "store") (memory $mem)))+ (func (export "result-u8-f32-payload") (param "a" (result u8 (error f32)))+ (result u32)+ (canon lift (core func $i "second")))+ (func (export "ret-ptr") (param "a" u32) (result (tuple u32 u32))+ (canon lift (core func $i "id") (memory $mem)))+)+"#;++ let engine = super::engine();+ let component = Component::new(&engine, component)?;+ let mut linker = Linker::new(&engine);+ linker+ .root()+ .func_wrap("host-option", |_, (a,): (Option<u8>,)| {+ assert_eq!(a, Some(3));+ Ok(())+ })?;+ let mut store = Store::new(&engine, ());+ let instance = linker.instantiate(&mut store, &component)?;++ // Each error poisons the instance, so use a fresh one for each.+ fn expect_err<P, R>(+ linker: &Linker<()>,+ component: &Component,+ name: &str,+ params: P,+ msg: &str,+ ) where+ P: ComponentNamedList + Lower + 'static,+ R: ComponentNamedList + Lift + 'static,+ {+ let mut store = Store::new(linker.engine(), ());+ let instance = linker.instantiate(&mut store, component).unwrap();+ let f = instance.get_typed_func::<P, R>(&mut store, name).unwrap();+ let err = f.call(&mut store, params).err().unwrap();+ assert!(format!("{err:?}").contains(msg), "{err:?}");+ }++ // Options and results lifted from flat values.+ let f = instance.get_typed_func::<(u32,), ()>(&mut store, "call-host")?;+ f.call(&mut store, (1,))?;+ let f = instance.get_typed_func::<(u32,), (Result<(), ()>,)>(&mut store, "result-unit")?;+ assert_eq!(f.call(&mut store, (0,))?, (Ok(()),));+ assert_eq!(f.call(&mut store, (1,))?, (Err(()),));+ expect_err::<(u32,), ()>(+ &linker,+ &component,+ "call-host",+ (2,),+ "invalid option discriminant",+ );+ expect_err::<(u32,), (Result<(), ()>,)>(+ &linker,+ &component,+ "result-unit",+ (2,),+ "invalid expected discriminant",+ );++ // Options and results lifted from memory.+ let f = instance.get_typed_func::<(u32, u32), (Option<u32>,)>(&mut store, "option-u32")?;+ assert_eq!(f.call(&mut store, (0, 0))?, (None,));+ assert_eq!(f.call(&mut store, (1, 0x1234fead))?, (Some(0x1234fead),));+ let f =+ instance.get_typed_func::<(u32, u32), (Result<i16, f32>,)>(&mut store, "result-s16-f32")?;+ assert_eq!(f.call(&mut store, (0, 100))?, (Ok(100),));+ let (ret,) = f.call(&mut store, (1, CANON_32BIT_NAN | 1))?;+ assert_eq!(ret.unwrap_err().to_bits(), CANON_32BIT_NAN | 1);+ expect_err::<(u32, u32), (Option<u32>,)>(+ &linker,+ &component,+ "option-u32",+ (2, 0),+ "invalid option discriminant",+ );+ expect_err::<(u32, u32), (Result<i16, f32>,)>(+ &linker,+ &component,+ "result-s16-f32",+ (2, 0),+ "invalid expected discriminant",+ );++ // Strings and lists with invalid pointers.+ expect_err::<(u32, u32), (WasmList<u8>,)>(+ &linker,+ &component,+ "list-u8",+ (100_000, 1),+ "list pointer/length out of bounds",+ );+ expect_err::<(u32, u32), (WasmList<u32>,)>(+ &linker,+ &component,+ "list-u32",+ (1, 1),+ "list pointer is not aligned",+ );+ expect_err::<(u32, u32), (WasmStr,)>(+ &linker,+ &component,+ "string",+ (100_000, 1),+ "string pointer/length out of bounds",+ );++ // A return pointer that is aligned but out of bounds.+ expect_err::<(u32,), ((u32, u32),)>(+ &linker,+ &component,+ "ret-ptr",+ (0xffff_fff8,),+ "pointer out of bounds of memory",+ );++ // A result whose `u8` and `f32` payloads share one flat `i32` slot.+ let f = instance+ .get_typed_func::<(Result<u8, f32>,), (u32,)>(&mut store, "result-u8-f32-payload")?;+ assert_eq!(f.call(&mut store, (Ok(7),))?, (7,));+ assert_eq!(f.call(&mut store, (Err(1.5),))?, (1.5f32.to_bits(),));+ let nan = f32::from_bits(CANON_32BIT_NAN | 1);+ assert_eq!(f.call(&mut store, (Err(nan),))?, (CANON_32BIT_NAN | 1,));++ Ok(())+}+#[test]fn errors_that_poison_instance() -> Result<()> {let component = format!(@@ -2961,53 +1610,6 @@ fn errors_that_poison_instance() -> Result<()> {}}-#[test]-fn run_export_with_internal_adapter() -> Result<()> {- let component = r#"-(component- (type $t (func (param "a" u32) (result u32)))- (component $a- (core module $m- (func (export "add-five") (param i32) (result i32)- local.get 0- i32.const 5- i32.add)- )- (core instance $m (instantiate $m))- (func (export "add-five") (type $t) (canon lift (core func $m "add-five")))- )- (component $b- (import "interface-v1" (instance $i- (export "add-five" (func (type $t)))))- (core module $m- (func $add-five (import "interface-0.1.0" "add-five") (param i32) (result i32))- (func) ;; causes index out of bounds- (func (export "run") (result i32) i32.const 0 call $add-five)- )- (core func $add-five (canon lower (func $i "add-five")))- (core instance $i (instantiate 0- (with "interface-0.1.0" (instance- (export "add-five" (func $add-five))- ))- ))- (func (result u32) (canon lift (core func $i "run")))- (export "run" (func 1))- )- (instance $a (instantiate $a))- (instance $b (instantiate $b (with "interface-v1" (instance $a))))- (export "run" (func $b "run"))-)-"#;- let engine = super::engine();- let component = Component::new(&engine, component)?;- let mut store = Store::new(&engine, ());- let linker = Linker::new(&engine);- let instance = linker.instantiate(&mut store, &component)?;- let run = instance.get_typed_func::<(), (u32,)>(&mut store, "run")?;- assert_eq!(run.call(&mut store, ())?, (5,));- Ok(())-}-enum RecurseKind {AThenA,AThenB,@@ -3090,11 +1692,6 @@ async fn thread_index_via_instantiation_sync() -> Result<()> {thread_index_via_instantiation(ApiStyle::Sync).await}-#[tokio::test]-async fn thread_index_via_instantiation_async() -> Result<()> {- thread_index_via_instantiation(ApiStyle::Async).await-}-async fn thread_index_via_instantiation(style: ApiStyle) -> Result<()> {let component = r#"(component@@ -3124,11 +1721,6 @@ async fn thread_index_via_call_sync() -> Result<()> {thread_index_via_call(ApiStyle::Sync).await}-#[tokio::test]-async fn thread_index_via_call_async() -> Result<()> {- thread_index_via_call(ApiStyle::Async).await-}-#[tokio::test]async fn thread_index_via_call_concurrent() -> Result<()> {thread_index_via_call(ApiStyle::Concurrent).await@@ -3263,11 +1855,6 @@ async fn thread_index_via_guest_call_sync() -> Result<()> {thread_index_via_guest_call(ApiStyle::Sync).await}-#[tokio::test]-async fn thread_index_via_guest_call_async() -> Result<()> {- thread_index_via_guest_call(ApiStyle::Async).await-}-#[tokio::test]async fn thread_index_via_guest_call_concurrent() -> Result<()> {thread_index_via_guest_call(ApiStyle::Concurrent).await@@ -3329,16 +1916,6 @@ async fn thread_index_via_guest_call(style: ApiStyle) -> Result<()> {Ok(())}-fn with_new_instance<T>(- engine: &Engine,- component: &Component,- fun: impl Fn(&mut Store<()>, Instance) -> wasmtime::Result<T>,-) -> wasmtime::Result<T> {- let mut store = Store::new(engine, ());- let instance = Linker::new(engine).instantiate(&mut store, component)?;- fun(&mut store, instance)-}-#[tokio::test]async fn drop_call_async_future() -> Result<()> {let component = r#"diff --git a/tests/misc_testsuite/component-model-threading/threading-builtins.wast b/tests/misc_testsuite/component-model-threading/threading-builtins.wastindex 9fa11cd390..4a66890f6a 100644--- a/tests/misc_testsuite/component-model-threading/threading-builtins.wast+++ b/tests/misc_testsuite/component-model-threading/threading-builtins.wast@@ -148,3 +148,73 @@)))(assert_trap (invoke "run" (str.const "x")) "cannot leave component instance")++;; `thread.index` is nonzero in a start function.+(component+ (core module $m+ (import "" "thread.index" (func $thread-index (result i32)))+ (func $start+ (if (i32.eqz (call $thread-index)) (then unreachable)))+ (start $start)+ )+ (core func $thread-index (canon thread.index))+ (core instance $m (instantiate $m (with "" (instance+ (export "thread.index" (func $thread-index))+ ))))+)++;; `thread.index` is nonzero in a call from the host.+(component+ (core module $m+ (import "" "thread.index" (func $thread-index (result i32)))+ (func (export "run")+ (if (i32.eqz (call $thread-index)) (then unreachable)))+ )+ (core func $thread-index (canon thread.index))+ (core instance $m (instantiate $m (with "" (instance+ (export "thread.index" (func $thread-index))+ ))))+ (func (export "run") (canon lift (core func $m "run")))+)+(assert_return (invoke "run"))++;; `thread.index` is nonzero in both the caller and callee of a guest-to-guest+;; call.+(component+ (component $c+ (core module $m+ (import "" "thread.index" (func $thread-index (result i32)))+ (func (export "run") (result i32)+ (call $thread-index))+ )+ (core func $thread-index (canon thread.index))+ (core instance $m (instantiate $m (with "" (instance+ (export "thread.index" (func $thread-index))+ ))))+ (func (export "run") (result u32) (canon lift (core func $m "run")))+ )+ (instance $c (instantiate $c))++ (component $d+ (import "c" (instance $c+ (export "run" (func (result u32)))+ ))+ (core func $run (canon lower (func $c "run")))+ (core module $m+ (import "" "thread.index" (func $thread-index (result i32)))+ (import "" "run" (func $run (result i32)))+ (func (export "run")+ (if (i32.eqz (call $thread-index)) (then unreachable))+ (if (i32.eqz (call $run)) (then unreachable)))+ )+ (core func $thread-index (canon thread.index))+ (core instance $m (instantiate $m (with "" (instance+ (export "thread.index" (func $thread-index))+ (export "run" (func $run))+ ))))+ (func (export "run") (canon lift (core func $m "run")))+ )+ (instance $d (instantiate $d (with "c" (instance $c))))+ (func (export "run") (alias export $d "run"))+)+(assert_return (invoke "run"))diff --git a/tests/misc_testsuite/component-model/adapter.wast b/tests/misc_testsuite/component-model/adapter.wastindex 79479c197d..d0dc71c90d 100644--- a/tests/misc_testsuite/component-model/adapter.wast+++ b/tests/misc_testsuite/component-model/adapter.wast@@ -176,3 +176,249 @@)(assert_return (invoke "run" (variant.const "a0")) (variant.const "a0"))++;; Exports where `realloc` traps or returns out-of-bounds pointers while the+;; host lowers arguments.+(component definition $Traps+ (type $many (func+ (param "s1" string) (param "s2" string) (param "s3" string)+ (param "s4" string) (param "s5" string) (param "s6" string)+ (param "s7" string) (param "s8" string) (param "s9" string)+ (param "s10" string)))++ ;; `realloc` traps.+ (core module $m+ (memory (export "memory") 1)+ (func (export "take-many") (param i32))+ (func (export "take-list") (param i32 i32))+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ unreachable)+ )+ (core instance $i (instantiate $m))+ (func (export "take-list-unreachable") (param "a" (list u8))+ (canon lift (core func $i "take-list") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+ (func (export "take-string-unreachable") (param "a" string)+ (canon lift (core func $i "take-list") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+ (func (export "take-many-unreachable") (type $many)+ (canon lift (core func $i "take-many") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))++ ;; `realloc` returns a pointer far beyond the end of memory.+ (core module $m2+ (memory (export "memory") 1)+ (func (export "take-many") (param i32))+ (func (export "take-list") (param i32 i32))+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ i32.const 0x3fffff00)+ )+ (core instance $i2 (instantiate $m2))+ (func (export "take-list-base-oob") (param "a" (list u8))+ (canon lift (core func $i2 "take-list") (memory (core memory $i2 "memory"))+ (realloc (core func $i2 "realloc"))))+ (func (export "take-string-base-oob") (param "a" string)+ (canon lift (core func $i2 "take-list") (memory (core memory $i2 "memory"))+ (realloc (core func $i2 "realloc"))))+ (func (export "take-many-base-oob") (type $many)+ (canon lift (core func $i2 "take-many") (memory (core memory $i2 "memory"))+ (realloc (core func $i2 "realloc"))))++ ;; `realloc` returns a pointer 4 bytes from the end of memory.+ (core module $m3+ (memory (export "memory") 1)+ (func (export "take-many") (param i32))+ (func (export "take-list") (param i32 i32))+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ i32.const 65532)+ )+ (core instance $i3 (instantiate $m3))+ (func (export "take-list-end-oob") (param "a" (list u8))+ (canon lift (core func $i3 "take-list") (memory (core memory $i3 "memory"))+ (realloc (core func $i3 "realloc"))))+ (func (export "take-string-end-oob") (param "a" string)+ (canon lift (core func $i3 "take-list") (memory (core memory $i3 "memory"))+ (realloc (core func $i3 "realloc"))))+ (func (export "take-many-end-oob") (type $many)+ (canon lift (core func $i3 "take-many") (memory (core memory $i3 "memory"))+ (realloc (core func $i3 "realloc"))))++ ;; The first allocation, for the space to store all the arguments, is+ ;; in-bounds, but all further allocations are out of bounds.+ (core module $m4+ (memory (export "memory") 1)+ (func (export "take-many") (param i32))+ (global $cnt (mut i32) (i32.const 0))+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (if (result i32) (global.get $cnt)+ (then (i32.const 100000))+ (else+ (global.set $cnt (i32.const 1))+ (i32.const 0))))+ )+ (core instance $i4 (instantiate $m4))+ (func (export "take-many-second-oob") (type $many)+ (canon lift (core func $i4 "take-many") (memory (core memory $i4 "memory"))+ (realloc (core func $i4 "realloc"))))+)++(component instance $Traps $Traps)+(assert_trap (invoke "take-list-unreachable" (list.const)) "unreachable")+(component instance $Traps $Traps)+(assert_trap (invoke "take-string-unreachable" (str.const "")) "unreachable")+(component instance $Traps $Traps)+(assert_trap+ (invoke "take-many-unreachable"+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const "")+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const ""))+ "unreachable")++(component instance $Traps $Traps)+(assert_trap (invoke "take-list-base-oob" (list.const))+ "realloc return: beyond end of memory")+(component instance $Traps $Traps)+(assert_trap (invoke "take-list-base-oob" (list.const (u8.const 1)))+ "realloc return: beyond end of memory")+(component instance $Traps $Traps)+(assert_trap (invoke "take-string-base-oob" (str.const ""))+ "realloc return: beyond end of memory")+(component instance $Traps $Traps)+(assert_trap (invoke "take-string-base-oob" (str.const "x"))+ "realloc return: beyond end of memory")+(component instance $Traps $Traps)+(assert_trap+ (invoke "take-many-base-oob"+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const "")+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const ""))+ "realloc return: beyond end of memory")++;; Empty things fit at the end of memory, but larger things don't.+(component instance $Traps $Traps)+(assert_return (invoke "take-list-end-oob" (list.const)))+(assert_return (invoke "take-list-end-oob"+ (list.const (u8.const 1) (u8.const 2) (u8.const 3) (u8.const 4))))+(assert_trap+ (invoke "take-list-end-oob"+ (list.const (u8.const 1) (u8.const 2) (u8.const 3) (u8.const 4) (u8.const 5)))+ "realloc return: beyond end of memory")+(component instance $Traps $Traps)+(assert_return (invoke "take-string-end-oob" (str.const "")))+(assert_return (invoke "take-string-end-oob" (str.const "abcd")))+(assert_trap (invoke "take-string-end-oob" (str.const "abcde"))+ "realloc return: beyond end of memory")+(component instance $Traps $Traps)+(assert_trap+ (invoke "take-many-end-oob"+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const "")+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const ""))+ "realloc return: beyond end of memory")++(component instance $Traps $Traps)+(assert_trap+ (invoke "take-many-second-oob"+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const "")+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const ""))+ "realloc return: beyond end of memory")+(component instance $Traps $Traps)+(assert_trap+ (invoke "take-many-second-oob"+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const "")+ (str.const "") (str.const "") (str.const "") (str.const "") (str.const "x"))+ "realloc return: beyond end of memory")++;; Returned lists and strings that are out of bounds.+(component definition $ListOob+ (core module $m+ (memory (export "memory") 1)+ (func (export "ret-list") (result i32)+ (i32.store offset=0 (i32.const 8) (i32.const 100000))+ (i32.store offset=4 (i32.const 8) (i32.const 1))+ i32.const 8)+ )+ (core instance $i (instantiate $m))+ (func (export "ret-list-u8") (result (list u8))+ (canon lift (core func $i "ret-list") (memory (core memory $i "memory"))))+ (func (export "ret-string") (result string)+ (canon lift (core func $i "ret-list") (memory (core memory $i "memory"))))+)+(component instance $ListOob $ListOob)+(assert_trap (invoke "ret-list-u8") "out of bounds")+(component instance $ListOob $ListOob)+(assert_trap (invoke "ret-string") "out of bounds")++;; Misaligned pointers from `realloc` and in return values.+(component definition $Align+ (core module $m+ (memory (export "memory") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ i32.const 1)+ (func (export "take-i32") (param i32))+ (func (export "ret-1") (result i32) i32.const 1)+ (func (export "ret-unaligned-list") (result i32)+ (i32.store offset=0 (i32.const 8) (i32.const 1))+ (i32.store offset=4 (i32.const 8) (i32.const 1))+ i32.const 8)+ )+ (core instance $i (instantiate $m))++ (func (export "many-params")+ (param "s1" string) (param "s2" string) (param "s3" string) (param "s4" string)+ (param "s5" string) (param "s6" string) (param "s7" string) (param "s8" string)+ (param "s9" string) (param "s10" string) (param "s11" string) (param "s12" string)+ (canon lift (core func $i "take-i32") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+ (func (export "string-ret") (result string)+ (canon lift (core func $i "ret-1") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+ (func (export "list-u32-ret") (result (list u32))+ (canon lift (core func $i "ret-unaligned-list") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+)+(component instance $Align $Align)+(assert_trap+ (invoke "many-params"+ (str.const "") (str.const "") (str.const "") (str.const "")+ (str.const "") (str.const "") (str.const "") (str.const "")+ (str.const "") (str.const "") (str.const "") (str.const ""))+ "realloc return: result not aligned")+(component instance $Align $Align)+(assert_trap (invoke "string-ret") "return pointer not aligned")+(component instance $Align $Align)+(assert_trap (invoke "list-u32-ret") "list pointer is not aligned")++;; An export of a nested component whose import is satisfied by another nested+;; component's export, which requires an adapter internal to this component.+(component+ (type $t (func (param "a" u32) (result u32)))+ (component $a+ (core module $m+ (func (export "add-five") (param i32) (result i32)+ local.get 0+ i32.const 5+ i32.add)+ )+ (core instance $m (instantiate $m))+ (func (export "add-five") (type $t) (canon lift (core func $m "add-five")))+ )+ (component $b+ (import "interface-v1" (instance $i+ (export "add-five" (func (type $t)))))+ (core module $m+ (func $add-five (import "interface-0.1.0" "add-five") (param i32) (result i32))+ (func) ;; causes index out of bounds+ (func (export "run") (result i32) i32.const 0 call $add-five)+ )+ (core func $add-five (canon lower (func $i "add-five")))+ (core instance $i (instantiate 0+ (with "interface-0.1.0" (instance+ (export "add-five" (func $add-five))+ ))+ ))+ (func (result u32) (canon lift (core func $i "run")))+ (export "run" (func 1))+ )+ (instance $a (instantiate $a))+ (instance $b (instantiate $b (with "interface-v1" (instance $a))))+ (export "run" (func $b "run"))+)+(assert_return (invoke "run") (u32.const 5))diff --git a/tests/misc_testsuite/component-model/async/many-params-with-retptr.wast b/tests/misc_testsuite/component-model/async/many-params-with-retptr.wastindex c30f40413a..ccf16dc689 100644--- a/tests/misc_testsuite/component-model/async/many-params-with-retptr.wast+++ b/tests/misc_testsuite/component-model/async/many-params-with-retptr.wast@@ -191,3 +191,214 @@))"unreachable")++;; Many parameters of an `async`-typed function lifted without `async`, which+;; are passed in memory. The callee checks the in-memory layout of the+;; parameters and then returns the same memory as its result.+(component+ (core module $m+ (memory (export "memory") 1)+ (global $last (mut i32) (i32.const 8))+ (func (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 "foo") (param $p i32) (result i32)+ (if (i32.ne (i32.load8_s offset=0 (local.get $p)) (i32.const -100)) (then unreachable))+ (if (i64.ne (i64.load offset=8 (local.get $p)) (i64.const 0x7fffffffffffffff))+ (then unreachable))+ (if (i32.ne (i32.load offset=16 (local.get $p)) (i32.const 0x7fc00001)) (then unreachable))+ (if (i32.ne (i32.load8_u offset=20 (local.get $p)) (i32.const 38)) (then unreachable))+ (if (i32.ne (i32.load16_s offset=22 (local.get $p)) (i32.const 18831)) (then unreachable))+ (if (i32.ne (i32.load offset=28 (local.get $p)) (i32.const 24)) (then unreachable))+ (if (i32.ne (i32.load offset=36 (local.get $p)) (i32.const 8)) (then unreachable))+ (if (i32.ne (i32.load8_u offset=40 (local.get $p)) (i32.const 1)) (then unreachable))+ (if (i32.ne (i32.load8_u offset=41 (local.get $p)) (i32.const 0)) (then unreachable))+ (if (i32.ne (i32.load offset=44 (local.get $p)) (i32.const 0x1f6a9)) (then unreachable))+ (if (i32.ne (i32.load offset=52 (local.get $p)) (i32.const 5)) (then unreachable))+ (if (i32.ne (i32.load offset=60 (local.get $p)) (i32.const 5)) (then unreachable))+ (if (i32.ne (i32.load offset=68 (local.get $p)) (i32.const 5)) (then unreachable))+ (local.get $p))+ )+ (core instance $i (instantiate $m))++ (type $tuple (tuple+ s8 ;; offset 0, size 1+ u64 ;; offset 8, size 8+ f32 ;; offset 16, size 4+ u8 ;; offset 20, size 1+ s16 ;; offset 22, size 2+ string ;; offset 24, size 8+ (list u32) ;; offset 32, size 8+ bool ;; offset 40, size 1+ bool ;; offset 41, size 1+ char ;; offset 44, size 4+ (list bool) ;; offset 48, size 8+ (list char) ;; offset 56, size 8+ (list string))) ;; offset 64, size 8+ (type $t (func async+ (param "p1" s8)+ (param "p2" u64)+ (param "p3" f32)+ (param "p4" u8)+ (param "p5" s16)+ (param "p6" string)+ (param "p7" (list u32))+ (param "p8" bool)+ (param "p9" bool)+ (param "p0" char)+ (param "pa" (list bool))+ (param "pb" (list char))+ (param "pc" (list string))+ (result $tuple)))+ (func (export "many-param") (type $t)+ (canon lift (core func $i "foo") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+)++(assert_return+ (invoke "many-param"+ (s8.const -100)+ (u64.const 0x7fffffffffffffff)+ (f32.const nan:0x400001)+ (u8.const 38)+ (s16.const 18831)+ (str.const "this is the first string")+ (list.const+ (u32.const 1) (u32.const 2) (u32.const 3) (u32.const 4)+ (u32.const 5) (u32.const 6) (u32.const 7) (u32.const 8))+ (bool.const true)+ (bool.const false)+ (char.const "π©")+ (list.const+ (bool.const false) (bool.const true) (bool.const false)+ (bool.const true) (bool.const true))+ (list.const+ (char.const "π") (char.const "π₯") (char.const "π")+ (char.const "π") (char.const "π‘"))+ (list.const+ (str.const "the quick")+ (str.const "brown fox")+ (str.const "was too lazy")+ (str.const "to jump over the dog")+ (str.const "what a demanding dog")))+ (tuple.const+ (s8.const -100)+ (u64.const 0x7fffffffffffffff)+ (f32.const nan:0x400001)+ (u8.const 38)+ (s16.const 18831)+ (str.const "this is the first string")+ (list.const+ (u32.const 1) (u32.const 2) (u32.const 3) (u32.const 4)+ (u32.const 5) (u32.const 6) (u32.const 7) (u32.const 8))+ (bool.const true)+ (bool.const false)+ (char.const "π©")+ (list.const+ (bool.const false) (bool.const true) (bool.const false)+ (bool.const true) (bool.const true))+ (list.const+ (char.const "π") (char.const "π₯") (char.const "π")+ (char.const "π") (char.const "π‘"))+ (list.const+ (str.const "the quick")+ (str.const "brown fox")+ (str.const "was too lazy")+ (str.const "to jump over the dog")+ (str.const "what a demanding dog"))))++;; Many results of an `async`-typed function lifted without `async`, which are+;; returned through memory.+(component+ (core module $m+ (memory (export "memory") 1)++ ;; Strings and list contents referenced by the result.+ (data (i32.const 100) "abcdef")+ (data (i32.const 112)+ "\01\00\00\00\02\00\00\00\03\00\00\00\04\00\00\00"+ "\05\00\00\00\06\00\00\00\07\00\00\00\08\00\00\00")+ (data (i32.const 144) "\00\01\00\01\01")+ (data (i32.const 152)+ "\4c\f3\01\00" ;; 'π'+ "\50\f9\01\00" ;; 'π₯'+ "\57\f3\01\00" ;; 'π'+ "\59\f3\01\00" ;; 'π'+ "\61\f3\01\00" ;; 'π‘'+ )+ (data (i32.const 172)+ "\66\00\00\00\02\00\00\00" ;; "cd"+ "\68\00\00\00\02\00\00\00" ;; "ef"+ )++ (func (export "foo") (result i32)+ (local $base i32)+ (local.set $base (i32.const 8))+ (i32.store8 offset=0 (local.get $base) (i32.const -100))+ (i64.store offset=8 (local.get $base) (i64.const 0x7fffffffffffffff))+ (i32.store offset=16 (local.get $base) (i32.const 0x7fc00001))+ (i32.store8 offset=20 (local.get $base) (i32.const 38))+ (i32.store16 offset=22 (local.get $base) (i32.const 18831))+ (i32.store offset=24 (local.get $base) (i32.const 100))+ (i32.store offset=28 (local.get $base) (i32.const 2))+ (i32.store offset=32 (local.get $base) (i32.const 112))+ (i32.store offset=36 (local.get $base) (i32.const 8))+ (i32.store8 offset=40 (local.get $base) (i32.const 1))+ (i32.store8 offset=41 (local.get $base) (i32.const 0))+ (i32.store offset=44 (local.get $base) (i32.const 0x1f6a9)) ;; 'π©'+ (i32.store offset=48 (local.get $base) (i32.const 144))+ (i32.store offset=52 (local.get $base) (i32.const 5))+ (i32.store offset=56 (local.get $base) (i32.const 152))+ (i32.store offset=60 (local.get $base) (i32.const 5))+ (i32.store offset=64 (local.get $base) (i32.const 172))+ (i32.store offset=68 (local.get $base) (i32.const 2))+ (local.get $base))+ )+ (core instance $i (instantiate $m))++ (type $tuple (tuple+ s8+ u64+ f32+ u8+ s16+ string+ (list u32)+ bool+ bool+ char+ (list bool)+ (list char)+ (list string)))+ (type $t (func async (result $tuple)))+ (func (export "many-results") (type $t)+ (canon lift (core func $i "foo") (memory (core memory $i "memory"))))+)++(assert_return (invoke "many-results")+ (tuple.const+ (s8.const -100)+ (u64.const 0x7fffffffffffffff)+ (f32.const nan:0x400001)+ (u8.const 38)+ (s16.const 18831)+ (str.const "ab")+ (list.const+ (u32.const 1) (u32.const 2) (u32.const 3) (u32.const 4)+ (u32.const 5) (u32.const 6) (u32.const 7) (u32.const 8))+ (bool.const true)+ (bool.const false)+ (char.const "π©")+ (list.const+ (bool.const false) (bool.const true) (bool.const false)+ (bool.const true) (bool.const true))+ (list.const+ (char.const "π") (char.const "π₯") (char.const "π")+ (char.const "π") (char.const "π‘"))+ (list.const (str.const "cd") (str.const "ef"))))diff --git a/tests/misc_testsuite/component-model/trap.wast b/tests/misc_testsuite/component-model/trap.wastindex 9653efe031..8e65a3b60d 100644--- a/tests/misc_testsuite/component-model/trap.wast+++ b/tests/misc_testsuite/component-model/trap.wast@@ -28,3 +28,17 @@)(assert_trap (invoke "g") "wasm `unreachable` instruction executed")++;; Lifted thunks that return and that trap.+(component+ (core module $m+ (func (export "thunk"))+ (func (export "thunk-trap") unreachable)+ )+ (core instance $i (instantiate $m))+ (func (export "thunk") (canon lift (core func $i "thunk")))+ (func (export "thunk-trap") (canon lift (core func $i "thunk-trap")))+)++(assert_return (invoke "thunk"))+(assert_trap (invoke "thunk-trap") "wasm `unreachable` instruction executed")diff --git a/tests/misc_testsuite/component-model/types.wast b/tests/misc_testsuite/component-model/types.wastindex 5ddff06a14..8563bc35d0 100644--- a/tests/misc_testsuite/component-model/types.wast+++ b/tests/misc_testsuite/component-model/types.wast@@ -379,3 +379,486 @@(assert_return (invoke "f" (u32.const 0)) (variant.const "a" (u32.const 0)))(assert_return (invoke "f" (u32.const 1)) (variant.const "b"))(assert_trap (invoke "f" (u32.const 2)) "discriminant 2 out of range [0..2)")++;; Lifting and lowering integers of all sizes.+(component definition $Ints+ (core module $m+ (func (export "take-i32-100") (param i32)+ (if (i32.ne (local.get 0) (i32.const 100)) (then unreachable)))+ (func (export "take-i64-100") (param i64)+ (if (i64.ne (local.get 0) (i64.const 100)) (then unreachable)))+ (func (export "ret-i32-0") (result i32) i32.const 0)+ (func (export "ret-i64-0") (result i64) i64.const 0)+ (func (export "ret-i32-minus-1") (result i32) i32.const -1)+ (func (export "ret-i64-minus-1") (result i64) i64.const -1)+ (func (export "ret-i32-100000") (result i32) i32.const 100000)+ )+ (core instance $i (instantiate $m))+ (func (export "take-u8") (param "a" u8) (canon lift (core func $i "take-i32-100")))+ (func (export "take-s8") (param "a" s8) (canon lift (core func $i "take-i32-100")))+ (func (export "take-u16") (param "a" u16) (canon lift (core func $i "take-i32-100")))+ (func (export "take-s16") (param "a" s16) (canon lift (core func $i "take-i32-100")))+ (func (export "take-u32") (param "a" u32) (canon lift (core func $i "take-i32-100")))+ (func (export "take-s32") (param "a" s32) (canon lift (core func $i "take-i32-100")))+ (func (export "take-u64") (param "a" u64) (canon lift (core func $i "take-i64-100")))+ (func (export "take-s64") (param "a" s64) (canon lift (core func $i "take-i64-100")))++ (func (export "ret-u8") (result u8) (canon lift (core func $i "ret-i32-0")))+ (func (export "ret-s8") (result s8) (canon lift (core func $i "ret-i32-0")))+ (func (export "ret-u16") (result u16) (canon lift (core func $i "ret-i32-0")))+ (func (export "ret-s16") (result s16) (canon lift (core func $i "ret-i32-0")))+ (func (export "ret-u32") (result u32) (canon lift (core func $i "ret-i32-0")))+ (func (export "ret-s32") (result s32) (canon lift (core func $i "ret-i32-0")))+ (func (export "ret-u64") (result u64) (canon lift (core func $i "ret-i64-0")))+ (func (export "ret-s64") (result s64) (canon lift (core func $i "ret-i64-0")))++ (func (export "retm1-u8") (result u8) (canon lift (core func $i "ret-i32-minus-1")))+ (func (export "retm1-s8") (result s8) (canon lift (core func $i "ret-i32-minus-1")))+ (func (export "retm1-u16") (result u16) (canon lift (core func $i "ret-i32-minus-1")))+ (func (export "retm1-s16") (result s16) (canon lift (core func $i "ret-i32-minus-1")))+ (func (export "retm1-u32") (result u32) (canon lift (core func $i "ret-i32-minus-1")))+ (func (export "retm1-s32") (result s32) (canon lift (core func $i "ret-i32-minus-1")))+ (func (export "retm1-u64") (result u64) (canon lift (core func $i "ret-i64-minus-1")))+ (func (export "retm1-s64") (result s64) (canon lift (core func $i "ret-i64-minus-1")))++ (func (export "retbig-u8") (result u8) (canon lift (core func $i "ret-i32-100000")))+ (func (export "retbig-s8") (result s8) (canon lift (core func $i "ret-i32-100000")))+ (func (export "retbig-u16") (result u16) (canon lift (core func $i "ret-i32-100000")))+ (func (export "retbig-s16") (result s16) (canon lift (core func $i "ret-i32-100000")))+ (func (export "retbig-u32") (result u32) (canon lift (core func $i "ret-i32-100000")))+ (func (export "retbig-s32") (result s32) (canon lift (core func $i "ret-i32-100000")))+)+(component instance $Ints $Ints)++;; Passing in 100 is valid for all primitives.+(assert_return (invoke "take-u8" (u8.const 100)))+(assert_return (invoke "take-s8" (s8.const 100)))+(assert_return (invoke "take-u16" (u16.const 100)))+(assert_return (invoke "take-s16" (s16.const 100)))+(assert_return (invoke "take-u32" (u32.const 100)))+(assert_return (invoke "take-s32" (s32.const 100)))+(assert_return (invoke "take-u64" (u64.const 100)))+(assert_return (invoke "take-s64" (s64.const 100)))++;; Zero can be returned as any integer.+(assert_return (invoke "ret-u8") (u8.const 0))+(assert_return (invoke "ret-s8") (s8.const 0))+(assert_return (invoke "ret-u16") (u16.const 0))+(assert_return (invoke "ret-s16") (s16.const 0))+(assert_return (invoke "ret-u32") (u32.const 0))+(assert_return (invoke "ret-s32") (s32.const 0))+(assert_return (invoke "ret-u64") (u64.const 0))+(assert_return (invoke "ret-s64") (s64.const 0))++;; Returning -1 reinterprets the bytes as defined by each type.+(assert_return (invoke "retm1-u8") (u8.const 0xff))+(assert_return (invoke "retm1-s8") (s8.const -1))+(assert_return (invoke "retm1-u16") (u16.const 0xffff))+(assert_return (invoke "retm1-s16") (s16.const -1))+(assert_return (invoke "retm1-u32") (u32.const 0xffffffff))+(assert_return (invoke "retm1-s32") (s32.const -1))+(assert_return (invoke "retm1-u64") (u64.const 0xffffffffffffffff))+(assert_return (invoke "retm1-s64") (s64.const -1))++;; Returning 100000 chops off bytes as necessary.+(assert_return (invoke "retbig-u8") (u8.const 0xa0))+(assert_return (invoke "retbig-s8") (s8.const -96))+(assert_return (invoke "retbig-u16") (u16.const 0x86a0))+(assert_return (invoke "retbig-s16") (s16.const -31072))+(assert_return (invoke "retbig-u32") (u32.const 100000))+(assert_return (invoke "retbig-s32") (s32.const 100000))++;; The core function traps if any value other than 100 is passed.+(component instance $Ints $Ints)+(assert_trap (invoke "take-u8" (u8.const 101)) "unreachable")+(component instance $Ints $Ints)+(assert_trap (invoke "take-s8" (s8.const 101)) "unreachable")+(component instance $Ints $Ints)+(assert_trap (invoke "take-u16" (u16.const 101)) "unreachable")+(component instance $Ints $Ints)+(assert_trap (invoke "take-s16" (s16.const 101)) "unreachable")+(component instance $Ints $Ints)+(assert_trap (invoke "take-u32" (u32.const 101)) "unreachable")+(component instance $Ints $Ints)+(assert_trap (invoke "take-s32" (s32.const 101)) "unreachable")+(component instance $Ints $Ints)+(assert_trap (invoke "take-u64" (u64.const 101)) "unreachable")+(component instance $Ints $Ints)+(assert_trap (invoke "take-s64" (s64.const 101)) "unreachable")++;; Floats, including NaN payloads, pass through unmodified.+(component+ (core module $m+ (func (export "i32.reinterpret_f32") (param f32) (result i32)+ local.get 0+ i32.reinterpret_f32)+ (func (export "i64.reinterpret_f64") (param f64) (result i64)+ local.get 0+ i64.reinterpret_f64)+ (func (export "f32.reinterpret_i32") (param i32) (result f32)+ local.get 0+ f32.reinterpret_i32)+ (func (export "f64.reinterpret_i64") (param i64) (result f64)+ local.get 0+ f64.reinterpret_i64)+ )+ (core instance $i (instantiate $m))++ (func (export "f32-to-u32") (param "a" f32) (result u32)+ (canon lift (core func $i "i32.reinterpret_f32")))+ (func (export "f64-to-u64") (param "a" f64) (result u64)+ (canon lift (core func $i "i64.reinterpret_f64")))+ (func (export "u32-to-f32") (param "a" u32) (result f32)+ (canon lift (core func $i "f32.reinterpret_i32")))+ (func (export "u64-to-f64") (param "a" u64) (result f64)+ (canon lift (core func $i "f64.reinterpret_i64")))+)++(assert_return (invoke "f32-to-u32" (f32.const 1)) (u32.const 0x3f800000))+(assert_return (invoke "f64-to-u64" (f64.const 2)) (u64.const 0x4000000000000000))+(assert_return (invoke "u32-to-f32" (u32.const 0x40400000)) (f32.const 3))+(assert_return (invoke "u64-to-f64" (u64.const 0x4010000000000000)) (f64.const 4))+(assert_return (invoke "u32-to-f32" (u32.const 0x7fc00001)) (f32.const nan:0x400001))+(assert_return (invoke "u64-to-f64" (u64.const 0x7ff8000000000001))+ (f64.const nan:0x8000000000001))+(assert_return (invoke "f32-to-u32" (f32.const nan:0x400001)) (u32.const 0x7fc00001))+(assert_return (invoke "f64-to-u64" (f64.const nan:0x8000000000001))+ (u64.const 0x7ff8000000000001))++;; Chars round-trip, and invalid code points fail to lift.+(component definition $Chars+ (core module $m+ (func (export "pass") (param i32) (result i32) local.get 0)+ )+ (core instance $i (instantiate $m))++ (func (export "u32-to-char") (param "a" u32) (result char)+ (canon lift (core func $i "pass")))+ (func (export "char-to-u32") (param "a" char) (result u32)+ (canon lift (core func $i "pass")))+)+(component instance $Chars $Chars)++(assert_return (invoke "char-to-u32" (char.const "x")) (u32.const 0x78))+(assert_return (invoke "u32-to-char" (u32.const 0x78)) (char.const "x"))+(assert_return (invoke "char-to-u32" (char.const "a")) (u32.const 0x61))+(assert_return (invoke "u32-to-char" (u32.const 0x61)) (char.const "a"))+(assert_return (invoke "char-to-u32" (char.const "\00")) (u32.const 0))+(assert_return (invoke "u32-to-char" (u32.const 0)) (char.const "\00"))+(assert_return (invoke "char-to-u32" (char.const "\n")) (u32.const 0x0a))+(assert_return (invoke "u32-to-char" (u32.const 0x0a)) (char.const "\n"))+(assert_return (invoke "char-to-u32" (char.const "π")) (u32.const 0x1f49d))+(assert_return (invoke "u32-to-char" (u32.const 0x1f49d)) (char.const "π"))++(component instance $Chars $Chars)+(assert_trap (invoke "u32-to-char" (u32.const 0xd800)) "integer out of range")+(component instance $Chars $Chars)+(assert_trap (invoke "u32-to-char" (u32.const 0xdfff)) "integer out of range")+(component instance $Chars $Chars)+(assert_trap (invoke "u32-to-char" (u32.const 0x110000)) "integer out of range")+(component instance $Chars $Chars)+(assert_trap (invoke "u32-to-char" (u32.const 0xffffffff)) "integer out of range")++;; A tuple returned through a return pointer.+(component definition $TupleResult+ (core module $m+ (memory (export "memory") 1)+ (func (export "foo") (param i32 i32 f32 f64) (result i32)+ (local $base i32)+ (local.set $base (i32.const 8))+ (i32.store8 offset=0 (local.get $base) (local.get 0))+ (i32.store16 offset=2 (local.get $base) (local.get 1))+ (f32.store offset=4 (local.get $base) (local.get 2))+ (f64.store offset=8 (local.get $base) (local.get 3))+ local.get $base)+ (func (export "invalid") (result i32)+ i32.const -8)+ )+ (core instance $i (instantiate $m))++ (type $result (tuple s8 u16 f32 f64))+ (func (export "tuple")+ (param "a" s8) (param "b" u16) (param "c" f32) (param "d" f64) (result $result)+ (canon lift (core func $i "foo") (memory (core memory $i "memory"))))+ (func (export "invalid") (result $result)+ (canon lift (core func $i "invalid") (memory (core memory $i "memory"))))+)+(component instance $TupleResult $TupleResult)+(assert_return+ (invoke "tuple" (s8.const -1) (u16.const 100) (f32.const 3) (f64.const 100))+ (tuple.const (s8.const -1) (u16.const 100) (f32.const 3) (f64.const 100)))+(assert_trap (invoke "invalid") "pointer out of bounds of memory")++;; Tuples passed as flattened parameters and results.+(component+ (core module $m+ (func (export "foo") (param i32 f64 i32) (result i32)+ (if (i32.ne (local.get 0) (i32.const 0)) (then unreachable))+ (if (f64.ne (local.get 1) (f64.const 1)) (then unreachable))+ (if (i32.ne (local.get 2) (i32.const 2)) (then unreachable))+ i32.const 3)+ )+ (core instance $i (instantiate $m))++ (func (export "foo")+ (param "a" (tuple s32 f64))+ (param "b" (tuple s8))+ (result (tuple u16))+ (canon lift (core func $i "foo")))+)+(assert_return+ (invoke "foo"+ (tuple.const (s32.const 0) (f64.const 1))+ (tuple.const (s8.const 2)))+ (tuple.const (u16.const 3)))++;; A `(tuple bool char)` lifted from memory.+(component definition $CharBool+ (core module $m+ (memory (export "memory") 1)+ (func (export "ret-tuple") (param i32 i32) (result i32)+ (i32.store offset=0 (i32.const 8) (local.get 0))+ (i32.store offset=4 (i32.const 8) (local.get 1))+ i32.const 8)+ )+ (core instance $i (instantiate $m))++ (func (export "ret-tuple") (param "a" u32) (param "b" u32) (result (tuple bool char))+ (canon lift (core func $i "ret-tuple") (memory (core memory $i "memory"))))+)+(component instance $CharBool $CharBool)+(assert_return (invoke "ret-tuple" (u32.const 0) (u32.const 0x61))+ (tuple.const (bool.const false) (char.const "a")))+(assert_return (invoke "ret-tuple" (u32.const 1) (u32.const 0x1f370))+ (tuple.const (bool.const true) (char.const "π°")))+(assert_return (invoke "ret-tuple" (u32.const 2) (u32.const 0x61))+ (tuple.const (bool.const true) (char.const "a")))+(assert_trap (invoke "ret-tuple" (u32.const 0) (u32.const 0xd800)) "integer out of range")++;; Lifting and lowering options.+(component definition $Option+ (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 "pass1") (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))+ (func (export "pass2") (param i32 i32 i32) (result i32)+ (local $base i32)+ (local.set $base (call $realloc (i32.const 0) (i32.const 0) (i32.const 4) (i32.const 12)))+ (i32.store offset=0 (local.get $base) (local.get 0))+ (i32.store offset=4 (local.get $base) (local.get 1))+ (i32.store offset=8 (local.get $base) (local.get 2))+ (local.get $base))+ )+ (core instance $i (instantiate $m))++ (func (export "option-u8-to-tuple") (param "a" (option u8)) (result (tuple u32 u32))+ (canon lift (core func $i "pass1") (memory (core memory $i "memory"))))+ (func (export "option-u32-to-tuple") (param "a" (option u32)) (result (tuple u32 u32))+ (canon lift (core func $i "pass1") (memory (core memory $i "memory"))))+ (func (export "option-string-to-tuple") (param "a" (option string)) (result (tuple u32 string))+ (canon lift (core func $i "pass2") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+ (func (export "to-option-u8") (param "a" u32) (param "b" u32) (result (option u8))+ (canon lift (core func $i "pass1") (memory (core memory $i "memory"))))+ (func (export "to-option-u32") (param "a" u32) (param "b" u32) (result (option u32))+ (canon lift (core func $i "pass1") (memory (core memory $i "memory"))))+ (func (export "to-option-string") (param "a" u32) (param "b" string) (result (option string))+ (canon lift (core func $i "pass2") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+)+(component instance $Option $Option)+(assert_return (invoke "option-u8-to-tuple" (option.none))+ (tuple.const (u32.const 0) (u32.const 0)))+(assert_return (invoke "option-u8-to-tuple" (option.some (u8.const 0)))+ (tuple.const (u32.const 1) (u32.const 0)))+(assert_return (invoke "option-u8-to-tuple" (option.some (u8.const 100)))+ (tuple.const (u32.const 1) (u32.const 100)))+(assert_return (invoke "option-u32-to-tuple" (option.none))+ (tuple.const (u32.const 0) (u32.const 0)))+(assert_return (invoke "option-u32-to-tuple" (option.some (u32.const 0)))+ (tuple.const (u32.const 1) (u32.const 0)))+(assert_return (invoke "option-u32-to-tuple" (option.some (u32.const 100)))+ (tuple.const (u32.const 1) (u32.const 100)))+(assert_return (invoke "option-string-to-tuple" (option.none))+ (tuple.const (u32.const 0) (str.const "")))+(assert_return (invoke "option-string-to-tuple" (option.some (str.const "")))+ (tuple.const (u32.const 1) (str.const "")))+(assert_return (invoke "option-string-to-tuple" (option.some (str.const "hello")))+ (tuple.const (u32.const 1) (str.const "hello")))+(assert_return (invoke "to-option-u8" (u32.const 0x0000) (u32.const 0)) (option.none))+(assert_return (invoke "to-option-u8" (u32.const 0x0001) (u32.const 0))+ (option.some (u8.const 0)))+(assert_return (invoke "to-option-u8" (u32.const 0xfd01) (u32.const 0))+ (option.some (u8.const 0xfd)))+(assert_return (invoke "to-option-u32" (u32.const 0) (u32.const 0)) (option.none))+(assert_return (invoke "to-option-u32" (u32.const 1) (u32.const 0))+ (option.some (u32.const 0)))+(assert_return (invoke "to-option-u32" (u32.const 1) (u32.const 0x1234fead))+ (option.some (u32.const 0x1234fead)))+(assert_return (invoke "to-option-string" (u32.const 0) (str.const "")) (option.none))+(assert_return (invoke "to-option-string" (u32.const 1) (str.const ""))+ (option.some (str.const "")))+(assert_return (invoke "to-option-string" (u32.const 1) (str.const "cheesecake"))+ (option.some (str.const "cheesecake")))+(assert_trap (invoke "to-option-u8" (u32.const 0x0002) (u32.const 0))+ "discriminant 2 out of range")+(component instance $Option $Option)+(assert_trap (invoke "to-option-u32" (u32.const 2) (u32.const 0))+ "discriminant 2 out of range")+(component instance $Option $Option)+(assert_trap (invoke "to-option-string" (u32.const 2) (str.const ""))+ "discriminant 2 out of range")++;; Lifting and lowering results.+(component definition $Result+ (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 "pass0") (param i32) (result i32)+ local.get 0)+ (func (export "pass1") (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))+ (func (export "pass2") (param i32 i32 i32) (result i32)+ (local $base i32)+ (local.set $base (call $realloc (i32.const 0) (i32.const 0) (i32.const 4) (i32.const 12)))+ (i32.store offset=0 (local.get $base) (local.get 0))+ (i32.store offset=4 (local.get $base) (local.get 1))+ (i32.store offset=8 (local.get $base) (local.get 2))+ (local.get $base))+ )+ (core instance $i (instantiate $m))++ (func (export "take-result-unit") (param "a" (result)) (result u32)+ (canon lift (core func $i "pass0")))+ (func (export "take-result-u8-f32") (param "a" (result u8 (error f32))) (result (tuple u32 u32))+ (canon lift (core func $i "pass1") (memory (core memory $i "memory"))))+ (type $list (list u8))+ (func (export "take-result-string") (param "a" (result string (error $list))) (result (tuple u32 string))+ (canon lift (core func $i "pass2") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+ (func (export "to-result-unit") (param "a" u32) (result (result))+ (canon lift (core func $i "pass0")))+ (func (export "to-result-s16-f32") (param "a" u32) (param "b" u32) (result (result s16 (error f32)))+ (canon lift (core func $i "pass1") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+)+(component instance $Result $Result)+(assert_return (invoke "take-result-unit" (result.ok)) (u32.const 0))+(assert_return (invoke "take-result-unit" (result.err)) (u32.const 1))+(assert_return (invoke "take-result-u8-f32" (result.ok (u8.const 1)))+ (tuple.const (u32.const 0) (u32.const 1)))+(assert_return (invoke "take-result-u8-f32" (result.err (f32.const 2)))+ (tuple.const (u32.const 1) (u32.const 0x40000000)))+(assert_return (invoke "take-result-string" (result.ok (str.const "hello")))+ (tuple.const (u32.const 0) (str.const "hello")))+(assert_return+ (invoke "take-result-string"+ (result.err (list.const+ (u8.const 0x67) (u8.const 0x6f) (u8.const 0x6f) (u8.const 0x64)+ (u8.const 0x62) (u8.const 0x79) (u8.const 0x65))))+ (tuple.const (u32.const 1) (str.const "goodbye")))+(assert_return (invoke "to-result-unit" (u32.const 0)) (result.ok))+(assert_return (invoke "to-result-unit" (u32.const 1)) (result.err))+(assert_return (invoke "to-result-s16-f32" (u32.const 0) (u32.const 0))+ (result.ok (s16.const 0)))+(assert_return (invoke "to-result-s16-f32" (u32.const 0) (u32.const 100))+ (result.ok (s16.const 100)))+(assert_return (invoke "to-result-s16-f32" (u32.const 1) (u32.const 0x3f800000))+ (result.err (f32.const 1)))+(assert_return (invoke "to-result-s16-f32" (u32.const 1) (u32.const 0x7fc00001))+ (result.err (f32.const nan:0x400001)))+(assert_trap (invoke "to-result-unit" (u32.const 2)) "discriminant 2 out of range")+(component instance $Result $Result)+(assert_trap (invoke "to-result-s16-f32" (u32.const 2) (u32.const 0))+ "discriminant 2 out of range")++;; A list of options and results, checked for its in-memory layout.+(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))++ ;; Each element is 16 bytes: the option discriminant at 0, its payload at+ ;; 1, the result discriminant at 4, and the error string's pointer and+ ;; length at 8 and 12.+ (func (export "take") (param $ptr i32) (param $len i32) (result i32)+ (local $ret i32)+ (if (i32.ne (local.get $len) (i32.const 3)) (then unreachable))++ ;; (option.none) (result.ok)+ (if (i32.ne (i32.load8_u offset=0 (local.get $ptr)) (i32.const 0)) (then unreachable))+ (if (i32.ne (i32.load8_u offset=4 (local.get $ptr)) (i32.const 0)) (then unreachable))++ ;; (option.some 2) (result.err "hello there")+ (if (i32.ne (i32.load8_u offset=16 (local.get $ptr)) (i32.const 1)) (then unreachable))+ (if (i32.ne (i32.load8_u offset=17 (local.get $ptr)) (i32.const 2)) (then unreachable))+ (if (i32.ne (i32.load8_u offset=20 (local.get $ptr)) (i32.const 1)) (then unreachable))+ (if (i32.ne (i32.load offset=28 (local.get $ptr)) (i32.const 11)) (then unreachable))+ (if (i32.ne (i32.load8_u (i32.load offset=24 (local.get $ptr))) (i32.const 0x68))+ (then unreachable))++ ;; (option.some 200) (result.err "general kenobi")+ (if (i32.ne (i32.load8_u offset=32 (local.get $ptr)) (i32.const 1)) (then unreachable))+ (if (i32.ne (i32.load8_u offset=33 (local.get $ptr)) (i32.const 200)) (then unreachable))+ (if (i32.ne (i32.load8_u offset=36 (local.get $ptr)) (i32.const 1)) (then unreachable))+ (if (i32.ne (i32.load offset=44 (local.get $ptr)) (i32.const 14)) (then unreachable))+ (if (i32.ne (i32.load8_u (i32.load offset=40 (local.get $ptr))) (i32.const 0x67))+ (then unreachable))++ ;; Return the same list back to the caller.+ (local.set $ret (call $realloc (i32.const 0) (i32.const 0) (i32.const 4) (i32.const 8)))+ (i32.store offset=0 (local.get $ret) (local.get $ptr))+ (i32.store offset=4 (local.get $ret) (local.get $len))+ (local.get $ret))+ )+ (core instance $i (instantiate $m))++ (type $a (option u8))+ (type $b (result (error string)))+ (type $input (list (tuple $a $b)))+ (func (export "take") (param "a" $input) (result $input)+ (canon lift (core func $i "take") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+)+(assert_return+ (invoke "take" (list.const+ (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")))))+ (list.const+ (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")))))