Repository navigation
Conversation
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.
|
Isn't the motivation for these is that names may come from a different programing language with different name rules? |
|
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) |
|
@rossberg that is a good point. |
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.
|
@zherczeg Yes, that is the motivation. Mangled or source-language names can hold characters outside idchar. #687 handled that for names written through @rossberg Agreed, quoting is better than The code metadata name keeps the |
|
The 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? |
Sounds reasonable to me. |
wasm2wat on a valid module whose local 0 is named
l i32) i32.const 1337 return ;;(name section, no flags):Found while auditing how wasm2wat writes names that come from the binary.
WriteNamehas replaced non-id characters with_since Replace invalid characters with_in wat-writer #687.WriteTypeBindingswrites param/local names withWriteString, so it skips that step. Local names come from the name section, which wasm2wat reads by default.OnCodeMetadataExprwrites themetadata.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 throughWriteNameviaWriteVar, so the declaration and its uses could also disagree.The fix sends bindings through
WriteName. With annotations enabled (the default),WriteNamenow 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.txtcovers both sites andtest/binary/invalid-name.txtis updated for the quoted output. The rest of the test suite passes.