Skip to content

Commit ba34197

Browse files
committed
fuzz-afl: Add info target
Decoding will parse the header as well, but it might still be interesting to fuzz the header parsing separately.
1 parent e376419 commit ba34197

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

fuzz-afl/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
out/
2+
out-*/

fuzz-afl/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ path = "src/fuzz_decode.rs"
1212
name = "reproduce_decode"
1313
path = "src/reproduce_decode.rs"
1414

15+
[[bin]]
16+
name = "fuzz_info"
17+
path = "src/fuzz_info.rs"
18+
19+
[[bin]]
20+
name = "reproduce_info"
21+
path = "src/reproduce_info.rs"
22+
1523
[dependencies]
1624
afl = "0.4"
1725
jpeg-decoder = { path = "../" }

fuzz-afl/src/fuzz_info.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use afl::fuzz;
2+
3+
use jpeg_decoder::{Decoder, ImageInfo};
4+
5+
#[inline(always)]
6+
fn get_info(data: &[u8]) -> Option<ImageInfo> {
7+
let mut decoder = Decoder::new(data);
8+
decoder.read_info().ok().and_then(|_| decoder.info())
9+
}
10+
11+
fn main() {
12+
fuzz!(|data: &[u8]| {
13+
let _ = get_info(data);
14+
});
15+
}

fuzz-afl/src/reproduce_decode.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
use jpeg_decoder::{Decoder, Error};
22

3+
mod utils;
4+
35
#[inline(always)]
46
fn decode(data: &[u8]) -> Result<Vec<u8>, Error> {
57
let mut decoder = Decoder::new(data);
68
decoder.decode()
79
}
810

911
fn main() {
10-
let args: Vec<String> = std::env::args().collect();
11-
if args.len() != 2 {
12-
println!("Usage: {} <path-to-crash>", args[0]);
13-
std::process::exit(1);
14-
}
15-
16-
let data = std::fs::read(&args[1]).expect(&format!("Could not open file {}", args[1]));
12+
let data = utils::read_file_from_args();
1713
match decode(&data) {
1814
Ok(bytes) => println!("Decoded {} bytes", bytes.len()),
1915
Err(e) => println!("Decoder returned an error: {:?}\nNote: Not a panic, this is fine.", e),

fuzz-afl/src/reproduce_info.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use jpeg_decoder::{Decoder, ImageInfo};
2+
3+
mod utils;
4+
5+
#[inline(always)]
6+
fn get_info(data: &[u8]) -> Option<ImageInfo> {
7+
let mut decoder = Decoder::new(data);
8+
decoder.read_info().ok().and_then(|_| decoder.info())
9+
}
10+
11+
fn main() {
12+
let data = utils::read_file_from_args();
13+
match get_info(&data) {
14+
Some(info) => println!("Info: {:?}", info),
15+
None => println!("Found no info in file"),
16+
};
17+
}

fuzz-afl/src/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub fn read_file_from_args() -> Vec<u8> {
2+
let args: Vec<String> = std::env::args().collect();
3+
if args.len() != 2 {
4+
println!("Usage: {} <path-to-crash>", args[0]);
5+
std::process::exit(1);
6+
}
7+
let data = std::fs::read(&args[1])
8+
.expect(&format!("Could not open file {}", args[1]));
9+
data
10+
}

0 commit comments

Comments
 (0)