Skip to content

Conversation

@keithw
Copy link
Member

@keithw keithw commented Jun 8, 2023

This PR lets the BinaryReader detect malformed modules in the binary format that:

  • are missing END opcodes at the end of functions
  • have too many END opcodes at the end of functions
  • have DELEGATE opcodes outside a TRY
  • have ELSE opcodes outside an IF

Previously we were only catching some of these situations later in validation. This is a prelude for a future PR that will make the spectest interpreter require that assert_malformed fail to parse the module (currently assert_malformed is treated the same as assert_invalid, i.e. the command passes as long as validation fails).

(;; STDERR ;;;
out/test/regress/regress-28/regress-28.wasm:000001c: error: type mismatch at end of function, expected [] but got [any]
000001c: error: EndFunctionBody callback failed
0000019: error: function body shorter than given size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we loose the filename here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, nice catch. :-/ It turns out that right now every error that's caught inside the BinaryReader itself is getting saved without a filename, because the BinaryReader doesn't know anything about filenames and when it creates an Error, it sets the error's Location where only the offset is set (the filename field is empty).

We can change this if you want -- e.g.

  • we could make BinaryReader take an optional filename in its constructor that's just used for error logging, or
  • we could make BinaryReaderIR/BinaryReaderInterp::OnError method modify the error's Location before saving it, i.e. from their own GetLocation() function (which produces a richer Location with the filename filled in)

I just tried this and it ended up changing a ton of the test diagnostic output, but it's definitely doable either way.

Hope you're okay if we merge this now and can revisit the filenames in error logging in a future PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I'm not too bothered about that.

@keithw keithw merged commit 10652e0 into main Jun 9, 2023
@keithw keithw deleted the binaryreader-malformed-checks branch June 9, 2023 03:03
@keithw keithw mentioned this pull request Oct 24, 2023
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 this pull request may close these issues.

3 participants