1;;! multi_memory = true 2 3;; Transcode a utf16 string to latin1+utf16, but the original string is 4;; out-of-bounds. Should report a first-class error. 5(component 6 (component $dst 7 (core module $m 8 (func (export "recv") (param i32 i32)) 9 (func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0) 10 (memory (export "memory") 1) 11 ) 12 (core instance $i (instantiate $m)) 13 (func (export "recv") (param "a" string) 14 (canon lift (core func $i "recv") (realloc (func $i "realloc")) (memory $i "memory") 15 string-encoding=latin1+utf16) 16 ) 17 ) 18 19 ;; Source component: uses utf16 encoding. 20 ;; Passes a string placed near the END of linear memory to $dst. 21 (component $src 22 (import "recv" (func $recv (param "a" string))) 23 (core module $libc 24 (memory (export "memory") 1) ;; 1 page = 65536 bytes 25 (func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0) 26 ) 27 (core instance $libc (instantiate $libc)) 28 ;; canon lower with utf16 — the source side of the mismatch. 29 (core func $recv_lowered (canon lower (func $recv) string-encoding=utf16 (memory $libc "memory"))) 30 (core module $m 31 (import "" "" (func $recv (param i32 i32))) 32 (import "libc" "memory" (memory 0)) 33 (func (export "run") 34 ;; Write 8 UTF-16 code units (16 bytes) of 'A' (U+0041) at the end of memory. 35 ;; Offsets 65520–65535: exactly fills to the last byte of the page. 36 (i32.store (i32.const 65520) (i32.const 0x00410041)) 37 (i32.store (i32.const 65524) (i32.const 0x00410041)) 38 (i32.store (i32.const 65528) (i32.const 0x00410041)) 39 (i32.store (i32.const 65532) (i32.const 0x00410041)) 40 41 ;; Pass ptr=65520, len=10 CODE UNITS (not bytes). 42 ;; We wrote 8 code units but claim 10 — the extra 2 are past end of memory. 43 (call $recv (i32.const 65520) (i32.const 10)) 44 ) 45 ) 46 (core instance $i (instantiate $m 47 (with "" (instance (export "" (func $recv_lowered)))) 48 (with "libc" (instance $libc)) 49 )) 50 (func (export "run") 51 (canon lift (core func $i "run")) 52 ) 53 ) 54 55 ;; Wire the components together and run. 56 (instance $dst_inst (instantiate $dst)) 57 (instance $i (instantiate $src (with "recv" (func $dst_inst "recv")))) 58 59 (export "run" (func $i "run")) 60) 61 62(assert_trap (invoke "run") "string content out-of-bounds") 63