Skip to content
Open
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
8 changes: 4 additions & 4 deletions lib/bolero-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ anyhow = "1"
bolero-generator = { version = "0.13.5", path = "../bolero-generator", default-features = false }
lazy_static = "1"
pretty-hex = { version = "0.4", default-features = false }
rand = { version = "0.9", optional = true }
rand_xoshiro = { version = "0.7", optional = true }
rand = { version = "0.10", optional = true }
rand_xoshiro = { version = "0.8", optional = true }

[dev-dependencies]
bolero-generator = { version = "0.13.5", path = "../bolero-generator", features = ["std"] }
rand = "0.9"
rand_xoshiro = "0.7"
rand = "0.10"
rand_xoshiro = "0.8"

[lints.rust.unexpected_cfgs]
level = "warn"
Expand Down
6 changes: 3 additions & 3 deletions lib/bolero-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ arbitrary = { version = "1.0", optional = true }
bolero-generator-derive = { version = "0.13.4", path = "../bolero-generator-derive" }
either = { version = "1.5", default-features = false, optional = true }
getrandom = { version = "0.3", optional = true }
rand_core = { version = "0.9", default-features = false }
rand_xoshiro = { version = "0.7", optional = true }
rand_core = { version = "0.10", default-features = false }
rand_xoshiro = { version = "0.8", optional = true }

[dev-dependencies]
insta = "1"
rand = "0.9"
rand = "0.10"

[lints.rust.unexpected_cfgs]
level = "warn"
Expand Down
2 changes: 1 addition & 1 deletion lib/bolero-generator/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
TypeGenerator, ValueGenerator,
};
use core::ops::Bound;
use rand_core::TryRngCore;
use rand_core::TryRng;

#[macro_use]
mod macros;
Expand Down
18 changes: 11 additions & 7 deletions lib/bolero-generator/src/driver/exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
uniform::{self, Uniform},
};
use alloc::vec::Vec;
use core::convert::Infallible;
use core::ops::{Bound, ControlFlow};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -318,25 +319,28 @@ impl_driver!(&mut Driver);

struct Rng<'a>(&'a mut State);

impl rand_core::RngCore for Rng<'_> {
fn next_u32(&mut self) -> u32 {
self.0.select(u32::MAX as _) as _
impl rand_core::TryRng for Rng<'_> {
type Error = Infallible;

fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(self.0.select(u32::MAX as _) as _)
}

fn next_u64(&mut self) -> u64 {
self.0.select(u64::MAX)
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(self.0.select(u64::MAX))
}

fn fill_bytes(&mut self, mut dest: &mut [u8]) {
fn try_fill_bytes(&mut self, mut dest: &mut [u8]) -> Result<(), Self::Error> {
while dest.len() >= 8 {
let (chunk, rest) = dest.split_at_mut(8);
dest = rest;
let value = self.next_u64();
let value = self.try_next_u64()?;
chunk.copy_from_slice(&value.to_be_bytes());
}

let value = self.0.select((1 << dest.len()) * 8);
dest.copy_from_slice(&value.to_be_bytes()[..dest.len()]);
Ok(())
}
}

Expand Down
18 changes: 9 additions & 9 deletions lib/bolero-generator/src/driver/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) use buffer_alloc::Buffer;
pub(crate) use buffer_no_alloc::Buffer;

#[derive(Debug)]
pub struct Rng<R: TryRngCore> {
pub struct Rng<R: TryRng> {
rng: R,
depth: usize,
max_depth: usize,
Expand All @@ -16,7 +16,7 @@ pub struct Rng<R: TryRngCore> {
buffer: Buffer,
}

impl<R: TryRngCore> Rng<R> {
impl<R: TryRng> Rng<R> {
pub fn new(rng: R, options: &Options) -> Self {
Self {
rng,
Expand Down Expand Up @@ -54,16 +54,16 @@ impl<R: TryRngCore> Rng<R> {
}
}

impl<R: TryRngCore> AsRef<R> for Rng<R> {
impl<R: TryRng> AsRef<R> for Rng<R> {
#[inline]
fn as_ref(&self) -> &R {
&self.rng
}
}

#[inline]
fn fill_bytes<R: TryRngCore>(rng: &mut R, bytes: &mut [u8]) -> Option<()> {
if TryRngCore::try_fill_bytes(rng, bytes).is_err() {
fn fill_bytes<R: TryRng>(rng: &mut R, bytes: &mut [u8]) -> Option<()> {
if TryRng::try_fill_bytes(rng, bytes).is_err() {
// if the rng fails to fill the remaining bytes, then we just start returning 0s
for byte in bytes.iter_mut() {
*byte = 0;
Expand All @@ -87,7 +87,7 @@ macro_rules! impl_sample {
};
}

impl<R: TryRngCore> FillBytes for Rng<R> {
impl<R: TryRng> FillBytes for Rng<R> {
// prefer sampling the larger values since it's faster to pull from the RNG
const SHOULD_SHRINK: bool = false;

Expand Down Expand Up @@ -124,7 +124,7 @@ impl<R: TryRngCore> FillBytes for Rng<R> {
impl_sample!(sample_isize, isize, try_next_u64);
}

impl<R: TryRngCore> Driver for Rng<R> {
impl<R: TryRng> Driver for Rng<R> {
gen_from_bytes!();

#[inline]
Expand Down Expand Up @@ -178,7 +178,7 @@ mod buffer_alloc {
pub const MAX_CAPACITY: usize = isize::MAX as _;

#[inline]
pub fn fill<R: TryRngCore>(&mut self, len: usize, rng: &mut R) -> Option<()> {
pub fn fill<R: TryRng>(&mut self, len: usize, rng: &mut R) -> Option<()> {
let data = &mut self.bytes;

let initial_len = data.len();
Expand Down Expand Up @@ -231,7 +231,7 @@ mod buffer_no_alloc {
pub const MAX_CAPACITY: usize = 256;

#[inline]
pub fn fill<R: TryRngCore>(&mut self, len: usize, rng: &mut R) -> Option<()> {
pub fn fill<R: TryRng>(&mut self, len: usize, rng: &mut R) -> Option<()> {
if cfg!(test) {
assert!(len <= Self::MAX_CAPACITY);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/bolero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ bolero-honggfuzz = { version = "0.13", path = "../bolero-honggfuzz" }

[target.'cfg(fuzzing_random)'.dependencies]
bolero-engine = { version = "0.13.4", path = "../bolero-engine", features = ["cache", "rng"] }
rand = { version = "0.9" }
rand = "0.10"

[target.'cfg(kani)'.dependencies]
bolero-kani = { version = "0.13", path = "../bolero-kani" }

[target.'cfg(not(any(fuzzing, kani)))'.dependencies]
bolero-engine = { version = "0.13.4", path = "../bolero-engine", features = ["cache", "rng"] }
rand = { version = "0.9" }
rand = "0.10"

[dev-dependencies]
rand = "0.9"
rand = "0.10"

[lints.rust.unexpected_cfgs]
level = "warn"
Expand Down
41 changes: 24 additions & 17 deletions lib/bolero/src/test/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use bolero_engine::{rng::Recommended as Rng, Seed};
use bolero_generator::{driver, TypeGenerator};
use rand::SeedableRng;
use core::convert::Infallible;
use rand::{Rng as _, SeedableRng};
use std::{io::Read, path::PathBuf};

pub use bolero_engine::input::*;
Expand Down Expand Up @@ -82,22 +83,25 @@ pub struct BufferedRng<'a> {
buffer: &'a mut Vec<u8>,
}

impl rand::RngCore for BufferedRng<'_> {
fn next_u32(&mut self) -> u32 {
impl rand::TryRng for BufferedRng<'_> {
type Error = Infallible;

fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
let mut data = [0; 4];
self.fill_bytes(&mut data);
u32::from_le_bytes(data)
self.try_fill_bytes(&mut data)?;
Ok(u32::from_le_bytes(data))
}

fn next_u64(&mut self) -> u64 {
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
let mut data = [0; 8];
self.fill_bytes(&mut data);
u64::from_le_bytes(data)
self.try_fill_bytes(&mut data)?;
Ok(u64::from_le_bytes(data))
}

fn fill_bytes(&mut self, bytes: &mut [u8]) {
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Self::Error> {
self.rng.fill_bytes(bytes);
self.buffer.extend_from_slice(bytes);
Ok(())
}
}

Expand All @@ -124,26 +128,29 @@ pub struct ReplayRng<'a> {
buffer: &'a [u8],
}

impl rand::RngCore for ReplayRng<'_> {
fn next_u32(&mut self) -> u32 {
impl rand::TryRng for ReplayRng<'_> {
type Error = Infallible;

fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
let mut data = [0; 4];
self.fill_bytes(&mut data);
u32::from_le_bytes(data)
self.try_fill_bytes(&mut data)?;
Ok(u32::from_le_bytes(data))
}

fn next_u64(&mut self) -> u64 {
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
let mut data = [0; 8];
self.fill_bytes(&mut data);
u64::from_le_bytes(data)
self.try_fill_bytes(&mut data)?;
Ok(u64::from_le_bytes(data))
}

fn fill_bytes(&mut self, bytes: &mut [u8]) {
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Self::Error> {
let len = self.buffer.len().min(bytes.len());
let (copy_from, remaining) = self.buffer.split_at(len);
let (copy_to, fill_to) = bytes.split_at_mut(len);
copy_to.copy_from_slice(copy_from);
fill_to.fill(0);
self.buffer = remaining;
Ok(())
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/bolero/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,14 @@ impl TestEngine {
}

fn rng_tests(&self) -> impl Iterator<Item = input::RngTest> {
use rand::{rngs::StdRng, RngCore, SeedableRng};
use rand::{
rngs::{StdRng, SysRng},
Rng as _, SeedableRng as _,
};

let iterations = self.rng_cfg.iterations_or_default();
// use StdRng for high entropy seeds
let mut seed_rng = StdRng::from_os_rng();
let mut seed_rng = StdRng::try_from_rng(&mut SysRng).unwrap();

(0..iterations).map(move |_| {
let mut seed = [0; size_of::<Seed>()];
Expand Down
Loading