Skip to content

Commit

Permalink
test: fuzz test for foyer-memory
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaguan committed Apr 14, 2024
1 parent f590f09 commit 541a329
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

/target

foyer-memory/fuzz/artifacts/cache_fuzz/
foyer-memory/fuzz/corpus/cache_fuzz

Cargo.lock
lcov.info

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"foyer-experimental-bench",
"foyer-intrusive",
"foyer-memory",
"foyer-memory/fuzz",
"foyer-storage",
"foyer-storage-bench",
"foyer-workspace-hack",
Expand Down
21 changes: 21 additions & 0 deletions foyer-memory/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "foyer-memory-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[dependencies]
ahash = "0.8"
foyer-memory = { path = ".." }
foyer-workspace-hack = { version = "0.4", path = "../../foyer-workspace-hack" }
libfuzzer-sys = "0.4"

[package.metadata]
cargo-fuzz = true

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

use std::sync::Arc;

use ahash::RandomState;
use libfuzzer_sys::fuzz_target;

use foyer_memory::{
Cache, DefaultCacheEventListener, FifoCacheConfig, FifoConfig, LfuCacheConfig, LfuConfig, LruCacheConfig,
LruConfig, S3FifoCacheConfig, S3FifoConfig,
};

type CacheKey = u8;
type CacheValue = u8;
const SHARDS: usize = 1;
const OBJECT_POOL_CAPACITY: usize = 16;

fn new_fifo_cache(capacity: usize) -> Arc<Cache<CacheKey, CacheValue>> {
let config = FifoCacheConfig {
capacity,
shards: SHARDS,
eviction_config: FifoConfig {},
object_pool_capacity: OBJECT_POOL_CAPACITY,
hash_builder: RandomState::default(),
event_listener: DefaultCacheEventListener::default(),
};

Arc::new(Cache::fifo(config))
}

fn new_lru_cache(capacity: usize) -> Arc<Cache<CacheKey, CacheValue>> {
let config = LruCacheConfig {
capacity,
shards: SHARDS,
eviction_config: LruConfig {
high_priority_pool_ratio: 0.1,
},
object_pool_capacity: OBJECT_POOL_CAPACITY,
hash_builder: RandomState::default(),
event_listener: DefaultCacheEventListener::default(),
};

Arc::new(Cache::lru(config))
}

fn new_lfu_cache(capacity: usize) -> Arc<Cache<CacheKey, CacheValue>> {
let config = LfuCacheConfig {
capacity,
shards: SHARDS,
eviction_config: LfuConfig {
window_capacity_ratio: 0.1,
protected_capacity_ratio: 0.8,
cmsketch_eps: 0.001,
cmsketch_confidence: 0.9,
},
object_pool_capacity: OBJECT_POOL_CAPACITY,
hash_builder: RandomState::default(),
event_listener: DefaultCacheEventListener::default(),
};

Arc::new(Cache::lfu(config))
}

fn new_s3fifo_cache(capacity: usize) -> Arc<Cache<CacheKey, CacheValue>> {
let config = S3FifoCacheConfig {
capacity,
shards: SHARDS,
eviction_config: S3FifoConfig {
small_queue_capacity_ratio: 0.1,
},
object_pool_capacity: OBJECT_POOL_CAPACITY,
hash_builder: RandomState::default(),
event_listener: DefaultCacheEventListener::default(),
};

Arc::new(Cache::s3fifo(config))
}

fn create_cache(op: u8) -> Arc<Cache<CacheKey, CacheValue>> {
match op % 4 {
0 => new_fifo_cache(20),
1 => new_lru_cache(20),
2 => new_lfu_cache(20),
3 => new_s3fifo_cache(20),
_ => unreachable!(),
}
}

fuzz_target!(|data: &[u8]| {
if data.is_empty() {
return;
}
let cache = create_cache(data[0]);
let mut it = data.iter();
for &op in it.by_ref() {
match op % 4 {
0 => {
cache.insert(op, op, 1);
}
1 => match cache.get(&op) {
Some(v) => assert_eq!(*v, op),
None => {}
},
2 => {
cache.remove(&op);
}
3 => {
// Low probability to clear the cache
if op % 10 == 1 {
cache.clear();
}
}
_ => unreachable!(),
}
}
});
2 changes: 0 additions & 2 deletions foyer-workspace-hack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ futures-executor = { version = "0.3" }
futures-sink = { version = "0.3" }
futures-util = { version = "0.3", default-features = false, features = ["async-await-macro", "channel", "io", "sink"] }
hashbrown = { version = "0.14", features = ["raw"] }
itertools = { version = "0.12" }
libc = { version = "0.2", features = ["extra_traits"] }
parking_lot = { version = "0.12", features = ["arc_lock", "deadlock_detection"] }
parking_lot_core = { version = "0.9", default-features = false, features = ["deadlock_detection"] }
Expand All @@ -42,7 +41,6 @@ tracing-core = { version = "0.1" }
[build-dependencies]
cc = { version = "1", default-features = false, features = ["parallel"] }
either = { version = "1", default-features = false, features = ["use_std"] }
itertools = { version = "0.12" }
proc-macro2 = { version = "1" }
quote = { version = "1" }
syn = { version = "2", features = ["extra-traits", "full", "visit-mut"] }
Expand Down

0 comments on commit 541a329

Please sign in to comment.