peepmatic: Introduce the peepmatic-fuzzing crate
This crate contains oracles, generators, and fuzz targets for use with fuzzing engines (e.g. libFuzzer). This doesn't contain the actual `libfuzzer_sys::fuzz_target!` definitions (those are in the `peepmatic-fuzz` crate) but does those definitions are one liners calling out to functions defined in this crate.
This commit is contained in:
71
cranelift/peepmatic/crates/fuzzing/src/compile.rs
Normal file
71
cranelift/peepmatic/crates/fuzzing/src/compile.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
//! Fuzz testing utilities related to AST pattern matching.
|
||||
|
||||
use peepmatic_runtime::PeepholeOptimizations;
|
||||
use std::path::Path;
|
||||
use std::str;
|
||||
|
||||
/// Attempt to interpret the given bytes as UTF-8 and then compile them as if
|
||||
/// they were source text of our DSL.
|
||||
pub fn compile(data: &[u8]) {
|
||||
let source = match str::from_utf8(data) {
|
||||
Err(_) => return,
|
||||
Ok(s) => s,
|
||||
};
|
||||
|
||||
let opt = match peepmatic::compile_str(source, Path::new("fuzz")) {
|
||||
Err(_) => return,
|
||||
Ok(o) => o,
|
||||
};
|
||||
|
||||
// Should be able to serialize and deserialize the peephole optimizer.
|
||||
let opt_bytes = bincode::serialize(&opt).expect("should serialize peephole optimizations OK");
|
||||
let _: PeepholeOptimizations =
|
||||
bincode::deserialize(&opt_bytes).expect("should deserialize peephole optimizations OK");
|
||||
|
||||
// Compiling the same source text again should be deterministic.
|
||||
let opt2 = peepmatic::compile_str(source, Path::new("fuzz"))
|
||||
.expect("should be able to compile source text again, if it compiled OK the first time");
|
||||
let opt2_bytes =
|
||||
bincode::serialize(&opt2).expect("should serialize second peephole optimizations OK");
|
||||
assert_eq!(opt_bytes, opt2_bytes);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn check_compile() {
|
||||
crate::check(|s: String| compile(s.as_bytes()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regression_0() {
|
||||
compile(
|
||||
b"
|
||||
(=> (bor (bor $x $y) $y) $x)
|
||||
(=> (bor (bor $x $z) $y) $x)
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regression_1() {
|
||||
compile(
|
||||
b"
|
||||
(=> (bor (bor $x $y) 0) $x)
|
||||
(=> (bor $x 0) $x)
|
||||
(=> (bor $y $x) $x)
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regression_2() {
|
||||
compile(
|
||||
b"
|
||||
(=> (sshr $x 11111111110) $x)
|
||||
",
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user