summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2019-04-17 19:52:20 -0700
committerGitHub <noreply@github.com>2019-04-17 19:52:20 -0700
commitf20f8c39adccc8593c0eea3b98378ca9549f9c26 (patch)
treeaa6e34b8304b8e6ec574fd2f42e1f23c81c2c086 /scripts
parentc7712d3757461da036f9c7714cec372d584733e3 (diff)
downloadbinaryen-f20f8c39adccc8593c0eea3b98378ca9549f9c26.tar.gz
binaryen-f20f8c39adccc8593c0eea3b98378ca9549f9c26.tar.bz2
binaryen-f20f8c39adccc8593c0eea3b98378ca9549f9c26.zip
Fuzz prep for wasm2js (#2022)
Get fuzzing to a runnable state.
Diffstat (limited to 'scripts')
-rw-r--r--scripts/fuzz_opt.py17
-rw-r--r--scripts/wasm2js.js97
2 files changed, 112 insertions, 2 deletions
diff --git a/scripts/fuzz_opt.py b/scripts/fuzz_opt.py
index 83ffc638a..faa831387 100644
--- a/scripts/fuzz_opt.py
+++ b/scripts/fuzz_opt.py
@@ -21,14 +21,14 @@ import re
import shutil
import time
-from test.shared import options
+from test.shared import options, NODEJS
# parameters
NANS = True
-FUZZ_OPTS = ['--all-features', '--disable-simd', '--disable-bulk-memory']
+FUZZ_OPTS = [] # '--all-features' etc
V8_OPTS = [
'--experimental-wasm-eh',
@@ -90,6 +90,18 @@ def compare(x, y, comment):
))
+def run_wasm2js(wasm):
+ wrapper = run([in_bin('wasm-opt'), wasm, '--emit-js-wrapper=/dev/stdout'])
+ main = run([in_bin('wasm2js'), wasm, '--emscripten'])
+ with open(os.path.join(options.binaryen_root, 'scripts', 'wasm2js.js')) as f:
+ glue = f.read()
+ with open('js.js', 'w') as f:
+ f.write(glue)
+ f.write(main)
+ f.write(wrapper)
+ return run([NODEJS, 'js.js', 'a.wasm'])
+
+
def run_vms(prefix):
def fix_output(out):
# large doubles may print slightly different on different VMs
@@ -138,6 +150,7 @@ def run_vms(prefix):
results = [fix_output(run_vm([in_bin('wasm-opt'), prefix + 'wasm', '--fuzz-exec-before']))]
# append to add results from VMs
+ results += [run_wasm2js(prefix + 'wasm')]
# results += [fix_output(run_vm([os.path.expanduser('d8'), prefix + 'js'] + V8_OPTS + ['--', prefix + 'wasm']))]
# results += [fix_output(run_vm([os.path.expanduser('~/.jsvu/jsc'), prefix + 'js', '--', prefix + 'wasm']))]
# spec has no mechanism to not halt on a trap. so we just check until the first trap, basically
diff --git a/scripts/wasm2js.js b/scripts/wasm2js.js
new file mode 100644
index 000000000..fe619f5f3
--- /dev/null
+++ b/scripts/wasm2js.js
@@ -0,0 +1,97 @@
+// wasm2js.js - enough of a polyfill for the WebAssembly object so that we can load
+// wasm2js code that way. Similar to the same file in emscripten, but tailored for
+// fuzzing purposes here.
+
+var WebAssembly = {
+ Memory: function(opts) {
+ return {
+ buffer: new ArrayBuffer(opts['initial'] * 64 * 1024),
+ grow: function(amount) {
+ var oldBuffer = this.buffer;
+ var ret = __growWasmMemory(amount);
+ assert(this.buffer !== oldBuffer); // the call should have updated us
+ return ret;
+ }
+ };
+ },
+
+ Table: function(opts) {
+ var ret = new Array(opts['initial']);
+ ret.grow = function(by) {
+ ret.push(null);
+ };
+ ret.set = function(i, func) {
+ ret[i] = func;
+ };
+ ret.get = function(i) {
+ return ret[i];
+ };
+ return ret;
+ },
+
+ Module: function(binary) {
+ // TODO: use the binary and info somehow - right now the wasm2js output is embedded in
+ // the main JS
+ return {};
+ },
+
+ Instance: function(module, info) {
+ // TODO: use the module and info somehow - right now the wasm2js output is embedded in
+ // the main JS
+ var decodeBase64 = typeof atob === 'function' ? atob : function (input) {
+ var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
+
+ var output = '';
+ var chr1, chr2, chr3;
+ var enc1, enc2, enc3, enc4;
+ var i = 0;
+ // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
+ do {
+ enc1 = keyStr.indexOf(input.charAt(i++));
+ enc2 = keyStr.indexOf(input.charAt(i++));
+ enc3 = keyStr.indexOf(input.charAt(i++));
+ enc4 = keyStr.indexOf(input.charAt(i++));
+
+ chr1 = (enc1 << 2) | (enc2 >> 4);
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
+ chr3 = ((enc3 & 3) << 6) | enc4;
+
+ output = output + String.fromCharCode(chr1);
+
+ if (enc3 !== 64) {
+ output = output + String.fromCharCode(chr2);
+ }
+ if (enc4 !== 64) {
+ output = output + String.fromCharCode(chr3);
+ }
+ } while (i < input.length);
+ return output;
+ };
+ var atob = decodeBase64;
+ // Additional imports
+ asmLibraryArg['__tempMemory__'] = 0; // risky!
+ // This will be replaced by the actual wasm2js code.
+ var exports = instantiate(asmLibraryArg, wasmMemory, wasmTable);
+ return {
+ 'exports': exports
+ };
+ },
+
+ instantiate: function(binary, info) {
+ return {
+ then: function(ok, err) {
+ ok({
+ 'instance': new WebAssembly.Instance(new WebAssembly.Module(binary, info))
+ });
+ }
+ };
+ }
+};
+
+var asmLibraryArg = {
+};
+
+var wasmMemory = new WebAssembly.Memory({ initial: 1 });
+var wasmTable = new WebAssembly.Table({ initial: 1 });
+