Skip to content

feat(smith): new mutation-based approach for fuzzing #842

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

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/apollo-smith/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ arbitrary = { version = "1.3.0", features = ["derive"] }
indexmap = "2.0.0"
once_cell = "1.9.0"
thiserror = "1.0.37"
paste = "1.0.0"
uuid = { version = "1.7.0", features = ["v4"] }

[dev-dependencies]
expect-test = "1.4"
1 change: 1 addition & 0 deletions crates/apollo-smith/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) mod input_object;
pub(crate) mod input_value;
pub(crate) mod interface;
pub(crate) mod name;
pub mod next;
pub(crate) mod object;
pub(crate) mod operation;
pub(crate) mod scalar;
Expand Down
2 changes: 2 additions & 0 deletions crates/apollo-smith/src/next/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Apollo-Smith Next
The next folder ****
83 changes: 83 additions & 0 deletions crates/apollo-smith/src/next/ast/definition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use arbitrary::Unstructured;

use apollo_compiler::ast::{Definition, InputObjectTypeDefinition, Name, Type};

pub(crate) trait DefinitionExt {
fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type>;
}

impl DefinitionExt for Definition {
fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type> {
let name = self.name().expect("definition must have a name").clone();
Ok(ty(u, name)?)
}
}

impl DefinitionExt for InputObjectTypeDefinition {
fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type> {
Ok(ty(u, self.name.clone())?)
}
}

fn ty(u: &mut Unstructured, name: Name) -> arbitrary::Result<Type> {
let mut ty = if u.arbitrary()? {
Type::Named(name)
} else {
Type::NonNullNamed(name)
};

for _ in 0..u.int_in_range(0..=5)? {
if u.arbitrary()? {
ty = Type::List(Box::new(ty))
} else {
ty = Type::NonNullList(Box::new(ty))
};
}
Ok(ty)
}

#[derive(Debug)]
pub(crate) enum DefinitionKind {
OperationDefinition,
FragmentDefinition,
DirectiveDefinition,
SchemaDefinition,
ScalarTypeDefinition,
ObjectTypeDefinition,
InterfaceTypeDefinition,
UnionTypeDefinition,
EnumTypeDefinition,
InputObjectTypeDefinition,
SchemaExtension,
ScalarTypeExtension,
ObjectTypeExtension,
InterfaceTypeExtension,
UnionTypeExtension,
EnumTypeExtension,
InputObjectTypeExtension,
}

impl DefinitionKind {
pub(crate) fn matches(&self, definition: &Definition) -> bool {
match (self, definition) {
(Self::OperationDefinition, Definition::OperationDefinition(_)) => true,
(Self::FragmentDefinition, Definition::FragmentDefinition(_)) => true,
(Self::DirectiveDefinition, Definition::DirectiveDefinition(_)) => true,
(Self::SchemaDefinition, Definition::SchemaDefinition(_)) => true,
(Self::ScalarTypeDefinition, Definition::ScalarTypeDefinition(_)) => true,
(Self::ObjectTypeDefinition, Definition::ObjectTypeDefinition(_)) => true,
(Self::InterfaceTypeDefinition, Definition::InterfaceTypeDefinition(_)) => true,
(Self::UnionTypeDefinition, Definition::UnionTypeDefinition(_)) => true,
(Self::EnumTypeDefinition, Definition::EnumTypeDefinition(_)) => true,
(Self::InputObjectTypeDefinition, Definition::InputObjectTypeDefinition(_)) => true,
(Self::SchemaExtension, Definition::SchemaExtension(_)) => true,
(Self::ScalarTypeExtension, Definition::ScalarTypeExtension(_)) => true,
(Self::ObjectTypeExtension, Definition::ObjectTypeExtension(_)) => true,
(Self::InterfaceTypeExtension, Definition::InterfaceTypeExtension(_)) => true,
(Self::UnionTypeExtension, Definition::UnionTypeExtension(_)) => true,
(Self::EnumTypeExtension, Definition::EnumTypeExtension(_)) => true,
(Self::InputObjectTypeExtension, Definition::InputObjectTypeExtension(_)) => true,
_ => false,
}
}
}
73 changes: 73 additions & 0 deletions crates/apollo-smith/src/next/ast/directive_definition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::ops::Deref;

use apollo_compiler::ast::{
Argument, Directive, DirectiveDefinition, DirectiveList, DirectiveLocation,
};
use apollo_compiler::{Node, Schema};

use crate::next::unstructured::Unstructured;

pub(crate) struct LocationFilter<I>(I, DirectiveLocation);

impl<'a, T> Iterator for LocationFilter<T>
where
T: Iterator<Item = &'a Node<DirectiveDefinition>>,
{
type Item = &'a Node<DirectiveDefinition>;

fn next(&mut self) -> Option<Self::Item> {
self.0.find(|d| d.locations.contains(&self.1))
}
}

pub(crate) trait DirectiveDefinitionIterExt {
fn with_location<'a>(self, location: DirectiveLocation) -> LocationFilter<Self>
where
Self: Iterator<Item = &'a Node<DirectiveDefinition>> + Sized;

fn try_collect<'a>(
self,
u: &mut Unstructured,
schema: &Schema,
) -> arbitrary::Result<DirectiveList>
where
Self: Iterator<Item = &'a Node<DirectiveDefinition>> + Sized;
}

impl<I: ?Sized> DirectiveDefinitionIterExt for I {
fn with_location<'a>(self, location: DirectiveLocation) -> LocationFilter<Self>
where
I: Iterator<Item = &'a Node<DirectiveDefinition>>,
Self: Sized,
{
LocationFilter(self, location)
}

fn try_collect<'a>(
mut self,
u: &mut Unstructured,
schema: &Schema,
) -> arbitrary::Result<DirectiveList>
where
Self: Iterator<Item = &'a Node<DirectiveDefinition>> + Sized,
{
let mut directives = DirectiveList::new();
while let Some(d) = self.next() {
let mut arguments = Vec::new();
for arg in &d.arguments {
if arg.is_required() || u.arbitrary()? {
arguments.push(Node::new(Argument {
name: arg.name.clone(),
value: Node::new(u.arbitrary_value(schema, arg.ty.deref())?),
}))
}
}

directives.push(Node::new(Directive {
name: d.name.clone(),
arguments,
}))
}
Ok(directives)
}
}
157 changes: 157 additions & 0 deletions crates/apollo-smith/src/next/ast/document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use paste::paste;

use crate::next::ast::definition::DefinitionKind;
use apollo_compiler::ast::{
Definition, DirectiveDefinition, Document, EnumTypeDefinition, EnumTypeExtension,
FragmentDefinition, InputObjectTypeDefinition, InputObjectTypeExtension,
InterfaceTypeDefinition, InterfaceTypeExtension, ObjectTypeDefinition, ObjectTypeExtension,
OperationDefinition, ScalarTypeDefinition, ScalarTypeExtension, SchemaDefinition,
SchemaExtension, UnionTypeDefinition, UnionTypeExtension,
};
use apollo_compiler::Node;

use crate::next::unstructured::Unstructured;

/// Macro to create accessors for definitions
macro_rules! access {
($ty: ty) => {
paste! {
fn [<random_ $ty:snake>](
&self,
u: &mut Unstructured,
) -> arbitrary::Result<Option<&Node<$ty>>> {
let mut existing = self
.target()
.definitions
.iter()
.filter_map(|d| {
if let Definition::$ty(definition) = d {
Some(definition)
} else {
None
}
})
.collect::<Vec<_>>();
match u.choose_index(existing.len()) {
Ok(idx)=> Ok(Some(existing.remove(idx))),
Err(arbitrary::Error::EmptyChoose)=> Ok(None),
Err(e)=> Err(e)
}

}

fn [<random_ $ty:snake _mut>](
&mut self,
u: &mut Unstructured,
) -> arbitrary::Result<Option<&mut Node<$ty>>> {
let mut existing = self
.target_mut()
.definitions
.iter_mut()
.filter_map(|d| {
if let Definition::$ty(definition) = d {
Some(definition)
} else {
None
}
})
.collect::<Vec<_>>();

match u.choose_index(existing.len()) {
Ok(idx)=> Ok(Some(existing.remove(idx))),
Err(arbitrary::Error::EmptyChoose)=> Ok(None),
Err(e)=> Err(e)
}
}

fn [<sample_ $ty:snake s>](
&self,
u: &mut Unstructured,
) -> arbitrary::Result<Vec<&Node<$ty>>> {
let existing = self
.target()
.definitions
.iter()
.filter_map(|d| {
if let Definition::$ty(definition) = d {
Some(definition)
} else {
None
}
})
.filter(|_| u.arbitrary().unwrap_or(false))
.collect::<Vec<_>>();

Ok(existing)
}
}
};
}

pub(crate) trait DocumentExt {
access!(OperationDefinition);
access!(FragmentDefinition);
access!(DirectiveDefinition);
access!(SchemaDefinition);
access!(ScalarTypeDefinition);
access!(ObjectTypeDefinition);
access!(InterfaceTypeDefinition);
access!(UnionTypeDefinition);
access!(EnumTypeDefinition);
access!(InputObjectTypeDefinition);
access!(SchemaExtension);
access!(ScalarTypeExtension);
access!(ObjectTypeExtension);
access!(InterfaceTypeExtension);
access!(UnionTypeExtension);
access!(EnumTypeExtension);
access!(InputObjectTypeExtension);

fn random_definition(
&self,
u: &mut Unstructured,
definitions: Vec<DefinitionKind>,
) -> arbitrary::Result<Option<&Definition>> {
let mut existing = self
.target()
.definitions
.iter()
.filter(|d| definitions.iter().any(|t| t.matches(*d)))
.collect::<Vec<_>>();
match u.choose_index(existing.len()) {
Ok(idx) => Ok(Some(existing.remove(idx))),
Err(arbitrary::Error::EmptyChoose) => Ok(None),
Err(e) => Err(e),
}
}

fn random_definition_mut(
&mut self,
u: &mut Unstructured,
definitions: Vec<DefinitionKind>,
) -> arbitrary::Result<Option<&mut Definition>> {
let mut existing = self
.target_mut()
.definitions
.iter_mut()
.filter(|d| definitions.iter().any(|t| t.matches(*d)))
.collect::<Vec<_>>();
match u.choose_index(existing.len()) {
Ok(idx) => Ok(Some(existing.remove(idx))),
Err(arbitrary::Error::EmptyChoose) => Ok(None),
Err(e) => Err(e),
}
}

fn target(&self) -> &Document;
fn target_mut(&mut self) -> &mut Document;
}

impl DocumentExt for Document {
fn target(&self) -> &Document {
self
}
fn target_mut(&mut self) -> &mut Document {
self
}
}
Loading