diff options
author | Thomas Lively <tlively@google.com> | 2024-08-20 18:20:39 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-20 18:20:39 -0700 |
commit | 9772dc6cf3ce153b1eb2a570901161194e06e6a6 (patch) | |
tree | 5479a59fb892d1612d5ebef2e43c1221bc59516a /src | |
parent | 435eeead73cf02a6fb5bc7d7a36dd45b8d700cb2 (diff) | |
download | binaryen-9772dc6cf3ce153b1eb2a570901161194e06e6a6.tar.gz binaryen-9772dc6cf3ce153b1eb2a570901161194e06e6a6.tar.bz2 binaryen-9772dc6cf3ce153b1eb2a570901161194e06e6a6.zip |
Fix encoding of heap type definitions (#6856)
The leading bytes that indicate what kind of heap type is being defined
are bytes, but we were previously treating them as SLEB128-encoded
values. Since we emit the smallest LEB encodings possible, we were
writing the correct bytes in output files, but we were also improperly
accepting binaries that used more than one byte to encode these values.
This was caught by an upstream spec test.
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-binary.h | 18 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 26 |
2 files changed, 22 insertions, 22 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 32eb7657c..8c1990224 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -376,15 +376,15 @@ enum EncodedType { // string reference types stringref = -0x19, // 0x67 // type forms - Func = -0x20, // 0x60 - Cont = -0x23, // 0x5d - Struct = -0x21, // 0x5f - Array = -0x22, // 0x5e - Sub = -0x30, // 0x50 - SubFinal = -0x31, // 0x4f - Shared = -0x1b, // 0x65 - // isorecursive recursion groups - Rec = -0x32, // 0x4e + Func = 0x60, + Cont = 0x5d, + Struct = 0x5f, + Array = 0x5e, + Sub = 0x50, + SubFinal = 0x4f, + SharedDef = 0x65, + Shared = -0x1b, // Also 0x65 as an SLEB128 + Rec = 0x4e, // block_type Empty = -0x40, // 0x40 }; diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 8565ddaad..865ca39ca 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -269,7 +269,7 @@ void WasmBinaryWriter::writeTypes() { // size 1 are implicit, so only emit a group header for larger groups. auto currGroup = type.getRecGroup(); if (lastGroup != currGroup && currGroup.size() > 1) { - o << S32LEB(BinaryConsts::EncodedType::Rec) << U32LEB(currGroup.size()); + o << uint8_t(BinaryConsts::EncodedType::Rec) << U32LEB(currGroup.size()); } lastGroup = currGroup; // Emit the type definition. @@ -277,9 +277,9 @@ void WasmBinaryWriter::writeTypes() { auto super = type.getDeclaredSuperType(); if (super || type.isOpen()) { if (type.isOpen()) { - o << S32LEB(BinaryConsts::EncodedType::Sub); + o << uint8_t(BinaryConsts::EncodedType::Sub); } else { - o << S32LEB(BinaryConsts::EncodedType::SubFinal); + o << uint8_t(BinaryConsts::EncodedType::SubFinal); } if (super) { o << U32LEB(1); @@ -289,11 +289,11 @@ void WasmBinaryWriter::writeTypes() { } } if (type.isShared()) { - o << S32LEB(BinaryConsts::EncodedType::Shared); + o << uint8_t(BinaryConsts::EncodedType::SharedDef); } switch (type.getKind()) { case HeapTypeKind::Func: { - o << S32LEB(BinaryConsts::EncodedType::Func); + o << uint8_t(BinaryConsts::EncodedType::Func); auto sig = type.getSignature(); for (auto& sigType : {sig.params, sig.results}) { o << U32LEB(sigType.size()); @@ -304,7 +304,7 @@ void WasmBinaryWriter::writeTypes() { break; } case HeapTypeKind::Struct: { - o << S32LEB(BinaryConsts::EncodedType::Struct); + o << uint8_t(BinaryConsts::EncodedType::Struct); auto fields = type.getStruct().fields; o << U32LEB(fields.size()); for (const auto& field : fields) { @@ -313,11 +313,11 @@ void WasmBinaryWriter::writeTypes() { break; } case HeapTypeKind::Array: - o << S32LEB(BinaryConsts::EncodedType::Array); + o << uint8_t(BinaryConsts::EncodedType::Array); writeField(type.getArray().element); break; case HeapTypeKind::Cont: - o << S32LEB(BinaryConsts::EncodedType::Cont); + o << uint8_t(BinaryConsts::EncodedType::Cont); writeHeapType(type.getContinuation().type); break; case HeapTypeKind::Basic: @@ -2405,7 +2405,7 @@ void WasmBinaryReader::readTypes() { for (size_t i = 0; i < builder.size(); i++) { BYN_TRACE("read one\n"); - auto form = getS32LEB(); + auto form = getInt8(); if (form == BinaryConsts::EncodedType::Rec) { uint32_t groupSize = getU32LEB(); if (groupSize == 0u) { @@ -2416,7 +2416,7 @@ void WasmBinaryReader::readTypes() { // allocate space for the extra types. builder.grow(groupSize - 1); builder.createRecGroup(i, groupSize); - form = getS32LEB(); + form = getInt8(); } std::optional<uint32_t> superIndex; if (form == BinaryConsts::EncodedType::Sub || @@ -2432,11 +2432,11 @@ void WasmBinaryReader::readTypes() { } superIndex = getU32LEB(); } - form = getS32LEB(); + form = getInt8(); } - if (form == BinaryConsts::Shared) { + if (form == BinaryConsts::SharedDef) { builder[i].setShared(); - form = getS32LEB(); + form = getInt8(); } if (form == BinaryConsts::EncodedType::Func) { builder[i] = readSignatureDef(); |