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

analysis: Change analysis interface to allow passing in properties #1993

Merged
merged 16 commits into from
Jan 20, 2025
Merged
2 changes: 2 additions & 0 deletions src/fuzz_introspector/analyses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from fuzz_introspector.analyses import runtime_coverage_analysis
from fuzz_introspector.analyses import sinks_analyser
from fuzz_introspector.analyses import annotated_cfg
from fuzz_introspector.analyses import source_code_line_analyser

# All optional analyses.
# Ordering here is important as top analysis will be shown first in the report
Expand All @@ -22,4 +23,5 @@
metadata.MetadataAnalysis,
sinks_analyser.SinkCoverageAnalyser,
annotated_cfg.FuzzAnnotatedCFG,
source_code_line_analyser.SourceCodeLineFunctionAnalyser,
]
86 changes: 86 additions & 0 deletions src/fuzz_introspector/analyses/source_code_line_analyser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2025 Fuzz Introspector Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Analysis plugin for introspection of the function on target line in target source file."""

import json
import logging

from typing import (Any, List, Dict)

from fuzz_introspector import (analysis, html_helpers)

from fuzz_introspector.datatypes import (project_profile, fuzzer_profile)

logger = logging.getLogger(name=__name__)


class SourceCodeLineFunctionAnalyser(analysis.AnalysisInterface):
name: str = 'SourceCodeLineFunctionAnalyser'

def __init__(self):
self.json_results: Dict[str, Any] = dict()
self.json_string_result = ''

@classmethod
def get_name(cls):
"""Return the analyser identifying name for processing.

:return: The identifying name of this analyser
:rtype: str
"""
return cls.name

def get_json_string_result(self) -> str:
"""Return the stored json string result.

:return: The json string result processed and stored
by this analyser
:rtype: str
"""
if self.json_string_result:
return self.json_string_result
return json.dumps(self.json_results)

def set_json_string_result(self, json_string):
"""Store the result of this analyser as json string result
for further processing in a later time.

:param json_string: A json string variable storing the
processing result of the analyser for future use
:type json_string: str
"""
self.json_string_result = json_string

def analysis_func(self,
table_of_contents: html_helpers.HtmlTableOfContents,
tables: List[str],
proj_profile: project_profile.MergedProjectProfile,
profiles: List[fuzzer_profile.FuzzerProfile],
basefolder: str, coverage_url: str,
conclusions: List[html_helpers.HTMLConclusion],
out_dir) -> str:
logger.info(f" - Running analysis {self.get_name()}")
logger.info(self.properties)
# Get all functions from the profiles
all_functions = list(proj_profile.all_functions.values())
all_functions.extend(proj_profile.all_constructors.values())

# Generate a Source File to Function Profile map and store in JSON Result
for function in all_functions:
func_list = self.json_results.get(function.function_source_file,
[])
func_list.append(function)
self.json_results[function.function_source_file] = func_list

return ''
11 changes: 9 additions & 2 deletions src/fuzz_introspector/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ class AnalysisInterface(abc.ABC):
json_string_result: str = ""
display_html: bool = False

def set_additional_properties(self, properties: dict[str, str]):
"""Allow setting additional properties for this analysis."""
self.properties = properties

@abc.abstractmethod
def analysis_func(self,
table_of_contents: html_helpers.HtmlTableOfContents,
Expand Down Expand Up @@ -230,9 +234,12 @@ def set_display_html(self, is_display_html):
self.display_html = is_display_html


def instantiate_analysis_interface(cls: Type[AnalysisInterface]):
def instantiate_analysis_interface(cls: Type[AnalysisInterface],
props: dict[str, str]):
"""Wrapper function to satisfy Mypy semantics"""
return cls()
analysis_interface = cls()
analysis_interface.set_additional_properties(props)
return analysis_interface


class FuzzBranchBlocker:
Expand Down
46 changes: 40 additions & 6 deletions src/fuzz_introspector/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Copyright 2024 Fuzz Introspector Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,6 +63,21 @@ def get_cmdline_parser() -> argparse.ArgumentParser:
default='',
type=str,
help='Base coverage URL.')
full_parser.add_argument('--analyses',
nargs='+',
default=[],
help='''
Analyses to run. Available options:
AnnotatedCFG, BugDigestorAnalysis, FuzzCalltreeAnalysis,
FuzzDriverSynthesizerAnalysis, FuzzEngineInputAnalysis,
FilePathAnalyser, ThirdPartyAPICoverageAnalyser,
MetadataAnalysis, OptimalTargets, RuntimeCoverageAnalysis,
SinkCoverageAnalyser, FunctionSourceLineAnalyser
''')
full_parser.add_argument('--properties',
nargs='*',
default=[],
help='Additional properties for analysis')

# Report generation command
report_parser = subparsers.add_parser(
Expand All @@ -88,7 +102,11 @@ def get_cmdline_parser() -> argparse.ArgumentParser:
],
help="""
Analyses to run. Available options:
OptimalTargets, FuzzEngineInput, ThirdPartyAPICoverageAnalyser
AnnotatedCFG, BugDigestorAnalysis, FuzzCalltreeAnalysis,
FuzzDriverSynthesizerAnalysis, FuzzEngineInputAnalysis,
FilePathAnalyser, ThirdPartyAPICoverageAnalyser,
MetadataAnalysis, OptimalTargets, RuntimeCoverageAnalysis,
SinkCoverageAnalyser, FunctionSourceLineAnalyser
""")
report_parser.add_argument("--enable-all-analyses",
action='store_true',
Expand All @@ -111,6 +129,10 @@ def get_cmdline_parser() -> argparse.ArgumentParser:
nargs="+",
default=["FuzzEngineInputAnalysis"],
help="State which analysis requires separate json report output")
report_parser.add_argument('--properties',
nargs='*',
default=[],
help='Additional properties for analysis')

# Command for correlating binary files to fuzzerLog files
correlate_parser = subparsers.add_parser(
Expand Down Expand Up @@ -163,10 +185,21 @@ def main() -> int:

logger.info("Running fuzz introspector post-processing")
if args.command == 'report':
return_code = commands.run_analysis_on_dir(
args.target_dir, args.coverage_url, args.analyses,
args.correlation_file, args.enable_all_analyses, args.name,
args.language, args.output_json)
props: dict[str, str] = {}
for property in args.properties:
if property.count('=') == 1:
key, value = property.split('=', 1)
props[key] = value

return_code = commands.run_analysis_on_dir(args.target_dir,
args.coverage_url,
args.analyses,
args.correlation_file,
args.enable_all_analyses,
args.name,
args.language,
args.output_json,
props=props)
logger.info("Ending fuzz introspector report generation")
elif args.command == 'correlate':
return_code = commands.correlate_binaries_to_logs(args.binaries_dir)
Expand All @@ -184,3 +217,4 @@ def main() -> int:

if __name__ == "__main__":
main()
"--enable-all-analyses",
19 changes: 14 additions & 5 deletions src/fuzz_introspector/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ def end_to_end(args) -> int:
else:
language = args.language

props: dict[str, str] = {}
for property in args.properties:
if property.count('=') == 1:
key, value = property.split('=', 1)
props[key] = value

return run_analysis_on_dir(target_folder=out_dir,
coverage_url=args.coverage_url,
analyses_to_run=[],
analyses_to_run=args.analyses,
correlation_file='',
enable_all_analyses=True,
enable_all_analyses=(not args.analyses),
report_name=args.name,
language=language,
out_dir=out_dir)
out_dir=out_dir,
props=props)


def run_analysis_on_dir(target_folder: str,
Expand All @@ -89,7 +96,8 @@ def run_analysis_on_dir(target_folder: str,
output_json: Optional[List[str]] = None,
parallelise: bool = True,
dump_files: bool = True,
out_dir: str = '') -> int:
out_dir: str = '',
props: dict[str, str] = {}) -> int:
"""Runs Fuzz Introspector analysis from based on the results
from a frontend run. The primary task is to aggregate the data
and generate a HTML report."""
Expand All @@ -114,7 +122,8 @@ def run_analysis_on_dir(target_folder: str,
output_json,
report_name,
dump_files,
out_dir=out_dir)
out_dir=out_dir,
props=props)

return constants.APP_EXIT_SUCCESS

Expand Down
3 changes: 3 additions & 0 deletions src/fuzz_introspector/frontends/frontend_jvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,9 @@ def extract_calltree(self,
if not visited_functions:
visited_functions = set()

if function and '].' not in function:
function = None

if not source_code and function:
source_code = self.find_source_with_method(function)

Expand Down
9 changes: 5 additions & 4 deletions src/fuzz_introspector/html_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def create_section_all_functions(table_of_contents, tables, proj_profile,
def create_section_optional_analyses(
table_of_contents, analyses_to_run, output_json, tables,
introspection_proj: analysis.IntrospectionProject, basefolder,
coverage_url, conclusions, dump_files, out_dir) -> str:
coverage_url, conclusions, dump_files, out_dir, props) -> str:
"""Creates the HTML sections containing optional analyses."""
html_report_core = ""
logger.info(" - Handling optional analyses")
Expand All @@ -662,7 +662,7 @@ def create_section_optional_analyses(
analysis_name = analysis_interface.get_name()
if analysis_name in combined_analyses:
analysis_instance = analysis.instantiate_analysis_interface(
analysis_interface)
analysis_interface, props)
analysis_instance.dump_files = dump_files

# Set display_html flag for the analysis_instance
Expand Down Expand Up @@ -727,7 +727,8 @@ def create_html_report(introspection_proj: analysis.IntrospectionProject,
output_json,
report_name,
dump_files,
out_dir: str = '') -> None:
out_dir: str = '',
props: dict[str, str] = {}) -> None:
"""
Logs a complete report. This is the current main place for looking at
data produced by fuzz introspector.
Expand Down Expand Up @@ -783,7 +784,7 @@ def create_html_report(introspection_proj: analysis.IntrospectionProject,
table_of_contents, analyses_to_run, output_json, tables,
introspection_proj, introspection_proj.proj_profile.basefolder,
introspection_proj.proj_profile.coverage_url, conclusions, dump_files,
out_dir)
out_dir, props)

# Create HTML showing the conclusions at the top of the report.
html_report_top += html_helpers.create_conclusions_box(conclusions)
Expand Down
Loading