Skip to content

Quote local names and replace invalid characters in code metadata names - #2907

Open
aizu-m wants to merge 2 commits into
WebAssembly:mainfrom
aizu-m:wat-writer-name-chars
Open

aizu-m wants to merge 2 commits into
WebAssembly:mainfrom
aizu-m:wat-writer-name-chars

Conversation

@aizu-m

@aizu-m aizu-m commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

wasm2wat on a valid module whose local 0 is named l i32) i32.const 1337 return ;; (name section, no flags):

(func (;0;) (type 0) (result i32)
  (local $l i32) i32.const 1337 return ;; i32)
  i32.const 1)

$ wasm-interp --run-all-exports orig.wasm
f() => i32:1
$ wasm2wat orig.wasm -o rt.wat && wat2wasm rt.wat -o rt.wasm
$ wasm-interp --run-all-exports rt.wasm
f() => i32:1337

Found while auditing how wasm2wat writes names that come from the binary.

  1. WriteName has replaced non-id characters with _ since Replace invalid characters with _ in wat-writer #687.
  2. WriteTypeBindings writes param/local names with WriteString, so it skips that step. Local names come from the name section, which wasm2wat reads by default.
  3. OnCodeMetadataExpr writes the metadata.code.* section name suffix raw. Same result with --enable-code-metadata.

A ) or ;; in the name ends the token. The rest is parsed as code when the text is reassembled. Uses of a local already go through WriteName via WriteVar, so the declaration and its uses could also disagree.

The fix sends bindings through WriteName. With annotations enabled (the default), WriteName now writes a name with non-id characters as a quoted identifier, e.g. $"l i32) i32.const 1337 return ;;", so it is kept exactly. The _ replacement stays as the fallback when annotations are disabled or the name isn't valid UTF-8. The code metadata name is an annotation id, and wabt ignores a quoted (@"metadata.code.x" ...) as an unknown annotation, so that site keeps the _ replacement. Names made only of valid id characters are unchanged. test/binary/invalid-name-chars.txt covers both sites and test/binary/invalid-name.txt is updated for the quoted output. The rest of the test suite passes.

WriteTypeBindings and OnCodeMetadataExpr wrote names taken from the
binary (name section local names, metadata.code.* section names) without
the replacement WriteName applies, so a name containing ')' or ';' could
end the token and inject text into the wasm2wat output.
@zherczeg

zherczeg commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Isn't the motivation for these is that names may come from a different programing language with different name rules?

@rossberg

rossberg commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

FWIW, wat allows quoting symbolic names since Wasm 3, so the above could safely be printed as

(func (;0;) (type 0) (result i32)
  (local $"l i32) i32.const 1337 return ;;" i32)
  i32.const 1)

@zherczeg

zherczeg commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

@rossberg that is a good point.
In wabt this feature is part of the annotation support, and this support is enabled by default.

When annotations are enabled (the default), WriteName now writes a name
containing characters outside idchar as $"..." so it is kept exactly,
instead of replacing those characters with '_'. The replacement is kept
as the fallback when annotations are disabled or the name is not valid
UTF-8.
@aizu-m aizu-m changed the title Replace invalid characters in local and code metadata names Quote local names and replace invalid characters in code metadata names Nov 15, 2026
@aizu-m

aizu-m commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

@zherczeg Yes, that is the motivation. Mangled or source-language names can hold characters outside idchar. #687 handled that for names written through WriteName, but local declarations went through WriteString. So the raw name ended the token, and the rest was parsed as code on reassembly.

@rossberg Agreed, quoting is better than _. Traced it through: the lexer already takes $"..." and ParseVarText accepts it when annotations are on. Pushed 8b505b7. WriteName now writes $"..." when the name has non-id characters and annotations are enabled. The _ replacement stays as the fallback otherwise, or when the name is not valid UTF-8. The repro now prints (local $"l i32) i32.const 1337 return ;;" i32) and the reassembled module still returns 1.

The code metadata name keeps the _ replacement. Checked: (@"metadata.code.x" "\01") assembles without error, but it is dropped as an unknown annotation and no custom section is written. test/binary/invalid-name.txt now expects $"hi hello hey". Full test suite passes.

@zherczeg

Copy link
Copy Markdown
Collaborator

The (@"metadata.code.x" "\01") should be valid, and the parser could process it:
https://github.com/WebAssembly/wabt/blob/main/src/wast-parser.cc#L777

Personally, I would extend the parser to process all valid forms of known annotations, then when invalid characters are present in an id, just throw an error, that annotations must be enabled (it is enabled by default). I don't really like the mixing of old and new methods.

@sbc100 what do you think?

@sbc100

sbc100 commented Nov 16, 2026

Copy link
Copy Markdown
Member

The (@"metadata.code.x" "\01") should be valid, and the parser could process it: https://github.com/WebAssembly/wabt/blob/main/src/wast-parser.cc#L777

Personally, I would extend the parser to process all valid forms of known annotations, then when invalid characters are present in an id, just throw an error, that annotations must be enabled (it is enabled by default). I don't really like the mixing of old and new methods.

[@sbc100](https://github.com/sbc100) what do you think?

Sounds reasonable to me.

This branch has not been deployed

No deployments
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.

4 participants