forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB5c8eff8Tahinli2 months ago2949 commits

fuzz.rs169 lines, 5.4 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 vps_id: 0,
40 id: 0,
41 chroma_format_idc: 1,
42 separate_colour_plane: false,
43 pic_width_in_luma_samples: 1920,
44 pic_height_in_luma_samples: 1088,
45 conf_win: ConformanceWindow {
46 left: 0,
47 right: 0,
48 top: 0,
49 bottom: 4,
50 },
51 bit_depth_luma_minus8: 0,
52 bit_depth_chroma_minus8: 0,
53 log2_max_poc_lsb_minus4: 4,
54 max_dec_pic_buffering_minus1: 0,
55 max_num_reorder_pics: 0,
56 log2_min_cb_size_minus3: 0,
57 log2_diff_max_min_cb_size: 3,
58 log2_min_tb_size_minus2: 0,
59 log2_diff_max_min_tb_size: 3,
60 max_transform_hierarchy_depth_inter: 0,
61 max_transform_hierarchy_depth_intra: 0,
62 scaling_list_enabled: false,
63 amp_enabled: false,
64 sao_enabled: true,
65 pcm_enabled: false,
66 pcm: None,
67 num_short_term_ref_pic_sets: 0,
68 short_term_ref_pic_sets: Vec::new(),
69 long_term_ref_pics_present: false,
70 num_long_term_ref_pics_sps: 0,
71 long_term_ref_pics_sps: Vec::new(),
72 temporal_mvp_enabled: true,
73 strong_intra_smoothing: true,
74 ptl: ProfileTierLevel::main(120),
75 vui: None,
76 }
77}
78
79#[test]
80fn ten_thousand_malformed_inputs_never_panic() {
81 let mut rng = Rng(0x5eed_1234_abcd_0001);
82 let sps = reference_sps();
83 let pps = Pps {
84 entropy_coding_sync_enabled: true,
85 dependent_slice_segments_enabled: true,
86 tiles_enabled: true,
87 cu_qp_delta_enabled: true,
88 deblocking_filter_control_present: true,
89 deblocking_filter_override_enabled: true,
90 slice_chroma_qp_offsets_present: true,
91 lists_modification_present: true,
92 output_flag_present: true,
93 cabac_init_present: true,
94 num_extra_slice_header_bits: 2,
95 weighted_pred: true,
96 weighted_bipred: true,
97 slice_segment_header_extension_present: true,
98 ..Pps::default()
99 };
100 let seeds = [
101 sps.to_rbsp(),
102 pps.to_rbsp(),
103 Vps {
104 id: 0,
105 ptl: ProfileTierLevel::main(120),
106 max_dec_pic_buffering_minus1: 1,
107 max_num_reorder_pics: 0,
108 }
109 .to_rbsp(),
110 ];
111
112 for round in 0..10_000u32 {
113 // Half the corpus is noise, half is a valid structure with bytes
114 // flipped — the shape that actually reaches a decoder off a disk.
115 let mut data = if round % 2 == 0 {
116 let len = rng.below(96);
117 (0..len).map(|_| rng.byte()).collect::<Vec<u8>>()
118 } else {
119 let mut base = seeds[rng.below(seeds.len())].clone();
120 let flips = 1 + rng.below(4);
121 for _ in 0..flips {
122 if base.is_empty() {
123 break;
124 }
125 let at = rng.below(base.len());
126 base[at] ^= 1 << rng.below(8);
127 }
128 if rng.below(4) == 0 {
129 base.truncate(rng.below(base.len().max(1)));
130 }
131 base
132 };
133
134 let _ = Vps::parse(&data);
135 let _ = Sps::parse(&data);
136 let _ = Pps::parse(&data);
137 for nal_type in [NalUnitType::IdrWRadl, NalUnitType::TrailR] {
138 let _ = SliceHeader::parse(&data, &sps, &pps, nal_type);
139 }
140 let _ = NalHeader::parse(&data);
141 let _ = unescape_rbsp(&data);
142
143 // The same bytes as a stream, with start codes sprinkled in.
144 if round % 3 == 0 {
145 let at = rng.below(data.len().max(1));
146 data.splice(at..at, [0, 0, 1, 0x26, 0x01]);
147 for nal in split_annex_b(&data) {
148 let rbsp = nal.rbsp();
149 let _ = Sps::parse(&rbsp);
150 let _ = SliceHeader::parse(&rbsp, &sps, &pps, nal.header.nal_type);
151 }
152 }
153 }
154}
155
156#[test]
157fn truncation_is_need_more_not_corrupt() {
158 let sps = reference_sps().to_rbsp();
159 let mut saw_need_more = false;
160 for len in 0..sps.len() {
161 match Sps::parse(&sps[..len]) {
162 Err(e) if e.is_need_more() => saw_need_more = true,
163 // A shorter prefix can also be a legal-looking but wrong SPS; what
164 // it may never be is a panic, which is the point of the loop.
165 _ => {}
166 }
167 }
168 assert!(saw_need_more, "no prefix of an SPS reported NeedMore");
169}