From a43caa75ce7293a4aea91228daf379f06817f5d8 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 11 Nov 2015 20:38:18 -0800 Subject: add simple example --- test/example/find_div0s.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test/example/find_div0s.cpp (limited to 'test/example/find_div0s.cpp') diff --git a/test/example/find_div0s.cpp b/test/example/find_div0s.cpp new file mode 100644 index 000000000..77a5d2c61 --- /dev/null +++ b/test/example/find_div0s.cpp @@ -0,0 +1,44 @@ + +#include +#include + +using namespace wasm; + +int main() { + // A module with a function with a division by zero in the body + Module module; + Function func; + func.name = "func"; + Binary div; + div.op = BinaryOp::DivS; + Const left; + left.value = 5; + Const right; + right.value = 0; + div.left = &left; + div.right = &right; + div.finalize(); + func.body = ÷ + module.addFunction(&func); + + // Print it out + std::cout << module; + + // Search it for divisions by zero: Walk the module, looking for + // that operation. + struct DivZeroSeeker : public WasmWalker { + void visitBinary(Binary* curr) { + // In every Binary, look for integer divisions + if (curr->op == BinaryOp::DivS || curr->op == BinaryOp::DivU) { + // Check if the right operand is a constant, and if it is 0 + auto right = curr->right->dyn_cast(); + if (right && right->value.getInteger() == 0) { + std::cout << "We found that " << curr->left << " is divided by zero\n"; + } + } + } + }; + DivZeroSeeker seeker; + seeker.startWalk(&module); +} + -- cgit v1.2.3 From e53badb26a1a3d2de0c7df30bb7e656299d91b21 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 11 Nov 2015 20:49:51 -0800 Subject: text --- README.md | 4 ++-- test/example/find_div0s.cpp | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'test/example/find_div0s.cpp') diff --git a/README.md b/README.md index ee9f0aaa6..5d90644b4 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ The `check.py` script supports some options: Same as Emscripten: MIT license. -(parts of `src/` are synced with `tools/optimizer/` in the main emscripten repo, for convenience) +(`src/emscripten-optimizer` is synced with `tools/optimizer/` in the main emscripten repo, for convenience) ## TODO @@ -138,5 +138,5 @@ Same as Emscripten: MIT license. **Binaryen** is a combination of **binary** - since WebAssembly is a *binary* format for the web - and **Emscripten** - which it can integrate with in order to compile C and C++ all the way to WebAssembly, via asm.js. Binaryen began as Emscripten's WebAssembly processing library (`wasm-emscripten`). -"Binaryen" is pronounced [in the same manner](http://www.makinggameofthrones.com/production-diary/2011/2/11/official-pronunciation-guide-for-game-of-thrones.html) as "[Targaryen](https://en.wikipedia.org/wiki/List_of_A_Song_of_Ice_and_Fire_characters#House_Targaryen)": bi-NAIR-ee-in. +"Binaryen" is pronounced [in the same manner](http://www.makinggameofthrones.com/production-diary/2011/2/11/official-pronunciation-guide-for-game-of-thrones.html) as "[Targaryen](https://en.wikipedia.org/wiki/List_of_A_Song_of_Ice_and_Fire_characters#House_Targaryen)": bi-NAIR-ee-in. Valar Morcodeis. diff --git a/test/example/find_div0s.cpp b/test/example/find_div0s.cpp index 77a5d2c61..be273390f 100644 --- a/test/example/find_div0s.cpp +++ b/test/example/find_div0s.cpp @@ -1,11 +1,18 @@ +// +// Tiny example, using Binaryen to walk a WebAssembly module in search +// for direct integer divisions by zero. To do so, we inherit from +// WasmWalker, and implement visitBinary, which is called on every +// Binary node in the module's functions. +// + #include #include using namespace wasm; int main() { - // A module with a function with a division by zero in the body + // A module with a function with a division by zero in the body. Module module; Function func; func.name = "func"; -- cgit v1.2.3 From afce1de1495cc5782ec55b016d4e864b316d9920 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 12 Nov 2015 10:18:20 -0800 Subject: simplify find_div0s --- test/example/find_div0s.cpp | 33 +++++++++++++++++++-------------- test/example/find_div0s.txt | 4 ++-- 2 files changed, 21 insertions(+), 16 deletions(-) (limited to 'test/example/find_div0s.cpp') diff --git a/test/example/find_div0s.cpp b/test/example/find_div0s.cpp index be273390f..60eed5f62 100644 --- a/test/example/find_div0s.cpp +++ b/test/example/find_div0s.cpp @@ -8,25 +8,30 @@ #include #include +#include using namespace wasm; int main() { - // A module with a function with a division by zero in the body. + // A simple WebAssembly module in S-Expression format. + char input[] = + "(module" + " (func $has_div_zero" + " (i32.div_s" + " (i32.const 5)" + " (i32.const 0)" + " )" + " )" + ")"; + + // Parse the S-Expression text, and prepare to build a WebAssembly module. + SExpressionParser parser(input); + Element& root = *parser.root; Module module; - Function func; - func.name = "func"; - Binary div; - div.op = BinaryOp::DivS; - Const left; - left.value = 5; - Const right; - right.value = 0; - div.left = &left; - div.right = &right; - div.finalize(); - func.body = ÷ - module.addFunction(&func); + + // The parsed code has just one element, the module. Build the module + // from that (and abort on any errors, but there won't be one here). + SExpressionWasmBuilder builder(module, *root[0], [&]() { abort(); }); // Print it out std::cout << module; diff --git a/test/example/find_div0s.txt b/test/example/find_div0s.txt index 44397fbde..554790493 100644 --- a/test/example/find_div0s.txt +++ b/test/example/find_div0s.txt @@ -1,7 +1,7 @@ (module (memory 0 4294967295) - (func $func - (none.div_s + (func $has_div_zero + (i32.div_s (i32.const 5) (i32.const 0) ) -- cgit v1.2.3