forked from Eijebong/Archipelago-fuzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz.py
More file actions
763 lines (614 loc) · 25.9 KB
/
fuzz.py
File metadata and controls
763 lines (614 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
__version__ = "0.2.2"
import sys
import os
ap_path = os.path.abspath(os.path.dirname(sys.argv[0]))
sys.path.insert(0, ap_path)
# Prevent multiprocess workers from spamming nonsense when KeyboardInterrupted
# I can't wait for this to hide actual issues...
if __name__ == "__mp_main__":
sys.stderr = None
from worlds import AutoWorldRegister
from Options import (
get_option_groups,
Choice,
Toggle,
Range,
ItemSet,
ItemDict,
LocationSet,
NumericOption,
OptionSet,
FreeText,
PlandoConnections,
OptionList,
PlandoTexts,
OptionDict,
OptionError,
)
from BaseClasses import PlandoOptions
from Utils import __version__ as __ap_version__
import Utils
import settings
from Generate import main as GenMain
from Fill import FillError
from Main import main as ERmain
from settings import get_settings
from argparse import Namespace, ArgumentParser
from concurrent.futures import TimeoutError
from collections import defaultdict
import threading
from contextlib import redirect_stderr, redirect_stdout
from enum import Enum
from functools import wraps
from io import StringIO
from multiprocessing import Pool
import importlib
import json
import functools
import logging
import multiprocessing
import platform
import random
import shutil
import signal
import string
import tempfile
import time
import traceback
import yaml
OUT_DIR = f"fuzz_output"
settings.no_gui = True
settings.skip_autosave = True
MP_HOOKS = []
# We patch this because AP can't keep its hands to itself and has to start a thread to clean stuff up.
# We could monkey patch the hell out of it but since it's an inner function, I feel like the complexity
# of it is unreasonable compared to just reimplement a logger
# especially since it allows us to not have to cheat user_path
# Taken from https://github.com/ArchipelagoMW/Archipelago/blob/0.5.1.Hotfix1/Utils.py#L488
# and removed everythinhg that had to do with files, typing and cleanup
def patched_init_logging(
name,
loglevel = logging.INFO,
write_mode = "w",
log_format = "[%(name)s at %(asctime)s]: %(message)s",
exception_logger = None,
*args,
**kwargs
):
loglevel: int = Utils.loglevel_mapping.get(loglevel, loglevel)
root_logger = logging.getLogger()
for handler in root_logger.handlers[:]:
root_logger.removeHandler(handler)
handler.close()
root_logger.setLevel(loglevel)
class Filter(logging.Filter):
def __init__(self, filter_name, condition) -> None:
super().__init__(filter_name)
self.condition = condition
def filter(self, record: logging.LogRecord) -> bool:
return self.condition(record)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.addFilter(Filter("NoFile", lambda record: not getattr(record, "NoStream", False)))
root_logger.addHandler(stream_handler)
# Relay unhandled exceptions to logger.
if not getattr(sys.excepthook, "_wrapped", False): # skip if already modified
orig_hook = sys.excepthook
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
logging.getLogger(exception_logger).exception("Uncaught exception",
exc_info=(exc_type, exc_value, exc_traceback))
return orig_hook(exc_type, exc_value, exc_traceback)
handle_exception._wrapped = True
sys.excepthook = handle_exception
logging.info(
f"Archipelago ({__ap_version__}) logging initialized"
f" on {platform.platform()}"
f" running Python {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
)
Utils.init_logging = patched_init_logging
class FuzzerException(Exception):
def __init__(self, desc, out_buf):
if isinstance(out_buf, str):
self.out_buf = out_buf
else:
self.out_buf = out_buf.getvalue()
self.desc = desc
super().__init__(desc)
def __reduce__(self):
return (self.__class__, (self.desc, self.out_buf))
def exception_in_causes(e, ty):
if isinstance(e, ty):
return True
if e.__cause__ is not None:
return exception_in_causes(e.__cause__, ty)
return False
def world_from_apworld_name(apworld_name):
for name, world in AutoWorldRegister.world_types.items():
if world.__module__.startswith(f"worlds.{apworld_name}"):
return name, world
raise Exception(f"Couldn't find loaded world with world: {apworld_name}")
# See https://github.com/yaml/pyyaml/issues/103
yaml.SafeDumper.ignore_aliases = lambda *args: True
# Adapted from archipelago'd generate_yaml_templates
# https://github.com/ArchipelagoMW/Archipelago/blob/f75a1ae1174fb467e5c5bd5568d7de3c806d5b1c/Options.py#L1504
def generate_random_yaml(world_name, meta):
def dictify_range(option):
data = {option.default: 50}
for sub_option in ["random", "random-low", "random-high"]:
if sub_option != option.default:
data[sub_option] = 0
notes = {}
for name, number in getattr(option, "special_range_names", {}).items():
notes[name] = f"equivalent to {number}"
if number in data:
data[name] = data[number]
del data[number]
else:
data[name] = 0
return data, notes
def sanitize(value):
if isinstance(value, frozenset):
return list(value)
return value
game_name, world = world_from_apworld_name(world_name)
if world is None:
raise Exception(f"Failed to resolve apworld from apworld name: {world_name}")
game_options = {}
option_groups = get_option_groups(world)
for group, options in option_groups.items():
for option_name, option_value in options.items():
override = meta.get(None, {}).get(option_name)
if not override:
override = meta.get(game_name, {}).get(option_name)
if override is not None:
game_options[option_name] = override
continue
game_options[option_name] = sanitize(
get_random_value(option_name, option_value)
)
yaml_content = {
"description": f"{game_name} Template, generated with https://github.com/Eijebong/Archipelago-fuzzer/tree/{__version__}",
"game": game_name,
"requires": {
"version": __ap_version__,
},
game_name: game_options,
}
res = yaml.safe_dump(yaml_content, sort_keys=False)
return res
def get_random_value(name, option):
if name == "item_links":
# Let's not fuck with item links right now, I'm scared
return option.default
if name == "megamix_mod_data":
# Megamix is a special child and requires this to be valid JSON. Since we can't provide that, just ignore it
return option.default
if issubclass(option, (PlandoConnections, PlandoTexts)):
# See, I was already afraid with item_links but now it's plain terror. Let's not ever touch this ever.
return option.default
if name == "gfxmod":
# XXX: LADX has this and it should be a choice but is freetext for some reason...
# Putting invalid values here means the gen fails even though it doesn't affect any logic
# Just return Link for now.
return "Link"
if issubclass(option, OptionDict):
# This is for example factorio's start_items and worldgen settings. I don't think it's worth randomizing those as I'm not expecting the generation outcome to change from them.
# Plus I have no idea how to randomize them in the first place :)
return option.default
if issubclass(option, (Choice, Toggle)):
valid_choices = [key for key in option.options.keys() if key not in option.aliases]
if not valid_choices:
valid_choices = list(option.options.keys())
return random.choice(valid_choices)
if issubclass(option, Range):
return random.randint(option.range_start, option.range_end)
if issubclass(option, (ItemSet, ItemDict, LocationSet)):
# I don't know what to do here so just return the default value instead of a random one.
# This affects options like start inventory, local items, non local
# items so it's not the end of the world if they don't get randomized
# but we might want to look into that later on
return option.default
if issubclass(option, OptionSet):
return random.sample(
list(option.valid_keys), k=random.randint(0, len(option.valid_keys))
)
if issubclass(option, OptionList):
return random.sample(
list(option.valid_keys), k=random.randint(0, len(option.valid_keys))
)
if issubclass(option, NumericOption):
return option("random").value
if issubclass(option, FreeText):
return "".join(
random.choice(string.ascii_letters) for i in range(random.randint(0, 255))
)
return option.default
def call_generate(yaml_path, args, output_path):
from settings import get_settings
settings = get_settings()
args = Namespace(
**{
"weights_file_path": settings.generator.weights_file_path,
"sameoptions": False,
"player_files_path": yaml_path,
"seed": random.randint(0, 1000000000),
"multi": 1,
"spoiler": 1,
"outputpath": output_path,
"race": False,
"meta_file_path": "meta-doesnt-exist.yaml",
"log_level": "info",
"yaml_output": 1,
"plando": PlandoOptions.items | PlandoOptions.connections | PlandoOptions.texts | PlandoOptions.bosses,
"skip_prog_balancing": False,
"skip_output": args.skip_output,
"csv_output": False,
"log_time": False,
"spoiler_only": False,
}
)
for hook in MP_HOOKS:
hook.before_generate(args)
erargs, seed = GenMain(args)
return ERmain(erargs, seed)
def gen_wrapper(yaml_path, apworld_name, i, args, queue, tmp):
global MP_HOOKS
out_buf = StringIO()
timer = None
if args.timeout > 0:
myself = os.getpid()
def stop():
queue.put_nowait((myself, apworld_name, i, yaml_path, out_buf))
queue.join()
timer = threading.Timer(args.timeout, stop)
timer.start()
raised = None
mw = None
try:
with redirect_stdout(out_buf), redirect_stderr(out_buf), tempfile.TemporaryDirectory(prefix="apfuzz", dir=tmp) as output_path:
try:
# If we have hooks defined in args but they're not registered yet, register them
if args.hook and not MP_HOOKS:
for hook_class_path in args.hook:
hook = find_hook(hook_class_path)
hook.setup_worker(args)
MP_HOOKS.append(hook)
mw = call_generate(yaml_path, args, output_path)
except Exception as e:
raised = e
finally:
try:
for hook in MP_HOOKS:
hook.after_generate(mw, output_path)
finally:
# Make sure to always stop the timeout timer, whatever happens
# If we don't, the timer could fire while we're stopping AP or
# dumping YAMLs, and that would be bad.
if timer is not None:
timer.cancel()
timer.join()
root_logger = logging.getLogger()
handlers = root_logger.handlers[:]
for handler in handlers:
root_logger.removeHandler(handler)
handler.close()
outcome = GenOutcome.Success
if raised:
is_timeout = isinstance(raised, TimeoutError)
is_option_error = exception_in_causes(raised, OptionError)
if is_timeout:
outcome = GenOutcome.Timeout
elif is_option_error:
outcome = GenOutcome.OptionError
else:
outcome = GenOutcome.Failure
for hook in MP_HOOKS:
outcome, raised = hook.reclassify_outcome(outcome, raised)
if outcome == GenOutcome.Success:
return outcome
if outcome == GenOutcome.OptionError and not args.dump_ignored:
return outcome
if outcome == GenOutcome.Timeout:
extra = f"[...] Generation killed here after {args.timeout}s"
else:
extra = "".join(traceback.format_exception(raised))
dump_generation_output(outcome, apworld_name, i, yaml_path, out_buf, extra)
return outcome, raised
except Exception as e:
raise FuzzerException("Fuzzer error", out_buf) from e
def dump_generation_output(outcome, apworld_name, i, yamls_dir, out_buf, extra=None):
if outcome == GenOutcome.Success:
return
if outcome == GenOutcome.OptionError:
error_ty = "ignored"
elif outcome == GenOutcome.Timeout:
error_ty = "timeout"
else:
error_ty = "error"
error_output_dir = os.path.join(OUT_DIR, error_ty, apworld_name, str(i))
os.makedirs(error_output_dir)
for yaml_file in os.listdir(yamls_dir):
shutil.copy(os.path.join(yamls_dir, yaml_file), error_output_dir)
error_log_path = os.path.join(error_output_dir, f"{i}.log")
with open(error_log_path, "w", encoding='utf-8') as fd:
fd.write(out_buf.getvalue())
if extra is not None:
fd.write(extra)
class GenOutcome:
Success = 0
Failure = 1
Timeout = 2
OptionError = 3
IS_TTY = sys.stdout.isatty()
SUCCESS = 0
FAILURE = 0
TIMEOUTS = 0
OPTION_ERRORS = 0
SUBMITTED = 0
REPORT = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: [])))
def gen_callback(yamls_dir, apworld_name, i, args, outcome):
try:
if isinstance(outcome, tuple):
outcome, exc = outcome
else:
exc = None
global SUCCESS, FAILURE, SUBMITTED, OPTION_ERRORS, TIMEOUTS
SUBMITTED -= 1
if outcome == GenOutcome.Success:
SUCCESS += 1
if IS_TTY:
print(".", end="")
elif outcome == GenOutcome.Failure:
REPORT[apworld_name][type(exc)][str(exc)].append(i)
FAILURE += 1
if IS_TTY:
print("F", end="")
elif outcome == GenOutcome.Timeout:
REPORT[apworld_name][TimeoutError][""].append(i)
TIMEOUTS += 1
if IS_TTY:
print("T", end="")
elif outcome == GenOutcome.OptionError:
OPTION_ERRORS += 1
if IS_TTY:
print("I", end="")
# If we're not on a TTY, print progress every once in a while
if not IS_TTY:
checks_done = SUCCESS + FAILURE + TIMEOUTS + OPTION_ERRORS
step = args.runs // 50
if step == 0 or (checks_done % step) == 0:
print(f"{checks_done} / {args.runs} done. {FAILURE} failures, {TIMEOUTS} timeouts, {OPTION_ERRORS} ignored.")
sys.stdout.flush()
try:
# Technically not useful but this will prevent me from removing things I don't want when I inevitably mix up the args somewhere...
if 'apfuzz' in yamls_dir:
shutil.rmtree(yamls_dir)
except: # noqa: E722
pass
except Exception as e:
print("Error while handling fuzzing result:")
traceback.print_exception(e)
print("This is most likely a fuzzer bug and should be reported")
def error(yamls_dir, apworld_name, i, args, raised):
try:
msg = StringIO()
if isinstance(raised, FuzzerException):
msg.write(raised.out_buf)
msg.write("\n".join(traceback.format_exception(raised)))
dump_generation_output(GenOutcome.Failure, apworld_name, i, yamls_dir, msg)
return gen_callback(yamls_dir, apworld_name, i, args, GenOutcome.Failure)
except Exception as e:
print("Error while handling fuzzing result:")
traceback.print_exception(e)
print("This is most likely a fuzzer bug and should be reported")
def print_status():
print()
print("Success:", SUCCESS)
print("Failures:", FAILURE)
print("Timeouts:", TIMEOUTS)
print("Ignored:", OPTION_ERRORS)
print()
print("Time taken:{:.2f}s".format(time.time() - START))
def find_hook(hook_path):
modulepath, objectpath = hook_path.split(':')
obj = importlib.import_module(modulepath)
for inner in objectpath.split('.'):
obj = getattr(obj, inner)
if not isinstance(obj, type):
raise RuntimeError("the hook argument should refer to a class in a module")
if issubclass(obj, BaseHook):
raise RuntimeError("the hook {} is not a subclass of `fuzz.BaseHook`)".format(hook_path))
return obj()
class BaseHook:
def setup_main(self, args):
"""
This function is guaranteed to only ever be called once, in the main process.
"""
pass
def setup_worker(self, args):
"""
This function is guaranteed to only ever be called once per worker process. It can be used to load extra apworlds for example.
"""
pass
def reclassify_outcome(self, outcome, raised):
"""
This function is called once after a generation outcome has been decided.
You can reclassify the outcome with this before it is returned to the main process by returning a new `GenOutcome`
Note that because timeouts are processed by the main process and not by the worker itself (as it is busy timing out),
this function can be called from both the main process and the workers.
"""
return outcome, raised
def before_generate(self, args):
pass
def after_generate(self, mw, output_path):
pass
def finalize(self):
pass
def write_report(report):
computed_report = {}
for game_name, game_report in report.items():
computed_report[game_name] = defaultdict(lambda: [])
for exc_type, exc_report in game_report.items():
for exc_str, yamls in exc_report.items():
if exc_type == FillError:
computed_report[game_name]["FillError"].extend(yamls)
else:
if exc_str:
computed_report[game_name][exc_str].extend(yamls)
else:
computed_report[game_name][str(exc_type)].extend(yamls)
with open(os.path.join(OUT_DIR, "report.json"), "w", encoding='utf-8') as fd:
fd.write(json.dumps(computed_report))
if __name__ == "__main__":
MAIN_HOOKS = []
def main(p, args, tmp):
global SUBMITTED
apworld_name = args.game
if args.meta:
with open(args.meta, "r", encoding='utf-8-sig') as fd:
meta = yaml.safe_load(fd.read())
else:
meta = {}
if apworld_name is not None:
world = world_from_apworld_name(apworld_name)
if world is None:
raise Exception(
f"Failed to resolve apworld from apworld name: {apworld_name}"
)
if os.path.exists(OUT_DIR):
shutil.rmtree(OUT_DIR)
os.makedirs(OUT_DIR)
for hook_class_path in args.hook:
hook = find_hook(hook_class_path)
hook.setup_main(args)
MAIN_HOOKS.append(hook)
sys.stdout.write("\x1b[2J\x1b[H")
sys.stdout.flush()
i = 0
valid_worlds = [
world.__module__.split(".")[1]
for world in AutoWorldRegister.world_types.values()
]
if "apsudoku" in valid_worlds:
valid_worlds.remove("apsudoku")
yamls_per_run_bounds = [int(arg) for arg in args.yamls_per_run.split("-")]
if len(yamls_per_run_bounds) not in {1, 2}:
raise Exception(
"Invalid value passed for `yamls_per_run`. Either pass an int or a range like `1-10`"
)
if len(yamls_per_run_bounds) == 2:
if yamls_per_run_bounds[0] >= yamls_per_run_bounds[1]:
raise Exception("Invalid range value passed for `yamls_per_run`.")
static_yamls = []
if args.with_static_worlds:
for yaml_file in os.listdir(args.with_static_worlds):
path = os.path.join(args.with_static_worlds, yaml_file)
if not os.path.isfile(path):
continue
with open(path, "r", encoding='utf-8-sig') as fd:
static_yamls.append(fd.read())
manager = multiprocessing.Manager()
queue = manager.Queue(1000)
def handle_timeouts():
while True:
try:
pid, apworld_name, i, yamls_dir, out_buf = queue.get()
os.kill(pid, signal.SIGTERM)
extra = f"[...] Generation killed here after {args.timeout}s"
outcome = GenOutcome.Timeout
for hook in MAIN_HOOKS:
outcome, _ = hook.reclassify_outcome(outcome, TimeoutError())
dump_generation_output(outcome, apworld_name, i, yamls_dir, out_buf, extra)
gen_callback(yamls_dir, apworld_name, i, args, outcome)
except KeyboardInterrupt:
break
except EOFError:
break
except Exception as exc:
extra = "[...] Exception while timing out:\n {}".format("\n".join(traceback.format_exception(exc)))
dump_generation_output(GenOutcome.Timeout, apworld_name, i, yamls_dir, out_buf, extra)
gen_callback(yamls_dir, apworld_name, i, args, outcome)
continue
timeout_handler = threading.Thread(target=handle_timeouts)
timeout_handler.daemon = True
timeout_handler.start()
while i < args.runs:
if apworld_name is None:
actual_apworld = random.choice(valid_worlds)
else:
actual_apworld = apworld_name
if len(yamls_per_run_bounds) == 1:
yamls_this_run = yamls_per_run_bounds[0]
else:
# +1 here to make the range inclusive
yamls_this_run = random.randrange(
yamls_per_run_bounds[0], yamls_per_run_bounds[1] + 1
)
random_yamls = [
generate_random_yaml(actual_apworld, meta) for _ in range(yamls_this_run)
]
SUBMITTED += 1
yamls_dir = tempfile.mkdtemp(prefix="apfuzz", dir=tmp)
for nb, yaml_content in enumerate(random_yamls):
yaml_path = os.path.join(yamls_dir, f"{i}-{nb}.yaml")
open(yaml_path, "wb").write(yaml_content.encode("utf-8"))
for nb, yaml_content in enumerate(static_yamls):
yaml_path = os.path.join(yamls_dir, f"static-{i}-{nb}.yaml")
open(yaml_path, "wb").write(yaml_content.encode("utf-8"))
last_job = p.apply_async(
gen_wrapper,
args=(yamls_dir, actual_apworld, i, args, queue, tmp),
callback=functools.partial(gen_callback, yamls_dir, actual_apworld, i, args),
error_callback=functools.partial(error, yamls_dir, actual_apworld, i, args),
)
while SUBMITTED >= args.jobs * 10:
# Poll the last job to keep the queue running
last_job.ready()
time.sleep(0.001)
i += 1
while SUBMITTED > 0:
last_job.ready()
time.sleep(0.05)
parser = ArgumentParser(prog="apfuzz")
parser.add_argument("-g", "--game", default=None)
parser.add_argument("-j", "--jobs", default=10, type=int)
parser.add_argument("-r", "--runs", type=int, required=True)
parser.add_argument("-n", "--yamls_per_run", default="1", type=str)
parser.add_argument("-t", "--timeout", default=15, type=int)
parser.add_argument("-m", "--meta", default=None, type=None)
parser.add_argument("--dump-ignored", default=False, action="store_true")
parser.add_argument("--with-static-worlds", default=None)
parser.add_argument("--hook", action="append", default=[])
parser.add_argument("--skip-output", default=False, action="store_true")
args = parser.parse_args()
# This is just to make sure that the host.yaml file exists by the time we fork
# so that a first run on a new installation doesn't throw out failures until
# the host.yaml from the first gen is written
get_settings()
crashed = False
try:
can_fork = hasattr(os, "fork")
# fork here is way faster because it doesn't have to reload all worlds, but it's only available on some platforms
# forking for every job also has the advantage of being sure that the process is "clean". Although I don't know if that actually matters
start_method = "fork" if can_fork else "spawn"
multiprocessing.set_start_method(start_method)
tmp = tempfile.TemporaryDirectory(prefix="apfuzz")
with Pool(processes=args.jobs, maxtasksperchild=None) as p:
START = time.time()
main(p, args, tmp.name)
except KeyboardInterrupt:
pass
except Exception as e:
crashed = True
traceback.print_exc()
finally:
for hook in MAIN_HOOKS:
hook.finalize()
tmp.cleanup()
if not crashed:
print_status()
write_report(REPORT)
sys.exit((FAILURE + TIMEOUTS) != 0)
sys.exit(2)