| 1 |
// Copyright (c) 2019-2022, The rav1e contributors. All rights reserved |
| 2 |
// |
| 3 |
// This source code is subject to the terms of the BSD 2 Clause License and |
| 4 |
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 5 |
// was not distributed with this source code in the LICENSE file, you can |
| 6 |
// obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 7 |
// Media Patent License 1.0 was not distributed with this source code in the |
| 8 |
// PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 9 |
|
| 10 |
use std::marker::PhantomData; |
| 11 |
use std::sync::Arc; |
| 12 |
|
| 13 |
use libfuzzer_sys::arbitrary::{Arbitrary, Error, Unstructured}; |
| 14 |
|
| 15 |
use crate::prelude::*; |
| 16 |
|
| 17 |
// Adding new fuzz targets |
| 18 |
// |
| 19 |
// 1. Add a function to this file which looks like this: |
| 20 |
// |
| 21 |
// pub fn fuzz_something(data: Data) { |
| 22 |
// // Invoke everything you need. |
| 23 |
// // |
| 24 |
// // Your function may accept a value of any type that implements |
| 25 |
// // Arbitrary [1]. This is how fuzzer affects the execution—by |
| 26 |
// // feeding in different bytes, which result in different |
| 27 |
// // arbitrary values being generated. |
| 28 |
// // [1]: https://docs.rs/arbitrary/0.3.3/arbitrary/trait.Arbitrary.html |
| 29 |
// // |
| 30 |
// // Derive Debug for the structures you create with arbitrary data. |
| 31 |
// } |
| 32 |
// |
| 33 |
// 2. cargo fuzz add something |
| 34 |
// 3. Copy the contents of any other .rs file from fuzz/fuzz_targets/ into the |
| 35 |
// newly created fuzz/fuzz_targets/something.rs and change the function |
| 36 |
// being called to fuzz_something. |
| 37 |
// |
| 38 |
// Now you can fuzz the new target with cargo fuzz. |
| 39 |
|
| 40 |
#[derive(Debug)] |
| 41 |
pub struct ArbitraryConfig { |
| 42 |
config: Config, |
| 43 |
} |
| 44 |
|
| 45 |
#[inline] |
| 46 |
fn arbitrary_rational(u: &mut Unstructured<'_>) -> Result<Rational, Error> { |
| 47 |
Ok(Rational::new(Arbitrary::arbitrary(u)?, Arbitrary::arbitrary(u)?)) |
| 48 |
} |
| 49 |
|
| 50 |
#[inline] |
| 51 |
fn arbitrary_color_description( |
| 52 |
u: &mut Unstructured<'_>, |
| 53 |
) -> Result<Option<ColorDescription>, Error> { |
| 54 |
if Arbitrary::arbitrary(u)? { |
| 55 |
return Ok(None); |
| 56 |
} |
| 57 |
Ok(Some(ColorDescription { |
| 58 |
color_primaries: *u.choose(&[ |
| 59 |
ColorPrimaries::BT709, |
| 60 |
ColorPrimaries::Unspecified, |
| 61 |
ColorPrimaries::BT470M, |
| 62 |
ColorPrimaries::BT470BG, |
| 63 |
ColorPrimaries::BT601, |
| 64 |
ColorPrimaries::SMPTE240, |
| 65 |
ColorPrimaries::GenericFilm, |
| 66 |
ColorPrimaries::BT2020, |
| 67 |
ColorPrimaries::XYZ, |
| 68 |
ColorPrimaries::SMPTE431, |
| 69 |
ColorPrimaries::SMPTE432, |
| 70 |
ColorPrimaries::EBU3213, |
| 71 |
])?, |
| 72 |
transfer_characteristics: *u.choose(&[ |
| 73 |
TransferCharacteristics::BT709, |
| 74 |
TransferCharacteristics::Unspecified, |
| 75 |
TransferCharacteristics::BT470M, |
| 76 |
TransferCharacteristics::BT470BG, |
| 77 |
TransferCharacteristics::BT601, |
| 78 |
TransferCharacteristics::SMPTE240, |
| 79 |
TransferCharacteristics::Linear, |
| 80 |
TransferCharacteristics::Log100, |
| 81 |
TransferCharacteristics::Log100Sqrt10, |
| 82 |
TransferCharacteristics::IEC61966, |
| 83 |
TransferCharacteristics::BT1361, |
| 84 |
TransferCharacteristics::SRGB, |
| 85 |
TransferCharacteristics::BT2020_10Bit, |
| 86 |
TransferCharacteristics::BT2020_12Bit, |
| 87 |
TransferCharacteristics::SMPTE2084, |
| 88 |
TransferCharacteristics::SMPTE428, |
| 89 |
TransferCharacteristics::HLG, |
| 90 |
])?, |
| 91 |
matrix_coefficients: *u.choose(&[ |
| 92 |
MatrixCoefficients::Identity, |
| 93 |
MatrixCoefficients::BT709, |
| 94 |
MatrixCoefficients::Unspecified, |
| 95 |
MatrixCoefficients::FCC, |
| 96 |
MatrixCoefficients::BT470BG, |
| 97 |
MatrixCoefficients::BT601, |
| 98 |
MatrixCoefficients::SMPTE240, |
| 99 |
MatrixCoefficients::YCgCo, |
| 100 |
MatrixCoefficients::BT2020NCL, |
| 101 |
MatrixCoefficients::BT2020CL, |
| 102 |
MatrixCoefficients::SMPTE2085, |
| 103 |
MatrixCoefficients::ChromatNCL, |
| 104 |
MatrixCoefficients::ChromatCL, |
| 105 |
MatrixCoefficients::ICtCp, |
| 106 |
])?, |
| 107 |
})) |
| 108 |
} |
| 109 |
|
| 110 |
#[inline] |
| 111 |
fn arbitrary_chromacity_point( |
| 112 |
u: &mut Unstructured<'_>, |
| 113 |
) -> Result<ChromaticityPoint, Error> { |
| 114 |
Ok(ChromaticityPoint { |
| 115 |
x: Arbitrary::arbitrary(u)?, |
| 116 |
y: Arbitrary::arbitrary(u)?, |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
#[inline] |
| 121 |
fn arbitrary_mastering_display( |
| 122 |
u: &mut Unstructured<'_>, |
| 123 |
) -> Result<Option<MasteringDisplay>, Error> { |
| 124 |
if Arbitrary::arbitrary(u)? { |
| 125 |
return Ok(None); |
| 126 |
} |
| 127 |
Ok(Some(MasteringDisplay { |
| 128 |
primaries: [ |
| 129 |
arbitrary_chromacity_point(u)?, |
| 130 |
arbitrary_chromacity_point(u)?, |
| 131 |
arbitrary_chromacity_point(u)?, |
| 132 |
], |
| 133 |
white_point: arbitrary_chromacity_point(u)?, |
| 134 |
max_luminance: Arbitrary::arbitrary(u)?, |
| 135 |
min_luminance: Arbitrary::arbitrary(u)?, |
| 136 |
})) |
| 137 |
} |
| 138 |
|
| 139 |
#[inline] |
| 140 |
fn arbitrary_content_light( |
| 141 |
u: &mut Unstructured<'_>, |
| 142 |
) -> Result<Option<ContentLight>, Error> { |
| 143 |
if Arbitrary::arbitrary(u)? { |
| 144 |
return Ok(None); |
| 145 |
} |
| 146 |
Ok(Some(ContentLight { |
| 147 |
max_content_light_level: Arbitrary::arbitrary(u)?, |
| 148 |
max_frame_average_light_level: Arbitrary::arbitrary(u)?, |
| 149 |
})) |
| 150 |
} |
| 151 |
|
| 152 |
impl Arbitrary<'_> for ArbitraryConfig { |
| 153 |
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self, Error> { |
| 154 |
let mut enc = EncoderConfig::with_speed_preset(Arbitrary::arbitrary(u)?); |
| 155 |
enc.width = Arbitrary::arbitrary(u)?; |
| 156 |
enc.height = Arbitrary::arbitrary(u)?; |
| 157 |
enc.bit_depth = u.int_in_range(0..=16)?; |
| 158 |
enc.still_picture = Arbitrary::arbitrary(u)?; |
| 159 |
enc.time_base = arbitrary_rational(u)?; |
| 160 |
enc.min_key_frame_interval = Arbitrary::arbitrary(u)?; |
| 161 |
enc.max_key_frame_interval = Arbitrary::arbitrary(u)?; |
| 162 |
enc.reservoir_frame_delay = Arbitrary::arbitrary(u)?; |
| 163 |
enc.low_latency = Arbitrary::arbitrary(u)?; |
| 164 |
enc.quantizer = Arbitrary::arbitrary(u)?; |
| 165 |
enc.min_quantizer = Arbitrary::arbitrary(u)?; |
| 166 |
enc.bitrate = Arbitrary::arbitrary(u)?; |
| 167 |
enc.tile_cols = Arbitrary::arbitrary(u)?; |
| 168 |
enc.tile_rows = Arbitrary::arbitrary(u)?; |
| 169 |
enc.tiles = Arbitrary::arbitrary(u)?; |
| 170 |
enc.speed_settings.rdo_lookahead_frames = Arbitrary::arbitrary(u)?; |
| 171 |
let config = Config::new().with_encoder_config(enc).with_threads(1); |
| 172 |
Ok(Self { config }) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
pub fn fuzz_construct_context(arbitrary: ArbitraryConfig) { |
| 177 |
let _: Result<Context<u16>, _> = arbitrary.config.new_context(); |
| 178 |
} |
| 179 |
|
| 180 |
fn encode_frames( |
| 181 |
ctx: &mut Context<u8>, mut frames: impl Iterator<Item = Frame<u8>>, |
| 182 |
) -> Result<(), EncoderStatus> { |
| 183 |
loop { |
| 184 |
let rv = ctx.receive_packet(); |
| 185 |
log::debug!("ctx.receive_packet() = {:#?}", rv); |
| 186 |
|
| 187 |
match rv { |
| 188 |
Ok(_packet) => {} |
| 189 |
Err(EncoderStatus::Encoded) => {} |
| 190 |
Err(EncoderStatus::LimitReached) => { |
| 191 |
break; |
| 192 |
} |
| 193 |
Err(EncoderStatus::NeedMoreData) => { |
| 194 |
ctx.send_frame(frames.next().map(Arc::new))?; |
| 195 |
} |
| 196 |
Err(EncoderStatus::EnoughData) => { |
| 197 |
unreachable!(); |
| 198 |
} |
| 199 |
Err(EncoderStatus::NotReady) => { |
| 200 |
unreachable!(); |
| 201 |
} |
| 202 |
Err(EncoderStatus::Failure) => { |
| 203 |
return Err(EncoderStatus::Failure); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
Ok(()) |
| 209 |
} |
| 210 |
|
| 211 |
#[derive(Debug)] |
| 212 |
pub struct ArbitraryEncoder { |
| 213 |
config: Config, |
| 214 |
frame_count: u8, |
| 215 |
pixels: Box<[u8]>, |
| 216 |
} |
| 217 |
|
| 218 |
impl Arbitrary<'_> for ArbitraryEncoder { |
| 219 |
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self, Error> { |
| 220 |
let enc = EncoderConfig { |
| 221 |
speed_settings: SpeedSettings::from_preset(u.int_in_range(0..=10)?), |
| 222 |
width: u.int_in_range(1..=256)?, |
| 223 |
height: u.int_in_range(1..=256)?, |
| 224 |
still_picture: Arbitrary::arbitrary(u)?, |
| 225 |
time_base: arbitrary_rational(u)?, |
| 226 |
min_key_frame_interval: u.int_in_range(0..=3)?, |
| 227 |
max_key_frame_interval: u.int_in_range(1..=4)?, |
| 228 |
low_latency: Arbitrary::arbitrary(u)?, |
| 229 |
quantizer: Arbitrary::arbitrary(u)?, |
| 230 |
min_quantizer: Arbitrary::arbitrary(u)?, |
| 231 |
bitrate: Arbitrary::arbitrary(u)?, |
| 232 |
tile_cols: u.int_in_range(0..=2)?, |
| 233 |
tile_rows: u.int_in_range(0..=2)?, |
| 234 |
tiles: u.int_in_range(0..=16)?, |
| 235 |
|
| 236 |
chroma_sampling: *u.choose(&[ |
| 237 |
ChromaSampling::Cs420, |
| 238 |
ChromaSampling::Cs422, |
| 239 |
ChromaSampling::Cs444, |
| 240 |
ChromaSampling::Cs400, |
| 241 |
])?, |
| 242 |
chroma_sample_position: *u.choose(&[ |
| 243 |
ChromaSamplePosition::Unknown, |
| 244 |
ChromaSamplePosition::Vertical, |
| 245 |
ChromaSamplePosition::Colocated, |
| 246 |
])?, |
| 247 |
pixel_range: *u.choose(&[PixelRange::Limited, PixelRange::Full])?, |
| 248 |
error_resilient: Arbitrary::arbitrary(u)?, |
| 249 |
reservoir_frame_delay: Arbitrary::arbitrary(u)?, |
| 250 |
|
| 251 |
sample_aspect_ratio: arbitrary_rational(u)?, |
| 252 |
bit_depth: 8, |
| 253 |
color_description: arbitrary_color_description(u)?, |
| 254 |
mastering_display: arbitrary_mastering_display(u)?, |
| 255 |
content_light: arbitrary_content_light(u)?, |
| 256 |
level_idx: Some(31), |
| 257 |
enable_timing_info: Arbitrary::arbitrary(u)?, |
| 258 |
switch_frame_interval: u.int_in_range(0..=3)?, |
| 259 |
tune: *u.choose(&[Tune::Psnr, Tune::Psychovisual])?, |
| 260 |
film_grain_params: None, |
| 261 |
}; |
| 262 |
|
| 263 |
let frame_count = |
| 264 |
if enc.still_picture { 1 } else { u.int_in_range(1..=3)? }; |
| 265 |
if u.is_empty() { |
| 266 |
return Err(Error::NotEnoughData); |
| 267 |
} |
| 268 |
let pixels = u.bytes(u.len())?.to_vec().into_boxed_slice(); |
| 269 |
let config = Config::new().with_encoder_config(enc).with_threads(1); |
| 270 |
Ok(Self { config, frame_count, pixels }) |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
pub fn fuzz_encode(arbitrary: ArbitraryEncoder) { |
| 275 |
let res = arbitrary.config.new_context(); |
| 276 |
if res.is_err() { |
| 277 |
return; |
| 278 |
} |
| 279 |
let mut context: Context<u8> = res.unwrap(); |
| 280 |
|
| 281 |
let mut pixels = arbitrary.pixels.iter().cycle(); |
| 282 |
let mut frame = context.new_frame(); |
| 283 |
let frames = (0..arbitrary.frame_count).map(|_| { |
| 284 |
for plane in &mut frame.planes { |
| 285 |
let stride = plane.cfg.stride; |
| 286 |
for row in plane.data_origin_mut().chunks_mut(stride) { |
| 287 |
for pixel in row { |
| 288 |
*pixel = *pixels.next().unwrap(); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
frame.clone() |
| 294 |
}); |
| 295 |
|
| 296 |
let _ = encode_frames(&mut context, frames); |
| 297 |
} |
| 298 |
|
| 299 |
#[derive(Debug)] |
| 300 |
pub struct DecodeTestParameters<T: Pixel> { |
| 301 |
w: usize, |
| 302 |
h: usize, |
| 303 |
speed: u8, |
| 304 |
q: usize, |
| 305 |
limit: usize, |
| 306 |
bit_depth: usize, |
| 307 |
chroma_sampling: ChromaSampling, |
| 308 |
min_keyint: u64, |
| 309 |
max_keyint: u64, |
| 310 |
switch_frame_interval: u64, |
| 311 |
low_latency: bool, |
| 312 |
error_resilient: bool, |
| 313 |
bitrate: i32, |
| 314 |
tile_cols_log2: usize, |
| 315 |
tile_rows_log2: usize, |
| 316 |
still_picture: bool, |
| 317 |
pixel: PhantomData<T>, |
| 318 |
} |
| 319 |
|
| 320 |
impl<T: Pixel> Arbitrary<'_> for DecodeTestParameters<T> { |
| 321 |
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self, Error> { |
| 322 |
let mut p = Self { |
| 323 |
w: u.int_in_range(16..=16 + 255)?, |
| 324 |
h: u.int_in_range(16..=16 + 255)?, |
| 325 |
speed: u.int_in_range(0..=10)?, |
| 326 |
q: u8::arbitrary(u)?.into(), |
| 327 |
limit: u.int_in_range(1..=3)?, |
| 328 |
bit_depth: 8, |
| 329 |
chroma_sampling: *u.choose(&[ |
| 330 |
ChromaSampling::Cs420, |
| 331 |
ChromaSampling::Cs422, |
| 332 |
ChromaSampling::Cs444, |
| 333 |
ChromaSampling::Cs400, |
| 334 |
])?, |
| 335 |
min_keyint: u.int_in_range(0..=3)?, |
| 336 |
max_keyint: u.int_in_range(1..=4)?, |
| 337 |
switch_frame_interval: u.int_in_range(0..=3)?, |
| 338 |
low_latency: bool::arbitrary(u)?, |
| 339 |
error_resilient: bool::arbitrary(u)?, |
| 340 |
bitrate: u16::arbitrary(u)?.into(), |
| 341 |
tile_cols_log2: u.int_in_range(0..=2)?, |
| 342 |
tile_rows_log2: u.int_in_range(0..=2)?, |
| 343 |
still_picture: bool::arbitrary(u)?, |
| 344 |
pixel: PhantomData, |
| 345 |
}; |
| 346 |
if matches!(T::type_enum(), PixelType::U16) { |
| 347 |
p.bit_depth = *u.choose(&[8, 10, 12])?; |
| 348 |
} |
| 349 |
if !p.low_latency { |
| 350 |
p.switch_frame_interval = 0; |
| 351 |
} |
| 352 |
if p.still_picture { |
| 353 |
p.limit = 1 |
| 354 |
} |
| 355 |
Ok(p) |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
#[cfg(feature = "decode_test_dav1d")] |
| 360 |
pub fn fuzz_encode_decode<T: Pixel>(p: DecodeTestParameters<T>) { |
| 361 |
use crate::test_encode_decode::*; |
| 362 |
|
| 363 |
let mut dec = get_decoder::<T>("dav1d", p.w, p.h); |
| 364 |
dec.encode_decode( |
| 365 |
true, |
| 366 |
p.w, |
| 367 |
p.h, |
| 368 |
p.speed, |
| 369 |
p.q, |
| 370 |
p.limit, |
| 371 |
p.bit_depth, |
| 372 |
p.chroma_sampling, |
| 373 |
p.min_keyint, |
| 374 |
p.max_keyint, |
| 375 |
p.switch_frame_interval, |
| 376 |
p.low_latency, |
| 377 |
p.error_resilient, |
| 378 |
p.bitrate, |
| 379 |
p.tile_cols_log2, |
| 380 |
p.tile_rows_log2, |
| 381 |
p.still_picture, |
| 382 |
None, |
| 383 |
); |
| 384 |
} |