WebAssembly/wasp

ref.func incorrectly gives "Undeclared function reference" if there is no reference to the function in a global (wat2wasm)

Closed this issue · 3 comments

The following code

(module
;;(type $t (func))
;;(global (ref $t) (ref.func $f))
(func $f (block
  (ref.func $f)
  (drop)
))
)

gives the following error:

/tmp/test.wat:5:4: Undeclared function reference 0
  (ref.func $f)

Assigning the reference to a global (uncommenting the commented code) allows the reference to the function to be taken in the code below with no error.

There is a comment in validate.cc:205 which seems suspect

        // ref.func indexes are implicitly declared by referencing them in a
        // constant expression.
binji commented

IIRC, functions have to be declared somewhere before the code section before they can be used by ref.func. You can do it with a global, but if you don't need a global you can declare it in a block like this:

(module
(elem declare func $f)
(func $f (block
  (ref.func $f)
  (drop)
))
)

I didn't see that in the function references proposal text.

binji commented

I'm not sure where that's mentioned either, but you can see the same error in the reference interpreter:

$ cat test.wat
(module
;;(elem declare func $f)
(func $f (block
  (ref.func $f)
  (drop)
))
)
$ ./wasm test.wat
test.wat:4.13-4.15: invalid module: undeclared function reference 0