blob: d25c134ce08814a00e2d96dd5c28990a058dbe0f [file]
# Copyright 2026 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Engine adapters for fuzz_target_runner.py."""
from __future__ import annotations
import dataclasses
import pathlib
# TODO(crbug.com/584810685): Add 'centipede' and 'auto' engine detection.
ENGINES = ('libfuzzer',)
def get_adapter(engine: str) -> EngineAdapter:
"""Returns a new adapter for an engine named in ENGINES."""
if engine == 'libfuzzer':
return LibFuzzerAdapter()
raise ValueError(f'unknown engine: {engine}')
@dataclasses.dataclass(frozen=True)
class Target:
"""Which fuzz target to run: an already-built binary in a GN out dir."""
build_dir: pathlib.Path
name: str
@property
def binary(self) -> pathlib.Path:
return self.build_dir / self.name
@dataclasses.dataclass(frozen=True)
class EngineOptions:
"""Engine-neutral fuzzing settings."""
# Fuzzing duration passed to the engine.
duration_s: int
# None means the engine picks a random seed for each run.
seed: int | None = None
# TODO(crbug.com/581948942): Add explicit engine resource and timeout limits.
class EngineAdapter:
"""Translates a Target and EngineOptions into one engine's command line."""
name = ''
def command(
self,
target: Target,
options: EngineOptions,
*,
corpus_dir: pathlib.Path,
crashes_dir: pathlib.Path,
) -> list[str]:
"""Returns the command line that runs the target under this engine.
The engine writes its corpus into corpus_dir and its crashes into
crashes_dir, and stops by itself after options.duration_s.
"""
raise NotImplementedError
class LibFuzzerAdapter(EngineAdapter):
name = 'libfuzzer'
def command(
self,
target: Target,
options: EngineOptions,
*,
corpus_dir: pathlib.Path,
crashes_dir: pathlib.Path,
) -> list[str]:
command = [
str(target.binary),
# libFuzzer starts this clock at startup, but only checks it between
# mutations: it runs every input of an initial corpus first.
f'-max_total_time={options.duration_s}',
# libFuzzer puts the crash file name right after the prefix.
f'-artifact_prefix={crashes_dir}/',
]
if options.seed is not None:
command.append(f'-seed={options.seed}')
# libFuzzer writes the inputs it keeps into the first corpus dir.
command.append(str(corpus_dir))
return command