From b7e09dc7f4cad2f30d929ec6fa2887c58dfb8a5f Mon Sep 17 00:00:00 2001 From: DavidKorczynski Date: Thu, 2 Jan 2025 18:21:23 +0000 Subject: [PATCH] core: avoid needing to specify language for analysis (#1931) * core: avoid needing to specify language for analysis Signed-off-by: David Korczynski * nit Signed-off-by: David Korczynski * nit Signed-off-by: David Korczynski --------- Signed-off-by: David Korczynski --- src/fuzz_introspector/commands.py | 3 ++ src/fuzz_introspector/frontends/oss_fuzz.py | 8 ++++- src/fuzz_introspector/utils.py | 33 +++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/fuzz_introspector/commands.py b/src/fuzz_introspector/commands.py index 9278b0033..9b82c017c 100644 --- a/src/fuzz_introspector/commands.py +++ b/src/fuzz_introspector/commands.py @@ -46,6 +46,9 @@ def correlate_binaries_to_logs(binaries_dir: str) -> int: def end_to_end(args) -> int: """Runs both frontend and backend.""" + if not args.language: + args.language = utils.detect_language(args.target_dir) + oss_fuzz.analyse_folder(args.language, args.target_dir, 'LLVMFuzzerTestOneInput') diff --git a/src/fuzz_introspector/frontends/oss_fuzz.py b/src/fuzz_introspector/frontends/oss_fuzz.py index 1994c0854..f81c2e9f9 100644 --- a/src/fuzz_introspector/frontends/oss_fuzz.py +++ b/src/fuzz_introspector/frontends/oss_fuzz.py @@ -223,7 +223,13 @@ def process_rust_project(target_dir, out): f.write(f'Call tree\n{calltree}') -def analyse_folder(language, directory, entrypoint, out='', module_only=False): +def analyse_folder(language: str = '', + directory: str = '', + entrypoint: str = '', + out='', + module_only=False): + """Runs a full frontend analysis on a given directory""" + if language == 'c': process_c_project(directory, entrypoint, out, module_only) if language.lower() in ['cpp', 'c++']: diff --git a/src/fuzz_introspector/utils.py b/src/fuzz_introspector/utils.py index 9e6295e28..8bd37bf91 100644 --- a/src/fuzz_introspector/utils.py +++ b/src/fuzz_introspector/utils.py @@ -21,6 +21,7 @@ import re import shutil import yaml +import pathlib from bs4 import BeautifulSoup @@ -564,3 +565,35 @@ def locate_rust_fuzz_item(funcname: str, item_list: List[str]) -> str: break return '' + + +def detect_language(directory) -> str: + """Given a folder finds the likely programming language of the project""" + language_extensions = { + 'c': ['.c', '.h'], + 'cpp': ['.cpp', '.cc', '.c++', '.h', '.hpp'], + 'jvm': ['.java'], + 'rust': ['.rs'] + } + paths_to_avoid = [ + '/src/aflplusplus', '/src/honggfuzz', '/src/libfuzzer', '/src/fuzztest' + ] + + language_counts: Dict[str, int] = {} + + for dirpath, _, filenames in os.walk(directory): + if any([x for x in paths_to_avoid if dirpath.startswith(x)]): + continue + for filename in filenames: + for language, extensions in language_extensions.items(): + if pathlib.Path(filename).suffix in extensions: + curr_count = language_counts.get(language, 0) + language_counts[language] = curr_count + 1 + + max_lang = '' + max_count = -1 + for language, count in language_counts.items(): + if count >= max_count: + max_count = count + max_lang = language + return max_lang