Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime crash: stack underflow when executing opcode switch #103

Closed
linxuanm opened this issue Dec 20, 2024 · 1 comment · Fixed by #105
Closed

runtime crash: stack underflow when executing opcode switch #103

linxuanm opened this issue Dec 20, 2024 · 1 comment · Fixed by #105

Comments

@linxuanm
Copy link

Hi! While writing spectests for Wizard we ran into a stack underflow crash with the reference interpreter switch5.wast:12.5-12.20: runtime crash: stack underflow (referring to the switch in $fn_1):

(module
  (type $f1 (func (result i32)))
  (type $c1 (cont $f1))
  (type $f2 (func (param (ref null $c1)) (result i32)))
  (type $c2 (cont $f2))
  (type $f3 (func (param (ref null $c2)) (result i32)))
  (type $c3 (cont $f3))
  (tag $e (result i32))

  (func $fn_1 (param (ref null $c2)) (result i32)
    (local.get 0)
    (switch $c2 $e)
    (i32.const 24)
  )
  (elem declare func $fn_1)

  (func $fn_2 (result i32)
    (cont.new $c3 (ref.func $fn_1))
    (switch $c3 $e)
    (drop)
    (i32.const -1)
  )
  (elem declare func $fn_2)

  (func (export "main") (result i32)
    (cont.new $c1 (ref.func $fn_2))
    (resume $c1 (on $e switch))
  )
)

(assert_return (invoke "main") (i32.const -1))

I don't think this is the intended behavior. Is this a problem in the reference interpreter? Thanks!

@dhil
Copy link
Member

dhil commented Dec 29, 2024

Thanks! The problem seems to be here

let cont' = Ref (ContRef (ref (Some (Int32.add (Lib.List32.length ts) 1l, ctxt'')))) in

The arity of the new continuation reference is mistakenly derived from the length of ts, which is codomain of the switch-tag. The arity of new continuation reference is known at the switch point as it can be read off the type annotation (though somewhat awkwardly).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants