|
| 1 | +require 'open3' |
| 2 | +require 'fileutils' |
| 3 | + |
| 4 | +GEM_ROOT = File.expand_path('../', __dir__) |
| 5 | +CORPUS_DIR = File.join(GEM_ROOT, 'corpus') |
| 6 | +DICT_FILE = File.join(GEM_ROOT, 'cbor.dict') |
| 7 | + |
| 8 | +assert('mruby-cbor-fuzzer: no crashes in fuzzing run') do |
| 9 | + findings_dir = File.join(GEM_ROOT, 'findings') |
| 10 | + FileUtils.mkdir_p(findings_dir) |
| 11 | + |
| 12 | + cmd = cmd_list('mruby-cbor-fuzzer') + [ |
| 13 | + CORPUS_DIR, |
| 14 | + "-dict=#{DICT_FILE}", |
| 15 | + "-jobs=7", |
| 16 | + "-artifact_prefix=#{findings_dir}/", |
| 17 | + "-max_len=4096", |
| 18 | + "-ignore_ooms=0" |
| 19 | + ] |
| 20 | + |
| 21 | + Dir.chdir(findings_dir) do |
| 22 | + pid = Process.spawn(*cmd, out: '/dev/null', err: '/dev/null') |
| 23 | + Process.detach(pid) |
| 24 | + |
| 25 | + # Kill fuzzer on Ctrl+C |
| 26 | + trap('INT') do |
| 27 | + puts "\nKilling fuzzer..." |
| 28 | + Process.kill(9, pid) |
| 29 | + exit(1) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + sleep 0.1 until (0..6).all? { |n| File.exist?(File.join(findings_dir, "fuzz-#{n}.log")) } |
| 34 | + |
| 35 | + threads = (0..6).map do |n| |
| 36 | + log = File.join(findings_dir, "fuzz-#{n}.log") |
| 37 | + Thread.new do |
| 38 | + File.open(log) do |f| |
| 39 | + loop do |
| 40 | + line = f.gets |
| 41 | + line ? $stderr.print("[#{n}] #{line}") : sleep(0.05) |
| 42 | + GC.start |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + loop do |
| 49 | + done = (0..6).count do |n| |
| 50 | + log = File.join(findings_dir, "fuzz-#{n}.log") |
| 51 | + File.exist?(log) && File.read(log) =~ /Done \d+|SUMMARY:/ |
| 52 | + end |
| 53 | + break if done == 16 |
| 54 | + sleep 5 |
| 55 | + end |
| 56 | + |
| 57 | + threads.each(&:kill) |
| 58 | + |
| 59 | + crashes = (0..6).select do |n| |
| 60 | + log = File.join(findings_dir, "fuzz-#{n}.log") |
| 61 | + File.exist?(log) && File.read(log).include?('SUMMARY:') |
| 62 | + end |
| 63 | + |
| 64 | + assert_true crashes.empty?, |
| 65 | + "crashes in workers: #{crashes.map { |n| "fuzz-#{n}.log" }.join(', ')}" |
| 66 | +end |
0 commit comments