Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/passes/StringLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,16 @@ struct StringLowering : public StringGathering {
// instead of emitting them into the JSON custom section.
bool useMagicImports;

StringLowering(bool useMagicImports = false)
: useMagicImports(useMagicImports) {}
// Whether to throw a fatal error on non-UTF8 strings that would not be able
// to use the "magic import" mechanism. Only usable in conjunction with magic
// imports.
bool assertUTF8;

StringLowering(bool useMagicImports = false, bool assertUTF8 = false)
: useMagicImports(useMagicImports), assertUTF8(assertUTF8) {
// If we are asserting valid UTF-8, we must be using magic imports.
assert(!assertUTF8 || useMagicImports);
}

void run(Module* module) override {
if (!module->features.has(FeatureSet::Strings)) {
Expand Down Expand Up @@ -238,6 +246,12 @@ struct StringLowering : public StringGathering {
global->module = "'";
global->base = Name(utf8.str());
} else {
if (assertUTF8) {
std::stringstream escaped;
String::printEscaped(escaped, utf8.str());
Fatal() << "Cannot lower non-UTF-16 string " << escaped.str()
<< '\n';
}
global->module = "string.const";
global->base = std::to_string(jsonImportIndex);
if (first) {
Expand Down Expand Up @@ -534,5 +548,8 @@ struct StringLowering : public StringGathering {
Pass* createStringGatheringPass() { return new StringGathering(); }
Pass* createStringLoweringPass() { return new StringLowering(); }
Pass* createStringLoweringMagicImportPass() { return new StringLowering(true); }
Pass* createStringLoweringMagicImportAssertPass() {
return new StringLowering(true, true);
}

} // namespace wasm
4 changes: 4 additions & 0 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ void PassRegistry::registerPasses() {
"string-lowering-magic-imports",
"same as string-lowering, but encodes well-formed strings as magic imports",
createStringLoweringMagicImportPass);
registerPass("string-lowering-magic-imports-assert",
"same as string-lowering-magic-imports, but raise a fatal error "
"if there are invalid strings",
createStringLoweringMagicImportAssertPass);
registerPass(
"strip", "deprecated; same as strip-debug", createStripDebugPass);
registerPass("stack-check",
Expand Down
1 change: 1 addition & 0 deletions src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Pass* createStackCheckPass();
Pass* createStringGatheringPass();
Pass* createStringLoweringPass();
Pass* createStringLoweringMagicImportPass();
Pass* createStringLoweringMagicImportAssertPass();
Pass* createStripDebugPass();
Pass* createStripDWARFPass();
Pass* createStripProducersPass();
Expand Down
5 changes: 5 additions & 0 deletions test/lit/help/wasm-metadce.test
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@
;; CHECK-NEXT: encodes well-formed strings as
;; CHECK-NEXT: magic imports
;; CHECK-NEXT:
;; CHECK-NEXT: --string-lowering-magic-imports-assert same as
;; CHECK-NEXT: string-lowering-magic-imports,
;; CHECK-NEXT: but raise a fatal error if there
;; CHECK-NEXT: are invalid strings
;; CHECK-NEXT:
;; CHECK-NEXT: --strip deprecated; same as strip-debug
;; CHECK-NEXT:
;; CHECK-NEXT: --strip-debug strip debug info (including the
Expand Down
5 changes: 5 additions & 0 deletions test/lit/help/wasm-opt.test
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,11 @@
;; CHECK-NEXT: encodes well-formed strings as
;; CHECK-NEXT: magic imports
;; CHECK-NEXT:
;; CHECK-NEXT: --string-lowering-magic-imports-assert same as
;; CHECK-NEXT: string-lowering-magic-imports,
;; CHECK-NEXT: but raise a fatal error if there
;; CHECK-NEXT: are invalid strings
;; CHECK-NEXT:
;; CHECK-NEXT: --strip deprecated; same as strip-debug
;; CHECK-NEXT:
;; CHECK-NEXT: --strip-debug strip debug info (including the
Expand Down
5 changes: 5 additions & 0 deletions test/lit/help/wasm2js.test
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@
;; CHECK-NEXT: encodes well-formed strings as
;; CHECK-NEXT: magic imports
;; CHECK-NEXT:
;; CHECK-NEXT: --string-lowering-magic-imports-assert same as
;; CHECK-NEXT: string-lowering-magic-imports,
;; CHECK-NEXT: but raise a fatal error if there
;; CHECK-NEXT: are invalid strings
;; CHECK-NEXT:
;; CHECK-NEXT: --strip deprecated; same as strip-debug
;; CHECK-NEXT:
;; CHECK-NEXT: --strip-debug strip debug info (including the
Expand Down
7 changes: 7 additions & 0 deletions test/lit/passes/string-lowering.wast
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@
;; RUN: wasm-opt %s --string-lowering-magic-imports -all -S -o - \
;; RUN: | filecheck %s --check-prefix=MAGIC
;;
;; If we use magic imports with asserts, we should get an error.
;;
;; RUN: not wasm-opt %s --string-lowering-magic-imports-assert -all -S -o - \
;; RUN: 2>&1 | filecheck %s --check-prefix=ASSERT
;;
;; CHECK: custom section "string.consts", size 136, contents: "[\"bar\",\"foo\",\"needs\\tescaping\\u0000.'#%\\\"- .\\r\\n\\\\08\\f\\n\\r\\t.\\ua66e\",\"unpaired high surrogate \\ud800 \",\"unpaired low surrogate \\udf48 \"]"
;;
;; MAGIC: custom section "string.consts", size 68, contents: "[\"unpaired high surrogate \\ud800 \",\"unpaired low surrogate \\udf48 \"]"
;;
;; ASSERT: Fatal: Cannot lower non-UTF-16 string "unpaired high surrogate \ef\bf\bd "

;; The custom section should parse OK using JSON.parse from node.
;; (Note we run --remove-unused-module-elements to remove externref-using
Expand Down