diff options
author | Sam Clegg <sbc@chromium.org> | 2021-04-07 15:06:13 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-07 15:06:13 -0700 |
commit | cf38b658ff20c2b53066dc2a51266c033c6d7178 (patch) | |
tree | 5c8110943f32abc6730050f8c1b946f96a2fb052 /src/wasm/wasm-binary.cpp | |
parent | a36c455f6a2e9fa48751c985fa49b33ce94277c4 (diff) | |
download | binaryen-cf38b658ff20c2b53066dc2a51266c033c6d7178.tar.gz binaryen-cf38b658ff20c2b53066dc2a51266c033c6d7178.tar.bz2 binaryen-cf38b658ff20c2b53066dc2a51266c033c6d7178.zip |
Write names subsections by increasing id (#3779)
See https://webassembly.github.io/spec/core/appendix/custom.html
"Each subsection may occur at most once, and in order of increasing id."
Diffstat (limited to 'src/wasm/wasm-binary.cpp')
-rw-r--r-- | src/wasm/wasm-binary.cpp | 60 |
1 files changed, 33 insertions, 27 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index df2bc8eba..14a72bcb4 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -789,32 +789,6 @@ void WasmBinaryWriter::writeNames() { } } - // elem names - { - std::vector<std::pair<Index, ElementSegment*>> elemsWithNames; - Index checked = 0; - for (auto& curr : wasm->elementSegments) { - if (curr->hasExplicitName) { - elemsWithNames.push_back({checked, curr.get()}); - } - checked++; - } - assert(checked == indexes.elemIndexes.size()); - - if (elemsWithNames.size() > 0) { - auto substart = - startSubsection(BinaryConsts::UserSections::Subsection::NameElem); - o << U32LEB(elemsWithNames.size()); - - for (auto& indexedElem : elemsWithNames) { - o << U32LEB(indexedElem.first); - writeEscapedName(indexedElem.second->name.str); - } - - finishSubsection(substart); - } - } - // memory names if (wasm->memory.exists && wasm->memory.hasExplicitName) { auto substart = @@ -849,7 +823,33 @@ void WasmBinaryWriter::writeNames() { } } - // memory names + // elem segment names + { + std::vector<std::pair<Index, ElementSegment*>> elemsWithNames; + Index checked = 0; + for (auto& curr : wasm->elementSegments) { + if (curr->hasExplicitName) { + elemsWithNames.push_back({checked, curr.get()}); + } + checked++; + } + assert(checked == indexes.elemIndexes.size()); + + if (elemsWithNames.size() > 0) { + auto substart = + startSubsection(BinaryConsts::UserSections::Subsection::NameElem); + o << U32LEB(elemsWithNames.size()); + + for (auto& indexedElem : elemsWithNames) { + o << U32LEB(indexedElem.first); + writeEscapedName(indexedElem.second->name.str); + } + + finishSubsection(substart); + } + } + + // data segment names if (wasm->memory.exists) { Index count = 0; for (auto& seg : wasm->memory.segments) { @@ -2942,8 +2942,14 @@ private: void WasmBinaryBuilder::readNames(size_t payloadLen) { BYN_TRACE("== readNames\n"); auto sectionPos = pos; + uint32_t lastType = 0; while (pos < sectionPos + payloadLen) { auto nameType = getU32LEB(); + if (lastType && nameType <= lastType) { + std::cerr << "warning: out-of-order name subsection: " << nameType + << std::endl; + } + lastType = nameType; auto subsectionSize = getU32LEB(); auto subsectionPos = pos; if (nameType == BinaryConsts::UserSections::Subsection::NameModule) { |