Skip to content

Commit

Permalink
fuzz: Add target encode_decode_hbd
Browse files Browse the repository at this point in the history
Share code with encode_decode such that the corpus can be reused.
Bit-depth is decoded last so that all other parameters are shared.
  • Loading branch information
barrbrain committed Jul 13, 2021
1 parent 8374283 commit bdee3b9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ name = "encode_decode"
path = "fuzz_targets/encode_decode.rs"
required-features = ["rav1e/decode_test_dav1d"]

[[bin]]
name = "encode_decode_hbd"
path = "fuzz_targets/encode_decode_hbd.rs"
required-features = ["rav1e/decode_test_dav1d"]

[[bin]]
name = "encode"
path = "fuzz_targets/encode.rs"
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/encode_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
extern crate rav1e;
use rav1e::fuzzing::*;

fuzz_target!(|data: DecodeTestParameters| {
fuzz_target!(|data: DecodeTestParameters<u8>| {
let _ = pretty_env_logger::try_init();

fuzz_encode_decode(data)
Expand Down
19 changes: 19 additions & 0 deletions fuzz/fuzz_targets/encode_decode_hbd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2019-2021, The rav1e contributors. All rights reserved
//
// This source code is subject to the terms of the BSD 2 Clause License and
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
// was not distributed with this source code in the LICENSE file, you can
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.

#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate rav1e;
use rav1e::fuzzing::*;

fuzz_target!(|data: DecodeTestParameters<u16>| {
let _ = pretty_env_logger::try_init();

fuzz_encode_decode(data)
});
14 changes: 10 additions & 4 deletions src/fuzzing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.

use std::marker::PhantomData;
use std::sync::Arc;

use libfuzzer_sys::arbitrary::{Arbitrary, Error, Unstructured};
Expand Down Expand Up @@ -295,7 +296,7 @@ pub fn fuzz_encode(arbitrary: ArbitraryEncoder) {
}

#[derive(Debug)]
pub struct DecodeTestParameters {
pub struct DecodeTestParameters<T: Pixel> {
w: usize,
h: usize,
speed: usize,
Expand All @@ -312,9 +313,10 @@ pub struct DecodeTestParameters {
tile_cols_log2: usize,
tile_rows_log2: usize,
still_picture: bool,
pixel: PhantomData<T>,
}

impl Arbitrary for DecodeTestParameters {
impl<T: Pixel> Arbitrary for DecodeTestParameters<T> {
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self, Error> {
let mut p = Self {
w: u.int_in_range(16..=16 + 255)?,
Expand All @@ -338,7 +340,11 @@ impl Arbitrary for DecodeTestParameters {
tile_cols_log2: u.int_in_range(0..=2)?,
tile_rows_log2: u.int_in_range(0..=2)?,
still_picture: bool::arbitrary(u)?,
pixel: PhantomData,
};
if matches!(T::type_enum(), PixelType::U16) {
p.bit_depth = *u.choose(&[8, 10, 12])?;
}
if !p.low_latency {
p.switch_frame_interval = 0;
}
Expand All @@ -350,10 +356,10 @@ impl Arbitrary for DecodeTestParameters {
}

#[cfg(feature = "decode_test_dav1d")]
pub fn fuzz_encode_decode(p: DecodeTestParameters) {
pub fn fuzz_encode_decode<T: Pixel>(p: DecodeTestParameters<T>) {
use crate::test_encode_decode::*;

let mut dec = get_decoder::<u8>("dav1d", p.w, p.h);
let mut dec = get_decoder::<T>("dav1d", p.w, p.h);
dec.encode_decode(
p.w,
p.h,
Expand Down

0 comments on commit bdee3b9

Please sign in to comment.