Repository navigation
Fix use-after-free resolving reference type names in wast-parser - #2805
Conversation
|
Should we have |
Move the truncation of resolve_ref_types_/resolve_type_vectors_/ resolve_funcs_ out of ParseModuleFieldList and into ParseModuleField, so a field that fails to parse drops its own deferred resolutions instead of the caller tracking them. The dispatch switch moves to ParseModuleFieldImpl unchanged; ParseModuleField now records the list sizes, calls it, and truncates on failure. No behavioural change: the six registration sites still exit 1 with only the expected parse diagnostics and no ASan report.
|
Agreed, that's better. Moved it into The dispatch switch moved into Re-checked after the change: all six registration sites (func param, func result, func local, table elem type, elem segment type, import func signature) exit 1 with only the expected parse diagnostics and no ASan report, and reverting just the truncation brings the heap-use-after-free straight back in |
| resolve_type_vectors_.begin() + type_vectors_size, | ||
| resolve_type_vectors_.end()); | ||
| resolve_funcs_.erase(resolve_funcs_.begin() + funcs_size, | ||
| resolve_funcs_.end()); |
There was a problem hiding this comment.
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.
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.
`wat2wasm`, `wast2json` and `wat-desugar` read freed memory on malformed text input — #2820. The parser holds raw `TypeVector*` pointers to block signatures in `resolve_type_vectors_` and only dereferences them once the whole module has parsed, so a block destroyed on an error path in between leaves a dangling entry behind. The diagnosis is @zherczeg's, from the issue thread: `ParseModuleField` was returning Ok for a field that had in fact failed. Instrumenting it shows exactly that — the field records 8 errors and still succeeds: ``` [DBG] ParseExprList: ParseExpr failed, synchronising and returning Ok anyway [DBG] field -> Ok, errors 0 -> 8 ``` `ParseInstrList` and `ParseExprList` synchronise past a bad sub-expression and then return `Result::Ok` unconditionally, so the failure never reaches `ParseModuleField` and the rollback added in #2805 never runs. So this just passes the error back. Both loops still recover and carry on, so every error is still reported — they only remember that something was dropped. `ParseModuleField` then fails as it should, and #2805's rollback discards the stale resolutions. ## The test churn 20 expected outputs get shorter, which is most of the diff: 470 lines removed against 43 added. Previously, having reported the real error, the parser would carry on and emit a second redundant one — nearly always `unexpected token ), expected EOF` pointing at the end of the module. That no longer appears: ``` out/test/parse/expr/bad-load-align.txt:3:25: error: unexpected token align=foo, expected ). (module (func (i32.load align=foo (i32.const 0)))) ^^^^^^^^^ -out/test/parse/expr/bad-load-align.txt:3:50: error: unexpected token ), expected EOF. -(module (func (i32.load align=foo (i32.const 0)))) - ^ ``` I checked the direction of every one of those diffs: they only ever remove lines. No test gains an error and no valid module starts being rejected. Still, it does touch spec test expectations, so shout if you would rather it were done differently. ## Test `test/parse/bad-block-ref-result-discarded.txt` is the reproducer from the issue. It is deliberately malformed, and it only *aborts* under a sanitizer, so realistically it is the asan CI jobs that would catch a regression rather than a normal run. Full suite is green locally against a debug ASAN/UBSAN build with all submodules checked out: 2495 tests, 0 failures. Fixes #2820. Co-authored-by: nishad shabbir <[email protected]>
drop the deferred reference-type resolutions a module field registered before it failed to parse, otherwise ParseModuleFieldList dereferences pointers into a field that was destroyed instead of appended.