Debian Codegraph @

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