Verified Commit ad261fcf authored by nagayama15's avatar nagayama15

feat(op_swap): Implement the operand swapping method

parent 8a6f7c11
add_library(kyut INTERFACE) add_library(kyut STATIC
kyut/methods/OperandSwapping.cpp
)
target_include_directories(kyut INTERFACE target_include_directories(kyut INTERFACE
"." "."
) )
target_link_libraries(kyut INTERFACE target_link_libraries(kyut
binaryen::binaryen binaryen::binaryen
fmtlib::fmt fmtlib::fmt
) )
This diff is collapsed.
#ifndef INCLUDE_kyut_methods_OperandSwapping_hpp
#define INCLUDE_kyut_methods_OperandSwapping_hpp
#include <cstddef>
namespace wasm {
class Module;
} // namespace wasm
namespace kyut {
class CircularBitStreamReader;
class BitStreamWriter;
} // namespace kyut
namespace kyut::methods::operand_swapping {
std::size_t embed(CircularBitStreamReader& r, wasm::Module& module);
std::size_t extract(BitStreamWriter& w, wasm::Module& module);
} // namespace kyut::methods::operand_swapping
#endif // INCLUDE_kyut_methods_OperandSwapping_hpp
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "cmdline.h" #include "cmdline.h"
#include "kyut/methods/ExportReordering.hpp" #include "kyut/methods/ExportReordering.hpp"
#include "kyut/methods/FunctionReordering.hpp" #include "kyut/methods/FunctionReordering.hpp"
#include "kyut/methods/OperandSwapping.hpp"
#include "wasm-io.h" #include "wasm-io.h"
namespace { namespace {
...@@ -15,7 +16,7 @@ int main(int argc, char* argv[]) { ...@@ -15,7 +16,7 @@ int main(int argc, char* argv[]) {
options.add("help", 'h', "Print help message"); options.add("help", 'h', "Print help message");
options.add("version", 'v', "Print version"); options.add("version", 'v', "Print version");
options.add<std::string>("method", 'm', "Embedding method (function-reordering, export-reordering)", true, "", cmdline::oneof<std::string>("function-reordering", "export-reordering")); options.add<std::string>("method", 'm', "Embedding method (function-reorder, export-reorder, operand-swap)", true, "", cmdline::oneof<std::string>("function-reorder", "export-reorder", "operand-swap"));
options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20)); options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20));
options.add<std::string>("dump", 0, "Output format (ascii, hex)", false, "ascii", cmdline::oneof<std::string>("ascii", "hex")); options.add<std::string>("dump", 0, "Output format (ascii, hex)", false, "ascii", cmdline::oneof<std::string>("ascii", "hex"));
...@@ -51,10 +52,12 @@ int main(int argc, char* argv[]) { ...@@ -51,10 +52,12 @@ int main(int argc, char* argv[]) {
kyut::BitStreamWriter w{}; kyut::BitStreamWriter w{};
std::size_t size_bits; std::size_t size_bits;
if (method == "function-reordering") { if (method == "function-reorder") {
size_bits = kyut::methods::function_reordering::extract(w, module, chunk_size); size_bits = kyut::methods::function_reordering::extract(w, module, chunk_size);
} else if (method == "export-reordering") { } else if (method == "export-reorder") {
size_bits = kyut::methods::export_reordering::extract(w, module, chunk_size); size_bits = kyut::methods::export_reordering::extract(w, module, chunk_size);
} else if (method == "operand-swap") {
size_bits = kyut::methods::operand_swapping::extract(w, module);
} else { } else {
WASM_UNREACHABLE(("unknown method: " + method).c_str()); WASM_UNREACHABLE(("unknown method: " + method).c_str());
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "cmdline.h" #include "cmdline.h"
#include "kyut/methods/ExportReordering.hpp" #include "kyut/methods/ExportReordering.hpp"
#include "kyut/methods/FunctionReordering.hpp" #include "kyut/methods/FunctionReordering.hpp"
#include "kyut/methods/OperandSwapping.hpp"
#include "support/colors.h" #include "support/colors.h"
#include "wasm-io.h" #include "wasm-io.h"
...@@ -17,7 +18,7 @@ int main(int argc, char* argv[]) { ...@@ -17,7 +18,7 @@ int main(int argc, char* argv[]) {
options.add("version", 'v', "Print version"); options.add("version", 'v', "Print version");
options.add<std::string>("output", 'o', "Output filename", true); options.add<std::string>("output", 'o', "Output filename", true);
options.add<std::string>("method", 'm', "Embedding method (function-reordering, export-reordering)", true, "", cmdline::oneof<std::string>("function-reordering", "export-reordering")); options.add<std::string>("method", 'm', "Embedding method (function-reorder, export-reorder, operand-swap)", true, "", cmdline::oneof<std::string>("function-reorder", "export-reorder", "operand-swap"));
options.add<std::string>("watermark", 'w', "Watermark to embed", true); options.add<std::string>("watermark", 'w', "Watermark to embed", true);
options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20)); options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20));
...@@ -68,10 +69,12 @@ int main(int argc, char* argv[]) { ...@@ -68,10 +69,12 @@ int main(int argc, char* argv[]) {
kyut::CircularBitStreamReader r{watermark}; kyut::CircularBitStreamReader r{watermark};
std::size_t size_bits; std::size_t size_bits;
if (method == "function-reordering") { if (method == "function-reorder") {
size_bits = kyut::methods::function_reordering::embed(r, module, chunk_size); size_bits = kyut::methods::function_reordering::embed(r, module, chunk_size);
} else if (method == "export-reordering") { } else if (method == "export-reorder") {
size_bits = kyut::methods::export_reordering::embed(r, module, chunk_size); size_bits = kyut::methods::export_reordering::embed(r, module, chunk_size);
} else if (method == "operand-swap") {
size_bits = kyut::methods::operand_swapping::embed(r, module);
} else { } else {
WASM_UNREACHABLE(("unknown method: " + method).c_str()); WASM_UNREACHABLE(("unknown method: " + method).c_str());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment