Skip to content

Commit

Permalink
validate unique local names, and use validation in wasm2js. fixes #1885
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Jan 23, 2019
1 parent e63c4a7 commit ab40fcf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/tools/feature-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ struct FeatureOptions : public Options {
[this](Options *o, const std::string& arguments) {
passOptions.features.setSIMD(false);
})
.add("--no-validation", "-n", "Disables validation, assumes inputs are correct",
Options::Arguments::Zero,
[this](Options* o, const std::string& argument) {
passOptions.validate = false;
});
;
}

Expand Down
5 changes: 0 additions & 5 deletions src/tools/optimization-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ struct OptimizationOptions : public FeatureOptions {
[this](Options* o, const std::string& argument) {
passOptions.shrinkLevel = atoi(argument.c_str());
})
.add("--no-validation", "-n", "Disables validation, assumes inputs are correct",
Options::Arguments::Zero,
[this](Options* o, const std::string& argument) {
passOptions.validate = false;
})
.add("--ignore-implicit-traps", "-iit", "Optimize under the helpful assumption that no surprising traps occur (from load, div/mod, etc.)",
Options::Arguments::Zero,
[this](Options*, const std::string&) {
Expand Down
10 changes: 9 additions & 1 deletion src/tools/wasm2js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
#include "support/file.h"
#include "wasm-s-parser.h"
#include "wasm2js.h"
#include "feature-options.h"

using namespace cashew;
using namespace wasm;

int main(int argc, const char *argv[]) {
Wasm2JSBuilder::Flags builderFlags;
Options options("wasm2js", "Transform .wasm/.wast files to asm.js");
FeatureOptions options("wasm2js", "Transform .wasm/.wast files to asm.js");
options
.add("--output", "-o", "Output file (stdout if not specified)",
Options::Arguments::One,
Expand Down Expand Up @@ -97,6 +98,13 @@ int main(int argc, const char *argv[]) {
Fatal() << "error in building module, std::bad_alloc (possibly invalid request for silly amounts of memory)";
}

if (options.passOptions.validate) {
if (!WasmValidator().validate(wasm, options.getFeatures())) {
WasmPrinter::printModule(&wasm);
Fatal() << "error in validating input";
}
}

if (options.debug) std::cerr << "asming..." << std::endl;
Wasm2JSBuilder wasm2js(builderFlags);
asmjs = wasm2js.processWasm(&wasm);
Expand Down
6 changes: 6 additions & 0 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,12 @@ void FunctionValidator::visitFunction(Function* curr) {
if (curr->imported()) {
shouldBeTrue(curr->type.is(), curr->name, "imported functions must have a function type");
}
// validate optional local names
std::set<Name> seen;
for (auto& pair : curr->localNames) {
Name name = pair.second;
shouldBeTrue(seen.insert(name).second, name, "local names must be unique");
}
}

static bool checkOffset(Expression* curr, Address add, Address max) {
Expand Down

0 comments on commit ab40fcf

Please sign in to comment.