-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
afl-fuzz fuzzes a single instruction
- Loading branch information
Showing
26 changed files
with
520 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
in | ||
out |
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "afl-fuzz" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
afl = "*" | ||
arbitrary = "*" | ||
|
||
[dependencies.vm2] | ||
path = ".." | ||
features = ["single_instruction_test"] | ||
|
||
[[bin]] | ||
name = "show_testcase" | ||
path = "src/show_testcase.rs" |
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
cargo afl build --release && cargo afl fuzz -i in -o out ../target/release/afl-fuzz -g 10k |
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use arbitrary::Arbitrary; | ||
use vm2::{MockWorld, VirtualMachine}; | ||
|
||
fn main() { | ||
afl::fuzz!(|data: &[u8]| { | ||
if let Ok(VmAndWorld { mut vm, mut world }) = arbitrary::Unstructured::new(data).arbitrary() | ||
{ | ||
let instruction = vm.get_first_instruction(); | ||
let result = vm.run_single_instruction(instruction, &mut world); | ||
} | ||
}); | ||
} | ||
|
||
#[derive(Arbitrary, Debug)] | ||
struct VmAndWorld { | ||
vm: VirtualMachine, | ||
world: MockWorld, | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::env; | ||
use std::fs; | ||
|
||
use arbitrary::Arbitrary; | ||
use vm2::MockWorld; | ||
use vm2::VirtualMachine; | ||
|
||
fn main() { | ||
let filename = env::args() | ||
.nth(1) | ||
.expect("Please provide the test case to show as argument."); | ||
|
||
let bytes = fs::read(filename).expect("Failed to read file"); | ||
|
||
let VmAndWorld { mut vm, mut world } = | ||
arbitrary::Unstructured::new(&bytes).arbitrary().unwrap(); | ||
|
||
println!("{:?}", vm.state); | ||
|
||
let instruction = vm.get_first_instruction(); | ||
vm.run_single_instruction(instruction, &mut world); | ||
} | ||
|
||
#[derive(Arbitrary, Debug)] | ||
struct VmAndWorld { | ||
vm: VirtualMachine, | ||
world: MockWorld, | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::{callframe::Callframe, predication::Flags, WorldDiff}; | ||
use arbitrary::Arbitrary; | ||
|
||
impl<'a> Arbitrary<'a> for Flags { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
Ok(Self::new(u.arbitrary()?, u.arbitrary()?, u.arbitrary()?)) | ||
} | ||
} | ||
|
||
impl<'a> Arbitrary<'a> for Callframe { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
Ok(Self { | ||
address: u.arbitrary()?, | ||
code_address: u.arbitrary()?, | ||
caller: u.arbitrary()?, | ||
exception_handler: u.arbitrary()?, | ||
context_u128: u.arbitrary()?, | ||
is_static: u.arbitrary()?, | ||
stack: u.arbitrary()?, | ||
sp: u.arbitrary()?, | ||
gas: u.arbitrary()?, | ||
stipend: u.arbitrary()?, | ||
near_calls: vec![], // TODO | ||
program: u.arbitrary()?, | ||
heap: u.arbitrary()?, | ||
aux_heap: u.arbitrary()?, | ||
heap_size: u.arbitrary()?, | ||
aux_heap_size: u.arbitrary()?, | ||
calldata_heap: u.arbitrary()?, | ||
heaps_i_am_keeping_alive: vec![], // TODO | ||
world_before_this_frame: WorldDiff::default().snapshot(), // TODO | ||
}) | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use super::mock_array::MockRead; | ||
use arbitrary::Arbitrary; | ||
use std::ops::{Index, IndexMut}; | ||
|
||
//#[derive(Debug, Clone)] | ||
type Heap = Vec<u8>; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Heaps { | ||
read: MockRead<HeapId, Heap>, | ||
} | ||
|
||
impl<'a> Arbitrary<'a> for Heaps { | ||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
Ok(Self { | ||
read: MockRead::new(vec![u.arbitrary()?; 1]), | ||
}) | ||
} | ||
} | ||
|
||
pub(crate) const CALLDATA_HEAP: HeapId = HeapId(1); | ||
pub const FIRST_HEAP: HeapId = HeapId(2); | ||
pub(crate) const FIRST_AUX_HEAP: HeapId = HeapId(3); | ||
|
||
impl Heaps { | ||
pub(crate) fn new(_: Vec<u8>) -> Self { | ||
unimplemented!("Should use arbitrary heap, not fresh heap in testing.") | ||
} | ||
|
||
pub(crate) fn allocate(&mut self) -> HeapId { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn deallocate(&mut self, _: HeapId) {} | ||
} | ||
|
||
impl Index<HeapId> for Heaps { | ||
type Output = Heap; | ||
|
||
fn index(&self, index: HeapId) -> &Self::Output { | ||
&self.read.get(index) | ||
} | ||
} | ||
|
||
impl IndexMut<HeapId> for Heaps { | ||
fn index_mut(&mut self, index: HeapId) -> &mut Self::Output { | ||
self.read.get_mut(index) | ||
} | ||
} | ||
|
||
impl PartialEq for Heaps { | ||
fn eq(&self, _: &Self) -> bool { | ||
false | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, PartialEq, Debug, Arbitrary)] | ||
pub struct HeapId(u32); | ||
|
||
impl HeapId { | ||
/// Only for dealing with external data structures, never use internally. | ||
pub fn from_u32_unchecked(value: u32) -> Self { | ||
Self(value) | ||
} | ||
|
||
pub fn to_u32(self) -> u32 { | ||
self.0 | ||
} | ||
} |
Oops, something went wrong.