Skip to content

thunk calls might be instrumented incorrectly #82

@StackOverflowExcept1on

Description

@StackOverflowExcept1on

for example, set the stack height limit to 64, but local.get $i will not be charged

(module
  (func $f (export "f")
    ;; 128 args in thunk func
    (param
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32

      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
      i32 i32 i32 i32 i32 i32 i32 i32
    )
    i32.const 0
    drop
  )
)

after instrumentation:

(module
  (type (;0;) (func (param i32 i32 i32 ))) ;; ... *i32 (128 times)*
  (func $f (;0;) (type 0) (param i32 i32 i32) ;; ... *i32 (128 times)*
    i32.const 0
    drop
  )
  (func (;1;) (type 0) (param i32 i32 i32) ;; ... *i32 (128 times)*
    local.get 0
    local.get 1
    local.get 2
    ;; ...
    local.get 127
    ;; ^^^ not charged
    global.get 0
    i32.const 3 ;; callee_stack_cost
    i32.add
    global.set 0
    global.get 0
    i32.const 64 ;; stack_limit
    i32.gt_u
    if ;; label = @1
      unreachable
    end
    call $f
    global.get 0
    i32.const 3 ;; callee_stack_cost
    i32.sub
    global.set 0
  )
  (global (;0;) (mut i32) i32.const 0)
  (export "f" (func 1))
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions