From 37b457a00a46e5604b2a8a388efaf0813b3d1170 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 26 Aug 2022 03:23:42 +0800 Subject: Fix order of local names in Names section (#4964) The wasm spec requires the order of local names in that section to be in increasing order: https://webassembly.github.io/spec/core/appendix/custom.html#binary-namemap --- src/wasm/wasm-binary.cpp | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'src/wasm/wasm-binary.cpp') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 87fb0e293..0a5933baf 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -857,28 +857,30 @@ void WasmBinaryWriter::writeNames() { // Pairs of (local index in IR, name). std::vector> localsWithNames; auto numLocals = func->getNumLocals(); - for (Index i = 0; i < numLocals; ++i) { - if (func->hasLocalName(i)) { - localsWithNames.push_back({i, func->getLocalName(i)}); + for (Index indexInFunc = 0; indexInFunc < numLocals; ++indexInFunc) { + if (func->hasLocalName(indexInFunc)) { + Index indexInBinary; + auto iter = funcMappedLocals.find(func->name); + if (iter != funcMappedLocals.end()) { + // TODO: handle multivalue + indexInBinary = iter->second[{indexInFunc, 0}]; + } else { + // No data on funcMappedLocals. That is only possible if we are an + // imported function, where there are no locals to map, and in + // that case the index is unchanged anyhow: parameters always have + // the same index, they are not mapped in any way. + assert(func->imported()); + indexInBinary = indexInFunc; + } + localsWithNames.push_back( + {indexInBinary, func->getLocalName(indexInFunc)}); } } assert(localsWithNames.size()); + std::sort(localsWithNames.begin(), localsWithNames.end()); o << U32LEB(index); o << U32LEB(localsWithNames.size()); - for (auto& [indexInFunc, name] : localsWithNames) { - // TODO: handle multivalue - Index indexInBinary; - auto iter = funcMappedLocals.find(func->name); - if (iter != funcMappedLocals.end()) { - indexInBinary = iter->second[{indexInFunc, 0}]; - } else { - // No data on funcMappedLocals. That is only possible if we are an - // imported function, where there are no locals to map, and in that - // case the index is unchanged anyhow: parameters always have the - // same index, they are not mapped in any way. - assert(func->imported()); - indexInBinary = indexInFunc; - } + for (auto& [indexInBinary, name] : localsWithNames) { o << U32LEB(indexInBinary); writeEscapedName(name.str); } -- cgit v1.2.3