Add fuzzing for object crdt (#1540)

Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/1540
This commit is contained in:
Arthur Carcano
2026-09-15 09:54:40 +00:00
committed by Alex
parent c887553a1a
commit 6292e9e880
3 changed files with 108 additions and 13 deletions
+7
View File
@@ -71,3 +71,10 @@ path = "fuzz_targets/k2v_item_crdt.rs"
test = false
doc = false
bench = false
[[bin]]
name = "object_crdt"
path = "fuzz_targets/object_crdt.rs"
test = false
doc = false
bench = false
+75
View File
@@ -0,0 +1,75 @@
#![no_main]
use garage_model::s3::object_table::{Object, ObjectVersion};
use garage_table::crdt::Crdt;
use libfuzzer_sys::fuzz_target;
/// Build an Object from an arbitrary version list, using a fixed bucket/key
/// so that version slices can be compared across merge results.
/// Duplicate versions (same uuid+timestamp) are dropped before construction.
fn make_object(mut versions: Vec<ObjectVersion>) -> Object {
versions.sort_by_key(|v| (v.timestamp, v.uuid));
versions.dedup_by_key(|v| (v.timestamp, v.uuid));
let mut res = Object::new([0u8; 32].into(), String::new(), versions);
res.strip_obsolete();
res
}
fuzz_target!(
|inputs: (Vec<ObjectVersion>, Vec<ObjectVersion>, Vec<ObjectVersion>)| {
let (v1, v2, v3) = inputs;
let a = make_object(v1);
let b = make_object(v2);
let c = make_object(v3);
// Idempotency: merge(a, a) == a
{
let mut a2 = a.clone();
a2.merge(&a.clone());
assert_eq!(
a2.versions(),
a.versions(),
"merge is not idempotent: {a2:#?} != {a:#?}"
);
}
// Commutativity: versions(merge(a, b)) == versions(merge(b, a))
let ab = {
let mut t = a.clone();
t.merge(&b);
t
};
let ba = {
let mut t = b.clone();
t.merge(&a);
t
};
assert_eq!(
ab.versions(),
ba.versions(),
"merge is not commutative: {ab:#?} != {ba:#?}"
);
// Associativity: versions(merge(merge(a, b), c)) == versions(merge(a, merge(b, c)))
let ab_c = {
let mut t = ab.clone();
t.merge(&c);
t
};
let bc = {
let mut t = b.clone();
t.merge(&c);
t
};
let a_bc = {
let mut t = a.clone();
t.merge(&bc);
t
};
assert_eq!(
ab_c.versions(),
a_bc.versions(),
"merge is not associative: {ab_c:#?} != {a_bc:#?}"
);
}
);
+26 -13
View File
@@ -281,6 +281,7 @@ mod v010 {
pub type HeaderList = Vec<(String, String)>;
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ChecksumAlgorithm {
Crc32,
Crc32c,
@@ -391,6 +392,7 @@ mod v2 {
/// An object
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Object {
/// The bucket in which the object is stored, used as partition key
pub bucket_id: Uuid,
@@ -404,6 +406,7 @@ mod v2 {
/// Information about a version of an object
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ObjectVersion {
/// Id of the version
pub uuid: Uuid,
@@ -415,6 +418,7 @@ mod v2 {
/// State of an object version
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ObjectVersionState {
/// The version is being received
Uploading {
@@ -433,6 +437,7 @@ mod v2 {
/// Data stored in object version
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ObjectVersionData {
/// The object was deleted, this Version is a tombstone to mark it as such
DeleteMarker,
@@ -447,6 +452,7 @@ mod v2 {
/// Metadata about the object version
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ObjectVersionMeta {
/// Size of the object. If object is encrypted/compressed,
/// this is always the size of the unencrypted/uncompressed data
@@ -459,6 +465,7 @@ mod v2 {
/// Encryption information + metadata
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ObjectVersionEncryption {
SseC {
/// Encrypted serialized `ObjectVersionInner` struct.
@@ -485,6 +492,7 @@ mod v2 {
/// have been migrated from Garage version before v2.0, as the distinction between
/// full-object and composite checksums was not implemented yet.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ObjectVersionMetaInner {
pub headers: HeaderList,
pub checksum: Option<ChecksumValue>,
@@ -498,6 +506,7 @@ mod v2 {
pub type HeaderList = Vec<(String, String)>;
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ChecksumType {
FullObject,
Composite,
@@ -632,6 +641,22 @@ impl Object {
pub fn versions(&self) -> &[ObjectVersion] {
&self.versions[..]
}
/// Remove versions which are obsolete, i.e. those that come
/// before the last version which `.is_complete()`.
pub fn strip_obsolete(&mut self) {
let last_complete = self
.versions
.iter()
.enumerate()
.rev()
.find(|(_, v)| v.is_complete())
.map(|(vi, _)| vi);
if let Some(last_vi) = last_complete {
self.versions = self.versions.drain(last_vi..).collect::<Vec<_>>();
}
}
}
impl Crdt for ObjectVersionState {
@@ -736,19 +761,7 @@ impl Crdt for Object {
}
}
// Remove versions which are obsolete, i.e. those that come
// before the last version which .is_complete().
let last_complete = self
.versions
.iter()
.enumerate()
.rev()
.find(|(_, v)| v.is_complete())
.map(|(vi, _)| vi);
if let Some(last_vi) = last_complete {
self.versions = self.versions.drain(last_vi..).collect::<Vec<_>>();
}
self.strip_obsolete();
}
}