summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Lively <7121787+tlively@users.noreply.github.com>2020-04-24 15:51:11 -0700
committerGitHub <noreply@github.com>2020-04-24 15:51:11 -0700
commit12445969dc5b32cbe82b829fe8c061f5d98fcbe7 (patch)
treef9b6bd44d3b7cd7864c148a698712b32f802348e /src
parent204cc7b7172d665ee731171d20995edb5897ef7d (diff)
downloadbinaryen-12445969dc5b32cbe82b829fe8c061f5d98fcbe7.tar.gz
binaryen-12445969dc5b32cbe82b829fe8c061f5d98fcbe7.tar.bz2
binaryen-12445969dc5b32cbe82b829fe8c061f5d98fcbe7.zip
Remove --fuzz-binary and simplify round trip (#2799)
Since the --roundtrip pass is more general than --fuzz-binary anyways. Also reimplements `ModuleUtils::clearModule` to use the module destructor and placement new to ensure that no members are missed.
Diffstat (limited to 'src')
-rw-r--r--src/ir/module-utils.h13
-rw-r--r--src/passes/RoundTrip.cpp43
-rw-r--r--src/tools/wasm-opt.cpp39
3 files changed, 20 insertions, 75 deletions
diff --git a/src/ir/module-utils.h b/src/ir/module-utils.h
index e589f5052..ab23649f7 100644
--- a/src/ir/module-utils.h
+++ b/src/ir/module-utils.h
@@ -99,17 +99,8 @@ inline void copyModule(const Module& in, Module& out) {
}
inline void clearModule(Module& wasm) {
- wasm.exports.clear();
- wasm.functions.clear();
- wasm.globals.clear();
- wasm.events.clear();
- wasm.table.clear();
- wasm.memory.clear();
- wasm.start = Name();
- wasm.userSections.clear();
- wasm.debugInfoFileNames.clear();
- wasm.updateMaps();
- wasm.allocator.clear();
+ wasm.~Module();
+ new (&wasm) Module;
}
// Renaming
diff --git a/src/passes/RoundTrip.cpp b/src/passes/RoundTrip.cpp
index 5410ad87b..754881fe4 100644
--- a/src/passes/RoundTrip.cpp
+++ b/src/passes/RoundTrip.cpp
@@ -20,17 +20,9 @@
// parameter.
//
-#ifdef _WIN32
-#include <io.h>
-#endif
-
-#include <cstdlib>
-#include <vector>
-
#include "ir/module-utils.h"
#include "pass.h"
-#include "support/file.h"
-#include "wasm-io.h"
+#include "wasm-binary.h"
#include "wasm.h"
using namespace std;
@@ -39,28 +31,19 @@ namespace wasm {
struct RoundTrip : public Pass {
void run(PassRunner* runner, Module* module) override {
- std::string templateName = "byn_round_trip_XXXXXX";
- std::vector<char> buffer(templateName.begin(), templateName.end());
- buffer.push_back(0);
-#ifndef _WIN32
- auto fd = mkstemp(buffer.data());
- WASM_UNUSED(fd);
- std::string tempName(buffer.begin(), buffer.end());
-#else
- std::string tempName = _mktemp(buffer.data());
-#endif
- // Write
- ModuleWriter writer;
- writer.setBinary(true);
- writer.setDebugInfo(runner->options.debugInfo);
- writer.write(*module, tempName);
- // Read
+ BufferWithRandomAccess buffer;
+ // Save features, which would not otherwise make it through a round trip if
+ // the target features section has been stripped.
+ auto features = module->features;
+ // Write, clear, and read the module
+ WasmBinaryWriter(module, buffer).write();
ModuleUtils::clearModule(*module);
- ModuleReader reader;
- reader.setDWARF(runner->options.debugInfo);
- reader.read(tempName, *module);
- // Clean up
- std::remove(tempName.c_str());
+ auto input = buffer.getAsChars();
+ WasmBinaryBuilder parser(*module, input);
+ parser.setDWARF(runner->options.debugInfo);
+ parser.read();
+ // Reapply features
+ module->features = features;
}
};
diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp
index 6fd3b7c8d..a215aca32 100644
--- a/src/tools/wasm-opt.cpp
+++ b/src/tools/wasm-opt.cpp
@@ -79,7 +79,6 @@ int main(int argc, const char* argv[]) {
bool converge = false;
bool fuzzExecBefore = false;
bool fuzzExecAfter = false;
- bool fuzzBinary = false;
std::string extraFuzzCommand;
bool translateToFuzz = false;
bool fuzzPasses = false;
@@ -127,12 +126,6 @@ int main(int argc, const char* argv[]) {
[&](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. "
@@ -328,28 +321,6 @@ int main(int argc, const char* argv[]) {
std::cout << "[extra-fuzz-command first output:]\n" << firstOutput << '\n';
}
- Module* curr = &wasm;
- Module other;
-
- if (fuzzExecAfter && fuzzBinary) {
- BufferWithRandomAccess buffer;
- // write the binary
- WasmBinaryWriter writer(&wasm, buffer);
- writer.write();
- // read the binary
- auto input = buffer.getAsChars();
- WasmBinaryBuilder parser(other, input);
- parser.read();
- options.applyFeatures(other);
- if (options.passOptions.validate) {
- bool valid = WasmValidator().validate(other);
- if (!valid) {
- Fatal() << "fuzz-binary must always generate a valid module";
- }
- }
- curr = &other;
- }
-
if (!options.runningPasses()) {
if (!options.quiet) {
std::cerr << "warning: no passes specified, not doing any work\n";
@@ -357,9 +328,9 @@ int main(int argc, const char* argv[]) {
} else {
BYN_TRACE("running passes...\n");
auto runPasses = [&]() {
- options.runPasses(*curr);
+ options.runPasses(wasm);
if (options.passOptions.validate) {
- bool valid = WasmValidator().validate(*curr);
+ bool valid = WasmValidator().validate(wasm);
if (!valid) {
exitOnInvalidWasm("error after opts");
}
@@ -371,7 +342,7 @@ int main(int argc, const char* argv[]) {
// size no longer decreasing.
auto getSize = [&]() {
BufferWithRandomAccess buffer;
- WasmBinaryWriter writer(curr, buffer);
+ WasmBinaryWriter writer(&wasm, buffer);
writer.write();
return buffer.size();
};
@@ -389,7 +360,7 @@ int main(int argc, const char* argv[]) {
}
if (fuzzExecAfter) {
- results.check(*curr);
+ results.check(wasm);
}
if (options.extra.count("output") == 0) {
@@ -407,7 +378,7 @@ int main(int argc, const char* argv[]) {
writer.setSourceMapFilename(outputSourceMapFilename);
writer.setSourceMapUrl(outputSourceMapUrl);
}
- writer.write(*curr, options.extra["output"]);
+ writer.write(wasm, options.extra["output"]);
if (extraFuzzCommand.size() > 0) {
auto secondOutput = runCommand(extraFuzzCommand);