Skip to content

wat-writer.cc: update text serialization of data memuse - #2501

Merged
keithw merged 2 commits into
mainfrom
fix-wat-write-memuse
Nov 7, 2024
Merged

keithw merged 2 commits into
mainfrom
fix-wat-write-memuse

Conversation

@keithw

@keithw keithw commented Nov 7, 2024

Copy link
Copy Markdown
Member

wat-writer.cc currently prints active data segments (targeting a non-0 memory) using the Wasm 1.0 format:

(data 𝑥:memidx_𝐼 ...)

... but the multi-memory feature really requires the current format:

(data id? 𝑥:memuse_𝐼 ...)
memuse_𝐼 ::= (memory 𝑥:memidx_𝐼)

This PR adjusts wat-writer.cc to print data segments (that target a non-0 memory) with the post-1.0 syntax. This only affects modules that use multi-memory feature so I don't think it will break anything.

Before this PR, WABT wasn't able to roundtrip this module with debug-names enabled:

(memory 0)
(memory $k i64 (data))

... because after round-tripping it would be printed like this (which is invalid because it's parsed so that the data segment itself has id $k but the memuse is memory 0 which is indexed by i32):

(module
  (memory (;0;) 0)
  (memory $k i64 0 0)
  (data (;0;) $k (i64.const 0) ""))

After this PR, it gets round-tripped like this:

(module
  (memory (;0;) 0)
  (memory $k i64 0 0)
  (data (;0;) (memory $k) (i64.const 0) ""))

@keithw
keithw requested a review from sbc100 November 7, 2024 02:40

@sbc100 sbc100 left a comment

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.

Are there not spec tests that cover this case ?

@keithw

keithw commented Nov 7, 2024 •

Copy link
Copy Markdown
Member Author

Are there not spec tests that cover this case ?

Unfortunately I don't think there's a spec test that would make us parse the Wasm 1.0 format when using multi-memory by itself. We could have caught this with a roundtrip test, but I don't think we roundtrip every spec test -- only the actual roundtrip tests. And I think it would have had to be a roundtrip test that uses multi-memory and has --debug-names enabled on the test to catch this (and not --stdout because then we don't do the full roundtrip comparison).

@keithw
keithw enabled auto-merge (squash) November 7, 2024 03:23
@keithw
keithw merged commit c1d97e9 into main Nov 7, 2024
@keithw
keithw deleted the fix-wat-write-memuse branch November 7, 2024 03:37
@SoniEx2

SoniEx2 commented Nov 7, 2024

Copy link
Copy Markdown
Collaborator

we probably should roundtrip every test - it's what the spec does, as far as we can tell. but that's a whole undertaking...

@keithw

keithw commented Nov 7, 2024

Copy link
Copy Markdown
Member Author

I agree that would be great (at least for valid modules) -- if it doesn't blow up testing time too much. We could make it an optional test chosen by an environment variable and run it in CI.

If we want to catch everything, ideally we'd be roundtripping with and without folding, with and without debug-names, with and without generate-names, with and without a roundtrip through the spec interpreter's (or wasm-tools's) text-to-binary and binary-to-text pipelines (since we sometimes have an out-of-date binary encoding of something that we don't catch because WABT's writer and reader are consistent).

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.

3 participants