-
Notifications
You must be signed in to change notification settings - Fork 1.7k
gc_fuzz: Struct fields #13101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khagankhan
wants to merge
7
commits into
bytecodealliance:main
Choose a base branch
from
khagankhan:struct-fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
gc_fuzz: Struct fields #13101
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec13434
Add struct fields: POD and externref
khagankhan 7c6e97d
Merge branch 'bytecodealliance:main' into struct-fields
khagankhan 26e7151
Merge branch 'bytecodealliance:main' into struct-fields
khagankhan 07ef78a
Merge branch 'bytecodealliance:main' into struct-fields
khagankhan b0c2f33
Update .expect() for try_from for all
khagankhan 552dd9f
Remove special case for 'externref'
khagankhan cb0b112
Merge branch 'bytecodealliance:main' into struct-fields
khagankhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| //! Mutators for the `gc` operations. | ||
| use crate::generators::gc_ops::limits::GcOpsLimits; | ||
| use crate::generators::gc_ops::ops::{GcOp, GcOps}; | ||
| use crate::generators::gc_ops::types::{TypeId, Types}; | ||
| use crate::generators::gc_ops::types::{CompositeType, FieldType, StructField, TypeId, Types}; | ||
| use mutatis::{ | ||
| Candidates, Context, DefaultMutate, Generate, Mutate, Result as MutResult, mutators as m, | ||
| }; | ||
|
|
@@ -54,8 +54,9 @@ impl TypesMutator { | |
| } else { | ||
| None | ||
| }; | ||
| types.insert_empty_struct(tid, gid, is_final, supertype); | ||
| log::debug!("Added empty struct {tid:?} to rec group {gid:?}"); | ||
| // Add struct with no fields; fields can be added later by `mutate_struct_fields`. | ||
| types.insert_struct(tid, gid, is_final, supertype, Vec::new()); | ||
| log::debug!("Added struct {tid:?} to rec group {gid:?}"); | ||
| Ok(()) | ||
| })?; | ||
| Ok(()) | ||
|
|
@@ -218,16 +219,17 @@ impl TypesMutator { | |
| return Ok(()); | ||
| } | ||
|
|
||
| // Collect (TypeId, is_final, supertype) for members of the source group. | ||
| let members: SmallVec<[(TypeId, bool, Option<TypeId>); 32]> = src_members | ||
| .iter() | ||
| .filter_map(|tid| { | ||
| types | ||
| .type_defs | ||
| .get(tid) | ||
| .map(|def| (*tid, def.is_final, def.supertype)) | ||
| }) | ||
| .collect(); | ||
| // Collect (TypeId, is_final, supertype, fields) for members of the source group. | ||
| let members: SmallVec<[(TypeId, bool, Option<TypeId>, Vec<StructField>); 32]> = | ||
| src_members | ||
| .iter() | ||
| .filter_map(|tid| { | ||
| types.type_defs.get(tid).map(|def| { | ||
| let CompositeType::Struct(ref st) = def.composite_type; | ||
| (*tid, def.is_final, def.supertype, st.fields.clone()) | ||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| if members.is_empty() { | ||
| return Ok(()); | ||
|
|
@@ -239,15 +241,15 @@ impl TypesMutator { | |
|
|
||
| // Allocate fresh type ids for each member and build old-to-new map. | ||
| let mut old_to_new: BTreeMap<TypeId, TypeId> = BTreeMap::new(); | ||
| for (old_tid, _, _) in &members { | ||
| for (old_tid, _, _, _) in &members { | ||
| old_to_new.insert(*old_tid, types.fresh_type_id(ctx.rng())); | ||
| } | ||
|
|
||
| // Insert duplicated defs, rewriting intra-group supertype edges to cloned ids. | ||
| for (old_tid, is_final, supertype) in &members { | ||
| for (old_tid, is_final, supertype, fields) in &members { | ||
| let new_tid = old_to_new[old_tid]; | ||
| let mapped_super = supertype.map(|st| *old_to_new.get(&st).unwrap_or(&st)); | ||
| types.insert_empty_struct(new_tid, new_gid, *is_final, mapped_super); | ||
| types.insert_struct(new_tid, new_gid, *is_final, mapped_super, fields.clone()); | ||
| } | ||
|
|
||
| log::debug!( | ||
|
|
@@ -390,6 +392,19 @@ impl TypesMutator { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Mutate struct fields (add/remove/modify via `m::vec`). | ||
| fn mutate_struct_fields( | ||
| &mut self, | ||
| c: &mut Candidates<'_>, | ||
| types: &mut Types, | ||
| ) -> mutatis::Result<()> { | ||
| for (_, def) in types.type_defs.iter_mut() { | ||
| let CompositeType::Struct(ref mut st) = def.composite_type; | ||
| m::vec(StructFieldMutator).mutate(c, &mut st.fields)?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Run all type / rec-group mutations. [`GcOpsLimits`] come from [`GcOps`]. | ||
| fn mutate_with_limits( | ||
| &mut self, | ||
|
|
@@ -405,10 +420,51 @@ impl TypesMutator { | |
| self.remove_group(c, types)?; | ||
| self.merge_groups(c, types)?; | ||
| self.split_group(c, types, limits)?; | ||
| self.mutate_struct_fields(c, types)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Mutator for [`StructField`]: used by `m::vec` to add, remove, and | ||
| /// modify fields within a struct type. | ||
| #[derive(Debug, Default)] | ||
| pub struct StructFieldMutator; | ||
|
|
||
| impl Mutate<StructField> for StructFieldMutator { | ||
| fn mutate(&mut self, c: &mut Candidates<'_>, field: &mut StructField) -> MutResult<()> { | ||
| c.mutation(|ctx| { | ||
| let old = format!("{field:?}"); | ||
| let idx = usize::try_from(ctx.rng().gen_u32()) | ||
| .expect("failed to convert rng output to usize") | ||
| % FieldType::ALL.len(); | ||
| field.field_type = FieldType::ALL[idx]; | ||
| field.mutable = (ctx.rng().gen_u32() % 2) == 0; | ||
| log::debug!("Mutated field {old} -> {field:?}"); | ||
| Ok(()) | ||
| })?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl Generate<StructField> for StructFieldMutator { | ||
| fn generate(&mut self, ctx: &mut Context) -> MutResult<StructField> { | ||
| let idx = usize::try_from(ctx.rng().gen_u32()) | ||
| .expect("failed to convert rng output to usize") | ||
| % FieldType::ALL.len(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this pattern is probably a little more straight forward as: let idx = ctx.rng().gen_index(FieldType::ALL.len());
let idx = u32::try_from(idx).unwrap();And maybe even better deduplicated as a |
||
| let field = StructField { | ||
| field_type: FieldType::ALL[idx], | ||
| mutable: (ctx.rng().gen_u32() % 2) == 0, | ||
| }; | ||
| log::debug!("Generated field {field:?}"); | ||
| Ok(field) | ||
| } | ||
| } | ||
|
|
||
| impl DefaultMutate for StructField { | ||
| type DefaultMutate = StructFieldMutator; | ||
| } | ||
|
|
||
| /// Mutator for [`GcOps`]. | ||
| /// | ||
| /// Also implements [`Mutate`] / [`Generate`] for [`GcOp`] so `m::vec` can mutate | ||
|
|
@@ -465,7 +521,7 @@ impl Generate<GcOps> for GcOpsMutator { | |
| let mut ops = GcOps::default(); | ||
| let mut session = mutatis::Session::new(); | ||
|
|
||
| for _ in 0..64 { | ||
| for _ in 0..2048 { | ||
| session.mutate(&mut ops)?; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can just
.unwrap():usize::try_from(my_u32).unwrap()is common enough that it doesn't need a diagnostic message and it also shouldn't ever fail on our supported architectures anyway. Same with the other instances of this here.