Implement strings in adapter modules (#4623)
* Implement strings in adapter modules This commit is a hefty addition to Wasmtime's support for the component model. This implements the final remaining type (in the current type hierarchy) unimplemented in adapter module trampolines: strings. Strings are the most complicated type to implement in adapter trampolines because they are highly structured chunks of data in memory (according to specific encodings). Additionally each lift/lower operation can choose its own encoding for strings meaning that Wasmtime, the host, may have to convert between any pairwise ordering of string encodings. The `CanonicalABI.md` in the component-model repo in general specifies all the fiddly bits of string encoding so there's not a ton of wiggle room for Wasmtime to get creative. This PR largely "just" implements that. The high-level architecture of this implementation is: * Fused adapters are first identified to determine src/dst string encodings. This statically fixes what transcoding operation is being performed. * The generated adapter will be responsible for managing calls to `realloc` and performing bounds checks. The adapter itself does not perform memory copies or validation of string contents, however. Instead each transcoding operation is modeled as an imported function into the adapter module. This means that the adapter module dynamically, during compile time, determines what string transcoders are needed. Note that an imported transcoder is not only parameterized over the transcoding operation but additionally which memory is the source and which is the destination. * The imported core wasm functions are modeled as a new `CoreDef::Transcoder` structure. These transcoders end up being small Cranelift-compiled trampolines. The Cranelift-compiled trampoline will load the actual base pointer of memory and add it to the relative pointers passed as function arguments. This trampoline then calls a transcoder "libcall" which enters Rust-defined functions for actual transcoding operations. * Each possible transcoding operation is implemented in Rust with a unique name and a unique signature depending on the needs of the transcoder. I've tried to document inline what each transcoder does. This means that the `Module::translate_string` in adapter modules is by far the largest translation method. The main reason for this is due to the management around calling the imported transcoder functions in the face of validating string pointer/lengths and performing the dance of `realloc`-vs-transcode at the right time. I've tried to ensure that each individual case in transcoding is documented well enough to understand what's going on as well. Additionally in this PR is a full implementation in the host for the `latin1+utf16` encoding which means that both lifting and lowering host strings now works with this encoding. Currently the implementation of each transcoder function is likely far from optimal. Where possible I've leaned on the standard library itself and for latin1-related things I'm leaning on the `encoding_rs` crate. I initially tried to implement everything with `encoding_rs` but was unable to uniformly do so easily. For now I settled on trying to get a known-correct (even in the face of endianness) implementation for all of these transcoders. If an when performance becomes an issue it should be possible to implement more optimized versions of each of these transcoding operations. Testing this commit has been somewhat difficult and my general plan, like with the `(list T)` type, is to rely heavily on fuzzing to cover the various cases here. In this PR though I've added a simple test that pushes some statically known strings through all the pairs of encodings between source and destination. I've attempted to pick "interesting" strings that one way or another stress the various paths in each transcoding operation to ideally get full branch coverage there. Additionally a suite of "negative" tests have also been added to ensure that validity of encoding is actually checked. * Fix a temporarily commented out case * Fix wasmtime-runtime tests * Update deny.toml configuration * Add `BSD-3-Clause` for the `encoding_rs` crate * Remove some unused licenses * Add an exemption for `encoding_rs` for now * Split up the `translate_string` method Move out all the closures and package up captured state into smaller lists of arguments. * Test out-of-bounds for zero-length strings
This commit is contained in:
@@ -12,6 +12,7 @@ mod instance;
|
||||
mod macros;
|
||||
mod nested;
|
||||
mod post_return;
|
||||
mod strings;
|
||||
|
||||
#[test]
|
||||
fn components_importing_modules() -> Result<()> {
|
||||
|
||||
578
tests/all/component_model/strings.rs
Normal file
578
tests/all/component_model/strings.rs
Normal file
@@ -0,0 +1,578 @@
|
||||
use super::REALLOC_AND_FREE;
|
||||
use anyhow::Result;
|
||||
use wasmtime::component::{Component, Linker};
|
||||
use wasmtime::{Engine, Store, StoreContextMut, Trap, TrapCode};
|
||||
|
||||
const UTF16_TAG: u32 = 1 << 31;
|
||||
|
||||
// Special cases that this tries to test:
|
||||
//
|
||||
// * utf8 -> utf8
|
||||
// * various code point sizes
|
||||
//
|
||||
// * utf8 -> utf16 - the adapter here will make a pessimistic allocation that's
|
||||
// twice the size of the utf8 encoding for the utf16 destination
|
||||
// * utf16 byte size is twice the utf8 size
|
||||
// * utf16 byte size is less than twice the utf8 size
|
||||
//
|
||||
// * utf8 -> latin1+utf16 - attempts to convert to latin1 then falls back to a
|
||||
// pessimistic utf16 allocation that's downsized if necessary
|
||||
// * utf8 fits exactly in latin1
|
||||
// * utf8 fits latin1 but is bigger byte-wise
|
||||
// * utf8 is not latin1 and fits utf16 allocation precisely (NOT POSSIBLE)
|
||||
// * utf8 is not latin1 and utf16 is smaller than allocation
|
||||
//
|
||||
// * utf16 -> utf8 - this starts with an optimistic size and then reallocates to
|
||||
// a pessimistic size, interesting cases are:
|
||||
// * utf8 size is 0.5x the utf16 byte size (perfect fit in initial alloc)
|
||||
// * utf8 size is 1.5x the utf16 byte size (perfect fit in larger alloc)
|
||||
// * utf8 size is 0.5x-1.5x the utf16 size (larger alloc is downsized)
|
||||
//
|
||||
// * utf16 -> utf16
|
||||
// * various code point sizes
|
||||
//
|
||||
// * utf16 -> latin1+utf16 - attempts to convert to latin1 then falls back to a
|
||||
// pessimistic utf16 allocation that's downsized if necessary
|
||||
// * utf16 fits exactly in latin1
|
||||
// * utf16 fits latin1 but is bigger byte-wise (NOT POSSIBLE)
|
||||
// * utf16 is not latin1 and fits utf16 allocation precisely
|
||||
// * utf16 is not latin1 and utf16 is smaller than allocation (NOT POSSIBLE)
|
||||
//
|
||||
// * compact-utf16 -> utf8 dynamically determines between one of
|
||||
// * latin1 -> utf8
|
||||
// * latin1 size matches utf8 size
|
||||
// * latin1 is smaller than utf8 size
|
||||
// * utf16 -> utf8
|
||||
// * covered above
|
||||
//
|
||||
// * compact-utf16 -> utf16 dynamically determines between one of
|
||||
// * latin1 -> utf16 - latin1 size always matches utf16
|
||||
// * test various code points
|
||||
// * utf16 -> utf16
|
||||
// * covered above
|
||||
//
|
||||
// * compact-utf16 -> compact-utf16 dynamically determines between one of
|
||||
// * latin1 -> latin1
|
||||
// * not much interesting here
|
||||
// * utf16 -> compact-utf16-to-compact-probably-utf16
|
||||
// * utf16 actually fits within latin1
|
||||
// * otherwise not more interesting than utf16 -> utf16
|
||||
//
|
||||
const STRINGS: &[&str] = &[
|
||||
"",
|
||||
// 1 byte in utf8, 2 bytes in utf16
|
||||
"x",
|
||||
"hello this is a particularly long string yes it is it keeps going",
|
||||
// 35 bytes in utf8, 23 units in utf16, 23 bytes in latin1
|
||||
"à á â ã ä å æ ç è é ê ë",
|
||||
// 47 bytes in utf8, 31 units in utf16
|
||||
"Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω Ϊ Ϋ ά έ ή",
|
||||
// 24 bytes in utf8, 8 units in utf16
|
||||
"STUVWXYZ",
|
||||
// 16 bytes in utf8, 8 units in utf16
|
||||
"ËÌÍÎÏÐÑÒ",
|
||||
// 4 bytes in utf8, 1 unit in utf16
|
||||
"\u{10000}",
|
||||
// latin1-compatible prefix followed by utf8/16-requiring suffix
|
||||
//
|
||||
// 24 bytes in utf8, 13 units in utf16, first 8 usvs are latin1-compatible
|
||||
"à ascii VWXYZ",
|
||||
];
|
||||
|
||||
static ENCODINGS: [&str; 3] = ["utf8", "utf16", "latin1+utf16"];
|
||||
|
||||
#[test]
|
||||
fn roundtrip() -> Result<()> {
|
||||
for debug in [true, false] {
|
||||
let mut config = component_test_util::config();
|
||||
config.debug_adapter_modules(debug);
|
||||
let engine = Engine::new(&config)?;
|
||||
for src in ENCODINGS {
|
||||
for dst in ENCODINGS {
|
||||
test_roundtrip(&engine, src, dst)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_roundtrip(engine: &Engine, src: &str, dst: &str) -> Result<()> {
|
||||
println!("src={src} dst={dst}");
|
||||
|
||||
let mk_echo = |name: &str, encoding: &str| {
|
||||
format!(
|
||||
r#"
|
||||
(component {name}
|
||||
(import "echo" (func $echo (param string) (result string)))
|
||||
(core instance $libc (instantiate $libc))
|
||||
(core func $echo (canon lower (func $echo)
|
||||
(memory $libc "memory")
|
||||
(realloc (func $libc "realloc"))
|
||||
string-encoding={encoding}
|
||||
))
|
||||
(core instance $echo (instantiate $echo
|
||||
(with "libc" (instance $libc))
|
||||
(with "" (instance (export "echo" (func $echo))))
|
||||
))
|
||||
(func (export "echo") (param string) (result string)
|
||||
(canon lift
|
||||
(core func $echo "echo")
|
||||
(memory $libc "memory")
|
||||
(realloc (func $libc "realloc"))
|
||||
string-encoding={encoding}
|
||||
)
|
||||
)
|
||||
)
|
||||
"#
|
||||
)
|
||||
};
|
||||
|
||||
let src = mk_echo("$src", src);
|
||||
let dst = mk_echo("$dst", dst);
|
||||
let component = format!(
|
||||
r#"
|
||||
(component
|
||||
(import "host" (func $host (param string) (result string)))
|
||||
|
||||
(core module $libc
|
||||
(memory (export "memory") 1)
|
||||
{REALLOC_AND_FREE}
|
||||
)
|
||||
(core module $echo
|
||||
(import "" "echo" (func $echo (param i32 i32 i32)))
|
||||
(import "libc" "memory" (memory 0))
|
||||
(import "libc" "realloc" (func $realloc (param i32 i32 i32 i32) (result i32)))
|
||||
|
||||
(func (export "echo") (param i32 i32) (result i32)
|
||||
(local $retptr i32)
|
||||
(local.set $retptr
|
||||
(call $realloc
|
||||
(i32.const 0)
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 8)))
|
||||
(call $echo
|
||||
(local.get 0)
|
||||
(local.get 1)
|
||||
(local.get $retptr))
|
||||
local.get $retptr
|
||||
)
|
||||
)
|
||||
|
||||
{src}
|
||||
{dst}
|
||||
|
||||
(instance $dst (instantiate $dst (with "echo" (func $host))))
|
||||
(instance $src (instantiate $src (with "echo" (func $dst "echo"))))
|
||||
(export "echo" (func $src "echo"))
|
||||
)
|
||||
"#
|
||||
);
|
||||
let component = Component::new(engine, &component)?;
|
||||
let mut store = Store::new(engine, String::new());
|
||||
let mut linker = Linker::new(engine);
|
||||
linker
|
||||
.root()
|
||||
.func_wrap("host", |store: StoreContextMut<String>, arg: String| {
|
||||
assert_eq!(*store.data(), arg);
|
||||
Ok(arg)
|
||||
})?;
|
||||
let instance = linker.instantiate(&mut store, &component)?;
|
||||
let func = instance.get_typed_func::<(String,), String, _>(&mut store, "echo")?;
|
||||
|
||||
for string in STRINGS {
|
||||
println!("testing string {string:?}");
|
||||
*store.data_mut() = string.to_string();
|
||||
let ret = func.call(&mut store, (string.to_string(),))?;
|
||||
assert_eq!(ret, *string);
|
||||
func.post_return(&mut store)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ptr_out_of_bounds() -> Result<()> {
|
||||
let engine = component_test_util::engine();
|
||||
for src in ENCODINGS {
|
||||
for dst in ENCODINGS {
|
||||
test_ptr_out_of_bounds(&engine, src, dst)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_ptr_out_of_bounds(engine: &Engine, src: &str, dst: &str) -> Result<()> {
|
||||
let test = |len: u32| -> Result<()> {
|
||||
let component = format!(
|
||||
r#"
|
||||
(component
|
||||
(component $c
|
||||
(core module $m
|
||||
(func (export "") (param i32 i32))
|
||||
(func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
|
||||
(memory (export "memory") 1)
|
||||
)
|
||||
(core instance $m (instantiate $m))
|
||||
(func (export "") (param string)
|
||||
(canon lift (core func $m "") (realloc (func $m "realloc")) (memory $m "memory")
|
||||
string-encoding={dst})
|
||||
)
|
||||
)
|
||||
|
||||
(component $c2
|
||||
(import "" (func $f (param string)))
|
||||
(core module $libc
|
||||
(memory (export "memory") 1)
|
||||
)
|
||||
(core instance $libc (instantiate $libc))
|
||||
(core func $f (canon lower (func $f) string-encoding={src} (memory $libc "memory")))
|
||||
(core module $m
|
||||
(import "" "" (func $f (param i32 i32)))
|
||||
|
||||
(func $start (call $f (i32.const 0x8000_0000) (i32.const {len})))
|
||||
(start $start)
|
||||
)
|
||||
(core instance (instantiate $m (with "" (instance (export "" (func $f))))))
|
||||
)
|
||||
|
||||
(instance $c (instantiate $c))
|
||||
(instance $c2 (instantiate $c2 (with "" (func $c ""))))
|
||||
)
|
||||
"#
|
||||
);
|
||||
let component = Component::new(engine, &component)?;
|
||||
let mut store = Store::new(engine, ());
|
||||
let trap = Linker::new(engine)
|
||||
.instantiate(&mut store, &component)
|
||||
.err()
|
||||
.unwrap()
|
||||
.downcast::<Trap>()?;
|
||||
assert_eq!(trap.trap_code(), Some(TrapCode::UnreachableCodeReached));
|
||||
Ok(())
|
||||
};
|
||||
|
||||
test(0)?;
|
||||
test(1)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Test that even if the ptr+len calculation overflows then a trap still
|
||||
// happens.
|
||||
#[test]
|
||||
fn ptr_overflow() -> Result<()> {
|
||||
let engine = component_test_util::engine();
|
||||
for src in ENCODINGS {
|
||||
for dst in ENCODINGS {
|
||||
test_ptr_overflow(&engine, src, dst)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_ptr_overflow(engine: &Engine, src: &str, dst: &str) -> Result<()> {
|
||||
let component = format!(
|
||||
r#"
|
||||
(component
|
||||
(component $c
|
||||
(core module $m
|
||||
(func (export "") (param i32 i32))
|
||||
(func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
|
||||
(memory (export "memory") 1)
|
||||
)
|
||||
(core instance $m (instantiate $m))
|
||||
(func (export "") (param string)
|
||||
(canon lift (core func $m "") (realloc (func $m "realloc")) (memory $m "memory")
|
||||
string-encoding={dst})
|
||||
)
|
||||
)
|
||||
|
||||
(component $c2
|
||||
(import "" (func $f (param string)))
|
||||
(core module $libc
|
||||
(memory (export "memory") 1)
|
||||
)
|
||||
(core instance $libc (instantiate $libc))
|
||||
(core func $f (canon lower (func $f) string-encoding={src} (memory $libc "memory")))
|
||||
(core module $m
|
||||
(import "" "" (func $f (param i32 i32)))
|
||||
|
||||
(func (export "f") (param i32) (call $f (i32.const 1000) (local.get 0)))
|
||||
)
|
||||
(core instance $m (instantiate $m (with "" (instance (export "" (func $f))))))
|
||||
(func (export "f") (param u32) (canon lift (core func $m "f")))
|
||||
)
|
||||
|
||||
(instance $c (instantiate $c))
|
||||
(instance $c2 (instantiate $c2 (with "" (func $c ""))))
|
||||
(export "f" (func $c2 "f"))
|
||||
)
|
||||
"#
|
||||
);
|
||||
|
||||
let component = Component::new(engine, &component)?;
|
||||
let mut store = Store::new(engine, ());
|
||||
|
||||
let mut test_overflow = |size: u32| -> Result<()> {
|
||||
println!("src={src} dst={dst} size={size:#x}");
|
||||
let instance = Linker::new(engine).instantiate(&mut store, &component)?;
|
||||
let func = instance.get_typed_func::<(u32,), (), _>(&mut store, "f")?;
|
||||
let trap = func
|
||||
.call(&mut store, (size,))
|
||||
.unwrap_err()
|
||||
.downcast::<Trap>()?;
|
||||
assert_eq!(trap.trap_code(), Some(TrapCode::UnreachableCodeReached));
|
||||
Ok(())
|
||||
};
|
||||
|
||||
let max = 1 << 31;
|
||||
|
||||
match src {
|
||||
"utf8" => {
|
||||
// This exceeds MAX_STRING_BYTE_LENGTH
|
||||
test_overflow(max)?;
|
||||
|
||||
if dst == "utf16" {
|
||||
// exceeds MAX_STRING_BYTE_LENGTH when multiplied
|
||||
test_overflow(max / 2)?;
|
||||
|
||||
// Technically this fails on the first string, not the second.
|
||||
// Ideally this would test the overflow check on the second
|
||||
// string though.
|
||||
test_overflow(max / 2 - 100)?;
|
||||
} else {
|
||||
// This will point into unmapped memory
|
||||
test_overflow(max - 100)?;
|
||||
}
|
||||
}
|
||||
|
||||
"utf16" => {
|
||||
test_overflow(max / 2)?;
|
||||
test_overflow(max / 2 - 100)?;
|
||||
}
|
||||
|
||||
"latin1+utf16" => {
|
||||
test_overflow((max / 2) | UTF16_TAG)?;
|
||||
// tag a utf16 string with the max length and it should overflow.
|
||||
test_overflow((max / 2 - 100) | UTF16_TAG)?;
|
||||
}
|
||||
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Test that that the pointer returned from `realloc` is bounds-checked.
|
||||
#[test]
|
||||
fn realloc_oob() -> Result<()> {
|
||||
let engine = component_test_util::engine();
|
||||
for src in ENCODINGS {
|
||||
for dst in ENCODINGS {
|
||||
test_realloc_oob(&engine, src, dst)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_realloc_oob(engine: &Engine, src: &str, dst: &str) -> Result<()> {
|
||||
let component = format!(
|
||||
r#"
|
||||
(component
|
||||
(component $c
|
||||
(core module $m
|
||||
(func (export "") (param i32 i32))
|
||||
(func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 100_000)
|
||||
(memory (export "memory") 1)
|
||||
)
|
||||
(core instance $m (instantiate $m))
|
||||
(func (export "") (param string)
|
||||
(canon lift (core func $m "") (realloc (func $m "realloc")) (memory $m "memory")
|
||||
string-encoding={dst})
|
||||
)
|
||||
)
|
||||
|
||||
(component $c2
|
||||
(import "" (func $f (param string)))
|
||||
(core module $libc
|
||||
(memory (export "memory") 1)
|
||||
)
|
||||
(core instance $libc (instantiate $libc))
|
||||
(core func $f (canon lower (func $f) string-encoding={src} (memory $libc "memory")))
|
||||
(core module $m
|
||||
(import "" "" (func $f (param i32 i32)))
|
||||
|
||||
(func (export "f") (call $f (i32.const 1000) (i32.const 10)))
|
||||
)
|
||||
(core instance $m (instantiate $m (with "" (instance (export "" (func $f))))))
|
||||
(func (export "f") (canon lift (core func $m "f")))
|
||||
)
|
||||
|
||||
(instance $c (instantiate $c))
|
||||
(instance $c2 (instantiate $c2 (with "" (func $c ""))))
|
||||
(export "f" (func $c2 "f"))
|
||||
)
|
||||
"#
|
||||
);
|
||||
|
||||
let component = Component::new(engine, &component)?;
|
||||
let mut store = Store::new(engine, ());
|
||||
|
||||
let instance = Linker::new(engine).instantiate(&mut store, &component)?;
|
||||
let func = instance.get_typed_func::<(), (), _>(&mut store, "f")?;
|
||||
let trap = func.call(&mut store, ()).unwrap_err().downcast::<Trap>()?;
|
||||
assert_eq!(trap.trap_code(), Some(TrapCode::UnreachableCodeReached));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Test that that the pointer returned from `realloc` is bounds-checked.
|
||||
#[test]
|
||||
fn raw_string_encodings() -> Result<()> {
|
||||
let engine = component_test_util::engine();
|
||||
test_invalid_string_encoding(&engine, "utf8", "utf8", &[0xff], 1)?;
|
||||
let array = b"valid string until \xffthen valid again";
|
||||
test_invalid_string_encoding(&engine, "utf8", "utf8", array, array.len() as u32)?;
|
||||
test_invalid_string_encoding(&engine, "utf8", "utf16", array, array.len() as u32)?;
|
||||
let array = b"symbol \xce\xa3 until \xffthen valid";
|
||||
test_invalid_string_encoding(&engine, "utf8", "utf8", array, array.len() as u32)?;
|
||||
test_invalid_string_encoding(&engine, "utf8", "utf16", array, array.len() as u32)?;
|
||||
test_invalid_string_encoding(&engine, "utf8", "latin1+utf16", array, array.len() as u32)?;
|
||||
test_invalid_string_encoding(&engine, "utf16", "utf8", &[0x01, 0xd8], 1)?;
|
||||
test_invalid_string_encoding(&engine, "utf16", "utf16", &[0x01, 0xd8], 1)?;
|
||||
test_invalid_string_encoding(
|
||||
&engine,
|
||||
"utf16",
|
||||
"latin1+utf16",
|
||||
&[0xff, 0xff, 0x01, 0xd8],
|
||||
2,
|
||||
)?;
|
||||
test_invalid_string_encoding(
|
||||
&engine,
|
||||
"latin1+utf16",
|
||||
"utf8",
|
||||
&[0x01, 0xd8],
|
||||
1 | UTF16_TAG,
|
||||
)?;
|
||||
test_invalid_string_encoding(
|
||||
&engine,
|
||||
"latin1+utf16",
|
||||
"utf16",
|
||||
&[0x01, 0xd8],
|
||||
1 | UTF16_TAG,
|
||||
)?;
|
||||
test_invalid_string_encoding(
|
||||
&engine,
|
||||
"latin1+utf16",
|
||||
"utf16",
|
||||
&[0xff, 0xff, 0x01, 0xd8],
|
||||
2 | UTF16_TAG,
|
||||
)?;
|
||||
test_invalid_string_encoding(
|
||||
&engine,
|
||||
"latin1+utf16",
|
||||
"latin1+utf16",
|
||||
&[0xab, 0x00, 0xff, 0xff, 0x01, 0xd8],
|
||||
3 | UTF16_TAG,
|
||||
)?;
|
||||
|
||||
// This latin1+utf16 string should get compressed to latin1 across the
|
||||
// boundary.
|
||||
test_valid_string_encoding(
|
||||
&engine,
|
||||
"latin1+utf16",
|
||||
"latin1+utf16",
|
||||
&[0xab, 0x00, 0xff, 0x00],
|
||||
2 | UTF16_TAG,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_invalid_string_encoding(
|
||||
engine: &Engine,
|
||||
src: &str,
|
||||
dst: &str,
|
||||
bytes: &[u8],
|
||||
len: u32,
|
||||
) -> Result<()> {
|
||||
let trap = test_raw_when_encoded(engine, src, dst, bytes, len)?.unwrap();
|
||||
let src = src.replace("latin1+", "");
|
||||
assert!(
|
||||
trap.to_string()
|
||||
.contains(&format!("invalid {src} encoding")),
|
||||
"bad error: {}",
|
||||
trap,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_valid_string_encoding(
|
||||
engine: &Engine,
|
||||
src: &str,
|
||||
dst: &str,
|
||||
bytes: &[u8],
|
||||
len: u32,
|
||||
) -> Result<()> {
|
||||
let err = test_raw_when_encoded(engine, src, dst, bytes, len)?;
|
||||
assert!(err.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_raw_when_encoded(
|
||||
engine: &Engine,
|
||||
src: &str,
|
||||
dst: &str,
|
||||
bytes: &[u8],
|
||||
len: u32,
|
||||
) -> Result<Option<Trap>> {
|
||||
let component = format!(
|
||||
r#"
|
||||
(component
|
||||
(component $c
|
||||
(core module $m
|
||||
(func (export "") (param i32 i32))
|
||||
(func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
|
||||
(memory (export "memory") 1)
|
||||
)
|
||||
(core instance $m (instantiate $m))
|
||||
(func (export "") (param string)
|
||||
(canon lift (core func $m "") (realloc (func $m "realloc")) (memory $m "memory")
|
||||
string-encoding={dst})
|
||||
)
|
||||
)
|
||||
|
||||
(component $c2
|
||||
(import "" (func $f (param string)))
|
||||
(core module $libc
|
||||
(memory (export "memory") 1)
|
||||
(func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
|
||||
)
|
||||
(core instance $libc (instantiate $libc))
|
||||
(core func $f (canon lower (func $f) string-encoding={src} (memory $libc "memory")))
|
||||
(core module $m
|
||||
(import "" "" (func $f (param i32 i32)))
|
||||
|
||||
(func (export "f") (param i32 i32 i32) (call $f (local.get 0) (local.get 2)))
|
||||
)
|
||||
(core instance $m (instantiate $m (with "" (instance (export "" (func $f))))))
|
||||
(func (export "f") (param (list u8)) (param u32) (canon lift (core func $m "f")
|
||||
(memory $libc "memory")
|
||||
(realloc (func $libc "realloc"))))
|
||||
)
|
||||
|
||||
(instance $c (instantiate $c))
|
||||
(instance $c2 (instantiate $c2 (with "" (func $c ""))))
|
||||
(export "f" (func $c2 "f"))
|
||||
)
|
||||
"#
|
||||
);
|
||||
|
||||
let component = Component::new(engine, &component)?;
|
||||
let mut store = Store::new(engine, ());
|
||||
|
||||
let instance = Linker::new(engine).instantiate(&mut store, &component)?;
|
||||
let func = instance.get_typed_func::<(&[u8], u32), (), _>(&mut store, "f")?;
|
||||
match func.call(&mut store, (bytes, len)) {
|
||||
Ok(_) => Ok(None),
|
||||
Err(e) => Ok(Some(e.downcast()?)),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user