move fuzzing code to lib

This commit is contained in:
nora 2021-12-31 17:31:49 +01:00
parent f222e0cb10
commit 6215924049
8 changed files with 102 additions and 23 deletions

View file

@ -19,13 +19,19 @@ path = ".."
members = ["."]
[[bin]]
name = "lex"
path = "fuzz_targets/lex.rs"
name = "lexer"
path = "fuzz_targets/lexer.rs"
test = false
doc = false
[[bin]]
name = "lex_parse"
path = "fuzz_targets/lex_parse.rs"
name = "parser"
path = "fuzz_targets/parser.rs"
test = false
doc = false
[[bin]]
name = "compiler"
path = "fuzz_targets/compiler.rs"
test = false
doc = false

View file

@ -2,6 +2,5 @@
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: String| {
let lexer = dilaria::Lexer::new(&data);
for _ in lexer {}
dilaria::_fuzz_compile(&data);
});

View file

@ -1,10 +0,0 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: String| {
let ast_alloc = dilaria::Bump::new();
let lexer = dilaria::Lexer::new(&data);
let _ast = dilaria::parse(lexer, &ast_alloc);
});

View file

@ -0,0 +1,6 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: String| {
dilaria::_fuzz_lex(&data);
});

View file

@ -0,0 +1,6 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: String| {
dilaria::_fuzz_parse(&data);
});

View file

@ -64,3 +64,38 @@ fn process_ast(program: &str, ast: Program, mut runtime: RtAlloc) {
Err(err) => errors::display_error(program, err),
}
}
// have the code here and not in the fuzzer, it's easier to find when it breaks like this
#[doc(hidden)]
pub fn _fuzz_compile(program: &str) {
// SAFETY: Just this scope
let mut runtime = unsafe { RtAlloc::new() };
let ast_alloc = Bump::new();
let lexer = lex::Lexer::new(program, &mut runtime);
let ast = parse::parse(lexer, &ast_alloc);
if let Ok(ast) = ast {
let bytecode_alloc = Bump::new();
let _bytecode = compile::compile(&ast, &bytecode_alloc, &mut runtime);
}
}
#[doc(hidden)]
pub fn _fuzz_parse(program: &str) {
// SAFETY: Just this scope
let mut runtime = unsafe { RtAlloc::new() };
let ast_alloc = Bump::new();
let lexer = lex::Lexer::new(program, &mut runtime);
let _ast = parse::parse(lexer, &ast_alloc);
}
#[doc(hidden)]
pub fn _fuzz_lex(program: &str) {
// SAFETY: Just this scope
let mut runtime = unsafe { RtAlloc::new() };
let lexer = lex::Lexer::new(program, &mut runtime);
for _ in lexer {}
}

View file

@ -34,6 +34,9 @@ const fn _check_val_size() {
}
}
const TRUE: Value = Value::Bool(true);
const FALSE: Value = Value::Bool(false);
struct Vm<'bc> {
_blocks: &'bc [FnBlock<'bc>],
current: &'bc FnBlock<'bc>,
@ -100,12 +103,42 @@ impl<'bc> Vm<'bc> {
(Value::Bool(a), Value::Bool(b)) => Ok(Value::Bool(a || b)),
_ => Err("bad type"),
})?,
Instr::CmpGreater => todo!(),
Instr::CmpGreaterEq => todo!(),
Instr::CmpLess => todo!(),
Instr::CmpLessEq => todo!(),
Instr::CmpEq => todo!(),
Instr::CmpNotEq => todo!(),
Instr::CmpGreater => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a > b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a.as_str() > b.as_str())),
_ => Err("bad type"),
})?,
Instr::CmpGreaterEq => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a >= b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a.as_str() >= b.as_str())),
_ => Err("bad type"),
})?,
Instr::CmpLess => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a < b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a.as_str() < b.as_str())),
_ => Err("bad type"),
})?,
Instr::CmpLessEq => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a <= b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a.as_str() <= b.as_str())),
_ => Err("bad type"),
})?,
Instr::CmpEq => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Null, Value::Null) => Ok(TRUE),
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a == b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a == b)),
(Value::Object(_a), Value::Object(_b)) => todo!(),
(Value::Array, Value::Array) => Ok(TRUE),
_ => Err("bad type"),
})?,
Instr::CmpNotEq => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Null, Value::Null) => Ok(FALSE),
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a != b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a != b)),
(Value::Object(_a), Value::Object(_b)) => todo!(),
(Value::Array, Value::Array) => Ok(FALSE),
_ => Err("bad type"),
})?,
Instr::Print => {
let val = self.stack.pop().unwrap();
println!("{}", val);

View file

@ -1 +1,5 @@
print "hi brit";
print "hi";
let is_bigger = "hallo" > "a";
print (not is_bigger) or true;