afl.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. """AFL++ next to the libFuzzer targets: the same parser sources and harnesses (tools/fuzz/*.cpp) built into build-afl/
  3. with clang's trace-pc-guard coverage, ASan and UBSan, linked with AFL++'s afl-compiler-rt (the image's AFL++ has no LLVM
  4. pass, and its GCC plugin was built for another GCC than the installed one). It starts from the corpus libFuzzer built
  5. (tools/runtime_analysis.py fuzz seeds).
  6. tools/fuzz/afl.py build [-j N]
  7. tools/fuzz/afl.py run <texture|package|mdl|sog|json> [-t seconds]
  8. AFL++ insists on ASan aborting, and the host's core_pattern pipes to systemd-coredump, which runs even with RLIMIT_CORE 0,
  9. so a crash would land in the user's crash reporter. The driver makes the process non dumpable (PR_SET_DUMPABLE 0), the
  10. kernel then starts no core handler. The targets only parse files, nothing renders.
  11. """
  12. import argparse
  13. import concurrent.futures
  14. import os
  15. import subprocess
  16. import sys
  17. from pathlib import Path
  18. HERE = Path(__file__).resolve().parent
  19. sys.path.insert(0, str(HERE.parent))
  20. import runtime_analysis as ra
  21. ROOT = ra.ROOT
  22. OUT = ROOT / 'build-afl'
  23. RUNTIME = Path('/usr/lib64/afl/afl-compiler-rt-64.o')
  24. SANITIZE = ['-fsanitize=address,undefined', '-fsanitize-coverage=trace-pc-guard']
  25. def build(jobs):
  26. objects = OUT / 'obj'
  27. objects.mkdir(parents=True, exist_ok=True)
  28. flags = ra.fuzz_compile_flags()
  29. sources = sorted(path for pattern in ra.FUZZ_SOURCES for path in (ROOT / 'src' / 'WallpaperEngine').glob(pattern))
  30. common = [flag for flag in ra.FUZZ_FLAGS if flag != '-fno-sanitize=vptr'] + ['-fno-sanitize=vptr']
  31. def compile_one(source):
  32. target = objects / (str(source.relative_to(ROOT)).replace('/', '_') + '.o')
  33. if target.exists() and target.stat().st_mtime > source.stat().st_mtime:
  34. return target
  35. subprocess.run(['clang++', *common, *SANITIZE, '-fPIC', *flags, '-c', str(source), '-o', str(target)], check=True)
  36. return target
  37. with concurrent.futures.ThreadPoolExecutor(jobs) as pool:
  38. objs = list(pool.map(compile_one, sources))
  39. globals_file = OUT / 'globals.cpp'
  40. globals_file.write_text('float g_Time;\nfloat g_TimeLast;\n')
  41. driver = OUT / 'afl_driver.o'
  42. subprocess.run(['clang++', *common, '-c', str(HERE / 'afl_driver.cpp'), '-o', str(driver)], check=True)
  43. for name in ra.FUZZ_TARGETS:
  44. subprocess.run(['clang++', *common, *SANITIZE, *flags, str(HERE / f'{name}.cpp'), str(globals_file), str(driver),
  45. str(RUNTIME), *map(str, objs), '-o', str(OUT / name), '-Wl,--unresolved-symbols=ignore-all',
  46. *ra.FUZZ_LIBS], check=True)
  47. print(f'built {OUT / name}')
  48. def run(target, seconds):
  49. corpus = ra.FUZZ_DIR / 'corpus' / target
  50. findings = OUT / 'findings' / target
  51. findings.parent.mkdir(parents=True, exist_ok=True)
  52. env = {**os.environ,
  53. 'ASAN_OPTIONS': 'abort_on_error=1:symbolize=0:detect_leaks=0:'
  54. 'allocator_may_return_null=1:max_allocation_size_mb=2048',
  55. 'UBSAN_OPTIONS': f'print_stacktrace=0:suppressions={ra.SUPP / "ubsan.supp"}',
  56. 'AFL_IGNORE_PROBLEMS': '1', 'AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES': '1',
  57. 'AFL_SKIP_CPUFREQ': '1', 'AFL_NO_AFFINITY': '1', 'AFL_NO_UI': '1'}
  58. subprocess.run(['sh', '-c', 'ulimit -c 0; exec "$@"', 'sh', 'afl-fuzz', '-i', str(corpus), '-o', str(findings),
  59. '-V', str(seconds), '-m', 'none', '-t', '20000', '--', str(OUT / target)], env=env, check=False)
  60. def main():
  61. parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
  62. sub = parser.add_subparsers(dest='command', required=True)
  63. b = sub.add_parser('build')
  64. b.add_argument('-j', '--jobs', type=int, default=6)
  65. r = sub.add_parser('run')
  66. r.add_argument('target', choices=ra.FUZZ_TARGETS)
  67. r.add_argument('-t', '--seconds', type=int, default=600)
  68. options = parser.parse_args()
  69. if options.command == 'build':
  70. build(options.jobs)
  71. else:
  72. run(options.target, options.seconds)
  73. if __name__ == '__main__':
  74. main()