Skip to content

Commit dc2c051

Browse files
authored
Fix 2 binary fuzz bugs (#1323)
* Check if there is a currFunction before using it (we need it for some stacky code; a valid wasm wouldn't need a function in that location anyhow, as what can be put in a memory/table offset is very limited). * Huge alignment led us to do a power of 2 shift that is undefined behavior. Also adds a test facility to check we don't crash on testcases.
1 parent 183be2f commit dc2c051

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

check.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ def run_wasm_merge_tests():
194194
with open(out + '.stdout') as f:
195195
fail_if_not_identical(f.read(), stdout)
196196

197+
def run_crash_tests():
198+
print "\n[ checking we don't crash on tricky inputs... ]\n"
199+
200+
for t in os.listdir(os.path.join('test', 'crash')):
201+
if t.endswith(('.wast', '.wasm')):
202+
print '..', t
203+
t = os.path.join('test', 'crash', t)
204+
cmd = WASM_OPT + [t]
205+
# expect a parse error to be reported
206+
run_command(cmd, expected_err='parse exception:', err_contains=True, expected_status=1)
207+
197208
def run_ctor_eval_tests():
198209
print '\n[ checking wasm-ctor-eval... ]\n'
199210

@@ -576,6 +587,7 @@ def main():
576587
asm2wasm.test_asm2wasm_binary()
577588
run_wasm_dis_tests()
578589
run_wasm_merge_tests()
590+
run_crash_tests()
579591
run_ctor_eval_tests()
580592
run_wasm_metadce_tests()
581593
if has_shell_timeout():

src/wasm/wasm-binary.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,9 @@ void WasmBinaryBuilder::pushBlockElements(Block* curr, size_t start, size_t end)
22022202
expressionStack.resize(start);
22032203
// if we have a consumable item and need it, use it
22042204
if (consumable != NONE && curr->list.back()->type == none) {
2205+
if (!currFunction) {
2206+
throw ParseException("need an extra var in a non-function context, invalid wasm");
2207+
}
22052208
Builder builder(wasm);
22062209
auto* item = curr->list[consumable]->cast<Drop>()->value;
22072210
auto temp = builder.addVar(currFunction, item->type);
@@ -2464,7 +2467,9 @@ void WasmBinaryBuilder::visitSetGlobal(SetGlobal *curr) {
24642467
}
24652468

24662469
void WasmBinaryBuilder::readMemoryAccess(Address& alignment, size_t bytes, Address& offset) {
2467-
alignment = Pow2(getU32LEB());
2470+
auto rawAlignment = getU32LEB();
2471+
if (rawAlignment > 4) throw ParseException("Alignment must be of a reasonable size");
2472+
alignment = Pow2(rawAlignment);
24682473
offset = getU32LEB();
24692474
}
24702475

test/crash/outside.wasm

183 Bytes
Binary file not shown.
871 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)