Skip to content
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

WASMParser reads ahead causing issues when unexpected code is encountered #314

Open
matetokodi opened this issue Dec 11, 2024 · 1 comment

Comments

@matetokodi
Copy link
Contributor

Found while running dhrystone (from https://github.com/bytecodealliance/wasm-micro-runtime/tree/main/tests/benchmarks/dhrystone) compiled with emcc with -O0

When readAheadLocalGetIfExists reads ahead in computeExprResultPosition it can encounter unknown / unsupported bytecodes; 0x1f in this specific case.

When it looks for a local.set code ahead, and finds one, it uses this data to index into a vector (m_localInfo) that is empty, causing a Segmentation Fault.

Surrounding bytes when inspected when stopped with the debugger (using lookaheadUnsigned8()):

0x05 else
0x20 local.get
0x1f (reserved / try_table?)
0x21 local.set   <====
0x3b i32.store16
0x3c i64.store8

However I was not able to find this byte sequence when I disassembled the .wasm file with wasm-objdump.

This is a functional but inelegant workaround that eliminated the crash:

--- a/src/parser/WASMParser.cpp
+++ b/src/parser/WASMParser.cpp
@@ -665,7 +665,9 @@ private:
     std::pair<Walrus::Optional<uint32_t>, size_t> readAheadLocalGetIfExists() // return localIndex and code length if exists
     {
         Walrus::Optional<uint8_t> mayLoadGetCode = lookaheadUnsigned8();
-        if (mayLoadGetCode.hasValue() && mayLoadGetCode.value() == 0x21) {
+        Walrus::Optional<uint8_t> mayLoadGetCodePrev = lookaheadUnsigned8(-1);
+        if (mayLoadGetCode.hasValue() && mayLoadGetCode.value() == 0x21 &&
+            !(mayLoadGetCodePrev.hasValue() && mayLoadGetCodePrev.value() == 0x1f)) {
             auto r = lookaheadUnsigned32(1);
             if (r.first) {
                 return std::make_pair(r.first, r.second + 1);

dhrystone_emcc_O0.zip

@zherczeg
Copy link
Collaborator

The problem is, it uses an unsupported byte code, called try_table. The wabt will catch this, but the byte prescanning (readAheadLocalGetIfExists) happens too early.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants