notes icon indicating copy to clipboard operation
notes copied to clipboard

wasm-metered code

Open void4 opened this issue 6 years ago • 0 comments

This is how metered code looks like.
A function ("metering/usegas") is added as an import.
It has one argument (the gas that is used in the block after the invocation).
At the top of every block (sequences, conditionals or loops),
this value is pushed on to the stack (here: 511).
Then the function is called. The function adds this value to an external counter.
This counter must not be accessible by the wasm code. wasm-metering also
checks that the usegas function is not called manually, 
to prevent overflow attacks. Usegas raises an exception if the counter
surpasses a certain value (the gas limit).

(module
  (type (;0;) (func (param i32)))
  (type (;1;) (func (param i32 i32)))
  (type (;2;) (func (param i32)))
  (import "system" "print_i32" (func (;0;) (type 0)))
  (import "metering" "usegas" (func (;1;) (type 2)))
  (func (;2;) (type 1) (param i32 i32)
    i32.const 511
    call 1
    get_local 0
    call 0
    get_local 1
    call 0)
  (export "add" (func 2)))

void4 avatar Aug 12 '18 14:08 void4