diff options
author | Alon Zakai <azakai@google.com> | 2024-06-17 15:21:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-17 15:21:05 -0700 |
commit | b377b6f175cf5904d8e65eb8f2184c7379f97297 (patch) | |
tree | 3cbcee20b4d712389485536b0ca38dbe60358f70 | |
parent | 1dd05202ced86548361d5efc439ad106007f8bb8 (diff) | |
download | binaryen-b377b6f175cf5904d8e65eb8f2184c7379f97297.tar.gz binaryen-b377b6f175cf5904d8e65eb8f2184c7379f97297.tar.bz2 binaryen-b377b6f175cf5904d8e65eb8f2184c7379f97297.zip |
Fix DataSegment name handling (#6673)
The code used i instead of index, as in this pseudocode:
for i in range(num_names):
index = readU32LEB() # index of the data segment to name
name = readName() # name to give that segment
data[i] = name # XXX 'i' should be 'index'
That (funnily enough) happened to always work before since we write names in
order. That is, normally given segments A,B,C we'd write then in the names section
as A,B,C. Then the reader, which had the bug, would always have i and index
identical in value anyhow. But if a wasm producer used different indexes, a
problem could happen.
To test this, add a binary file that has a reversed name section.
Fixes #6672
-rw-r--r-- | src/wasm/wasm-binary.cpp | 2 | ||||
-rw-r--r-- | test/lit/binary/data-names.test | 25 | ||||
-rw-r--r-- | test/lit/binary/data-names.test.wasm | bin | 0 -> 119 bytes |
3 files changed, 26 insertions, 1 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 924bf1601..e08644eae 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -3700,7 +3700,7 @@ void WasmBinaryReader::readNames(size_t payloadLen) { auto rawName = getInlineString(); auto name = processor.process(rawName); if (index < wasm.dataSegments.size()) { - wasm.dataSegments[i]->setExplicitName(name); + wasm.dataSegments[index]->setExplicitName(name); } else { std::cerr << "warning: data index out of bounds in name section, " "data subsection: " diff --git a/test/lit/binary/data-names.test b/test/lit/binary/data-names.test new file mode 100644 index 000000000..ab7baf5d7 --- /dev/null +++ b/test/lit/binary/data-names.test @@ -0,0 +1,25 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. +;; RUN: wasm-opt -all %s.wasm -q -S -o - | filecheck %s + +;; Test that data segment names roundtrip properly. The wasm file contains +;; +;; (module +;; (memory $mem 10) +;; (data $passive1 "passive1") +;; (data $passive2 "passive2") +;; (data $active1 (offset i32.const 0) "active1") +;; (data $active2 (offset i32.const 0) "active2") +;; ) +;; +;; But the names section is *reversed*: the name of the last data segment is +;; first, and so forth. We must still give the proper segments their names. + +;; CHECK: (memory $mem 10) + +;; CHECK: (data $passive1 "passive1") + +;; CHECK: (data $passive2 "passive2") + +;; CHECK: (data $active1 (i32.const 0) "active1") + +;; CHECK: (data $active2 (i32.const 0) "active2") diff --git a/test/lit/binary/data-names.test.wasm b/test/lit/binary/data-names.test.wasm Binary files differnew file mode 100644 index 000000000..7384aea50 --- /dev/null +++ b/test/lit/binary/data-names.test.wasm |