@@ -123,16 +123,21 @@ def get_random_initial_content():
123
123
# allows us to debug any such failures that we run into.
124
124
retry = True
125
125
126
+ # Temporary files to clean up
127
+ temp_files = []
128
+
126
129
127
130
# Generate a random wasm file, and return a string that creates a typed array of
128
131
# those bytes, suitable for use in a JS file, in the form
129
132
#
130
133
# new Uint8Array([..wasm_contents..])
131
134
#
132
135
# Receives the testcase index and the output dir.
133
- def get_wasm_contents (i , output_dir ):
134
- input_data_file_path = os .path .join (output_dir , f'{ i } .input' )
135
- wasm_file_path = os .path .join (output_dir , f'{ i } .wasm' )
136
+ #
137
+ # Also returns the name of the wasm file.
138
+ def get_wasm_contents (name , output_dir , extra_args = []):
139
+ input_data_file_path = os .path .join (output_dir , f'{ name } .input' )
140
+ wasm_file_path = os .path .join (output_dir , f'{ name } .wasm' )
136
141
137
142
# wasm-opt may fail to run in rare cases (when the fuzzer emits code it
138
143
# detects as invalid). Just try again in such a case.
@@ -144,7 +149,7 @@ def get_wasm_contents(i, output_dir):
144
149
145
150
# Generate a command to use wasm-opt with the proper args to generate
146
151
# wasm content from the input data.
147
- cmd = [FUZZER_BINARY_PATH ] + FUZZER_ARGS
152
+ cmd = [FUZZER_BINARY_PATH ] + FUZZER_ARGS + extra_args
148
153
cmd += ['-o' , wasm_file_path , input_data_file_path ]
149
154
150
155
# Sometimes use a file from the initial content testcases.
@@ -177,16 +182,19 @@ def get_wasm_contents(i, output_dir):
177
182
with open (wasm_file_path , 'rb' ) as file :
178
183
wasm_contents = file .read ()
179
184
180
- # Clean up temp files.
181
- os .remove (wasm_file_path )
182
- os .remove (input_data_file_path )
185
+ # Note temp files.
186
+ global temp_files
187
+ temp_files += [
188
+ wasm_file_path ,
189
+ input_data_file_path
190
+ ]
183
191
184
192
# Convert to a string, and wrap into a typed array.
185
193
wasm_contents = ',' .join ([str (c ) for c in wasm_contents ])
186
194
js = f'new Uint8Array([{ wasm_contents } ])'
187
195
if initial_content :
188
196
js = f'{ js } /* using initial content { os .path .basename (initial_content )} */'
189
- return js
197
+ return js , wasm_file_path
190
198
191
199
192
200
# Returns the contents of a .js fuzz file, given the index of the testcase and
@@ -198,17 +206,22 @@ def get_js_file_contents(i, output_dir):
198
206
199
207
# Prepend the wasm contents, so they are used (rather than the normal
200
208
# mechanism where the wasm file's name is provided in argv).
201
- wasm_contents = get_wasm_contents (i , output_dir )
209
+ wasm_contents , wasm_file = get_wasm_contents (i , output_dir )
202
210
pre = f'var binary = { wasm_contents } ;\n '
203
211
bytes = wasm_contents .count (',' )
204
212
205
213
# Sometimes add a second wasm file as well.
206
214
has_second = False
207
215
if system_random .random () < 0.333 :
208
216
has_second = True
209
- wasm_contents = get_wasm_contents (i , output_dir )
210
- pre += f'var secondBinary = { wasm_contents } ;\n '
211
- bytes += wasm_contents .count (',' )
217
+ # Most of the time, import the first file.
218
+ args = []
219
+ if system_random .random () < 0.8 :
220
+ args = [f'--fuzz-import={ wasm_file } ' ]
221
+ second_wasm_contents , second_wasm_file = \
222
+ get_wasm_contents (f'{ i } _second' , output_dir , args )
223
+ pre += f'var secondBinary = { second_wasm_contents } ;\n '
224
+ bytes += second_wasm_contents .count (',' )
212
225
213
226
js = pre + '\n ' + js
214
227
@@ -243,7 +256,9 @@ def get_js_file_contents(i, output_dir):
243
256
]
244
257
if has_second :
245
258
extra_js_operations += [
246
- 'build(secondBinary)' ,
259
+ # Build the second binary, marking it as second so it imports the
260
+ # first.
261
+ 'build(secondBinary, true)' ,
247
262
]
248
263
249
264
for i in range (num ):
@@ -307,6 +322,11 @@ def main(argv):
307
322
308
323
print (f'Created { num } testcases.' )
309
324
325
+ for temp in temp_files :
326
+ os .remove (temp )
327
+
328
+ print ('Cleaned up.' )
329
+
310
330
311
331
if __name__ == '__main__' :
312
332
main (sys .argv )
0 commit comments