A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Verify alignment of returned component-model strings (#12897)
Alex Crichton83909b0b27fb
diff --git a/crates/environ/src/component/info.rs b/crates/environ/src/component/info.rsindex 40feaa1c88..4222dd7568 100644--- a/crates/environ/src/component/info.rs+++ b/crates/environ/src/component/info.rs@@ -660,6 +660,15 @@ impl FixedEncoding {FixedEncoding::Latin1 => 1,}}++ /// Returns the alignment of strings using this encoding.+ pub fn align(&self) -> u8 {+ match self {+ FixedEncoding::Utf8 => 1,+ FixedEncoding::Utf16 => 2,+ FixedEncoding::Latin1 => 2,+ }+ }}/// Description of a new resource declared in a `GlobalInitializer::Resource`diff --git a/crates/environ/src/fact/trampoline.rs b/crates/environ/src/fact/trampoline.rsindex 0c6e673759..23b54a7699 100644--- a/crates/environ/src/fact/trampoline.rs+++ b/crates/environ/src/fact/trampoline.rs@@ -1817,7 +1817,7 @@ impl<'a, 'b> Compiler<'a, 'b> {let dst_mem = self.malloc(dst_opts,MallocSize::Local(dst_byte_len.idx),- dst_enc.width().into(),+ dst_enc.align().into(),);WasmString {ptr: dst_mem.addr,diff --git a/crates/wasmtime/src/runtime/component/func/typed.rs b/crates/wasmtime/src/runtime/component/func/typed.rsindex f1fb7de6a9..2650234350 100644--- a/crates/wasmtime/src/runtime/component/func/typed.rs+++ b/crates/wasmtime/src/runtime/component/func/typed.rs@@ -1680,17 +1680,21 @@ pub struct WasmStr {impl WasmStr {pub(crate) fn new(ptr: usize, len: usize, cx: &mut LiftContext<'_>) -> Result<WasmStr> {- let byte_len = match cx.options().string_encoding {- StringEncoding::Utf8 => Some(len),- StringEncoding::Utf16 => len.checked_mul(2),+ let (byte_len, align) = match cx.options().string_encoding {+ StringEncoding::Utf8 => (Some(len), 1_usize),+ StringEncoding::Utf16 => (len.checked_mul(2), 2),StringEncoding::CompactUtf16 => {if len & UTF16_TAG == 0 {- Some(len)+ (Some(len), 2)} else {- (len ^ UTF16_TAG).checked_mul(2)+ ((len ^ UTF16_TAG).checked_mul(2), 2)}}};+ debug_assert!(align.is_power_of_two());+ if ptr & (align - 1) != 0 {+ bail!("string pointer not aligned to {align}");+ }match byte_len.and_then(|len| ptr.checked_add(len)) {Some(n) if n <= cx.memory().len() => cx.consume_fuel(n - ptr)?,_ => bail!("string pointer/length out of bounds of memory"),diff --git a/tests/misc_testsuite/component-model/strings.wast b/tests/misc_testsuite/component-model/strings.wastnew file mode 100644index 0000000000..f55540cd47--- /dev/null+++ b/tests/misc_testsuite/component-model/strings.wast@@ -0,0 +1,21 @@+;; Returning an unaligned utf16 string is invalid+(component definition $A+ (core module $m+ (memory (export "m") 1)+ (func (export "f") (result i32)+ (i32.store (i32.const 4) (i32.const 1))+ (i32.store (i32.const 8) (i32.const 0))+ i32.const 4+ )+ )+ (core instance $m (instantiate $m))+ (func (export "f1") (result string)+ (canon lift (core func $m "f") (memory $m "m") string-encoding=utf16))+ (func (export "f2") (result string)+ (canon lift (core func $m "f") (memory $m "m") string-encoding=latin1+utf16))++)+(component instance $A $A)+(assert_trap (invoke "f1") "string pointer not aligned to 2")+(component instance $A $A)+(assert_trap (invoke "f2") "string pointer not aligned to 2")