forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

fuzz.rs174 lines, 5.6 KiBHistoryRawDownload
1//! Ten thousand malformed parameter sets and slice headers, none of which may
2//! panic.
3//!
4//! The parse side of this crate is what a hardware decode path feeds from, and
5//! its input is whatever a file on disk contains. Truncation must come back as
6//! `NeedMore`, a rule violation as `Corrupt`, and a construct this family does
7//! not implement as `Unsupported` — never as an index out of bounds.
8
9use ec_h265_syntax::nal::{NalHeader, split_annex_b, unescape_rbsp};
10use ec_h265_syntax::ps::{ConformanceWindow, ProfileTierLevel};
11use ec_h265_syntax::slice::SliceHeader;
12use ec_h265_syntax::{NalUnitType, Pps, Sps, Vps};
13
14/// xorshift64*, so the corpus is the same on every run and a failure is
15/// reproducible from its seed alone.
16struct Rng(u64);
17
18impl Rng {
19 fn next(&mut self) -> u64 {
20 let mut x = self.0;
21 x ^= x >> 12;
22 x ^= x << 25;
23 x ^= x >> 27;
24 self.0 = x;
25 x.wrapping_mul(0x2545_f491_4f6c_dd1d)
26 }
27
28 fn byte(&mut self) -> u8 {
29 (self.next() >> 33) as u8
30 }
31
32 fn below(&mut self, n: usize) -> usize {
33 (self.next() >> 33) as usize % n.max(1)
34 }
35}
36
37fn reference_sps() -> Sps {
38 Sps {
39 max_latency_increase_plus1: 0,
40 extension_flags: 0,
41 range: Default::default(),
42 vps_id: 0,
43 id: 0,
44 chroma_format_idc: 1,
45 separate_colour_plane: false,
46 pic_width_in_luma_samples: 1920,
47 pic_height_in_luma_samples: 1088,
48 conf_win: ConformanceWindow {
49 left: 0,
50 right: 0,
51 top: 0,
52 bottom: 4,
53 },
54 bit_depth_luma_minus8: 0,
55 bit_depth_chroma_minus8: 0,
56 log2_max_poc_lsb_minus4: 4,
57 max_dec_pic_buffering_minus1: 0,
58 max_num_reorder_pics: 0,
59 log2_min_cb_size_minus3: 0,
60 log2_diff_max_min_cb_size: 3,
61 log2_min_tb_size_minus2: 0,
62 log2_diff_max_min_tb_size: 3,
63 max_transform_hierarchy_depth_inter: 0,
64 max_transform_hierarchy_depth_intra: 0,
65 scaling_list_enabled: false,
66 scaling_list: None,
67 amp_enabled: false,
68 sao_enabled: true,
69 pcm_enabled: false,
70 pcm: None,
71 num_short_term_ref_pic_sets: 0,
72 short_term_ref_pic_sets: Vec::new(),
73 long_term_ref_pics_present: false,
74 num_long_term_ref_pics_sps: 0,
75 long_term_ref_pics_sps: Vec::new(),
76 temporal_mvp_enabled: true,
77 strong_intra_smoothing: true,
78 ptl: ProfileTierLevel::main(120),
79 vui: None,
80 }
81}
82
83#[test]
84fn ten_thousand_malformed_inputs_never_panic() {
85 let mut rng = Rng(0x5eed_1234_abcd_0001);
86 let sps = reference_sps();
87 let pps = Pps {
88 entropy_coding_sync_enabled: true,
89 dependent_slice_segments_enabled: true,
90 tiles_enabled: true,
91 cu_qp_delta_enabled: true,
92 deblocking_filter_control_present: true,
93 deblocking_filter_override_enabled: true,
94 slice_chroma_qp_offsets_present: true,
95 lists_modification_present: true,
96 output_flag_present: true,
97 cabac_init_present: true,
98 num_extra_slice_header_bits: 2,
99 weighted_pred: true,
100 weighted_bipred: true,
101 slice_segment_header_extension_present: true,
102 ..Pps::default()
103 };
104 let seeds = [
105 sps.to_rbsp(),
106 pps.to_rbsp(),
107 Vps {
108 id: 0,
109 ptl: ProfileTierLevel::main(120),
110 max_dec_pic_buffering_minus1: 1,
111 max_num_reorder_pics: 0,
112 timing: None,
113 }
114 .to_rbsp(),
115 ];
116
117 for round in 0..10_000u32 {
118 // Half the corpus is noise, half is a valid structure with bytes
119 // flipped — the shape that actually reaches a decoder off a disk.
120 let mut data = if round % 2 == 0 {
121 let len = rng.below(96);
122 (0..len).map(|_| rng.byte()).collect::<Vec<u8>>()
123 } else {
124 let mut base = seeds[rng.below(seeds.len())].clone();
125 let flips = 1 + rng.below(4);
126 for _ in 0..flips {
127 if base.is_empty() {
128 break;
129 }
130 let at = rng.below(base.len());
131 base[at] ^= 1 << rng.below(8);
132 }
133 if rng.below(4) == 0 {
134 base.truncate(rng.below(base.len().max(1)));
135 }
136 base
137 };
138
139 let _ = Vps::parse(&data);
140 let _ = Sps::parse(&data);
141 let _ = Pps::parse(&data);
142 for nal_type in [NalUnitType::IdrWRadl, NalUnitType::TrailR] {
143 let _ = SliceHeader::parse(&data, &sps, &pps, nal_type);
144 }
145 let _ = NalHeader::parse(&data);
146 let _ = unescape_rbsp(&data);
147
148 // The same bytes as a stream, with start codes sprinkled in.
149 if round % 3 == 0 {
150 let at = rng.below(data.len().max(1));
151 data.splice(at..at, [0, 0, 1, 0x26, 0x01]);
152 for nal in split_annex_b(&data) {
153 let rbsp = nal.rbsp();
154 let _ = Sps::parse(&rbsp);
155 let _ = SliceHeader::parse(&rbsp, &sps, &pps, nal.header.nal_type);
156 }
157 }
158 }
159}
160
161#[test]
162fn truncation_is_need_more_not_corrupt() {
163 let sps = reference_sps().to_rbsp();
164 let mut saw_need_more = false;
165 for len in 0..sps.len() {
166 match Sps::parse(&sps[..len]) {
167 Err(e) if e.is_need_more() => saw_need_more = true,
168 // A shorter prefix can also be a legal-looking but wrong SPS; what
169 // it may never be is a panic, which is the point of the loop.
170 _ => {}
171 }
172 }
173 assert!(saw_need_more, "no prefix of an SPS reported NeedMore");
174}