Files
wasmtime/cranelift/peepmatic/crates/fuzzing/src/compile.rs
Nick Fitzgerald ee5982fd16 peepmatic: Be generic over the operator type
This lets us avoid the cost of `cranelift_codegen::ir::Opcode` to
`peepmatic_runtime::Operator` conversion overhead, and paves the way for
allowing Peepmatic to support non-clif optimizations (e.g. vcode optimizations).

Rather than defining our own `peepmatic::Operator` type like we used to, now the
whole `peepmatic` crate is effectively generic over a `TOperator` type
parameter. For the Cranelift integration, we use `cranelift_codegen::ir::Opcode`
as the concrete type for our `TOperator` type parameter. For testing, we also
define a `TestOperator` type, so that we can test Peepmatic code without
building all of Cranelift, and we can keep them somewhat isolated from each
other.

The methods that `peepmatic::Operator` had are now translated into trait bounds
on the `TOperator` type. These traits need to be shared between all of
`peepmatic`, `peepmatic-runtime`, and `cranelift-codegen`'s Peepmatic
integration. Therefore, these new traits live in a new crate:
`peepmatic-traits`. This crate acts as a header file of sorts for shared
trait/type/macro definitions.

Additionally, the `peepmatic-runtime` crate no longer depends on the
`peepmatic-macro` procedural macro crate, which should lead to faster build
times for Cranelift when it is using pre-built peephole optimizers.
2020-07-17 16:16:49 -07:00

80 lines
2.1 KiB
Rust

//! Fuzz testing utilities related to AST pattern matching.
use peepmatic_runtime::PeepholeOptimizations;
use peepmatic_test_operator::TestOperator;
use std::path::Path;
use std::str;
// To avoid timeouts, don't deal with inputs larger than this.
const MAX_LEN: usize = 2048;
/// 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]) {
if data.len() > MAX_LEN {
return;
}
let source = match str::from_utf8(data) {
Err(_) => return,
Ok(s) => s,
};
let opt = match peepmatic::compile_str::<TestOperator>(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<TestOperator> =
bincode::deserialize(&opt_bytes).expect("should deserialize peephole optimizations OK");
// Compiling the same source text again should be deterministic.
let opt2 = peepmatic::compile_str::<TestOperator>(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)
",
);
}
}