From 7bb7e1559d638fd2b62f4ce39df7cec7c4a2b2c1 Mon Sep 17 00:00:00 2001 From: David Korczynski Date: Thu, 11 Apr 2024 03:58:41 -0700 Subject: [PATCH] core: harden analysis against projects without fuzzers Signed-off-by: David Korczynski --- .../datatypes/project_profile.py | 32 +++++++++++++++---- src/fuzz_introspector/html_helpers.py | 6 +++- src/fuzz_introspector/html_report.py | 7 ++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/fuzz_introspector/datatypes/project_profile.py b/src/fuzz_introspector/datatypes/project_profile.py index 5fce724e8..374ff4af0 100644 --- a/src/fuzz_introspector/datatypes/project_profile.py +++ b/src/fuzz_introspector/datatypes/project_profile.py @@ -183,10 +183,17 @@ def get_function_summaries(self) -> Tuple[int, int, int, float, float]: reached_func_count = self._get_total_reached_function_count() unreached_func_count = self._get_total_unreached_function_count() total_functions = reached_func_count + unreached_func_count - reached_percentage = (float(reached_func_count) / - float(total_functions)) * 100 - unreached_percentage = (float(unreached_func_count) / - float(total_functions)) * 100 + try: + reached_percentage = (float(reached_func_count) / + float(total_functions)) * 100 + except ZeroDivisionError: + reached_percentage = 0.0 + + try: + unreached_percentage = (float(unreached_func_count) / + float(total_functions)) * 100 + except ZeroDivisionError: + unreached_percentage = 0.0 return (total_functions, reached_func_count, unreached_func_count, reached_percentage, unreached_percentage) @@ -273,8 +280,11 @@ def reached_func_percentage(self): reached_func_count = self._get_total_reached_function_count() unreached_func_count = self._get_total_unreached_function_count() total_functions = reached_func_count + unreached_func_count - reached_percentage = (float(reached_func_count) / - float(total_functions)) * 100 + try: + reached_percentage = (float(reached_func_count) / + float(total_functions)) * 100 + except ZeroDivisionError: + reached_percentage = 0.0 return reached_percentage def get_profiles_coverage_files(self) -> List[str]: @@ -377,7 +387,11 @@ def write_stats_to_summary_file(self) -> None: unreached_func_percentage) = self.get_function_summaries() covered_funcs = self.get_all_runtime_covered_functions() - cov_percentage = round(len(covered_funcs) / total_functions, 2) * 100.0 + try: + cov_percentage = round(len(covered_funcs) / total_functions, + 2) * 100.0 + except ZeroDivisionError: + cov_percentage = 0.0 json_report.add_project_key_value_to_report( "stats", { @@ -411,6 +425,10 @@ def _set_basefolder(self) -> None: continue all_strs.append(os.path.dirname(f.function_source_file)) + if len(all_strs) == 0: + self.basefolder = "" + return + self.basefolder = utils.longest_common_prefix(all_strs) + "/" def _get_total_unreached_function_count(self) -> int: diff --git a/src/fuzz_introspector/html_helpers.py b/src/fuzz_introspector/html_helpers.py index 8103dcd12..2e90bce65 100644 --- a/src/fuzz_introspector/html_helpers.py +++ b/src/fuzz_introspector/html_helpers.py @@ -325,7 +325,11 @@ def create_percentage_graph(title: str, numerator: int, """Creates a percentage tag within a
tag. This is used to show "how much X is of Y" for a {numerator, denominator} pair. """ - percentage = round(float(numerator) / float(denominator), 2) * 100.0 + try: + percentage = round(float(numerator) / float(denominator), 2) * 100.0 + except ZeroDivisionError: + percentage = 0.0 + subtitle = f"{numerator} / {denominator}" return f"""
diff --git a/src/fuzz_introspector/html_report.py b/src/fuzz_introspector/html_report.py index 93575dcf6..4a965d36b 100644 --- a/src/fuzz_introspector/html_report.py +++ b/src/fuzz_introspector/html_report.py @@ -205,8 +205,11 @@ def create_boxed_top_summary_info( title="No coverage data was found", description=html_constants.WARNING_NO_COVERAGE)) # Add coverage conclusion - coverage_percentage = float( - len(covered_funcs) / float(proj_profile.total_functions) * 100.0) + try: + coverage_percentage = float( + len(covered_funcs) / float(proj_profile.total_functions) * 100.0) + except ZeroDivisionError: + coverage_percentage = 0.0 if coverage_percentage > 50.0: sentence = f"""Fuzzers reach {"%.5s%%"%(str(coverage_percentage))} code coverage.""" conclusions.append(