-
Notifications
You must be signed in to change notification settings - Fork 871
[wasm2js] Escape module names in generated JavaScript output #8377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sumleo
wants to merge
3
commits into
WebAssembly:main
Choose a base branch
from
sumleo:fix-wasm2js-string-escaping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| #include "passes/passes.h" | ||
| #include "support/base64.h" | ||
| #include "support/file.h" | ||
| #include "support/string.h" | ||
| #include "wasm-builder.h" | ||
| #include "wasm-io.h" | ||
| #include "wasm-validator.h" | ||
|
|
@@ -2652,6 +2653,15 @@ void Wasm2JSBuilder::addMemoryGrowFunc(Ref ast, Module* wasm) { | |
| ast->push_back(memoryGrowFunc); | ||
| } | ||
|
|
||
| // Escape a string for safe inclusion in a JavaScript string literal. | ||
| // WebAssembly module/base names are arbitrary UTF-8 and may contain characters | ||
| // that are JS string metacharacters (quotes, backslashes, newlines, etc.). | ||
| static std::string escapeJSString(std::string_view str) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use |
||
| std::ostringstream ss; | ||
| String::printEscapedJS(ss, str); | ||
| return ss.str(); | ||
| } | ||
|
|
||
| // Wasm2JSBuilder emits the core of the module - the functions etc. that would | ||
| // be the asm.js function in an asm.js world. This class emits the rest of the | ||
| // "glue" around that. | ||
|
|
@@ -2732,7 +2742,7 @@ void Wasm2JSGlue::emitPreES6() { | |
| baseModuleMap[base] = module; | ||
| if (!seenModules.contains(module)) { | ||
| out << "import * as " << asmangle(module.toString()) << " from '" | ||
| << module << "';\n"; | ||
| << escapeJSString(module.toString()) << "';\n"; | ||
| seenModules.insert(module); | ||
| } | ||
| }; | ||
|
|
@@ -2795,7 +2805,7 @@ void Wasm2JSGlue::emitPostES6() { | |
| if (seenModules.contains(import->module)) { | ||
| return; | ||
| } | ||
| out << " \"" << import->module | ||
| out << " \"" << escapeJSString(import->module.toString()) | ||
| << "\": " << asmangle(import->module.toString()) << ",\n"; | ||
| seenModules.insert(import->module); | ||
| }); | ||
|
|
@@ -2806,7 +2816,7 @@ void Wasm2JSGlue::emitPostES6() { | |
| if (ABI::wasm2js::isHelper(import->base)) { | ||
| return; | ||
| } | ||
| out << " \"" << import->module << "\": {\n"; | ||
| out << " \"" << escapeJSString(import->module.toString()) << "\": {\n"; | ||
| out << " " << asmangle(import->base.toString()) << ": { buffer : mem" | ||
| << moduleName.str << " }\n"; | ||
| out << " },\n"; | ||
|
|
@@ -2821,7 +2831,7 @@ void Wasm2JSGlue::emitPostES6() { | |
| if (seenModules.contains(import->module)) { | ||
| return; | ||
| } | ||
| out << " \"" << import->module | ||
| out << " \"" << escapeJSString(import->module.toString()) | ||
| << "\": " << asmangle(import->module.toString()) << ",\n"; | ||
| seenModules.insert(import->module); | ||
| }); | ||
|
|
@@ -2925,8 +2935,9 @@ void Wasm2JSGlue::emitMemory() { | |
| if (auto* get = segment.offset->dynCast<GlobalGet>()) { | ||
| auto internalName = get->name; | ||
| auto importedGlobal = wasm.getGlobal(internalName); | ||
| return std::string("imports['") + importedGlobal->module.toString() + | ||
| "']['" + importedGlobal->base.toString() + "']"; | ||
| return std::string("imports['") + | ||
| escapeJSString(importedGlobal->module.toString()) + "']['" + | ||
| escapeJSString(importedGlobal->base.toString()) + "']"; | ||
| } | ||
| Fatal() << "non-constant offsets aren't supported yet\n"; | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #include <sstream> | ||
|
|
||
| #include "support/string.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using namespace wasm; | ||
|
|
||
| using StringTest = ::testing::Test; | ||
|
|
||
| namespace { | ||
|
|
||
| std::string escapeJS(std::string_view str) { | ||
| std::stringstream ss; | ||
| String::printEscapedJS(ss, str); | ||
| return ss.str(); | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| TEST_F(StringTest, PrintEscapedJSPlain) { | ||
| EXPECT_EQ(escapeJS(""), ""); | ||
| EXPECT_EQ(escapeJS("env"), "env"); | ||
| EXPECT_EQ(escapeJS("wasi_snapshot_preview1"), "wasi_snapshot_preview1"); | ||
| } | ||
|
|
||
| TEST_F(StringTest, PrintEscapedJSQuotes) { | ||
| // Both quote characters are escaped so the result is safe in either a | ||
| // single- or double-quoted literal. | ||
| EXPECT_EQ(escapeJS("it's"), "it\\'s"); | ||
| EXPECT_EQ(escapeJS("say \"hi\""), "say \\\"hi\\\""); | ||
| EXPECT_EQ(escapeJS("mixed'and\"quotes"), "mixed\\'and\\\"quotes"); | ||
| } | ||
|
|
||
| TEST_F(StringTest, PrintEscapedJSBackslash) { | ||
| EXPECT_EQ(escapeJS("a\\b"), "a\\\\b"); | ||
| // A backslash followed by a quote must not collapse into a lone escape. | ||
| EXPECT_EQ(escapeJS("\\'"), "\\\\\\'"); | ||
| } | ||
|
|
||
| TEST_F(StringTest, PrintEscapedJSNewlines) { | ||
| EXPECT_EQ(escapeJS("line1\nline2"), "line1\\nline2"); | ||
| EXPECT_EQ(escapeJS("line1\r\nline2"), "line1\\r\\nline2"); | ||
| } | ||
|
|
||
| TEST_F(StringTest, PrintEscapedJSLineSeparators) { | ||
| // U+2028 and U+2029 are valid unescaped in JSON strings but are line | ||
| // terminators in JavaScript, so they must be escaped. | ||
| EXPECT_EQ(escapeJS("a\xE2\x80\xA8" | ||
| "b"), | ||
| "a\\u2028b"); | ||
| EXPECT_EQ(escapeJS("a\xE2\x80\xA9" | ||
| "b"), | ||
| "a\\u2029b"); | ||
| // Other characters in the same UTF-8 range pass through unchanged. | ||
| EXPECT_EQ(escapeJS("a\xE2\x80\xA6" | ||
| "b"), | ||
| "a\xE2\x80\xA6" | ||
| "b"); | ||
| // A truncated sequence at the end of the string is passed through | ||
| // byte-by-byte rather than read out of bounds. | ||
| EXPECT_EQ(escapeJS("\xE2\x80"), "\xE2\x80"); | ||
| } | ||
|
|
||
| TEST_F(StringTest, PrintEscapedJSNonASCII) { | ||
| // Non-ASCII UTF-8 is passed through unmodified. | ||
| EXPECT_EQ(escapeJS("m\xC3\xB3" | ||
| "dulo"), | ||
| "m\xC3\xB3" | ||
| "dulo"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import * as mod_ule_x from 'mod\'ule\"x'; | ||
|
|
||
| function asmFunc(imports) { | ||
| var Math_imul = Math.imul; | ||
| var Math_fround = Math.fround; | ||
| var Math_abs = Math.abs; | ||
| var Math_clz32 = Math.clz32; | ||
| var Math_min = Math.min; | ||
| var Math_max = Math.max; | ||
| var Math_floor = Math.floor; | ||
| var Math_ceil = Math.ceil; | ||
| var Math_trunc = Math.trunc; | ||
| var Math_sqrt = Math.sqrt; | ||
| var mod_ule_x = imports["mod\'ule\"x"]; | ||
| var base = mod_ule_x["ba\\se\'"]; | ||
| function exported() { | ||
| base(); | ||
| } | ||
|
|
||
| return { | ||
| "exported": exported | ||
| }; | ||
| } | ||
|
|
||
| var retasmFunc = asmFunc({ | ||
| "mod\'ule\"x": mod_ule_x, | ||
| }); | ||
| export var exported = retasmFunc.exported; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import * as mod_ule_x from 'mod\'ule\"x'; | ||
|
|
||
| function asmFunc(imports) { | ||
| var Math_imul = Math.imul; | ||
| var Math_fround = Math.fround; | ||
| var Math_abs = Math.abs; | ||
| var Math_clz32 = Math.clz32; | ||
| var Math_min = Math.min; | ||
| var Math_max = Math.max; | ||
| var Math_floor = Math.floor; | ||
| var Math_ceil = Math.ceil; | ||
| var Math_trunc = Math.trunc; | ||
| var Math_sqrt = Math.sqrt; | ||
| var mod_ule_x = imports["mod\'ule\"x"]; | ||
| var base = mod_ule_x["ba\\se\'"]; | ||
| function exported() { | ||
| base(); | ||
| } | ||
|
|
||
| return { | ||
| "exported": exported | ||
| }; | ||
| } | ||
|
|
||
| var retasmFunc = asmFunc({ | ||
| "mod\'ule\"x": mod_ule_x, | ||
| }); | ||
| export var exported = retasmFunc.exported; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| (module | ||
| (import "mod'ule\"x" "ba\\se'" (func $base)) | ||
| (func $exported (export "exported") | ||
| (call $base) | ||
| ) | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagine there are many other characters we will want to escape so that the output remains human readable. Instead of adding a new escaping method, can we reuse
printEscapedJSON, maybe by adding a flag for the encoding of the input? Alternatively, we could haveprintEscapedJSconvert its input to WTF16 and then callprintEscapedJSON. We can move this comment about LINE SEPARATOR and PARAGRAPH SEPARATOR intoprintEscapedJSONso we remember to continue escaping them in the future.