Skip to content

Get and log the source code of the fuzz target and build script. #1065

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

Merged
merged 6 commits into from
Jun 13, 2025
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
6 changes: 4 additions & 2 deletions experiment/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ def create_ossfuzz_project(benchmark: Benchmark,
f'{benchmark.target_path}\n')

if not build_script_path or os.path.getsize(build_script_path) == 0:
return name
# Return the generated project path to the caller.
return generated_project_path

# Copy generated build script to generated_project_path
shutil.copyfile(
Expand All @@ -300,7 +301,8 @@ def create_ossfuzz_project(benchmark: Benchmark,
with open(os.path.join(generated_project_path, 'Dockerfile'), 'a') as f:
f.write('\nCOPY agent-build.sh /src/build.sh\n')

return name
# Return the generated project path to the caller.
return generated_project_path

@staticmethod
def create_ossfuzz_project_with_gdb(benchmark: Benchmark,
Expand Down
45 changes: 43 additions & 2 deletions stage/execution_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ class ExecutionStage(BaseStage):
stages to analyze and improve on. It uses OSS-Fuzz infra to perform these
tasks."""

def get_source_from_project(self, project: str, filename: str) -> str:
"""Retrieves the source code of the fuzz target from the generated oss-fuzz
project."""
target_path = os.path.join(project, filename)
if os.path.isfile(target_path):
with open(target_path, 'r') as file:
return file.read()
return ''

def log_fuzz_target_and_build_script(self, fuzz_target_source: str,
build_script_source: str) -> None:
"""Logs the fuzz target and build script source code from the generated
oss-fuzz project."""

if fuzz_target_source:
self.logger.info('\nFuzz target source: \n%s\n', fuzz_target_source)
else:
self.logger.error('Fuzz target source not found')

if build_script_source:
self.logger.info('\nBuild script source: \n%s\n', build_script_source)
else:
self.logger.warning('Build script source not found. ' \
'Original build script will be used.')

def execute(self, result_history: list[Result]) -> Result:
"""Executes the fuzz target and build script in the latest result."""
last_result = result_history[-1]
Expand Down Expand Up @@ -60,13 +85,24 @@ def execute(self, result_history: list[Result]) -> Result:
f'{self.trial:02d}.fuzz_target')
build_script_path = os.path.join(last_result.work_dirs.fuzz_targets,
f'{self.trial:02d}.build_script')
evaluator.create_ossfuzz_project(benchmark, generated_oss_fuzz_project,
fuzz_target_path, build_script_path)
generated_project_path = evaluator.create_ossfuzz_project(
benchmark, generated_oss_fuzz_project, fuzz_target_path,
build_script_path)

status_path = os.path.join(last_result.work_dirs.status,
f'{self.trial:02d}')
os.makedirs(status_path, exist_ok=True)

# Get the source code of the fuzz target and build script from the generated oss-fuzz projects.
fuzz_target_source = self.get_source_from_project(
generated_project_path, f'{self.trial:02d}.fuzz_target')

build_script_source = self.get_source_from_project(
generated_project_path, f'{self.trial:02d}.build_script')

self.log_fuzz_target_and_build_script(fuzz_target_source,
build_script_source)

# Try building and running the new target.

# TODO: Log build failure.
Expand Down Expand Up @@ -136,6 +172,11 @@ def execute(self, result_history: list[Result]) -> Result:
else:
run_log_content = ''

# Add fuzz_target_source and build_script_source to the report log.
run_log_content = (f'Fuzz target source:\n{fuzz_target_source}\n'
f'Build script source:\n{build_script_source}\n'
f'{run_log_content}')

runresult = RunResult(
benchmark=benchmark,
trial=self.trial,
Expand Down
Loading