Skip to content

Test | Adds sysex insert fuzz test #71

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

Merged
Merged
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
11 changes: 11 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
arbitrary = { version = "1.0", features = [
"derive",
] }
rand = "0.9.1"

[dependencies.midi2]
path = "../midi2"
Expand All @@ -32,3 +36,10 @@ path = "./fuzz_targets/sysex7_payload_roundtrip.rs"
test = false
doc = false
bench = false

[[bin]]
name = "generic_sysex_inserting_payloads"
path = "./fuzz_targets/generic_sysex_inserting_payloads.rs"
test = false
doc = false
bench = false
126 changes: 126 additions & 0 deletions fuzz/fuzz_targets/generic_sysex_inserting_payloads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use midi2::Sysex;
use rand::{Rng, SeedableRng};

struct FixedSizeBuffer<U: midi2::buffer::Unit>(Vec<U>);

impl<U: midi2::buffer::Unit> midi2::buffer::Buffer for FixedSizeBuffer<U> {
type Unit = U;
fn buffer(&self) -> &[Self::Unit] {
&self.0
}
}

impl<U: midi2::buffer::Unit> midi2::buffer::BufferMut for FixedSizeBuffer<U> {
fn buffer_mut(&mut self) -> &mut [Self::Unit] {
&mut self.0
}
}

impl<U: midi2::buffer::Unit> midi2::buffer::BufferTryResize for FixedSizeBuffer<U> {
fn try_resize(&mut self, new_size: usize) -> Result<(), midi2::error::BufferOverflow> {
if new_size > self.0.len() {
return Err(midi2::error::BufferOverflow);
}
Ok(())
}
}

impl<U: midi2::buffer::Unit> FixedSizeBuffer<U> {
fn new(size: usize) -> Self {
Self(std::iter::repeat_n(U::zero(), size).collect())
}
}

#[derive(arbitrary::Arbitrary, Debug)]
struct InputData {
seed: u64,
initial_data: Vec<u8>,
data_to_insert: Vec<u8>,
}

const MAX_BUFFER_SIZE: usize = 1024;

trait IntoByte<B> {
fn byte(&self) -> B;
}

impl IntoByte<midi2::ux::u7> for u8 {
fn byte(&self) -> midi2::ux::u7 {
midi2::num::u7::new(self & 0x7F)
}
}

impl IntoByte<u8> for u8 {
fn byte(&self) -> u8 {
*self
}
}

fn test_case<B, M>(data: &InputData, mut message: M, index: usize)
where
B: midi2::buffer::Buffer + midi2::buffer::BufferTryResize + midi2::buffer::BufferMut,
M: midi2::Sysex<B>,
<M as Sysex<B>>::Byte: Eq + core::fmt::Debug,
u8: IntoByte<<M as Sysex<B>>::Byte>,
{
let Ok(()) = message.try_set_payload(data.initial_data.iter().map(u8::byte)) else {
return;
};

{
let initial = message.payload().collect::<Vec<_>>();
assert_eq!(
initial,
data.initial_data.iter().map(u8::byte).collect::<Vec<_>>()
);
}

let Ok(()) = message.try_insert_payload(data.data_to_insert.iter().map(u8::byte), index) else {
return;
};

let actual = message.payload().collect::<Vec<_>>();
let expected = {
let mut ret = data.initial_data.clone();
ret.splice(index..index, data.data_to_insert.clone());
ret.iter().map(u8::byte).collect::<Vec<_>>()
};
assert_eq!(actual, expected);
}

fuzz_target!(|data: InputData| {
let mut rng = rand::rngs::StdRng::seed_from_u64(data.seed);
let fized_size_buffer_size = rng.random_range(4..MAX_BUFFER_SIZE);
let index = if data.initial_data.is_empty() {
0
} else {
rng.random_range(0..data.initial_data.len())
};
test_case(
&data,
midi2::sysex8::Sysex8::<FixedSizeBuffer<u32>>::try_new_with_buffer(
FixedSizeBuffer::<u32>::new(fized_size_buffer_size),
)
.unwrap(),
index,
);
test_case(
&data,
midi2::sysex7::Sysex7::<FixedSizeBuffer<u32>>::try_new_with_buffer(
FixedSizeBuffer::<u32>::new(fized_size_buffer_size),
)
.unwrap(),
index,
);
test_case(
&data,
midi2::sysex7::Sysex7::<FixedSizeBuffer<u8>>::try_new_with_buffer(
FixedSizeBuffer::<u8>::new(fized_size_buffer_size),
)
.unwrap(),
index,
);
});
4 changes: 2 additions & 2 deletions midi2/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ pub trait Sysex<B: crate::buffer::Buffer> {
where
B: crate::buffer::BufferMut + crate::buffer::BufferResize,
{
self.insert_payload(std::iter::once(byte), self.payload_size());
self.insert_payload(core::iter::once(byte), self.payload_size());
}

/// Pushes the provided byte into the back of the
Expand All @@ -629,7 +629,7 @@ pub trait Sysex<B: crate::buffer::Buffer> {
where
B: crate::buffer::BufferMut + crate::buffer::BufferTryResize,
{
self.try_insert_payload(std::iter::once(byte), self.payload_size())
self.try_insert_payload(core::iter::once(byte), self.payload_size())
}
}

Expand Down