diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-11 20:38:18 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-11 20:38:18 -0800 |
commit | a43caa75ce7293a4aea91228daf379f06817f5d8 (patch) | |
tree | cf19d50e137e9c4a80180bfb90d084f418e337f5 /test/example/find_div0s.cpp | |
parent | ed47fbc9ce48e712548d937195e0f7d333da55c4 (diff) | |
download | binaryen-a43caa75ce7293a4aea91228daf379f06817f5d8.tar.gz binaryen-a43caa75ce7293a4aea91228daf379f06817f5d8.tar.bz2 binaryen-a43caa75ce7293a4aea91228daf379f06817f5d8.zip |
add simple example
Diffstat (limited to 'test/example/find_div0s.cpp')
-rw-r--r-- | test/example/find_div0s.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
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 <ostream> +#include <wasm.h> + +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<Const>(); + if (right && right->value.getInteger() == 0) { + std::cout << "We found that " << curr->left << " is divided by zero\n"; + } + } + } + }; + DivZeroSeeker seeker; + seeker.startWalk(&module); +} + |