Debian Codegraph @

crates/wit-component/tests/components/adapt-inject-stack-with-realloc/adapt-old.wat

raw
1 (module
2 (import "new" "get-two" (func $get_two (param i32)))
3 (import "__main_module__" "cabi_realloc" (func $cabi_realloc (param i32 i32 i32 i32) (result i32)))
4 (import "env" "memory" (memory 0))
5
6 (global $__stack_pointer (mut i32) i32.const 0)
7 (global $some_other_mutable_global (mut i32) i32.const 0)
8
9 ;; `wit-component` should use this to track the status of a lazy stack
10 ;; allocation:
11 (global $allocation_state (mut i32) i32.const 0)
12
13 ;; This is a sample adapter which is adapting between ABI. This exact function
14 ;; signature is imported by `module.wat` and we're implementing it here with a
15 ;; canonical-abi function that returns two integers. The canonical ABI for
16 ;; returning two integers is different than the ABI of this function, hence
17 ;; the adapter here.
18 ;;
19 ;; The purpose of this test case is to exercise the `$__stack_pointer` global.
20 ;; The stack pointer here needs to be initialized to something valid for
21 ;; this adapter module which is done with an injected `start` function into
22 ;; this adapter module when it's bundled into a component.
23 (func (export "get_sum") (result i32)
24 (local i32 i32)
25
26 ;; `wit-component` should have injected a call to a function that allocates
27 ;; the stack and sets $allocation_state to 2
28 (if (i32.ne (global.get $allocation_state) (i32.const 2)) (then (unreachable)))
29
30 ;; First, allocate a page using $cabi_realloc and write to it. This tests
31 ;; that we can use the main module's allocator if present (or else a
32 ;; substitute synthesized by `wit-component`).
33 (local.set 0
34 (call $cabi_realloc
35 (i32.const 0)
36 (i32.const 0)
37 (i32.const 8)
38 (i32.const 65536)))
39
40 (i32.store (local.get 0) (i32.const 42))
41 (i32.store offset=65532 (local.get 0) (i32.const 42))
42
43 ;; Allocate 8 bytes of stack space for the two u32 return values. The
44 ;; original stack pointer is saved in local 0 and the stack frame for this
45 ;; function is saved in local 1.
46 global.get $__stack_pointer
47 local.tee 0
48 i32.const 8
49 i32.sub
50 local.tee 1
51 global.set $__stack_pointer
52
53 ;; Call the imported function which will return two u32 values into the
54 ;; return pointer specified here, our stack frame.
55 local.get 1
56 call $get_two
57
58 ;; Compute the result of this function by adding together the two return
59 ;; values.
60 (i32.add
61 (i32.load (local.get 1))
62 (i32.load offset=4 (local.get 1)))
63
64 ;; Test that if there is another mutable global in this module that it
65 ;; doesn't affect the detection of the stack pointer. This extra mutable
66 ;; global should not be initialized or tampered with as part of the
67 ;; initialize-the-stack-pointer injected function
68 (global.set $some_other_mutable_global (global.get $some_other_mutable_global))
69
70 ;; Restore the stack pointer to the value it was at prior to entering this
71 ;; function.
72 local.get 0
73 global.set $__stack_pointer
74 )
75
76 )