Skip to content

Instantly share code, notes, and snippets.

@agryaznov
Last active November 2, 2023 11:55
Show Gist options
  • Save agryaznov/67c6d7e49935afb74c500a0e508ef647 to your computer and use it in GitHub Desktop.
Save agryaznov/67c6d7e49935afb74c500a0e508ef647 to your computer and use it in GitHub Desktop.
wasm instumentation examples
(module
(import "seal0" "seal_balance" (func $seal_balance (param i32 i32)))
(import "env" "memory" (memory 1 1))
;; [0, 8) reserved for $seal_balance output
;; [8, 16) length of the buffer for $seal_balance
;; [16, inf) zero initialized
(data (i32.const 8) "\08")
;; Utility function for making assertions.
(func $assert (param i32)
(block $ok
(br_if $ok
(local.get 0)
)
(unreachable)))
;; Contract constructor
(func (export "deploy"))
;; Contract call
(func (export "call")
(call $seal_balance (i32.const 0) (i32.const 8))
;; Balance should be encoded as a u64.
(call $assert
(i32.eq
(i32.load (i32.const 8))
(i32.const 8)))
;; Assert the free balance to be zero.
(call $assert
(i64.eq
(i64.load (i32.const 0))
(i64.const 0)))))
(module
(type (;0;) (func (param i32 i32)))
(type (;1;) (func (param i32)))
(type (;2;) (func))
(import "seal0" "seal_balance" (func (;0;) (type 0)))
(import "env" "memory" (memory (;0;) 1 1))
(func (;1;) (type 1) (param i32)
block ;; label = @1
local.get 0
br_if 0 (;@1;)
unreachable
end)
(func (;2;) (type 2))
(func (;3;) (type 2)
i32.const 0
i32.const 8
call 0
i32.const 8
i32.load
i32.const 8
i32.eq
call 1
i32.const 0
i64.load
i64.const 0
i64.eq
call 1)
(export "deploy" (func 2))
(export "call" (func 3))
(data (;0;) (i32.const 8) "\08"))
(module
(type (;0;) (func (param i32 i32)))
(type (;1;) (func (param i32)))
(type (;2;) (func))
(type (;3;) (func (param i64)))
(import "seal0" "seal_balance" (func (;0;) (type 0)))
(import "env" "memory" (memory (;0;) 1 1))
(import "env" "gas" (func (;1;) (type 3)))
(func (;2;) (type 1) (param i32)
i64.const 3
call 1
block ;; label = @1
local.get 0
br_if 0 (;@1;)
i64.const 1
call 1
unreachable
end)
(func (;3;) (type 2))
(func (;4;) (type 2)
i64.const 13
call 1
i32.const 0
i32.const 8
call 0
i32.const 8
i32.load
i32.const 8
i32.eq
call 2
i32.const 0
i64.load
i64.const 0
i64.eq
call 2)
(export "deploy" (func 3))
(export "call" (func 4))
(data (;0;) (i32.const 8) "\08"))
(module
(type (;0;) (func (param i32 i32)))
(type (;1;) (func (param i32)))
(type (;2;) (func))
(type (;3;) (func (param i64)))
(import "seal0" "seal_balance" (func (;0;) (type 0)))
(import "env" "memory" (memory (;0;) 1 1))
(func (;1;) (type 1) (param i32)
i64.const 14
call 4
block ;; label = @1
local.get 0
br_if 0 (;@1;)
i64.const 12
call 4
unreachable
end)
(func (;2;) (type 2))
(func (;3;) (type 2)
i64.const 24
call 4
i32.const 0
i32.const 8
call 0
i32.const 8
i32.load
i32.const 8
i32.eq
call 1
i32.const 0
i64.load
i64.const 0
i64.eq
call 1)
(func (;4;) (type 3) (param i64)
global.get 0
local.get 0
i64.ge_u
if ;; label = @1
global.get 0
local.get 0
i64.sub
global.set 0
else
i64.const -1
global.set 0
unreachable
end)
(global (;0;) (mut i64) (i64.const 0))
(export "deploy" (func 2))
(export "call" (func 3))
(export "gas_left" (global 0))
(data (;0;) (i32.const 8) "\08"))
(func $gas (param i64)
global.get 0
local.get 0
i64.ge_u
if
global.get 0
local.get 0
i64.sub
global.set 0
else
i64.const -1
global.set 0
unreachable
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment