Skip to content
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

Dearbitrary #187

Draft
wants to merge 7 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
7 changes: 7 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ jobs:
run: cargo test --verbose --all-features --all
- name: Build Examples
run: cargo build --examples --all-features --all
kani:
name: Kani Verification
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: rustup update
- uses: model-checking/kani-github-action
clippy:
name: Clippy
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ members = ["./fuzz"]

[dev-dependencies]
exhaustigen = "0.1.0"

[target.'cfg(kani)'.dependencies]
paste = "1.0"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kani)'] }
28 changes: 14 additions & 14 deletions fuzz/fuzz_targets/int_in_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ fn fuzz(data: &[u8]) -> Result<()> {
let mut u = Unstructured::new(data);

let choices = [
assert_in_range::<u8>,
assert_in_range::<i8>,
assert_in_range::<u16>,
assert_in_range::<i16>,
assert_in_range::<u32>,
assert_in_range::<i32>,
assert_in_range::<u64>,
assert_in_range::<i64>,
assert_in_range::<u64>,
assert_in_range::<i64>,
assert_in_range::<u128>,
assert_in_range::<i128>,
assert_in_range::<u8, 1>,
assert_in_range::<i8, 1>,
assert_in_range::<u16, 2>,
assert_in_range::<i16, 2>,
assert_in_range::<u32, 4>,
assert_in_range::<i32, 4>,
assert_in_range::<u64, 8>,
assert_in_range::<i64, 8>,
assert_in_range::<u64, 8>,
assert_in_range::<i64, 8>,
assert_in_range::<u128, 16>,
assert_in_range::<i128, 16>,
];

let f = u.choose(&choices[..])?;
f(&mut u)
}

fn assert_in_range<'a, 'b, T>(u: &'a mut Unstructured<'b>) -> Result<()>
fn assert_in_range<'a, 'b, T, const BYTES: usize>(u: &'a mut Unstructured<'b>) -> Result<()>
where
T: Arbitrary<'b> + Int + Display,
T: Arbitrary<'b> + Int<BYTES> + Display,
{
let range = RangeInclusive::<T>::arbitrary(u)?;
let start = *range.start();
Expand Down
2 changes: 2 additions & 0 deletions src/back_note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Length information is generally encoded here.
See [UnstructuredBuilder::extend_from_dearbitrary_iter_rev_with_length]
41 changes: 41 additions & 0 deletions src/dearbitrary/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::{error, fmt};

/// An enumeration of dearbitrartion errors
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
/// The instance's size is unsupported by its corresponding [Arbitrary] type
TooLarge,
/// The instance's details are too specific to this platform to be represented by its corresponding [Arbitrary] type,
TooSpecific
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::TooLarge => write!(
f,
"This type instance is too large to be losslessly reconstructed by Arbitrary after dearbitration."
),
Error::TooSpecific => write!(
f,
"This type instance is too specific to the platform to be lossly reconstructed by Arbitrary after dearbitration on other platforms."
),
}
}
}

impl error::Error for Error {}

/// A `Result` with the error type fixed as `arbitrary::Error`.
///
/// Either an `Ok(T)` or `Err(arbitrary::Error)`.
pub type Result<T, E = Error> = std::result::Result<T, E>;

#[cfg(test)]
mod tests {
#[test]
fn can_use_custom_error_types_with_result() -> super::Result<(), String> {
Ok(())
}
}
2 changes: 2 additions & 0 deletions src/dearbitrary/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod error;
pub use error::*;
Loading