1b242f5a6SAlex Crichton;;! component_model_async = true
2b242f5a6SAlex Crichton;;! reference_types = true
3b242f5a6SAlex Crichton;;! gc_types = true
4b242f5a6SAlex Crichton
5b242f5a6SAlex Crichton;; This test previously caused Wasmtime to panic while handling a trap due to an
6b242f5a6SAlex Crichton;; improperly disposed fiber.  That bug is fixed now, and this test helps ensure
7b242f5a6SAlex Crichton;; it stays fixed.
8b242f5a6SAlex Crichton;;
9b242f5a6SAlex Crichton;; (Copied from https://github.com/bytecodealliance/wasmtime/issues/11668#issue-3402875697)
10b242f5a6SAlex Crichton(component
11b242f5a6SAlex Crichton  (component $A
12b242f5a6SAlex Crichton    (core module $a
13b242f5a6SAlex Crichton      (func (export "run") (result i32)
14b242f5a6SAlex Crichton        i32.const 1)
15b242f5a6SAlex Crichton      (func (export "run-cb") (param i32 i32 i32) (result i32)
16b242f5a6SAlex Crichton        unreachable)
17b242f5a6SAlex Crichton    )
18b242f5a6SAlex Crichton
19b242f5a6SAlex Crichton    (core instance $a (instantiate $a))
20*8992b99bSJoel Dice    (func (export "run") async
21b242f5a6SAlex Crichton      (canon lift (core func $a "run") async (callback (func $a "run-cb"))))
22b242f5a6SAlex Crichton  )
23b242f5a6SAlex Crichton  (component $B
24*8992b99bSJoel Dice    (import "a" (instance $a (export "run" (func async))))
25b242f5a6SAlex Crichton
26b242f5a6SAlex Crichton    (core module $libc (memory (export "memory") 1))
27b242f5a6SAlex Crichton    (core instance $libc (instantiate $libc))
28b242f5a6SAlex Crichton
29b242f5a6SAlex Crichton    (core func $run (canon lower (func $a "run") async))
30b242f5a6SAlex Crichton    (core func $new (canon waitable-set.new))
31b242f5a6SAlex Crichton    (core func $join (canon waitable.join))
32b242f5a6SAlex Crichton    (core func $drop (canon waitable-set.drop))
33b242f5a6SAlex Crichton    (core func $wait (canon waitable-set.wait (memory $libc "memory")))
34b242f5a6SAlex Crichton
35b242f5a6SAlex Crichton    (core module $b
36b242f5a6SAlex Crichton      (import "" "run" (func $run_a (result i32)))
37b242f5a6SAlex Crichton      (import "" "new" (func $new (result i32)))
38b242f5a6SAlex Crichton      (import "" "join" (func $join (param i32 i32)))
39b242f5a6SAlex Crichton      (import "" "drop" (func $drop (param i32)))
40b242f5a6SAlex Crichton      (import "" "wait" (func $wait (param i32 i32) (result i32)))
41b242f5a6SAlex Crichton
42b242f5a6SAlex Crichton      (func (export "run")
43b242f5a6SAlex Crichton        (local $ret i32)
44b242f5a6SAlex Crichton        (local $set i32)
45b242f5a6SAlex Crichton
46b242f5a6SAlex Crichton        (local.set $ret (call $run_a))
47b242f5a6SAlex Crichton
48b242f5a6SAlex Crichton        ;; make sure it's in the "started" state
49b242f5a6SAlex Crichton        (if (i32.ne (i32.and (local.get $ret) (i32.const 0xf)) (i32.const 1))
50b242f5a6SAlex Crichton          (then (unreachable)))
51b242f5a6SAlex Crichton
52b242f5a6SAlex Crichton        ;; extract the waitable handle
53b242f5a6SAlex Crichton        (local.set $ret (i32.shr_u (local.get $ret) (i32.const 4)))
54b242f5a6SAlex Crichton
55b242f5a6SAlex Crichton        ;; Make a waitable set and insert our handle into it
56b242f5a6SAlex Crichton        (local.set $set (call $new))
57b242f5a6SAlex Crichton        (call $join (local.get $ret) (local.get $set))
58b242f5a6SAlex Crichton
59b242f5a6SAlex Crichton        ;; wait for something to happen filling in memory address 4, but don't
60b242f5a6SAlex Crichton        ;; actually see what happened since this traps right now.
61b242f5a6SAlex Crichton        (call $wait (local.get $set) (i32.const 4))
62b242f5a6SAlex Crichton        drop
63b242f5a6SAlex Crichton      )
64b242f5a6SAlex Crichton    )
65b242f5a6SAlex Crichton    (core instance $b (instantiate $b
66b242f5a6SAlex Crichton      (with "" (instance
67b242f5a6SAlex Crichton        (export "run" (func $run))
68b242f5a6SAlex Crichton        (export "new" (func $new))
69b242f5a6SAlex Crichton        (export "join" (func $join))
70b242f5a6SAlex Crichton        (export "drop" (func $drop))
71b242f5a6SAlex Crichton        (export "wait" (func $wait))
72b242f5a6SAlex Crichton      ))
73b242f5a6SAlex Crichton    ))
74*8992b99bSJoel Dice    (func (export "run") async
75b242f5a6SAlex Crichton      (canon lift (core func $b "run")))
76b242f5a6SAlex Crichton  )
77b242f5a6SAlex Crichton
78b242f5a6SAlex Crichton  (instance $a (instantiate $A))
79b242f5a6SAlex Crichton  (instance $b (instantiate $B (with "a" (instance $a))))
80b242f5a6SAlex Crichton  (export "run" (func $b "run"))
81b242f5a6SAlex Crichton)
82b242f5a6SAlex Crichton
83b242f5a6SAlex Crichton(assert_trap (invoke "run") "wasm `unreachable` instruction executed")
84