Skip to content

@Ekam219: Fix Logging and Error Handling for get_benchmark_final_target_code #919

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
30 changes: 28 additions & 2 deletions report/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,32 @@ def getsize(self) -> int:
class Results:
"""Results provides functions to explore the experiment results in a
particular directory."""

def get_benchmark_final_target_code(self, sample_id: str) -> Optional[str]:
"""Retrieve source code for a sample_id (format: 'benchmark/sample').

Args:
sample_id: A string in the format 'benchmark/sample'.

Returns:
The source code as a string, or None if the source code is missing or the sample_id is invalid.
"""
try:
# Split the sample_id into benchmark and sample
benchmark, sample = sample_id.split('/')
except ValueError:
# Log an error if the sample_id format is invalid
logging.error(f"Invalid sample_id format: '{sample_id}'. Expected 'benchmark/sample'.")
return None

# Retrieve the source code using the existing method
code = self.get_final_target_code(benchmark, sample)
if not code:
# Log a warning if the source code is missing
logging.warning(f"Missing source code for {sample_id}")
return None

return code

def __init__(self, results_dir='results', benchmark_set='all'):
self._results_dir = results_dir
Expand Down Expand Up @@ -284,7 +310,7 @@ def get_final_target_code(self, benchmark: str, sample: str) -> str:
targets_dir = os.path.join(self._results_dir, benchmark, 'fixed_targets')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I reckon this could be the cause.
Previously OSS-Fuzz-Gen saves the final fuzz target in to this dir.
Now it is placed into:

  • results/output-libraw-_zn6libraw14crxdecodeplaneepvj/fuzz_targets/{sample_id}.fuzz_target
  • results/output-libraw-_zn6libraw14crxdecodeplaneepvj/fuzz_targets/{sample_id}.build_script

# TODO(donggeliu): Make this consistent with agent output.
if not os.path.exists(targets_dir):
return ''
return None

for name in sorted(FileSystem(targets_dir).listdir()):
path = os.path.join(targets_dir, name)
Expand All @@ -293,7 +319,7 @@ def get_final_target_code(self, benchmark: str, sample: str) -> str:
code = f.read()
code = json.dumps(code)
return code
return ''
return None

def get_logs(self, benchmark: str, sample: str) -> list[LogPart]:
status_dir = os.path.join(self._results_dir, benchmark, 'status')
Expand Down
2 changes: 1 addition & 1 deletion report/templates/crash.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"target_binary": "{{ sample.target_binary }}",
"reproducer": "{{ sample.reproducer }}",
"run_log": "{{ sample.run_log }}",
"source_code": {{ get_benchmark_final_target_code(sample.id) | replace('\\n', '\\\\n')}},
"source_code": {{ get_benchmark_final_target_code(sample.id) | tojson }},
"model": "{{ model }}"
}{% if not loop.last %},{% endif %}
{% endfor %}
Expand Down
15 changes: 8 additions & 7 deletions report/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ def _write_benchmark_index(self, benchmark: Benchmark, samples: List[Sample],
def _write_benchmark_crash(self, benchmark: Benchmark, samples: List[Sample]):
"""Generate the benchmark crash.json and write to filesystem."""
try:
rendered = self._jinja.render('crash.json',
benchmark=benchmark.signature,
samples=samples,
get_benchmark_final_target_code=partial(
self._results.get_final_target_code,
benchmark.id))
rendered = self._jinja.render(
'crash.json',
benchmark=benchmark.signature,
samples=samples,
# Changed line below to use new error-handling method
get_benchmark_final_target_code=self._results.get_benchmark_final_target_code
)
self._write(f'benchmark/{benchmark.id}/crash.json', rendered)
except Exception as e:
logging.error('Failed to write benchmark/%s/crash.json:\n%s',
Expand Down Expand Up @@ -328,4 +329,4 @@ def main():

if __name__ == '__main__':
logging.getLogger().setLevel(os.environ.get('LOGLEVEL', 'WARN').upper())
main()
main()
Loading