Skip to content

Fix import section count when using compact imports - #2870

Merged
sbc100 merged 2 commits into
WebAssembly:mainfrom
saqibkh:fix-compact-imports-count
Oct 2, 2026
Merged

sbc100 merged 2 commits into
WebAssembly:mainfrom
saqibkh:fix-compact-imports-count

Conversation

@saqibkh

@saqibkh saqibkh commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

The compact import section proposal encodes the import section as section_2(list(imports)), where a single entry can contain several imports, so the section count is the number of entries rather than the number of imports. wabt's writer and reader both used the total import count, which made wabt's output unreadable by other decoders and caused wabt to reject conforming modules.

  • binary-writer: write the number of entries as the import section count.
  • binary-reader: iterate over entries, keeping a separate running import index; OnImportCount now reports the entry count.
  • wasm-objdump: count imports in the prepass so the total number of imports is still displayed (otherwise three existing --enable-all dump tests would print Import[1] followed by two imports; happy to print the raw entry count and update those tests instead if preferred).
  • Updated test/binary/compact-imports.txt (hand-written binary, now with a single entry after the compact runs) and test/dump/compact-imports.txt.

Non-compact encoding is unchanged. run-tests.py passes.

Fixes #2845

Under the compact import section proposal the import section is encoded
as `section_2(list(imports))`, where a single `imports` entry can contain
multiple imports.  The leading count is therefore the number of entries
in the section, not the total number of imports.

Previously both the writer and the reader used the total number of
imports, so modules written by wabt could not be read by other tools and
conforming modules (such as those in the proposal's spec tests) were
rejected by wabt.

- binary-writer: Write the number of entries as the section count.
- binary-reader: Loop over the entries, keeping a separate running import
  index.  OnImportCount now reports the number of entries.
- wasm-objdump: Count the imports during the prepass so that the total
  number of imports is still reported.

Fixes: WebAssembly#2845
Comment thread src/binary-reader.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So i still counts total imports, is that what we want? Do could we use j here and completely remove i?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j restarts at 0 for each compact group, but OnImport/OnImport* take the index of the import within the whole section (it shows up in the -v logging output and objdump uses it), so we still need that. However it doesn't need to be tracked separately: ReadImport already bumps exactly one of the per-kind counters for each import, so the index is just their sum. I've removed i from ReadImportSection and the ReadImport parameter, and compute the index there instead.

The index of each import can be derived from the per-kind import counters that ReadImport already maintains, so there is no need to track it separately while iterating over the import section entries.
@sbc100
sbc100 merged commit 93a552c into WebAssembly:main Oct 2, 2026
17 checks passed
@saqibkh
saqibkh deleted the fix-compact-imports-count branch October 2, 2026 15:58
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

Successfully merging this pull request may close these issues.

compact imports broken

2 participants