Skip to content

Fix Invalid Instruction format in fuzzgen#4738

Merged
jameysharp merged 3 commits into
bytecodealliance:mainfrom
afonso360:fix-inst-fmt
Aug 20, 2022
Merged

Fix Invalid Instruction format in fuzzgen#4738
jameysharp merged 3 commits into
bytecodealliance:mainfrom
afonso360:fix-inst-fmt

Conversation

@afonso360
Copy link
Copy Markdown
Contributor

👋 Hey,

So in #4733 oss-fuzz discovered that we have pretty much been using the wrong instruction formats for all opcodes for a while... (Thanks @jameysharp for digging into this!).

This PR does 2 things:

  • Force fuzzgen to use the correct InstructionFormat for each opcode
  • Add an assert to all InstructionFormat inserters to ensure that this does not happen again (even for other users of cranelift).

Fixes #4733
cc: @cfallin

Copy link
Copy Markdown
Contributor

@jameysharp jameysharp left a comment

Choose a reason for hiding this comment

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

I'm quite pleased with your fuzzgen changes here. I was afraid this would be much more complicated to fix!

@cfallin, what's your take on the new assert when constructing instructions? Is there ever a case where someone should be allowed to use a different instruction-format constructor than the standard format for an opcode?

@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:meta Everything related to the meta-language. labels Aug 19, 2022
@fitzgen
Copy link
Copy Markdown
Member

fitzgen commented Aug 19, 2022

Is there ever a case where someone should be allowed to use a different instruction-format constructor than the standard format for an opcode?

That should never happen. Would break our ISLE extractors for matching clif instructions, at the very least. But a ton of helpers only look for certain opcodes in certain formats, etc.

@jameysharp
Copy link
Copy Markdown
Contributor

That should never happen. Would break our ISLE extractors for matching clif instructions, at the very least. But a ton of helpers only look for certain opcodes in certain formats, etc.

Just to be clear: If it should never happen, then a runtime assertion that it never happens is a good idea, right?

@fitzgen
Copy link
Copy Markdown
Member

fitzgen commented Aug 19, 2022

Yeah, probably a debug assertion. (I haven't looked at this patch at all)

@cfallin
Copy link
Copy Markdown
Member

cfallin commented Aug 19, 2022

Indeed it should never happen, and this is usually enforced by the generated builder interface's per-instruction methods, but nothing currently stops someone from manually putting the wrong pieces together in the data structure or invoking the per-instruction-format method with an opcode as in the test here. That will break a lot of things (including ISLE matchers as Nick notes). Our best defense is to guard against this at construction time as best we can.

An assert in the per-instruction-format builder API seems totally reasonable, but let's make it a debug_assert rather than assert so that we don't have any runtime impact (the debug assert will still be tested in the fuzzing configuration).

@jameysharp jameysharp enabled auto-merge (squash) August 20, 2022 00:40
@jameysharp jameysharp merged commit d620705 into bytecodealliance:main Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:meta Everything related to the meta-language. cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cranelift-fuzzgen: Instruction format doesn't have a designated operand, bad opcode

4 participants