AFLplusplus / AFLplusplus/LibAFL

Make `current_corpus_idx` infallible?

Open
#2,200 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2.6k
Forks
481
Avg merge
2d 30m
Merged PRs (30d)
16

Description

Currently, [`current_corpus_idx`](https://github.com/AFLplusplus/LibAFL/blob/0777873aaef62e309075cb4b64ef04e0b0124afe/libafl/src/corpus/mod.rs#L186) returns an `Option`. However, most places where `current_(testcase|corpus_idx)` are called don't meaningfully handle it returning `None`, instead just propagating the error. In most fuzzers, we'd actually expect the `None`-handling branch to be dead code. I wonder if there's a way to *actually* make this method infallible. Hopefully this could even result in some performance gains by eliminating these branches? The revised trait would look like this:
```rust
/// Trait for types which track the current corpus index
pub trait HasCurrentCorpusIdx {
/// Set the current corpus index; we have started processing this corpus entry
fn set_corpus_idx(&mut self, idx: CorpusId) -> Result<(), Error>;

/// Fetch the current corpus index
fn current_corpus_idx(&self) -> Result;
}
```
The difficulty is that the `State` would have to choose an initial index *at the time it is constructed* (probably using a `Scheduler`). Right now, we generally construct `State`s before loading/generating the initial corpus. Perhaps there's some kind of type-state pattern, like there could be an `InitialStdState` that doesn't implement `HasCurrentCorpusIdx`, but has methods to load/generate the initial corpus *and* pick an initial index, and they all return `StdState` (which *would* implement the infallible version of `HasCurrentCorpusIdx`).

Just an idea :shrug: Feel free to close if not helpful!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.