Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stats parsing for honggfuzz. #1180

Merged
merged 2 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions src/python/bot/fuzzers/honggfuzz/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from bot.fuzzers import dictionary_manager
from bot.fuzzers import engine
from metrics import logs
from system import environment
from system import new_process

Expand All @@ -41,6 +42,7 @@

_CRASH_REGEX = re.compile('Crash: saved as \'(.*)\'')
_HF_SANITIZER_LOG_PREFIX = 'HF.sanitizer.log'
_STATS_PREFIX = 'Summary '


class HonggfuzzError(Exception):
Expand All @@ -66,6 +68,36 @@ def _find_sanitizer_stacktrace(reproducers_dir):
return None


def _get_reproducer_path(line):
"""Get the reproducer path, if any."""
crash_match = _CRASH_REGEX.match(line)
if not crash_match:
return None

return crash_match.group(1)


def _get_stats(line):
"""Get stats, if any."""
if not line.startswith(_STATS_PREFIX):
return None

parts = line[len(_STATS_PREFIX):].split()
stats = {}

for part in parts:
if ':' not in part:
logs.log_error('Invalid stat part.', value=part)

key, value = part.split(':', 2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we bail out if ':' not in part ? just to avoid exception, maybe log_error ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

try:
stats[key] = int(value)
except (ValueError, TypeError):
logs.log_error('Invalid stat value.', key=key, value=value)

return stats


class HonggfuzzEngine(engine.Engine):
"""honggfuzz engine implementation."""

Expand Down Expand Up @@ -125,20 +157,22 @@ def fuzz(self, target_path, options, reproducers_dir, max_time):
sanitizer_stacktrace = _find_sanitizer_stacktrace(reproducers_dir)

crashes = []
stats = None
for line in log_lines:
crash_match = _CRASH_REGEX.match(line)
if not crash_match:
reproducer_path = _get_reproducer_path(line)
if reproducer_path:
crashes.append(
engine.Crash(reproducer_path, sanitizer_stacktrace, [],
int(fuzz_result.time_executed)))
continue

reproducer_path = crash_match.group(1)
crashes.append(
engine.Crash(reproducer_path, sanitizer_stacktrace, [],
int(fuzz_result.time_executed)))
break
stats = _get_stats(line)

if stats is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to initialize stats var too, otherwise can end up with var not defined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

stats = {}

# TODO(ochang): Parse stats.
return engine.FuzzResult(fuzz_result.output, fuzz_result.command, crashes,
{}, fuzz_result.time_executed)
stats, fuzz_result.time_executed)

def reproduce(self, target_path, input_path, arguments, max_time):
"""Reproduce a crash given an input.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ def setUp(self):

os.environ['BUILD_DIR'] = DATA_DIR

def assert_has_stats(self, results):
"""Assert that stats exist."""
self.assertIn('iterations', results.stats)
self.assertIn('time', results.stats)
self.assertIn('speed', results.stats)
self.assertIn('crashes_count', results.stats)
self.assertIn('timeout_count', results.stats)
self.assertIn('new_units_added', results.stats)
self.assertIn('slowest_unit_ms', results.stats)
self.assertIn('guard_nb', results.stats)
self.assertIn('branch_coverage_percent', results.stats)
self.assertIn('peak_rss_mb', results.stats)

def test_reproduce(self):
"""Tests reproducing a crash."""
testcase_path, _ = setup_testcase_and_corpus('crash', 'empty_corpus')
Expand Down Expand Up @@ -107,6 +120,7 @@ def test_fuzz_no_crash(self):
], results.command)

self.assertGreater(len(os.listdir(corpus_path)), 0)
self.assert_has_stats(results)

def test_fuzz_crash(self):
"""Test fuzzing that results in a crash."""
Expand Down Expand Up @@ -148,3 +162,5 @@ def test_fuzz_crash(self):

with open(crash.input_path) as f:
self.assertEqual('A', f.read()[0])

self.assert_has_stats(results)