Add peek function to Corpus (#2238)

* add peek function to Corpus

* send 0 as next peek in NopCorpus

* rename peek to peek_next_free_id

* fix clippy

* add peek_next_free_id to libafl_libfuzzer

* impl peek_next_free_id for ArtifactCorpus
This commit is contained in:
Aarnav 2024-05-22 04:30:47 -07:00 committed by GitHub
parent 4b67b55b29
commit 1ed1c4eb31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 46 additions and 0 deletions

View File

@ -147,6 +147,12 @@ where
self.inner.next(idx)
}
/// Peek the next free corpus id
#[inline]
fn peek_next_free_id(&self) -> CorpusId {
self.inner.peek_next_free_id()
}
#[inline]
fn prev(&self, idx: CorpusId) -> Option<CorpusId> {
self.inner.prev(idx)

View File

@ -264,6 +264,12 @@ where
self._insert(testcase, false)
}
#[must_use]
/// Peek the next free corpus id
pub fn peek_next_free_id(&self) -> CorpusId {
CorpusId::from(self.progressive_idx)
}
/// Insert a testcase assigning a `CorpusId` to it
pub fn insert_disabled(&mut self, testcase: RefCell<Testcase<I>>) -> CorpusId {
self._insert(testcase, true)
@ -430,6 +436,12 @@ where
&mut self.current
}
/// Peek the next free corpus id
#[inline]
fn peek_next_free_id(&self) -> CorpusId {
self.storage.peek_next_free_id()
}
#[inline]
fn next(&self, idx: CorpusId) -> Option<CorpusId> {
self.storage.enabled.next(idx)

View File

@ -149,6 +149,12 @@ where
self.inner.next(idx)
}
/// Peek the next free corpus id
#[inline]
fn peek_next_free_id(&self) -> CorpusId {
self.inner.peek_next_free_id()
}
#[inline]
fn prev(&self, idx: CorpusId) -> Option<CorpusId> {
self.inner.prev(idx)

View File

@ -131,6 +131,9 @@ pub trait Corpus: UsesInput + Serialize + for<'de> Deserialize<'de> {
/// Get the next corpus id
fn next(&self, id: CorpusId) -> Option<CorpusId>;
/// Peek the next free corpus id
fn peek_next_free_id(&self) -> CorpusId;
/// Get the prev corpus id
fn prev(&self, id: CorpusId) -> Option<CorpusId>;

View File

@ -87,6 +87,12 @@ where
&self.empty
}
/// Peek the next free corpus id
#[inline]
fn peek_next_free_id(&self) -> CorpusId {
CorpusId::from(0_usize)
}
/// Current testcase scheduled (mutable)
#[inline]
fn current_mut(&mut self) -> &mut Option<CorpusId> {

View File

@ -106,6 +106,12 @@ where
self.inner.replace(idx, testcase)
}
/// Peek the next free corpus id
#[inline]
fn peek_next_free_id(&self) -> CorpusId {
self.inner.peek_next_free_id()
}
/// Removes an entry from the corpus, returning it if it was present.
#[inline]
fn remove(&mut self, idx: CorpusId) -> Result<Testcase<I>, Error> {

View File

@ -203,6 +203,9 @@ where
fn next(&self, id: CorpusId) -> Option<CorpusId> {
self.mapping.enabled.next(id)
}
fn peek_next_free_id(&self) -> CorpusId {
self.mapping.peek_next_free_id()
}
fn prev(&self, id: CorpusId) -> Option<CorpusId> {
self.mapping.enabled.prev(id)
@ -356,6 +359,10 @@ where
maybe_last.ok_or_else(|| Error::illegal_argument("Can only get the last corpus ID."))
}
fn peek_next_free_id(&self) -> CorpusId {
CorpusId::from(self.count)
}
// This just calls Self::get as ArtifactCorpus disregards disabled entries
fn get_from_all(&self, id: CorpusId) -> Result<&RefCell<Testcase<Self::Input>>, Error> {
self.get(id)