Skip to content

Commit

Permalink
WIP wasm gc instructions in wasm-smith
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen committed Jan 16, 2024
1 parent f401690 commit 2cf45fc
Show file tree
Hide file tree
Showing 2 changed files with 1,572 additions and 530 deletions.
82 changes: 63 additions & 19 deletions crates/wasm-smith/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,24 @@ impl Module {
Ok(())
}

fn val_type_is_sub_type(&self, a: ValType, b: ValType) -> bool {
match (a, b) {
(a, b) if a == b => true,
(ValType::Ref(a), ValType::Ref(b)) => self.ref_type_is_sub_type(a, b),
_ => false,
}
}

/// Is `a` a subtype of `b`?
fn ref_type_is_sub_type(&self, a: RefType, b: RefType) -> bool {
if a == b {
return true;
}

// TODO FITZGEN: do proper subtype checking
false
}

fn arbitrary_types(&mut self, u: &mut Unstructured) -> Result<()> {
assert!(self.config.min_types <= self.config.max_types);
while self.types.len() < self.config.min_types {
Expand Down Expand Up @@ -601,28 +619,14 @@ impl Module {
}
}

fn arbitrary_matching_ref_type(
&mut self,
u: &mut Unstructured,
ty: RefType,
) -> Result<RefType> {
fn arbitrary_matching_ref_type(&self, u: &mut Unstructured, ty: RefType) -> Result<RefType> {
Ok(RefType {
// TODO: For now, only create allow nullable reference
// types. Eventually we should support non-nullable reference types,
// but this means that we will also need to recognize when it is
// impossible to create an instance of the reference (eg `(ref
// nofunc)` has no instances, and self-referential types that
// contain a non-null self-reference are also impossible to create).
nullable: true,
nullable: ty.nullable,
heap_type: self.arbitrary_matching_heap_type(u, ty.heap_type)?,
})
}

fn arbitrary_matching_heap_type(
&mut self,
u: &mut Unstructured,
ty: HeapType,
) -> Result<HeapType> {
fn arbitrary_matching_heap_type(&self, u: &mut Unstructured, ty: HeapType) -> Result<HeapType> {
use HeapType as HT;
let mut choices = vec![ty];
match ty {
Expand Down Expand Up @@ -716,7 +720,7 @@ impl Module {
}

fn arbitrary_super_type_of_ref_type(
&mut self,
&self,
u: &mut Unstructured,
ty: RefType,
) -> Result<RefType> {
Expand All @@ -733,7 +737,7 @@ impl Module {
}

fn arbitrary_super_type_of_heap_type(
&mut self,
&self,
u: &mut Unstructured,
ty: HeapType,
) -> Result<HeapType> {
Expand Down Expand Up @@ -860,6 +864,45 @@ impl Module {
}
}

fn arbitrary_ref_type(&self, u: &mut Unstructured) -> Result<RefType> {
Ok(RefType {
nullable: true,
heap_type: self.arbitrary_heap_type(u)?,
})
}

fn arbitrary_heap_type(&self, u: &mut Unstructured) -> Result<HeapType> {
assert!(self.config.reference_types_enabled);

if self.config.gc_enabled && !self.types.is_empty() && u.arbitrary()? {
let type_ref_limit = u32::try_from(self.types.len()).unwrap();
let idx = u.int_in_range(0..=type_ref_limit)?;
return Ok(HeapType::Concrete(idx));
}

let mut choices = vec![HeapType::Func, HeapType::Extern];
if self.config.exceptions_enabled {
choices.push(HeapType::Exn);
}
if self.config.gc_enabled {
choices.extend(
[
HeapType::Any,
HeapType::None,
HeapType::NoExtern,
HeapType::NoFunc,
HeapType::Eq,
HeapType::Struct,
HeapType::Array,
HeapType::I31,
]
.iter()
.copied(),
);
}
u.choose(&choices).copied()
}

fn arbitrary_func_type(
&mut self,
u: &mut Unstructured,
Expand Down Expand Up @@ -2212,6 +2255,7 @@ flags! {
Table,
Memory,
Control,
Aggregate,
}
}

Expand Down
Loading

0 comments on commit 2cf45fc

Please sign in to comment.