From 11f0b003eb8b7ebf88c82ed7f63b6d0c21cdfc5b Mon Sep 17 00:00:00 2001 From: Afonso Bordado Date: Wed, 10 Aug 2022 22:17:11 +0100 Subject: [PATCH] cranelift: Build a runtest case from fuzzer TestCase's (#4590) * cranelift: Build a runtest case from fuzzer TestCase's * cranelift: Add a default expected output for a fuzzgen case --- cranelift/fuzzgen/src/lib.rs | 55 +++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/cranelift/fuzzgen/src/lib.rs b/cranelift/fuzzgen/src/lib.rs index 6e5b138dc3b5..26151697a2eb 100644 --- a/cranelift/fuzzgen/src/lib.rs +++ b/cranelift/fuzzgen/src/lib.rs @@ -8,13 +8,13 @@ use cranelift::codegen::ir::Function; use cranelift::codegen::Context; use cranelift::prelude::*; use cranelift_native::builder_with_options; +use std::fmt; mod config; mod function_generator; pub type TestCaseInput = Vec; -#[derive(Debug)] pub struct TestCase { pub func: Function, /// Generate multiple test inputs for each test case. @@ -22,6 +22,59 @@ pub struct TestCase { pub inputs: Vec, } +impl fmt::Debug for TestCase { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + r#";; Fuzzgen test case + +test interpret +test run +set enable_llvm_abi_extensions +target aarch64 +target s390x +target x86_64 + +"# + )?; + + writeln!(f, "{}", self.func)?; + + writeln!(f, "; Note: the results in the below test cases are simply a placeholder and probably will be wrong\n")?; + + for input in self.inputs.iter() { + // TODO: We don't know the expected outputs, maybe we can run the interpreter + // here to figure them out? Should work, however we need to be careful to catch + // panics in case its the interpreter that is failing. + // For now create a placeholder output consisting of the zero value for the type + let returns = &self.func.signature.returns; + let placeholder_output = returns + .iter() + .map(|param| DataValue::read_from_slice(&[0; 16][..], param.value_type)) + .map(|val| format!("{}", val)) + .collect::>() + .join(", "); + + // If we have no output, we don't need the == condition + let test_condition = match returns.len() { + 0 => String::new(), + 1 => format!(" == {}", placeholder_output), + _ => format!(" == [{}]", placeholder_output), + }; + + let args = input + .iter() + .map(|val| format!("{}", val)) + .collect::>() + .join(", "); + + writeln!(f, "; run: {}({}){}", self.func.name, args, test_condition)?; + } + + Ok(()) + } +} + impl<'a> Arbitrary<'a> for TestCase { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { FuzzGen::new(u)