about summary refs log tree commit diff
path: root/custom_mutators/rust/example
diff options
context:
space:
mode:
authorjulihoh <julihoh@users.noreply.github.com>2021-02-27 15:05:13 +0100
committerGitHub <noreply@github.com>2021-02-27 15:05:13 +0100
commita5da9ce42cab1eab93cf80ca744944ae26e6ab58 (patch)
tree1292da64c63078318d789b2248c247d59173abf2 /custom_mutators/rust/example
parent79e02c2a9b2532624d7f187783ccbe9cbb04ce9b (diff)
downloadafl++-a5da9ce42cab1eab93cf80ca744944ae26e6ab58.tar.gz
custom mutator rust support (#752)
* custom mutator rust support

* clarify how to view documentation for rust mutators

* remove `FuzzResult` hack and clarify lifetimes of CustomMutator::fuzz

* rename TErr associated tyep to Error to be more idiomatic

* fix warnings

* add example for fallible custom mutator

* make Fallible Custom Mutator the default and implement it's handle_err method by default

* rename CustomMutator::handle_err to handle_error

* add example mutator using lain
Diffstat (limited to 'custom_mutators/rust/example')
-rw-r--r--custom_mutators/rust/example/Cargo.toml13
-rw-r--r--custom_mutators/rust/example/src/lib.rs50
2 files changed, 63 insertions, 0 deletions
diff --git a/custom_mutators/rust/example/Cargo.toml b/custom_mutators/rust/example/Cargo.toml
new file mode 100644
index 00000000..0c89b200
--- /dev/null
+++ b/custom_mutators/rust/example/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "example"
+version = "0.1.0"
+authors = ["Julius Hohnerlein <julihoh@users.noreply.github.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+custom_mutator = { path = "../custom_mutator" }
+
+[lib]
+crate-type = ["cdylib"]
\ No newline at end of file
diff --git a/custom_mutators/rust/example/src/lib.rs b/custom_mutators/rust/example/src/lib.rs
new file mode 100644
index 00000000..4f9345c0
--- /dev/null
+++ b/custom_mutators/rust/example/src/lib.rs
@@ -0,0 +1,50 @@
+#![allow(unused_variables)]
+
+use custom_mutator::{export_mutator, CustomMutator};
+use std::os::raw::c_uint;
+
+struct ExampleMutator;
+
+impl CustomMutator for ExampleMutator {
+    type Error = ();
+
+    fn init(seed: c_uint) -> Result<Self, ()> {
+        Ok(Self)
+    }
+
+    fn fuzz<'b, 's: 'b>(
+        &'s mut self,
+        buffer: &'b mut [u8],
+        add_buff: Option<&[u8]>,
+        max_size: usize,
+    ) -> Result<Option<&'b [u8]>, ()> {
+        buffer.reverse();
+        Ok(Some(buffer))
+    }
+}
+
+struct OwnBufferExampleMutator {
+    own_buffer: Vec<u8>,
+}
+
+impl CustomMutator for OwnBufferExampleMutator {
+    type Error = ();
+
+    fn init(seed: c_uint) -> Result<Self, ()> {
+        Ok(Self {
+            own_buffer: Vec::new(),
+        })
+    }
+
+    fn fuzz<'b, 's: 'b>(
+        &'s mut self,
+        buffer: &'b mut [u8],
+        add_buff: Option<&[u8]>,
+        max_size: usize,
+    ) -> Result<Option<&'b [u8]>, ()> {
+        self.own_buffer.reverse();
+        Ok(Some(self.own_buffer.as_slice()))
+    }
+}
+
+export_mutator!(ExampleMutator);