-
Notifications
You must be signed in to change notification settings - Fork 821
Fix use-after-free resolving reference type names in wast-parser #2805
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
Merged
+63
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| ;;; TOOL: wat2wasm | ||
| ;;; ARGS: --enable-function-references | ||
| ;;; ERROR: 1 | ||
| (module | ||
| (type $t (func)) | ||
|
|
||
| ;; Each field below names a reference type, which defers its resolution to | ||
| ;; the end of the module, and then fails to parse. | ||
| (func (param (ref $t)) (foo)) | ||
| (func (result (ref null $t)) (foo)) | ||
| (func (local (ref $t)) (foo)) | ||
| (table 0 (ref null $t) (foo)) | ||
| (elem declare (ref null $t) (item ref.null $t) foo) | ||
| (import "a" "b" (func (param (ref $t)) foo)) | ||
|
|
||
| (memory 1) | ||
| ) | ||
| (;; STDERR ;;; | ||
| out/test/parse/bad-ref-type-in-failed-field.txt:9:27: error: unexpected token "foo", expected an instr. | ||
| (func (param (ref $t)) (foo)) | ||
| ^^^ | ||
| out/test/parse/bad-ref-type-in-failed-field.txt:10:33: error: unexpected token "foo", expected an instr. | ||
| (func (result (ref null $t)) (foo)) | ||
| ^^^ | ||
| out/test/parse/bad-ref-type-in-failed-field.txt:11:27: error: unexpected token "foo", expected an instr. | ||
| (func (local (ref $t)) (foo)) | ||
| ^^^ | ||
| out/test/parse/bad-ref-type-in-failed-field.txt:12:27: error: unexpected token "foo", expected an instr. | ||
| (table 0 (ref null $t) (foo)) | ||
| ^^^ | ||
| out/test/parse/bad-ref-type-in-failed-field.txt:13:50: error: unexpected token foo, expected ). | ||
| (elem declare (ref null $t) (item ref.null $t) foo) | ||
| ^^^ | ||
| out/test/parse/bad-ref-type-in-failed-field.txt:14:42: error: unexpected token foo, expected ). | ||
| (import "a" "b" (func (param (ref $t)) foo)) | ||
| ^^^ | ||
| ;;; STDERR ;;) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still a little confused, if we are returning failure, when why does it matter about the state of these members? If we fail to parse why is anybody accessing these after this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The failure doesn't propagate.
ParseModuleFieldListswallows it:It resynchronises to the next module field and carries on parsing, so wat2wasm reports every parse error in one pass rather than stopping at the first. The failed field's result is dropped right there.
So control still reaches the three resolve loops at the bottom of that same function, which walk
resolve_ref_types_/resolve_type_vectors_/resolve_funcs_and dereference the pointers recorded while the field was being parsed. Those point into the field's localunique_ptr, which was destroyed on the error path instead of being handed toAppendField. That's the freed read, and a freed write vialocal_types.Set()in the func-local case.The one case where nobody touches them is when
Synchronizefails too, i.e. the bad field is the last thing in the file, since then we return early and never reach the loops. That's why the test has a(memory 1)after the broken fields: it gives Synchronize somewhere to land so the loops are actually reached.