summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tools/wasm-as.cpp1
-rw-r--r--src/tools/wasm-opt.cpp29
-rw-r--r--test/unit/test_errors.py40
-rw-r--r--test/unit/test_features.py2
-rw-r--r--test/unit/test_parsing_error.py19
5 files changed, 59 insertions, 32 deletions
diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp
index 67a7d6f14..b4e1e18fa 100644
--- a/src/tools/wasm-as.cpp
+++ b/src/tools/wasm-as.cpp
@@ -126,7 +126,6 @@ int main(int argc, const char* argv[]) {
wasm,
WasmValidator::Globally |
(options.extra["validate"] == "web" ? WasmValidator::Web : 0))) {
- WasmPrinter::printModule(&wasm);
Fatal() << "Error: input module is not valid.\n";
}
}
diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp
index 2fd78a3da..c4443bde4 100644
--- a/src/tools/wasm-opt.cpp
+++ b/src/tools/wasm-opt.cpp
@@ -217,6 +217,17 @@ int main(int argc, const char* argv[]) {
BYN_TRACE("reading...\n");
+ auto exitOnInvalidWasm = [&](const char* message) {
+ // If the user asked to print the module, print it even if invalid,
+ // as otherwise there is no way to print the broken module (the pass
+ // to print would not be reached).
+ if (std::find(options.passes.begin(), options.passes.end(), "print") !=
+ options.passes.end()) {
+ WasmPrinter::printModule(&wasm);
+ }
+ Fatal() << message;
+ };
+
if (!translateToFuzz) {
ModuleReader reader;
// Enable DWARF parsing if we were asked for debug info, and were not
@@ -228,13 +239,13 @@ int main(int argc, const char* argv[]) {
} catch (ParseException& p) {
p.dump(std::cerr);
std::cerr << '\n';
- Fatal() << "error in parsing input";
+ Fatal() << "error parsing wasm";
} catch (MapParseException& p) {
p.dump(std::cerr);
std::cerr << '\n';
- Fatal() << "error in parsing wasm source map";
+ Fatal() << "error parsing wasm source map";
} catch (std::bad_alloc&) {
- Fatal() << "error in building module, std::bad_alloc (possibly invalid "
+ Fatal() << "error building module, std::bad_alloc (possibly invalid "
"request for silly amounts of memory)";
}
@@ -242,8 +253,7 @@ int main(int argc, const char* argv[]) {
if (options.passOptions.validate) {
if (!WasmValidator().validate(wasm)) {
- WasmPrinter::printModule(&wasm);
- Fatal() << "error in validating input";
+ exitOnInvalidWasm("error validating input");
}
}
} else {
@@ -260,8 +270,7 @@ int main(int argc, const char* argv[]) {
if (options.passOptions.validate) {
if (!WasmValidator().validate(wasm)) {
WasmPrinter::printModule(&wasm);
- std::cerr << "translate-to-fuzz must always generate a valid module";
- abort();
+ Fatal() << "error after translate-to-fuzz";
}
}
}
@@ -320,9 +329,8 @@ int main(int argc, const char* argv[]) {
if (options.passOptions.validate) {
bool valid = WasmValidator().validate(other);
if (!valid) {
- WasmPrinter::printModule(&other);
+ Fatal() << "fuzz-binary must always generate a valid module";
}
- assert(valid);
}
curr = &other;
}
@@ -338,9 +346,8 @@ int main(int argc, const char* argv[]) {
if (options.passOptions.validate) {
bool valid = WasmValidator().validate(*curr);
if (!valid) {
- WasmPrinter::printModule(&*curr);
+ exitOnInvalidWasm("error after opts");
}
- assert(valid);
}
};
runPasses();
diff --git a/test/unit/test_errors.py b/test/unit/test_errors.py
new file mode 100644
index 000000000..15c5a44e6
--- /dev/null
+++ b/test/unit/test_errors.py
@@ -0,0 +1,40 @@
+import os
+
+from scripts.test import shared
+from . import utils
+
+
+class ErrorsTest(utils.BinaryenTestCase):
+ def test_parsing_error_msg(self):
+ module = '''
+(module
+ (func $foo
+ (abc)
+ )
+)
+'''
+ p = shared.run_process(shared.WASM_OPT + ['--print', '-o', os.devnull],
+ input=module, check=False, capture_output=True)
+ self.assertNotEqual(p.returncode, 0)
+ self.assertIn("parse exception: abc (at 4:4)", p.stderr)
+
+ def test_validation_error_msg(self):
+ def test(args=[], extra_expected=None):
+ module = '''
+(module
+ (memory (shared 10 20))
+)
+'''
+ p = shared.run_process(shared.WASM_OPT + ['-o', os.devnull] + args,
+ input=module, check=False, capture_output=True)
+ self.assertNotEqual(p.returncode, 0)
+ self.assertIn('memory is shared, but atomics are disabled', p.stderr)
+ if extra_expected:
+ self.assertIn(extra_expected, p.stdout)
+
+ test()
+ # when the user asks to print the module, we print it even if it is
+ # invalid, for debugging (otherwise, an invalid module would not reach
+ # the stage of runnning passes, and print is a pass, so nothing would
+ # be printed)
+ test(['--print'], '(module')
diff --git a/test/unit/test_features.py b/test/unit/test_features.py
index e0a3064d5..e77468366 100644
--- a/test/unit/test_features.py
+++ b/test/unit/test_features.py
@@ -10,7 +10,7 @@ class FeatureValidationTest(utils.BinaryenTestCase):
['--mvp-features', '--print', '-o', os.devnull],
input=module, check=False, capture_output=True)
self.assertIn(error, p.stderr)
- self.assertIn('Fatal: error in validating input', p.stderr)
+ self.assertIn('Fatal: error validating input', p.stderr)
self.assertNotEqual(p.returncode, 0)
p = shared.run_process(
shared.WASM_OPT + ['--mvp-features', '--print', '-o', os.devnull] +
diff --git a/test/unit/test_parsing_error.py b/test/unit/test_parsing_error.py
deleted file mode 100644
index c9128045e..000000000
--- a/test/unit/test_parsing_error.py
+++ /dev/null
@@ -1,19 +0,0 @@
-import os
-
-from scripts.test import shared
-from . import utils
-
-
-class ParsingErrorTest(utils.BinaryenTestCase):
- def test_parsing_error_msg(self):
- module = '''
-(module
- (func $foo
- (abc)
- )
-)
-'''
- p = shared.run_process(shared.WASM_OPT + ['--print', '-o', os.devnull],
- input=module, check=False, capture_output=True)
- self.assertNotEqual(p.returncode, 0)
- self.assertIn("parse exception: abc (at 4:4)", p.stderr)