1;;! multi_memory = true
2;;! hogs_memory = true
3
4;; Sending a massive string
5(component definition $A
6  (component $A
7    (core module $m
8      (memory (export "m") 1)
9      (func (export "f") (param i32 i32) unreachable)
10      (func (export "realloc") (param i32 i32 i32 i32) (result i32) unreachable)
11    )
12    (core instance $i (instantiate $m))
13    (func (export "f") (param "x" string)
14      (canon lift
15        (core func $i "f")
16        (memory $i "m")
17        (realloc (func $i "realloc"))
18      )
19    )
20  )
21  (instance $a (instantiate $A))
22
23  (component $B
24    (import "f" (func $f (param "x" string)))
25    (core module $libc (memory (export "mem") 1))
26    (core instance $libc (instantiate $libc))
27    (core func $f (canon lower (func $f) (memory $libc "mem")))
28    (core module $m
29      (import "" "f" (func $f (param i32 i32)))
30      (import "" "mem" (memory 1))
31
32      (func (export "run") (param i32)
33        (call $f (i32.const 0) (local.get 0)))
34
35      (func (export "grow") (param i32) (result i32)
36        (memory.grow (local.get 0)))
37    )
38    (core instance $i (instantiate $m
39      (with "" (instance
40        (export "f" (func $f))
41        (export "mem" (memory $libc "mem"))
42      ))
43    ))
44    (func (export "run") (param "x" u32) (canon lift (core func $i "run")))
45    (func (export "grow") (param "x" u32) (result s32)
46      (canon lift (core func $i "grow")))
47  )
48  (instance $b (instantiate $B (with "f" (func $a "f"))))
49  (export "run" (func $b "run"))
50  (export "grow" (func $b "grow"))
51)
52
53;; Wildly out of bounds is just rejected
54(component instance $A $A)
55(assert_trap (invoke "run" (u32.const 0x8000_0000)) "string content out-of-bounds")
56
57;; In-bounds, and just under the limit. Should hit the `unreachable` in the
58;; `realloc`.
59(component instance $A $A)
60(assert_return (invoke "grow" (u32.const 65530)) (s32.const 1))
61(assert_trap (invoke "run" (u32.const 0x7fff_ffff)) "unreachable")
62
63;; Size exceeds `(1<<31)-1`
64(component instance $A $A)
65(assert_return (invoke "grow" (u32.const 65530)) (s32.const 1))
66(assert_trap (invoke "run" (u32.const 0x8000_0000)) "string content out-of-bounds")
67