summaryrefslogtreecommitdiff
path: root/src/tools/wasm-opt.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2019-04-26 16:59:41 -0700
committerGitHub <noreply@github.com>2019-04-26 16:59:41 -0700
commitdb9124f1de0478dcac525009b6f1589b44a7edd8 (patch)
treefa26395a0f6cca53cf5cb6e10189f989c5bfa847 /src/tools/wasm-opt.cpp
parent87636dccd404a340d75acb1d96301581343f29ca (diff)
downloadbinaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.tar.gz
binaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.tar.bz2
binaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.zip
Apply format changes from #2048 (#2059)
Mass change to apply clang-format to everything. We are applying this in a PR by me so the (git) blame is all mine ;) but @aheejin did all the work to get clang-format set up and all the manual work to tidy up some things to make the output nicer in #2048
Diffstat (limited to 'src/tools/wasm-opt.cpp')
-rw-r--r--src/tools/wasm-opt.cpp225
1 files changed, 147 insertions, 78 deletions
diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp
index 519713298..e9f369d22 100644
--- a/src/tools/wasm-opt.cpp
+++ b/src/tools/wasm-opt.cpp
@@ -21,21 +21,21 @@
#include <memory>
+#include "execution-results.h"
+#include "fuzzing.h"
+#include "js-wrapper.h"
+#include "optimization-options.h"
#include "pass.h"
+#include "shell-interface.h"
+#include "spec-wrapper.h"
#include "support/command-line.h"
#include "support/file.h"
+#include "wasm-binary.h"
+#include "wasm-interpreter.h"
+#include "wasm-io.h"
#include "wasm-printing.h"
#include "wasm-s-parser.h"
#include "wasm-validator.h"
-#include "wasm-io.h"
-#include "wasm-interpreter.h"
-#include "wasm-binary.h"
-#include "shell-interface.h"
-#include "optimization-options.h"
-#include "execution-results.h"
-#include "fuzzing.h"
-#include "js-wrapper.h"
-#include "spec-wrapper.h"
using namespace wasm;
@@ -45,7 +45,7 @@ std::string runCommand(std::string command) {
std::string output;
const int MAX_BUFFER = 1024;
char buffer[MAX_BUFFER];
- FILE *stream = popen(command.c_str(), "r");
+ FILE* stream = popen(command.c_str(), "r");
while (fgets(buffer, MAX_BUFFER, stream) != NULL) {
output.append(buffer);
}
@@ -81,69 +81,130 @@ int main(int argc, const char* argv[]) {
OptimizationOptions options("wasm-opt", "Read, write, and optimize files");
options
- .add("--output", "-o", "Output file (stdout if not specified)",
- Options::Arguments::One,
- [](Options* o, const std::string& argument) {
- o->extra["output"] = argument;
- Colors::disable();
- })
- .add("--emit-text", "-S", "Emit text instead of binary for the output file",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& argument) { emitBinary = false; })
- .add("--debuginfo", "-g", "Emit names section and debug info",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { debugInfo = true; })
- .add("--converge", "-c", "Run passes to convergence, continuing while binary size decreases",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { converge = true; })
- .add("--fuzz-exec-before", "-feh", "Execute functions before optimization, helping fuzzing find bugs",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { fuzzExecBefore = true; })
- .add("--fuzz-exec", "-fe", "Execute functions before and after optimization, helping fuzzing find bugs",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { fuzzExecBefore = fuzzExecAfter = true; })
- .add("--fuzz-binary", "-fb", "Convert to binary and back after optimizations and before fuzz-exec, helping fuzzing find binary format bugs",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { fuzzBinary = true; })
- .add("--extra-fuzz-command", "-efc", "An extra command to run on the output before and after optimizing. The output is compared between the two, and an error occurs if they are not equal",
- Options::Arguments::One,
- [&](Options *o, const std::string& arguments) { extraFuzzCommand = arguments; })
- .add("--translate-to-fuzz", "-ttf", "Translate the input into a valid wasm module *somehow*, useful for fuzzing",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { translateToFuzz = true; })
- .add("--fuzz-passes", "-fp", "Pick a random set of passes to run, useful for fuzzing. this depends on translate-to-fuzz (it picks the passes from the input)",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { fuzzPasses = true; })
- .add("--no-fuzz-nans", "", "don't emit NaNs when fuzzing, and remove them at runtime as well (helps avoid nondeterminism between VMs)",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { fuzzNaNs = false; })
- .add("--no-fuzz-memory", "", "don't emit memory ops when fuzzing",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { fuzzMemory = false; })
- .add("--emit-js-wrapper", "-ejw", "Emit a JavaScript wrapper file that can run the wasm with some test values, useful for fuzzing",
- Options::Arguments::One,
- [&](Options *o, const std::string& arguments) { emitJSWrapper = arguments; })
- .add("--emit-spec-wrapper", "-esw", "Emit a wasm spec interpreter wrapper file that can run the wasm with some test values, useful for fuzzing",
- Options::Arguments::One,
- [&](Options *o, const std::string& arguments) { emitSpecWrapper = arguments; })
- .add("--input-source-map", "-ism", "Consume source map from the specified file",
- Options::Arguments::One,
- [&inputSourceMapFilename](Options *o, const std::string& argument) { inputSourceMapFilename = argument; })
- .add("--output-source-map", "-osm", "Emit source map to the specified file",
- Options::Arguments::One,
- [&outputSourceMapFilename](Options *o, const std::string& argument) { outputSourceMapFilename = argument; })
- .add("--output-source-map-url", "-osu", "Emit specified string as source map URL",
- Options::Arguments::One,
- [&outputSourceMapUrl](Options *o, const std::string& argument) { outputSourceMapUrl = argument; })
- .add_positional("INFILE", Options::Arguments::One,
- [](Options* o, const std::string& argument) {
- o->extra["infile"] = argument;
- });
+ .add("--output",
+ "-o",
+ "Output file (stdout if not specified)",
+ Options::Arguments::One,
+ [](Options* o, const std::string& argument) {
+ o->extra["output"] = argument;
+ Colors::disable();
+ })
+ .add("--emit-text",
+ "-S",
+ "Emit text instead of binary for the output file",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& argument) { emitBinary = false; })
+ .add("--debuginfo",
+ "-g",
+ "Emit names section and debug info",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { debugInfo = true; })
+ .add("--converge",
+ "-c",
+ "Run passes to convergence, continuing while binary size decreases",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { converge = true; })
+ .add(
+ "--fuzz-exec-before",
+ "-feh",
+ "Execute functions before optimization, helping fuzzing find bugs",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { fuzzExecBefore = true; })
+ .add("--fuzz-exec",
+ "-fe",
+ "Execute functions before and after optimization, helping fuzzing "
+ "find bugs",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) {
+ fuzzExecBefore = fuzzExecAfter = true;
+ })
+ .add("--fuzz-binary",
+ "-fb",
+ "Convert to binary and back after optimizations and before fuzz-exec, "
+ "helping fuzzing find binary format bugs",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { fuzzBinary = true; })
+ .add("--extra-fuzz-command",
+ "-efc",
+ "An extra command to run on the output before and after optimizing. "
+ "The output is compared between the two, and an error occurs if they "
+ "are not equal",
+ Options::Arguments::One,
+ [&](Options* o, const std::string& arguments) {
+ extraFuzzCommand = arguments;
+ })
+ .add(
+ "--translate-to-fuzz",
+ "-ttf",
+ "Translate the input into a valid wasm module *somehow*, useful for "
+ "fuzzing",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { translateToFuzz = true; })
+ .add("--fuzz-passes",
+ "-fp",
+ "Pick a random set of passes to run, useful for fuzzing. this depends "
+ "on translate-to-fuzz (it picks the passes from the input)",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { fuzzPasses = true; })
+ .add("--no-fuzz-nans",
+ "",
+ "don't emit NaNs when fuzzing, and remove them at runtime as well "
+ "(helps avoid nondeterminism between VMs)",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { fuzzNaNs = false; })
+ .add("--no-fuzz-memory",
+ "",
+ "don't emit memory ops when fuzzing",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { fuzzMemory = false; })
+ .add("--emit-js-wrapper",
+ "-ejw",
+ "Emit a JavaScript wrapper file that can run the wasm with some test "
+ "values, useful for fuzzing",
+ Options::Arguments::One,
+ [&](Options* o, const std::string& arguments) {
+ emitJSWrapper = arguments;
+ })
+ .add("--emit-spec-wrapper",
+ "-esw",
+ "Emit a wasm spec interpreter wrapper file that can run the wasm with "
+ "some test values, useful for fuzzing",
+ Options::Arguments::One,
+ [&](Options* o, const std::string& arguments) {
+ emitSpecWrapper = arguments;
+ })
+ .add("--input-source-map",
+ "-ism",
+ "Consume source map from the specified file",
+ Options::Arguments::One,
+ [&inputSourceMapFilename](Options* o, const std::string& argument) {
+ inputSourceMapFilename = argument;
+ })
+ .add("--output-source-map",
+ "-osm",
+ "Emit source map to the specified file",
+ Options::Arguments::One,
+ [&outputSourceMapFilename](Options* o, const std::string& argument) {
+ outputSourceMapFilename = argument;
+ })
+ .add("--output-source-map-url",
+ "-osu",
+ "Emit specified string as source map URL",
+ Options::Arguments::One,
+ [&outputSourceMapUrl](Options* o, const std::string& argument) {
+ outputSourceMapUrl = argument;
+ })
+ .add_positional("INFILE",
+ Options::Arguments::One,
+ [](Options* o, const std::string& argument) {
+ o->extra["infile"] = argument;
+ });
options.parse(argc, argv);
Module wasm;
- if (options.debug) std::cerr << "reading...\n";
+ if (options.debug)
+ std::cerr << "reading...\n";
if (!translateToFuzz) {
ModuleReader reader;
@@ -159,7 +220,8 @@ int main(int argc, const char* argv[]) {
std::cerr << '\n';
Fatal() << "error in parsing wasm source map";
} catch (std::bad_alloc&) {
- Fatal() << "error in building module, std::bad_alloc (possibly invalid request for silly amounts of memory)";
+ Fatal() << "error in building module, std::bad_alloc (possibly invalid "
+ "request for silly amounts of memory)";
}
options.applyFeatures(wasm);
@@ -218,7 +280,9 @@ int main(int argc, const char* argv[]) {
std::string firstOutput;
if (extraFuzzCommand.size() > 0 && options.extra.count("output") > 0) {
- if (options.debug) std::cerr << "writing binary before opts, for extra fuzz command..." << std::endl;
+ if (options.debug)
+ std::cerr << "writing binary before opts, for extra fuzz command..."
+ << std::endl;
ModuleWriter writer;
writer.setDebug(options.debug);
writer.setBinary(emitBinary);
@@ -252,12 +316,12 @@ int main(int argc, const char* argv[]) {
}
if (options.runningPasses()) {
- if (options.debug) std::cerr << "running passes...\n";
+ if (options.debug)
+ std::cerr << "running passes...\n";
auto runPasses = [&]() {
options.runPasses(*curr);
if (options.passOptions.validate) {
- bool valid =
- WasmValidator().validate(*curr);
+ bool valid = WasmValidator().validate(*curr);
if (!valid) {
WasmPrinter::printModule(&*curr);
}
@@ -276,10 +340,13 @@ int main(int argc, const char* argv[]) {
};
auto lastSize = getSize();
while (1) {
- if (options.debug) std::cerr << "running iteration for convergence (" << lastSize << ")...\n";
+ if (options.debug)
+ std::cerr << "running iteration for convergence (" << lastSize
+ << ")...\n";
runPasses();
auto currSize = getSize();
- if (currSize >= lastSize) break;
+ if (currSize >= lastSize)
+ break;
lastSize = currSize;
}
}
@@ -292,7 +359,8 @@ int main(int argc, const char* argv[]) {
if (options.extra.count("output") == 0) {
std::cerr << "(no output file specified, not emitting output)\n";
} else {
- if (options.debug) std::cerr << "writing..." << std::endl;
+ if (options.debug)
+ std::cerr << "writing..." << std::endl;
ModuleWriter writer;
writer.setDebug(options.debug);
writer.setBinary(emitBinary);
@@ -305,7 +373,8 @@ int main(int argc, const char* argv[]) {
if (extraFuzzCommand.size() > 0) {
auto secondOutput = runCommand(extraFuzzCommand);
- std::cout << "[extra-fuzz-command second output:]\n" << firstOutput << '\n';
+ std::cout << "[extra-fuzz-command second output:]\n"
+ << firstOutput << '\n';
if (firstOutput != secondOutput) {
std::cerr << "extra fuzz command output differs\n";
abort();