Skip to content

Commit aea3565

Browse files
committed
Add remove all to fuzzer
1 parent e1fb263 commit aea3565

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

fuzz/fuzz_targets/map.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum Op {
3535
Store(StoreOp),
3636
Fetch(u8),
3737
Remove(u8),
38+
RemoveAll,
3839
}
3940

4041
#[derive(Arbitrary, Debug, Clone)]
@@ -64,7 +65,11 @@ enum CacheType {
6465

6566
fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
6667
let mut flash = MockFlashBase::<PAGES, WORD_SIZE, WORDS_PER_PAGE>::new(
67-
if ops.ops.iter().any(|op| matches!(op, Op::Remove(_))) {
68+
if ops
69+
.ops
70+
.iter()
71+
.any(|op| matches!(op, Op::Remove(_) | Op::RemoveAll))
72+
{
6873
WriteCountCheck::Twice
6974
} else {
7075
WriteCountCheck::OnceOnly
@@ -190,6 +195,7 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
190195
value: MockFlashError::EarlyShutoff(_),
191196
backtrace: _backtrace,
192197
}) => {
198+
// Check if the item is still there. It might or it might not and either is fine
193199
match block_on(sequential_storage::map::fetch_item::<u8, &[u8], _>(
194200
&mut flash,
195201
FLASH_RANGE,
@@ -220,6 +226,54 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
220226
Err(e) => panic!("{e:?}"),
221227
}
222228
}
229+
Op::RemoveAll => {
230+
match block_on(sequential_storage::map::remove_all_items::<u8, _>(
231+
&mut flash,
232+
FLASH_RANGE,
233+
&mut cache,
234+
&mut buf.0,
235+
)) {
236+
Ok(()) => {
237+
map.clear();
238+
}
239+
Err(Error::Storage {
240+
value: MockFlashError::EarlyShutoff(_),
241+
backtrace: _backtrace,
242+
}) => {
243+
for key in map.keys().copied().collect::<Vec<_>>() {
244+
// Check if the item is still there. It might or it might not and either is fine
245+
match block_on(sequential_storage::map::fetch_item::<u8, &[u8], _>(
246+
&mut flash,
247+
FLASH_RANGE,
248+
&mut cache,
249+
&mut buf.0,
250+
key,
251+
)) {
252+
Ok(Some(_)) => {
253+
#[cfg(fuzzing_repro)]
254+
eprintln!("Early shutoff when removing item {key}! Originated from:\n{_backtrace:#}");
255+
}
256+
_ => {
257+
// Could not fetch the item we stored...
258+
#[cfg(fuzzing_repro)]
259+
eprintln!("Early shutoff when removing item {key}! (but it still removed fully). Originated from:\n{_backtrace:#}");
260+
// Even though we got a shutoff, it still managed to store well
261+
map.remove(&key);
262+
}
263+
}
264+
}
265+
}
266+
267+
Err(Error::Corrupted {
268+
backtrace: _backtrace,
269+
}) => {
270+
#[cfg(fuzzing_repro)]
271+
eprintln!("Corrupted when removing! Originated from:\n{_backtrace:#}");
272+
panic!("Corrupted!");
273+
}
274+
Err(e) => panic!("{e:?}"),
275+
}
276+
}
223277
}
224278
}
225279
}

src/map.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ pub async fn remove_all_items<K: Key, S: MultiwriteNorFlash>(
599599
)
600600
}
601601

602+
/// If `search_key` is None, then all items will be removed
602603
async fn remove_item_inner<K: Key, S: MultiwriteNorFlash>(
603604
flash: &mut S,
604605
flash_range: Range<u32>,
@@ -650,15 +651,13 @@ async fn remove_item_inner<K: Key, S: MultiwriteNorFlash>(
650651
item::MaybeItem::Corrupted(_, _) => continue,
651652
item::MaybeItem::Erased(_, _) => continue,
652653
item::MaybeItem::Present(item) => {
653-
let item_match = if let Some(search_key) = &search_key {
654-
let (item_key, _) = K::deserialize_from(item.data())?;
655-
&item_key == search_key
656-
} else {
657-
false
654+
let item_match = match &search_key {
655+
Some(search_key) => &K::deserialize_from(item.data())?.0 == search_key,
656+
_ => true,
658657
};
659658
// If this item has the same key as the key we're trying to erase, then erase the item.
660659
// But keep going! We need to erase everything.
661-
if search_key.is_none() || item_match {
660+
if item_match {
662661
item.header
663662
.erase_data(flash, flash_range.clone(), cache, item_address)
664663
.await?;

0 commit comments

Comments
 (0)