aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/fuzz_compiler.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/drivers/fuzz_compiler.py')
-rw-r--r--tests/drivers/fuzz_compiler.py40
1 files changed, 23 insertions, 17 deletions
diff --git a/tests/drivers/fuzz_compiler.py b/tests/drivers/fuzz_compiler.py
index 512ec26..0fe0d27 100644
--- a/tests/drivers/fuzz_compiler.py
+++ b/tests/drivers/fuzz_compiler.py
@@ -1,17 +1,23 @@
#!/usr/bin/env python3
"""Deterministic grammar fuzzing for the parser, runtime, and ownership paths.
-Usage: python3 tests/drivers/fuzz_compiler.py [path/to/zy] [path/to/zym] [--js]
+Usage: python3 tests/drivers/fuzz_compiler.py [path/to/zy] [path/to/zym] [--js [path/to/tinyvm.js]]
+(--js alone runs build/js/tinyvm.js, from make js)
"""
import random
import subprocess
import sys
-args = [arg for arg in sys.argv[1:] if arg != "--js"]
-js = "--js" in sys.argv[1:]
-zy = args[0] if len(args) > 0 else "bin/zy"
-zym = args[1] if len(args) > 1 else "bin/zym"
+argv = sys.argv[1:]
+js = None
+if "--js" in argv:
+ k = argv.index("--js")
+ del argv[k]
+ js = argv.pop(k) if k < len(argv) and argv[k].endswith(".js") else "build/js/tinyvm.js"
+args = argv
+zy = args[0] if len(args) > 0 else "build/bin/zy"
+zym = args[1] if len(args) > 1 else "build/bin/zym"
rng = random.Random(0x5A17)
values = []
library_values = []
@@ -33,26 +39,26 @@ def expr(depth):
return f"({left} {op} {right})", value
-lines = ["include std;", "fn churn(n) {", " int i = 0;", " while (i < n) {", " { int* p = new(int); *p = i; }", " i++;", " }", " return 0;", "}", "fn stable() { return 73; }", "fn main() {", " let base = live_objects();"]
+lines = ["namespace;", "include rt, run, std;", "fn churn(n) {", " int i = 0;", " while (i < n) {", " { int* p = rt.new(int); *p = i; }", " i++;", " }", " return 0;", "}", "fn stable() { return 73; }", "fn main() {", " let base = rt.live_objects();"]
for _ in range(128):
source, value = expr(4)
- lines.append(f" put_n({source}); put_c(32);")
+ lines.append(f" rt.put_n({source}); rt.put_c(32);")
values.append(value)
for _ in range(32):
a = "".join(rng.choice("abc") for _ in range(rng.randrange(11)))
b = "".join(rng.choice("abc") for _ in range(rng.randrange(6)))
- lines.append(f' put_n(Text.len("{a}")); put_c(32); put_n(Text.eq("{a}", "{b}")); put_c(32);')
- lines.append(f' put_n(Text.starts("{a}", "{b}")); put_c(32); put_n(Text.ends("{a}", "{b}")); put_c(32); put_n(Text.find("{a}", "{b}")); put_c(32);')
- lines.append(f' put_n(Bytes.len(b"{a}")); put_c(32); put_n(Bytes.eq(b"{a}", b"{b}")); put_c(32);')
- lines.append(f' put_n(Bytes.starts(b"{a}", b"{b}")); put_c(32); put_n(Bytes.ends(b"{a}", b"{b}")); put_c(32); put_n(Bytes.find(b"{a}", b"{b}")); put_c(32);')
+ lines.append(f' rt.put_n(std.Text.len("{a}")); rt.put_c(32); rt.put_n(std.Text.eq("{a}", "{b}")); rt.put_c(32);')
+ lines.append(f' rt.put_n(std.Text.starts("{a}", "{b}")); rt.put_c(32); rt.put_n(std.Text.ends("{a}", "{b}")); rt.put_c(32); rt.put_n(std.Text.find("{a}", "{b}")); rt.put_c(32);')
+ lines.append(f' rt.put_n(std.Bytes.len(b"{a}")); rt.put_c(32); rt.put_n(std.Bytes.eq(b"{a}", b"{b}")); rt.put_c(32);')
+ lines.append(f' rt.put_n(std.Bytes.starts(b"{a}", b"{b}")); rt.put_c(32); rt.put_n(std.Bytes.ends(b"{a}", b"{b}")); rt.put_c(32); rt.put_n(std.Bytes.find(b"{a}", b"{b}")); rt.put_c(32);')
library_values += [len(a), int(a == b), int(a.startswith(b)), int(a.endswith(b)), a.find(b), len(a.encode()), int(a == b), int(a.startswith(b)), int(a.endswith(b)), a.find(b)]
lines += [
- " churn(300); gc(); put_n(live_objects() - base); put_c(32);",
- ' char* source = "fn f() { return 7; }"; let parsed = tokens(source);',
- ' put_n(parsed[0] > 0); put_c(32); put_n(eq_w(source, "fn f() { return 7; }")); put_c(32);',
+ " churn(300); rt.gc(); rt.put_n(rt.live_objects() - base); rt.put_c(32);",
+ ' char* source = "fn f() { return 7; }"; let parsed = run.tokens(source);',
+ ' rt.put_n(parsed[0] > 0); rt.put_c(32); rt.put_n(rt.eq_w(source, "fn f() { return 7; }")); rt.put_c(32);',
' char* eval_source = "return 42;"; int e = 0;',
- ' while (e < 16) { let q = tokens(eval_source); put_n(eval(q)); put_c(32); e++; }',
- ' put_n(stable()); put_c(32); put_n(eq_w(eval_source, "return 42;")); put_c(10);',
+ ' while (e < 16) { let q = run.tokens(eval_source); rt.put_n(run.eval(q)); rt.put_c(32); e++; }',
+ ' rt.put_n(stable()); rt.put_c(32); rt.put_n(rt.eq_w(eval_source, "return 42;")); rt.put_c(10);',
" return 0;",
"}",
]
@@ -78,7 +84,7 @@ def run(binary):
hosts = [(zy, [zy]), (zym, [zym])]
if js:
- hosts.append(("javascript", ["node", "src/js/tinyvm.js"]))
+ hosts.append(("javascript", ["node", js]))
outputs = [run(command) for _, command in hosts]
if any(output != outputs[0] for output in outputs[1:]):
raise SystemExit("compiler hosts disagreed on deterministic fuzz output")