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
1 change: 1 addition & 0 deletions scripts/fuzz_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ def write_commands(commands, filename):
["--reorder-functions"],
["--reorder-locals"],
["--flatten", "--rereloop"],
["--roundtrip"],
["--rse"],
["--simplify-locals"],
["--simplify-locals-nonesting"],
Expand Down
18 changes: 16 additions & 2 deletions src/ir/module-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ inline Event* copyEvent(Event* event, Module& out) {
return ret;
}

inline void copyModule(Module& in, Module& out) {
// we use names throughout, not raw points, so simple copying is fine
inline void copyModule(const Module& in, Module& out) {
// we use names throughout, not raw pointers, so simple copying is fine
// for everything *but* expressions
for (auto& curr : in.functionTypes) {
out.addFunctionType(make_unique<FunctionType>(*curr));
Expand Down Expand Up @@ -136,6 +136,20 @@ inline void copyModule(Module& in, Module& out) {
out.debugInfoFileNames = in.debugInfoFileNames;
}

inline void clearModule(Module& wasm) {
wasm.functionTypes.clear();
wasm.exports.clear();
wasm.functions.clear();
wasm.globals.clear();
wasm.events.clear();
wasm.table.segments.clear();
wasm.memory.segments.clear();
wasm.start = Name();
wasm.userSections.clear();
wasm.debugInfoFileNames.clear();
wasm.updateMaps();
}

// Renaming

// Rename functions along with all their uses.
Expand Down
1 change: 1 addition & 0 deletions src/passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set(passes_SOURCES
PrintCallGraph.cpp
PrintFeatures.cpp
PrintFunctionMap.cpp
RoundTrip.cpp
StackIR.cpp
Strip.cpp
StripTargetFeatures.cpp
Expand Down
71 changes: 71 additions & 0 deletions src/passes/RoundTrip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2019 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//
// Write the module to binary, and load it from there. This is useful in
// testing to check for the effects of roundtripping in a single wasm-opt
// 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.h"

using namespace std;

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
Module newModule;
ModuleReader reader;
reader.read(tempName, newModule);
// Clean up
std::remove(tempName.c_str());
// Swap in
ModuleUtils::clearModule(*module);
ModuleUtils::copyModule(newModule, *module);
}
};

Pass* createRoundTripPass() { return new RoundTrip(); }

} // namespace wasm
3 changes: 3 additions & 0 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ void PassRegistry::registerPasses() {
createReReloopPass);
registerPass(
"rse", "remove redundant local.sets", createRedundantSetEliminationPass);
registerPass("roundtrip",
"write the module to binary, then read it",
createRoundTripPass);
registerPass("safe-heap",
"instrument loads and stores to check for invalid behavior",
createSafeHeapPass);
Expand Down
1 change: 1 addition & 0 deletions src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Pass* createReorderFunctionsPass();
Pass* createReorderLocalsPass();
Pass* createReReloopPass();
Pass* createRedundantSetEliminationPass();
Pass* createRoundTripPass();
Pass* createSafeHeapPass();
Pass* createSimplifyLocalsPass();
Pass* createSimplifyGlobalsPass();
Expand Down
7 changes: 7 additions & 0 deletions test/passes/roundtrip.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(module
(type $0 (func))
(export "foo" (func $0))
(func $0 (; 0 ;)
(unreachable)
)
)
11 changes: 11 additions & 0 deletions test/passes/roundtrip.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(module
(func "foo"
;; binaryen skips unreachable code while reading the binary format
(unreachable)
(nop)
(nop)
(nop)
(nop)
(nop)
)
)