Skip to content
Closed
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cranelift/control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition.workspace = true

[dependencies]
arbitrary = { version = "1.3.0" }
yoke = "0.7.1"

[features]

Expand Down
86 changes: 56 additions & 30 deletions cranelift/control/src/chaos.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,62 @@
use arbitrary::{Arbitrary, Unstructured};
use yoke::Yoke;

/// The control plane of chaos mode.
/// Please see the [crate-level documentation](crate).
#[derive(Debug, Clone, Default)]
pub struct ControlPlane {
data: Vec<u8>,
/// This is used as a little optimization to avoid additional heap
/// allocations when using `Unstructured` internally. See the source of
/// [`ControlPlane::shuffle`] for an example.
tmp: Vec<u8>,
// The cart is the vector of pseudo-random data generated by a fuzzer.
// The yoke is the slice of unused bytes in that vector.
data: Yoke<&'static [u8], Vec<u8>>,
}

impl Default for ControlPlane {
fn default() -> Self {
Self::new(Vec::new())
}
}

impl Clone for ControlPlane {
fn clone(&self) -> Self {
Self::new(self.data.get().to_vec())
}
}

impl std::fmt::Debug for ControlPlane {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ControlPlane")
.field("unused_bytes", &self.data.get())
.field("backing_vector", self.data.backing_cart())
.finish()
}
}

impl Arbitrary<'_> for ControlPlane {
fn arbitrary<'a>(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self {
data: u.arbitrary()?,
tmp: Vec::new(),
})
u.arbitrary().map(Self::new)
}
}

impl ControlPlane {
fn new(data: Vec<u8>) -> Self {
let data = Yoke::attach_to_cart(data, |slice| slice);
Self { data }
}

/// Returns a pseudo-random boolean if the control plane was constructed
/// with `arbitrary`.
///
/// The default value `false` will always be returned if the
/// pseudo-random data is exhausted or the control plane was constructed
/// with `default`.
pub fn get_decision(&mut self) -> bool {
self.data.pop().unwrap_or_default() & 1 == 1
let mut res = false;
let data = std::mem::take(self).data.map_project(|unused_bytes, _| {
let mut u = Unstructured::new(unused_bytes);
res = u.arbitrary().unwrap_or_default();
u.take_rest()
});
*self = Self { data };
res
}

/// Shuffles the items in the slice into a pseudo-random permutation if
Expand All @@ -38,25 +66,23 @@ impl ControlPlane {
/// performed if the pseudo-random data is exhausted or the control
/// plane was constructed with `default`.
pub fn shuffle<T>(&mut self, slice: &mut [T]) {
let mut u = Unstructured::new(&self.data);

// adapted from:
// https://docs.rs/arbitrary/1.3.0/arbitrary/struct.Unstructured.html#examples-1
let mut to_permute = &mut slice[..];

while to_permute.len() > 1 {
if let Ok(idx) = u.choose_index(to_permute.len()) {
to_permute.swap(0, idx);
to_permute = &mut to_permute[1..];
} else {
break;
}
}
let data = std::mem::take(self).data.map_project(|unused_bytes, _| {
let mut u = Unstructured::new(unused_bytes);

// adapted from:
// https://docs.rs/arbitrary/1.3.0/arbitrary/struct.Unstructured.html#examples-1
let mut to_permute = &mut slice[..];

// take remaining bytes
let rest = u.take_rest();
self.tmp.resize(rest.len(), 0); // allocates once per control plane
self.tmp.copy_from_slice(rest);
std::mem::swap(&mut self.data, &mut self.tmp);
while to_permute.len() > 1 {
if let Ok(idx) = u.choose_index(to_permute.len()) {
to_permute.swap(0, idx);
to_permute = &mut to_permute[1..];
} else {
break;
}
}
u.take_rest()
});
*self = Self { data };
}
}