11#![ no_main]
22
33use arbitrary:: Arbitrary ;
4- use core:: fmt:: { Debug } ;
4+ use core:: fmt:: Debug ;
55use libfuzzer_sys:: fuzz_target;
66use replace_with:: replace_with_or_abort;
77use std:: fmt:: { Arguments , Formatter , Write } ;
8- use std:: io:: { Cursor , Seek , SeekFrom } ;
98use std:: io:: Write as IoWrite ;
9+ use std:: io:: { Cursor , Seek , SeekFrom } ;
1010use std:: path:: PathBuf ;
1111use tikv_jemallocator:: Jemalloc ;
1212use zip:: result:: { ZipError , ZipResult } ;
@@ -93,22 +93,36 @@ fn do_operation<'k>(
9393 flush_on_finish_file : bool ,
9494 files_added : & mut usize ,
9595 stringifier : & mut impl Write ,
96- panic_on_error : bool
96+ panic_on_error : bool ,
9797) -> Result < ( ) , Box < dyn std:: error:: Error > > {
9898 writer. set_flush_on_finish_file ( flush_on_finish_file) ;
99- let FileOperation { basic, mut path, reopen} = operation;
99+ let FileOperation {
100+ basic,
101+ mut path,
102+ reopen,
103+ } = operation;
100104 match basic {
101105 BasicFileOperation :: WriteNormalFile {
102- contents, mut options, ..
106+ contents,
107+ mut options,
108+ ..
103109 } => {
104110 let uncompressed_size = contents. iter ( ) . map ( |chunk| chunk. len ( ) ) . sum :: < usize > ( ) ;
105111 if uncompressed_size >= u32:: MAX as usize {
106112 options = options. large_file ( true ) ;
107113 }
108114 if options == FullFileOptions :: default ( ) {
109- writeln ! ( stringifier, "writer.start_file_from_path({:?}, Default::default())?;" , path) ?;
115+ writeln ! (
116+ stringifier,
117+ "writer.start_file_from_path({:?}, Default::default())?;" ,
118+ path
119+ ) ?;
110120 } else {
111- writeln ! ( stringifier, "writer.start_file_from_path({:?}, {:?})?;" , path, options) ?;
121+ writeln ! (
122+ stringifier,
123+ "writer.start_file_from_path({:?}, {:?})?;" ,
124+ path, options
125+ ) ?;
112126 }
113127 writer. start_file_from_path ( & * path, options) ?;
114128 for chunk in contents. iter ( ) {
@@ -118,12 +132,20 @@ fn do_operation<'k>(
118132 * files_added += 1 ;
119133 }
120134 BasicFileOperation :: WriteDirectory ( options) => {
121- writeln ! ( stringifier, "writer.add_directory_from_path(&{:?}, {:?})?;" , path, options) ?;
135+ writeln ! (
136+ stringifier,
137+ "writer.add_directory_from_path(&{:?}, {:?})?;" ,
138+ path, options
139+ ) ?;
122140 writer. add_directory_from_path ( & * path, options. to_owned ( ) ) ?;
123141 * files_added += 1 ;
124142 }
125143 BasicFileOperation :: WriteSymlinkWithTarget { target, options } => {
126- writeln ! ( stringifier, "writer.add_symlink_from_path(&{:?}, {:?}, {:?});" , path, target, options) ?;
144+ writeln ! (
145+ stringifier,
146+ "writer.add_symlink_from_path(&{:?}, {:?}, {:?});" ,
147+ path, target, options
148+ ) ?;
127149 writer. add_symlink_from_path ( & * path, target, options. to_owned ( ) ) ?;
128150 * files_added += 1 ;
129151 }
@@ -132,8 +154,20 @@ fn do_operation<'k>(
132154 return Ok ( ( ) ) ;
133155 } ;
134156 deduplicate_paths ( & mut path, & base_path) ;
135- do_operation ( writer, * base, false , flush_on_finish_file, files_added, stringifier, panic_on_error) ?;
136- writeln ! ( stringifier, "writer.shallow_copy_file_from_path({:?}, {:?});" , base_path, path) ?;
157+ do_operation (
158+ writer,
159+ * base,
160+ false ,
161+ flush_on_finish_file,
162+ files_added,
163+ stringifier,
164+ panic_on_error,
165+ ) ?;
166+ writeln ! (
167+ stringifier,
168+ "writer.shallow_copy_file_from_path({:?}, {:?});" ,
169+ base_path, path
170+ ) ?;
137171 writer. shallow_copy_file_from_path ( & * base_path, & * path) ?;
138172 * files_added += 1 ;
139173 }
@@ -142,38 +176,65 @@ fn do_operation<'k>(
142176 return Ok ( ( ) ) ;
143177 } ;
144178 deduplicate_paths ( & mut path, & base_path) ;
145- do_operation ( writer, * base, false , flush_on_finish_file, files_added, stringifier, panic_on_error) ?;
146- writeln ! ( stringifier, "writer.deep_copy_file_from_path({:?}, {:?});" , base_path, path) ?;
179+ do_operation (
180+ writer,
181+ * base,
182+ false ,
183+ flush_on_finish_file,
184+ files_added,
185+ stringifier,
186+ panic_on_error,
187+ ) ?;
188+ writeln ! (
189+ stringifier,
190+ "writer.deep_copy_file_from_path({:?}, {:?});" ,
191+ base_path, path
192+ ) ?;
147193 writer. deep_copy_file_from_path ( & * base_path, path) ?;
148194 * files_added += 1 ;
149195 }
150- BasicFileOperation :: MergeWithOtherFile { operations, initial_junk } => {
196+ BasicFileOperation :: MergeWithOtherFile {
197+ operations,
198+ initial_junk,
199+ } => {
151200 if initial_junk. is_empty ( ) {
152- writeln ! ( stringifier, "let sub_writer = {{\n \
153- let mut writer = ZipWriter::new(Cursor::new(Vec::new()));") ?;
201+ writeln ! (
202+ stringifier,
203+ "let sub_writer = {{\n \
204+ let mut writer = ZipWriter::new(Cursor::new(Vec::new()));"
205+ ) ?;
154206 } else {
155- writeln ! ( stringifier,
156- "let sub_writer = {{\n \
207+ writeln ! (
208+ stringifier,
209+ "let sub_writer = {{\n \
157210 let mut initial_junk = Cursor::new(vec!{:?});\n \
158211 initial_junk.seek(SeekFrom::End(0))?;
159- let mut writer = ZipWriter::new(initial_junk);" , initial_junk) ?;
212+ let mut writer = ZipWriter::new(initial_junk);" ,
213+ initial_junk
214+ ) ?;
160215 }
161216 let mut initial_junk = Cursor :: new ( initial_junk. into_vec ( ) ) ;
162217 initial_junk. seek ( SeekFrom :: End ( 0 ) ) ?;
163218 let mut other_writer = zip:: ZipWriter :: new ( initial_junk) ;
164219 let mut inner_files_added = 0 ;
165- operations. into_vec ( ) . into_iter ( ) . for_each ( |( operation, abort) | {
166- let _ = do_operation (
167- & mut other_writer,
168- operation,
169- abort,
170- false ,
171- & mut inner_files_added,
172- stringifier,
173- panic_on_error
174- ) ;
175- } ) ;
176- writeln ! ( stringifier, "writer\n }};\n writer.merge_archive(sub_writer.finish_into_readable()?)?;" ) ?;
220+ operations
221+ . into_vec ( )
222+ . into_iter ( )
223+ . for_each ( |( operation, abort) | {
224+ let _ = do_operation (
225+ & mut other_writer,
226+ operation,
227+ abort,
228+ false ,
229+ & mut inner_files_added,
230+ stringifier,
231+ panic_on_error,
232+ ) ;
233+ } ) ;
234+ writeln ! (
235+ stringifier,
236+ "writer\n }};\n writer.merge_archive(sub_writer.finish_into_readable()?)?;"
237+ ) ?;
177238 writer. merge_archive ( other_writer. finish_into_readable ( ) ?) ?;
178239 * files_added += inner_files_added;
179240 }
@@ -193,15 +254,19 @@ fn do_operation<'k>(
193254 match reopen {
194255 ReopenOption :: DoNotReopen => {
195256 writeln ! ( stringifier, "writer" ) ?;
196- return Ok ( ( ) )
197- } ,
257+ return Ok ( ( ) ) ;
258+ }
198259 ReopenOption :: ViaFinish => {
199260 let old_comment = writer. get_raw_comment ( ) . to_owned ( ) ;
200- writeln ! ( stringifier, "let mut writer = ZipWriter::new_append(writer.finish()?)?;" ) ?;
261+ writeln ! (
262+ stringifier,
263+ "let mut writer = ZipWriter::new_append(writer.finish()?)?;"
264+ ) ?;
201265 replace_with_or_abort ( writer, |old_writer : zip:: ZipWriter < Cursor < Vec < u8 > > > | {
202266 ( || -> ZipResult < zip:: ZipWriter < Cursor < Vec < u8 > > > > {
203267 zip:: ZipWriter :: new_append ( old_writer. finish ( ) ?)
204- } ) ( ) . unwrap_or_else ( |_| {
268+ } ) ( )
269+ . unwrap_or_else ( |_| {
205270 if panic_on_error {
206271 panic ! ( "Failed to create new ZipWriter" )
207272 }
@@ -214,11 +279,15 @@ fn do_operation<'k>(
214279 }
215280 ReopenOption :: ViaFinishIntoReadable => {
216281 let old_comment = writer. get_raw_comment ( ) . to_owned ( ) ;
217- writeln ! ( stringifier, "let mut writer = ZipWriter::new_append(writer.finish()?)?;" ) ?;
282+ writeln ! (
283+ stringifier,
284+ "let mut writer = ZipWriter::new_append(writer.finish()?)?;"
285+ ) ?;
218286 replace_with_or_abort ( writer, |old_writer| {
219287 ( || -> ZipResult < zip:: ZipWriter < Cursor < Vec < u8 > > > > {
220288 zip:: ZipWriter :: new_append ( old_writer. finish ( ) ?)
221- } ) ( ) . unwrap_or_else ( |_| {
289+ } ) ( )
290+ . unwrap_or_else ( |_| {
222291 if panic_on_error {
223292 panic ! ( "Failed to create new ZipWriter" )
224293 }
@@ -231,7 +300,7 @@ fn do_operation<'k>(
231300 Ok ( ( ) )
232301}
233302
234- impl < ' k > FuzzTestCase < ' k > {
303+ impl < ' k > FuzzTestCase < ' k > {
235304 fn execute ( self , stringifier : & mut impl Write , panic_on_error : bool ) -> ZipResult < ( ) > {
236305 let mut initial_junk = Cursor :: new ( self . initial_junk . into_vec ( ) ) ;
237306 initial_junk. seek ( SeekFrom :: End ( 0 ) ) ?;
@@ -253,7 +322,7 @@ impl <'k> FuzzTestCase<'k> {
253322 self . flush_on_finish_file ,
254323 & mut files_added,
255324 stringifier,
256- panic_on_error
325+ panic_on_error,
257326 ) ;
258327 }
259328 if final_reopen {
@@ -265,14 +334,21 @@ impl <'k> FuzzTestCase<'k> {
265334 }
266335}
267336
268- impl < ' k > Debug for FuzzTestCase < ' k > {
337+ impl < ' k > Debug for FuzzTestCase < ' k > {
269338 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
270339 if self . initial_junk . is_empty ( ) {
271- writeln ! ( f, "let mut writer = ZipWriter::new(Cursor::new(Vec::new()));" ) ?;
340+ writeln ! (
341+ f,
342+ "let mut writer = ZipWriter::new(Cursor::new(Vec::new()));"
343+ ) ?;
272344 } else {
273- writeln ! ( f, "let mut initial_junk = Cursor::new(vec!{:?});\n \
345+ writeln ! (
346+ f,
347+ "let mut initial_junk = Cursor::new(vec!{:?});\n \
274348 initial_junk.seek(SeekFrom::End(0))?;\n \
275- let mut writer = ZipWriter::new(initial_junk);", & self . initial_junk) ?;
349+ let mut writer = ZipWriter::new(initial_junk);",
350+ & self . initial_junk
351+ ) ?;
276352 }
277353 let _ = self . clone ( ) . execute ( f, false ) ;
278354 Ok ( ( ) )
0 commit comments