Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/wabt/wast-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class WastParser {
ReferenceVars*, Errors*);
Result ParseModuleFieldList(Module*);
Result ParseModuleField(Module*);
Result ParseModuleFieldImpl(Module*);
Result ParseDataModuleField(Module*);
Result ParseElemModuleField(Module*);
Result ParseTagModuleField(Module*);
Expand Down
25 changes: 25 additions & 0 deletions src/wast-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,31 @@ Result WastParser::ParseModuleFieldList(Module* module) {

Result WastParser::ParseModuleField(Module* module) {
WABT_TRACE(ParseModuleField);
// A field is only appended to the module once it has parsed successfully; a
// field that fails to parse is destroyed instead, so the deferred reference
// type resolutions registered while parsing it would point into freed
// memory. Remember where the resolve lists ended and drop those entries
// again if the field fails.
size_t ref_types_size = resolve_ref_types_.size();
size_t type_vectors_size = resolve_type_vectors_.size();
size_t funcs_size = resolve_funcs_.size();

Result result = ParseModuleFieldImpl(module);

if (Failed(result)) {
resolve_ref_types_.erase(resolve_ref_types_.begin() + ref_types_size,
resolve_ref_types_.end());
resolve_type_vectors_.erase(
resolve_type_vectors_.begin() + type_vectors_size,
resolve_type_vectors_.end());
resolve_funcs_.erase(resolve_funcs_.begin() + funcs_size,
resolve_funcs_.end());

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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. ParseModuleFieldList swallows it:

if (Failed(ParseModuleField(module))) {
  CHECK_RESULT(Synchronize(IsModuleField));
}

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 local unique_ptr, which was destroyed on the error path instead of being handed to AppendField. That's the freed read, and a freed write via local_types.Set() in the func-local case.

The one case where nobody touches them is when Synchronize fails 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.

}

return result;
}

Result WastParser::ParseModuleFieldImpl(Module* module) {
switch (Peek(1)) {
case TokenType::Data: return ParseDataModuleField(module);
case TokenType::Elem: return ParseElemModuleField(module);
Expand Down
37 changes: 37 additions & 0 deletions test/parse/bad-ref-type-in-failed-field.txt
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 ;;)
Loading