From b248fd2f96f7ba77850b4c61cdb238585e386262 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 24 Nov 2026 10:47:14 -0800 Subject: [PATCH 1/2] Fix utf16 hinting for `latin1+utf16` strings sent between 32- and 64-bit memories This is the Wasmtime version of this bug fix in the CM spec: https://github.com/WebAssembly/component-model/pull/772 --- crates/environ/src/fact/trampoline.rs | 44 ++- .../component-model/strings-memory64.wast | 316 ++++++++++++++++++ 2 files changed, 353 insertions(+), 7 deletions(-) create mode 100644 tests/misc_testsuite/component-model/strings-memory64.wast diff --git a/crates/environ/src/fact/trampoline.rs b/crates/environ/src/fact/trampoline.rs index 12d4eff92892..0fd2c6ad266f 100644 --- a/crates/environ/src/fact/trampoline.rs +++ b/crates/environ/src/fact/trampoline.rs @@ -43,7 +43,11 @@ use wasmtime_component_util::{DiscriminantSize, FlagsSize}; use super::DataModel; const MAX_STRING_BYTE_LENGTH: u32 = (1 << 31) - 1; -const UTF16_TAG: u32 = 1 << 31; + +/// The tag that the `Utf16ToCompactProbablyUtf16` transcoder sets on its +/// returned length to indicate utf16. This is always bit 31, regardless of +/// memory width, and is not the guest-visible tag; see `ptr_utf16_tag`. +const TRANSCODER_UTF16_TAG: u32 = 1 << 31; /// This value is arbitrarily chosen and should be fine to change at any time, /// it just seemed like a halfway reasonable starting point. @@ -1658,7 +1662,7 @@ impl<'a, 'b> Compiler<'a, 'b> { // Test the tag big to see if this is a utf16 or a latin1 string // at runtime... self.instruction(LocalGet(src_str.len.idx)); - self.ptr_uconst(src_mem_opts, UTF16_TAG); + self.ptr_utf16_tag(src_mem_opts); self.ptr_and(src_mem_opts); self.ptr_if(src_mem_opts, BlockType::Empty); @@ -1666,7 +1670,7 @@ impl<'a, 'b> Compiler<'a, 'b> { // so further calculations have the right value. Afterwards the // string transcode proceeds assuming utf16. self.instruction(LocalGet(src_str.len.idx)); - self.ptr_uconst(src_mem_opts, UTF16_TAG); + self.ptr_utf16_tag(src_mem_opts); self.ptr_xor(src_mem_opts); self.instruction(LocalSet(src_str.len.idx)); @@ -2230,7 +2234,7 @@ impl<'a, 'b> Compiler<'a, 'b> { // source code unit length. if self.module.tunables.debug_adapter_modules { self.instruction(LocalGet(dst.len.idx)); - self.ptr_uconst(dst_mem_opts, !UTF16_TAG); + self.ptr_uconst(dst_mem_opts, !TRANSCODER_UTF16_TAG); self.ptr_and(dst_mem_opts); self.convert_src_len_to_dst(src.len.idx, src_mem_opts.ptr(), dst_mem_opts.ptr()); self.ptr_ne(dst_mem_opts); @@ -2239,11 +2243,28 @@ impl<'a, 'b> Compiler<'a, 'b> { self.instruction(End); } - // If the UTF16_TAG is set then utf16 was used and the destination + // Move the transcoder's utf16 tag to the destination memory's tag. + if dst_mem_opts.memory64() { + self.instruction(LocalGet(dst.len.idx)); + self.instruction(I64Const(TRANSCODER_UTF16_TAG.into())); + self.instruction(I64And); + self.instruction(I64Const(0)); + self.instruction(I64Ne); + self.instruction(If(BlockType::Empty)); + self.instruction(LocalGet(dst.len.idx)); + self.instruction(I64Const( + (u64::from(TRANSCODER_UTF16_TAG) | (1 << 63)).cast_signed(), + )); + self.instruction(I64Xor); + self.instruction(LocalSet(dst.len.idx)); + self.instruction(End); + } + + // If the utf16 tag is set then utf16 was used and the destination // should be appropriately sized. Bail out of the "is this string // empty" block and fall through otherwise to resizing. self.instruction(LocalGet(dst.len.idx)); - self.ptr_uconst(dst_mem_opts, UTF16_TAG); + self.ptr_utf16_tag(dst_mem_opts); self.ptr_and(dst_mem_opts); self.ptr_br_if(dst_mem_opts, 0); @@ -2416,7 +2437,7 @@ impl<'a, 'b> Compiler<'a, 'b> { // Tag the returned pointer as utf16 self.instruction(LocalGet(dst.len.idx)); - self.ptr_uconst(dst_mem_opts, UTF16_TAG); + self.ptr_utf16_tag(dst_mem_opts); self.ptr_or(dst_mem_opts); self.instruction(LocalSet(dst.len.idx)); @@ -4302,6 +4323,15 @@ impl<'a, 'b> Compiler<'a, 'b> { } } + /// Pushes the `latin1+utf16` utf16 tag for `opts`'s memory. + fn ptr_utf16_tag(&mut self, opts: &LinearMemoryOptions) { + if opts.memory64() { + self.instruction(I64Const(i64::MIN)); + } else { + self.instruction(I32Const(i32::MIN)); + } + } + fn ptr_iconst(&mut self, opts: &LinearMemoryOptions, val: i32) { if opts.memory64() { self.instruction(I64Const(val.into())); diff --git a/tests/misc_testsuite/component-model/strings-memory64.wast b/tests/misc_testsuite/component-model/strings-memory64.wast new file mode 100644 index 000000000000..fa22057a1686 --- /dev/null +++ b/tests/misc_testsuite/component-model/strings-memory64.wast @@ -0,0 +1,316 @@ +;;! component_model_memory64 = true +;;! memory64 = true +;;! multi_memory = true +;;! bulk_memory = true + +;; Test latin1+utf16 strings passed between components whose memories have +;; different address types. + +;; i64 caller -> i32 callee: utf16-tagged input whose code points all fit in +;; latin1 must arrive deflated to latin1; wide content stays utf16. +(component + (component $C + (core module $M + (memory (export "mem") 1) + (global $next (mut i32) (i32.const 1024)) + (func (export "realloc") (param $old i32) (param $osize i32) (param $align i32) (param $nsize i32) (result i32) + (local $r i32) + ;; shrinking realloc: keep the (8-aligned) allocation in place + (if (i32.and (i32.ne (local.get $old) (i32.const 0)) + (i32.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + ;; fresh 8-aligned bump allocation + (global.set $next (i32.and (i32.add (global.get $next) (i32.const 7)) (i32.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i32.add (global.get $next) (local.get $nsize))) + ;; growing realloc must preserve the old contents + (if (i32.ne (local.get $old) (i32.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + + ;; response "\u{f8}k" latin1 = F8 6B (2 code units, untagged) + (data (i32.const 64) "\f8\6b") + ;; response "n\u{2603}" utf16 = 006E 2603 (2 code units, tagged) + (data (i32.const 68) "\6e\00\03\26") + + ;; expect "AB" deflated to latin1 = 41 42 (2 code units, untagged) + (func (export "deflate") (param $ptr i32) (param $tagged i32) (result i32) + (if (i32.ne (local.get $tagged) (i32.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x4241)) (then unreachable)) + (i32.store (i32.const 0) (i32.const 64)) + (i32.store (i32.const 4) (i32.const 2)) + (i32.const 0)) + + ;; expect "\u{2603}" kept as utf16 = 2603 (1 code unit, i32-tagged) + (func (export "keep-wide") (param $ptr i32) (param $tagged i32) (result i32) + (if (i32.ne (local.get $tagged) (i32.const 0x80000001)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x2603)) (then unreachable)) + (i32.store (i32.const 0) (i32.const 68)) + (i32.store (i32.const 4) (i32.const 0x80000002)) + (i32.const 0)) + ) + (core instance $m (instantiate $M)) + (func (export "deflate") (param "s" string) (result string) + (canon lift (core func $m "deflate") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + (func (export "keep-wide") (param "s" string) (result string) + (canon lift (core func $m "keep-wide") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + ) + + (component $D + (import "deflate" (func $deflate (param "s" string) (result string))) + (import "keep-wide" (func $keep-wide (param "s" string) (result string))) + (core module $Libc + (memory (export "mem") i64 1) + (global $next (mut i64) (i64.const 1024)) + (func (export "realloc") (param $old i64) (param $osize i64) (param $align i64) (param $nsize i64) (result i64) + (local $r i64) + (if (i32.and (i64.ne (local.get $old) (i64.const 0)) + (i64.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + (global.set $next (i64.and (i64.add (global.get $next) (i64.const 7)) (i64.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i64.add (global.get $next) (local.get $nsize))) + (if (i64.ne (local.get $old) (i64.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + ) + (core instance $libc (instantiate $Libc)) + (core func $deflate' (canon lower (func $deflate) string-encoding=latin1+utf16 + (memory (core memory $libc "mem")) (realloc (core func $libc "realloc")))) + (core func $keep-wide' (canon lower (func $keep-wide) string-encoding=latin1+utf16 + (memory (core memory $libc "mem")) (realloc (core func $libc "realloc")))) + (core module $Main + (import "" "mem" (memory i64 1)) + (import "" "deflate" (func $deflate (param i64 i64 i64))) + (import "" "keep-wide" (func $keep-wide (param i64 i64 i64))) + + ;; argument "AB" over-encoded as utf16 = 0041 0042 (2 code units, tagged) + (data (i64.const 16) "\41\00\42\00") + ;; argument "\u{2603}" utf16 = 2603 (1 code unit, tagged) + (data (i64.const 24) "\03\26") + + (func (export "run") (result i32) + (local $p i64) + (call $deflate (i64.const 16) (i64.const 0x8000000000000002) (i64.const 32)) + ;; expect "\u{f8}k" latin1 = F8 6B (2 code units, untagged) + (local.set $p (i64.load (i64.const 32))) + (if (i64.ne (i64.load (i64.const 40)) (i64.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $p)) (i32.const 0x6BF8)) (then unreachable)) + (call $keep-wide (i64.const 24) (i64.const 0x8000000000000001) (i64.const 32)) + ;; expect "n\u{2603}" utf16 = 006E 2603 (2 code units, i64-tagged) + (local.set $p (i64.load (i64.const 32))) + (if (i64.ne (i64.load (i64.const 40)) (i64.const 0x8000000000000002)) (then unreachable)) + (if (i32.ne (i32.load (local.get $p)) (i32.const 0x2603006E)) (then unreachable)) + (i32.const 42)) + ) + (core instance $main (instantiate $Main (with "" (instance + (export "mem" (memory $libc "mem")) + (export "deflate" (func $deflate')) + (export "keep-wide" (func $keep-wide')) + )))) + (func (export "run") (result u32) (canon lift (core func $main "run"))) + ) + + (instance $c (instantiate $C)) + (instance $d (instantiate $D + (with "deflate" (func $c "deflate")) + (with "keep-wide" (func $c "keep-wide")) + )) + (func (export "run") (alias export $d "run")) +) +(assert_return (invoke "run") (u32.const 42)) + +;; i32 caller -> i64 callee: the same strings in the other direction. +(component + (component $C + (core module $M + (memory (export "mem") i64 1) + (global $next (mut i64) (i64.const 1024)) + (func (export "realloc") (param $old i64) (param $osize i64) (param $align i64) (param $nsize i64) (result i64) + (local $r i64) + (if (i32.and (i64.ne (local.get $old) (i64.const 0)) + (i64.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + (global.set $next (i64.and (i64.add (global.get $next) (i64.const 7)) (i64.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i64.add (global.get $next) (local.get $nsize))) + (if (i64.ne (local.get $old) (i64.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + + ;; response "\u{f8}k" latin1 = F8 6B (2 code units, untagged) + (data (i64.const 64) "\f8\6b") + ;; response "n\u{2603}" utf16 = 006E 2603 (2 code units, tagged) + (data (i64.const 68) "\6e\00\03\26") + + ;; expect "AB" deflated to latin1 = 41 42 (2 code units, untagged) + (func (export "deflate") (param $ptr i64) (param $tagged i64) (result i64) + (if (i64.ne (local.get $tagged) (i64.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x4241)) (then unreachable)) + (i64.store (i64.const 0) (i64.const 64)) + (i64.store (i64.const 8) (i64.const 2)) + (i64.const 0)) + + ;; expect "\u{2603}" kept as utf16 = 2603 (1 code unit, i64-tagged) + (func (export "keep-wide") (param $ptr i64) (param $tagged i64) (result i64) + (if (i64.ne (local.get $tagged) (i64.const 0x8000000000000001)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x2603)) (then unreachable)) + (i64.store (i64.const 0) (i64.const 68)) + (i64.store (i64.const 8) (i64.const 0x8000000000000002)) + (i64.const 0)) + ) + (core instance $m (instantiate $M)) + (func (export "deflate") (param "s" string) (result string) + (canon lift (core func $m "deflate") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + (func (export "keep-wide") (param "s" string) (result string) + (canon lift (core func $m "keep-wide") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + ) + + (component $D + (import "deflate" (func $deflate (param "s" string) (result string))) + (import "keep-wide" (func $keep-wide (param "s" string) (result string))) + (core module $Libc + (memory (export "mem") 1) + (global $next (mut i32) (i32.const 1024)) + (func (export "realloc") (param $old i32) (param $osize i32) (param $align i32) (param $nsize i32) (result i32) + (local $r i32) + (if (i32.and (i32.ne (local.get $old) (i32.const 0)) + (i32.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + (global.set $next (i32.and (i32.add (global.get $next) (i32.const 7)) (i32.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i32.add (global.get $next) (local.get $nsize))) + (if (i32.ne (local.get $old) (i32.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + ) + (core instance $libc (instantiate $Libc)) + (core func $deflate' (canon lower (func $deflate) string-encoding=latin1+utf16 + (memory (core memory $libc "mem")) (realloc (core func $libc "realloc")))) + (core func $keep-wide' (canon lower (func $keep-wide) string-encoding=latin1+utf16 + (memory (core memory $libc "mem")) (realloc (core func $libc "realloc")))) + (core module $Main + (import "" "mem" (memory 1)) + (import "" "deflate" (func $deflate (param i32 i32 i32))) + (import "" "keep-wide" (func $keep-wide (param i32 i32 i32))) + + ;; argument "AB" over-encoded as utf16 = 0041 0042 (2 code units, tagged) + (data (i32.const 16) "\41\00\42\00") + ;; argument "\u{2603}" utf16 = 2603 (1 code unit, tagged) + (data (i32.const 24) "\03\26") + + (func (export "run") (result i32) + (local $p i32) + (call $deflate (i32.const 16) (i32.const 0x80000002) (i32.const 8)) + ;; expect "\u{f8}k" latin1 = F8 6B (2 code units, untagged) + (local.set $p (i32.load (i32.const 8))) + (if (i32.ne (i32.load (i32.const 12)) (i32.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $p)) (i32.const 0x6BF8)) (then unreachable)) + (call $keep-wide (i32.const 24) (i32.const 0x80000001) (i32.const 8)) + ;; expect "n\u{2603}" utf16 = 006E 2603 (2 code units, i32-tagged) + (local.set $p (i32.load (i32.const 8))) + (if (i32.ne (i32.load (i32.const 12)) (i32.const 0x80000002)) (then unreachable)) + (if (i32.ne (i32.load (local.get $p)) (i32.const 0x2603006E)) (then unreachable)) + (i32.const 43)) + ) + (core instance $main (instantiate $Main (with "" (instance + (export "mem" (memory $libc "mem")) + (export "deflate" (func $deflate')) + (export "keep-wide" (func $keep-wide')) + )))) + (func (export "run") (result u32) (canon lift (core func $main "run"))) + ) + + (instance $c (instantiate $C)) + (instance $d (instantiate $D + (with "deflate" (func $c "deflate")) + (with "keep-wide" (func $c "keep-wide")) + )) + (func (export "run") (alias export $d "run")) +) +(assert_return (invoke "run") (u32.const 43)) + +;; i32 utf8 caller -> i64 latin1+utf16 callee: latin1-representable input +;; arrives as latin1, and other input is inflated to i64-tagged utf16. +(component + (component $C + (core module $M + (memory (export "mem") i64 1) + (global $next (mut i64) (i64.const 1024)) + (func (export "realloc") (param $old i64) (param $osize i64) (param $align i64) (param $nsize i64) (result i64) + (local $r i64) + (if (i32.and (i64.ne (local.get $old) (i64.const 0)) + (i64.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + (global.set $next (i64.and (i64.add (global.get $next) (i64.const 7)) (i64.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i64.add (global.get $next) (local.get $nsize))) + (if (i64.ne (local.get $old) (i64.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + + ;; expect "AB" as latin1 = 41 42 (2 code units, untagged) + (func (export "narrow") (param $ptr i64) (param $tagged i64) + (if (i64.ne (local.get $tagged) (i64.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x4241)) (then unreachable))) + + ;; expect "a\u{2603}" as utf16 = 0061 2603 (2 code units, i64-tagged) + (func (export "wide") (param $ptr i64) (param $tagged i64) + (if (i64.ne (local.get $tagged) (i64.const 0x8000000000000002)) (then unreachable)) + (if (i32.ne (i32.load (local.get $ptr)) (i32.const 0x26030061)) (then unreachable))) + ) + (core instance $m (instantiate $M)) + (func (export "narrow") (param "s" string) + (canon lift (core func $m "narrow") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + (func (export "wide") (param "s" string) + (canon lift (core func $m "wide") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + ) + + (component $D + (import "narrow" (func $narrow (param "s" string))) + (import "wide" (func $wide (param "s" string))) + (core module $Libc + (memory (export "mem") 1) + ) + (core instance $libc (instantiate $Libc)) + (core func $narrow' (canon lower (func $narrow) string-encoding=utf8 + (memory (core memory $libc "mem")))) + (core func $wide' (canon lower (func $wide) string-encoding=utf8 + (memory (core memory $libc "mem")))) + (core module $Main + (import "" "mem" (memory 1)) + (import "" "narrow" (func $narrow (param i32 i32))) + (import "" "wide" (func $wide (param i32 i32))) + + ;; argument "AB" utf8 = 41 42 + (data (i32.const 16) "AB") + ;; argument "a\u{2603}" utf8 = 61 E2 98 83 + (data (i32.const 24) "\61\e2\98\83") + + (func (export "run") (result i32) + (call $narrow (i32.const 16) (i32.const 2)) + (call $wide (i32.const 24) (i32.const 4)) + (i32.const 44)) + ) + (core instance $main (instantiate $Main (with "" (instance + (export "mem" (memory $libc "mem")) + (export "narrow" (func $narrow')) + (export "wide" (func $wide')) + )))) + (func (export "run") (result u32) (canon lift (core func $main "run"))) + ) + + (instance $c (instantiate $C)) + (instance $d (instantiate $D + (with "narrow" (func $c "narrow")) + (with "wide" (func $c "wide")) + )) + (func (export "run") (alias export $d "run")) +) +(assert_return (invoke "run") (u32.const 44)) From f557d855168faddd2741082d79a436f096df88df Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 24 Nov 2026 14:21:32 -0800 Subject: [PATCH 2/2] Address review feedback --- crates/cranelift/src/compiler/component.rs | 19 ++++++++++- crates/environ/src/component/info.rs | 6 ++++ crates/environ/src/fact/trampoline.rs | 33 ++++++------------- .../src/runtime/vm/component/libcalls.rs | 2 +- .../component-model/strings-memory64.wast | 2 +- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/crates/cranelift/src/compiler/component.rs b/crates/cranelift/src/compiler/component.rs index 22fe9322df96..f14923d3c002 100644 --- a/crates/cranelift/src/compiler/component.rs +++ b/crates/cranelift/src/compiler/component.rs @@ -1933,13 +1933,30 @@ impl TrampolineCompiler<'_> { } Transcode::Utf8ToUtf16 - | Transcode::Utf16ToCompactProbablyUtf16 | Transcode::Utf8ToCompactUtf16 | Transcode::Utf16ToCompactUtf16 => { self.raise_if_transcode_trapped(results[0]); raw_results.push(self.cast_from_pointer(results[0], to64)); } + Transcode::Utf16ToCompactProbablyUtf16 => { + self.raise_if_transcode_trapped(results[0]); + let mut len = self.cast_from_pointer(results[0], to64); + + // The libcall's utf16 tag is not the high bit of a 64-bit + // memory's pointer width, so move it there. + if to64 { + let tag = Transcode::UTF16_TO_COMPACT_PROBABLY_UTF16_TAG; + let tag_bit = self.builder.ins().band_imm_u(len, i64::from(tag)); + let untagged = self.builder.ins().bxor(len, tag_bit); + let shift = i64::from(63 - tag.trailing_zeros()); + let new_tag_bit = self.builder.ins().ishl_imm_u(tag_bit, shift); + len = self.builder.ins().bor(untagged, new_tag_bit); + } + + raw_results.push(len); + } + Transcode::Latin1ToUtf8 | Transcode::Utf16ToUtf8 | Transcode::Utf8ToLatin1 diff --git a/crates/environ/src/component/info.rs b/crates/environ/src/component/info.rs index 6ec6ca01bcec..179fd80c9d5d 100644 --- a/crates/environ/src/component/info.rs +++ b/crates/environ/src/component/info.rs @@ -633,6 +633,12 @@ pub enum Transcode { } impl Transcode { + /// The bit that the `Utf16ToCompactProbablyUtf16` host libcall sets in its + /// returned length to indicate utf16. The libcall's trampoline moves it to + /// the high bit of the destination memory's pointer width, which is the + /// guest-visible `latin1+utf16` tag. + pub const UTF16_TO_COMPACT_PROBABLY_UTF16_TAG: u32 = 1 << 31; + /// Get this transcoding's symbol fragment. pub fn symbol_fragment(&self) -> &'static str { match self { diff --git a/crates/environ/src/fact/trampoline.rs b/crates/environ/src/fact/trampoline.rs index 0fd2c6ad266f..5d030530c911 100644 --- a/crates/environ/src/fact/trampoline.rs +++ b/crates/environ/src/fact/trampoline.rs @@ -44,11 +44,6 @@ use super::DataModel; const MAX_STRING_BYTE_LENGTH: u32 = (1 << 31) - 1; -/// The tag that the `Utf16ToCompactProbablyUtf16` transcoder sets on its -/// returned length to indicate utf16. This is always bit 31, regardless of -/// memory width, and is not the guest-visible tag; see `ptr_utf16_tag`. -const TRANSCODER_UTF16_TAG: u32 = 1 << 31; - /// This value is arbitrarily chosen and should be fine to change at any time, /// it just seemed like a halfway reasonable starting point. const INITIAL_FUEL: usize = 1_000; @@ -2234,7 +2229,7 @@ impl<'a, 'b> Compiler<'a, 'b> { // source code unit length. if self.module.tunables.debug_adapter_modules { self.instruction(LocalGet(dst.len.idx)); - self.ptr_uconst(dst_mem_opts, !TRANSCODER_UTF16_TAG); + self.ptr_utf16_untag_mask(dst_mem_opts); self.ptr_and(dst_mem_opts); self.convert_src_len_to_dst(src.len.idx, src_mem_opts.ptr(), dst_mem_opts.ptr()); self.ptr_ne(dst_mem_opts); @@ -2243,23 +2238,6 @@ impl<'a, 'b> Compiler<'a, 'b> { self.instruction(End); } - // Move the transcoder's utf16 tag to the destination memory's tag. - if dst_mem_opts.memory64() { - self.instruction(LocalGet(dst.len.idx)); - self.instruction(I64Const(TRANSCODER_UTF16_TAG.into())); - self.instruction(I64And); - self.instruction(I64Const(0)); - self.instruction(I64Ne); - self.instruction(If(BlockType::Empty)); - self.instruction(LocalGet(dst.len.idx)); - self.instruction(I64Const( - (u64::from(TRANSCODER_UTF16_TAG) | (1 << 63)).cast_signed(), - )); - self.instruction(I64Xor); - self.instruction(LocalSet(dst.len.idx)); - self.instruction(End); - } - // If the utf16 tag is set then utf16 was used and the destination // should be appropriately sized. Bail out of the "is this string // empty" block and fall through otherwise to resizing. @@ -4332,6 +4310,15 @@ impl<'a, 'b> Compiler<'a, 'b> { } } + /// Pushes the mask that clears `ptr_utf16_tag` for `opts`'s memory. + fn ptr_utf16_untag_mask(&mut self, opts: &LinearMemoryOptions) { + if opts.memory64() { + self.instruction(I64Const(i64::MAX)); + } else { + self.instruction(I32Const(i32::MAX)); + } + } + fn ptr_iconst(&mut self, opts: &LinearMemoryOptions, val: i32) { if opts.memory64() { self.instruction(I64Const(val.into())); diff --git a/crates/wasmtime/src/runtime/vm/component/libcalls.rs b/crates/wasmtime/src/runtime/vm/component/libcalls.rs index 76144d5a4828..0021a78d2b1c 100644 --- a/crates/wasmtime/src/runtime/vm/component/libcalls.rs +++ b/crates/wasmtime/src/runtime/vm/component/libcalls.rs @@ -15,7 +15,7 @@ use core::ptr::NonNull; use core::slice; use wasmtime_environ::component::*; -const UTF16_TAG: usize = 1 << 31; +const UTF16_TAG: usize = Transcode::UTF16_TO_COMPACT_PROBABLY_UTF16_TAG as usize; macro_rules! signature { (@ty size) => (usize); diff --git a/tests/misc_testsuite/component-model/strings-memory64.wast b/tests/misc_testsuite/component-model/strings-memory64.wast index fa22057a1686..1e7e5c784234 100644 --- a/tests/misc_testsuite/component-model/strings-memory64.wast +++ b/tests/misc_testsuite/component-model/strings-memory64.wast @@ -291,7 +291,7 @@ ;; argument "AB" utf8 = 41 42 (data (i32.const 16) "AB") ;; argument "a\u{2603}" utf8 = 61 E2 98 83 - (data (i32.const 24) "\61\e2\98\83") + (data (i32.const 24) "a\u{2603}") (func (export "run") (result i32) (call $narrow (i32.const 16) (i32.const 2))