fuzzgen: Initial SIMD support (#5885)

* fuzzgen: Initial SIMD support

* riscv64: Address PR Feedback

Thanks!
This commit is contained in:
Afonso Bordado
2023-02-28 11:33:11 +00:00
committed by GitHub
parent ae881407cd
commit 480c45b854
5 changed files with 69 additions and 36 deletions

View File

@@ -7,27 +7,39 @@ use cranelift::codegen::isa::CallConv;
use arbitrary::Unstructured;
use cranelift::prelude::{Ieee32, Ieee64};
use target_lexicon::Architecture;
/// A trait for generating random Cranelift datastructures.
pub trait CraneliftArbitrary {
fn _type(&mut self) -> Result<Type>;
fn _type(&mut self, architecture: Architecture) -> Result<Type>;
fn callconv(&mut self) -> Result<CallConv>;
fn abi_param(&mut self) -> Result<AbiParam>;
fn signature(&mut self, max_params: usize, max_rets: usize) -> Result<Signature>;
fn abi_param(&mut self, architecture: Architecture) -> Result<AbiParam>;
fn signature(
&mut self,
architecture: Architecture,
max_params: usize,
max_rets: usize,
) -> Result<Signature>;
fn datavalue(&mut self, ty: Type) -> Result<DataValue>;
}
impl<'a> CraneliftArbitrary for &mut Unstructured<'a> {
fn _type(&mut self) -> Result<Type> {
fn _type(&mut self, architecture: Architecture) -> Result<Type> {
// TODO: It would be nice if we could get these directly from cranelift
let scalars = [
I8, I16, I32, I64, I128, F32, F64,
// R32, R64,
];
// TODO: vector types
// TODO: RISCV does not support SIMD yet
let supports_simd = !matches!(architecture, Architecture::Riscv64(_));
let choices = if supports_simd {
&[
I8, I16, I32, I64, I128, // Scalar Integers
F32, F64, // Scalar Floats
I8X16, I16X8, I32X4, I64X2, // SIMD Integers
F32X4, F64X2, // SIMD Floats
][..]
} else {
&[I8, I16, I32, I64, I128, F32, F64][..]
};
let ty = self.choose(&scalars[..])?;
Ok(*ty)
Ok(*self.choose(choices)?)
}
fn callconv(&mut self) -> Result<CallConv> {
@@ -35,8 +47,8 @@ impl<'a> CraneliftArbitrary for &mut Unstructured<'a> {
Ok(CallConv::SystemV)
}
fn abi_param(&mut self) -> Result<AbiParam> {
let value_type = self._type()?;
fn abi_param(&mut self, architecture: Architecture) -> Result<AbiParam> {
let value_type = self._type(architecture)?;
// TODO: There are more argument purposes to be explored...
let purpose = ArgumentPurpose::Normal;
let extension = if value_type.is_int() {
@@ -56,16 +68,21 @@ impl<'a> CraneliftArbitrary for &mut Unstructured<'a> {
})
}
fn signature(&mut self, max_params: usize, max_rets: usize) -> Result<Signature> {
fn signature(
&mut self,
architecture: Architecture,
max_params: usize,
max_rets: usize,
) -> Result<Signature> {
let callconv = self.callconv()?;
let mut sig = Signature::new(callconv);
for _ in 0..max_params {
sig.params.push(self.abi_param()?);
sig.params.push(self.abi_param(architecture)?);
}
for _ in 0..max_rets {
sig.returns.push(self.abi_param()?);
sig.returns.push(self.abi_param(architecture)?);
}
Ok(sig)
@@ -88,6 +105,9 @@ impl<'a> CraneliftArbitrary for &mut Unstructured<'a> {
// such as Signaling NaN's / NaN's with payload, so generate floats from integers.
F32 => DataValue::F32(Ieee32::with_bits(self.arbitrary::<u32>()?)),
F64 => DataValue::F64(Ieee64::with_bits(self.arbitrary::<u64>()?)),
ty if ty.is_vector() && ty.bits() == 128 => {
DataValue::V128(self.arbitrary::<[u8; 16]>()?)
}
_ => unimplemented!(),
})
}