bytecodealliance / bytecodealliance/endive

Validator: `catch_ref` and `catch_all_ref` wrongly assume nullable exception type

Open Beginner friendly
#189 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
296
Forks
21
Avg merge
2d 12h
Merged PRs (30d)
29

Description

When a catch block is reached with an exception reference, it can be declared with a non-nullable `(ref exn)`, e.g. this is a valid WebAssembly module:

```wast
(module
(tag $my_error)
(func $try_and_catch
(block $handler (result (ref exn))
(try_table (catch_ref $my_error $handler)
)
(return)
)
(drop)
)
(export "main" (func $try_and_catch))
)
```

`wasm-tools validate` agrees with this being valid, I used `wasm-tools parse` to turn it into a binary module. Attempting to load this module with endive fails:

```
Exception in thread "main" run.endive.wasm.InvalidException: type mismatch: instruction requires [ref[-23]] but stack has [refnull[-23]]
at run.endive.wasm.Validator.validateFunction(Validator.java:2799)
at run.endive.wasm.Validator.validateFunctions(Validator.java:906)
at run.endive.wasm.WasmModule$Builder.build(WasmModule.java:289)
at run.endive.wasm.Parser.parse(Parser.java:301)
at run.endive.wasm.Parser.parse(Parser.java:262)
at run.endive.wasm.Parser.parse(Parser.java:254)
```

The reason is that in `validateFunction`, validating a `try_block` pushes a `ValType.ExnRef` for `CATCH_REF` and `CATCH_ALL_REF`. `exnref` (aka `(ref null exn)` is not the same as `(ref exn)` though, which is what is [required by the spec](https://webassembly.github.io/spec/core/valid/instructions.html#xref-syntax-instructions-syntax-instr-control-mathsf-catch-ref-x-l).

Contributor guide

Open the contributing guide

Research direction

Start in Validator.java at validateFunction, where try_block handling pushes ValType.ExnRef for CATCH_REF and CATCH_ALL_REF. Use the WebAssembly module in the issue as the reproduction case and verify that validation accepts its non-nullable (ref exn) handler reference without the reported type mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, wasm
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.