From 48f8c85e67026e9737bbbe89ad9d2e192e25b2df Mon Sep 17 00:00:00 2001 From: Dongge Liu Date: Fri, 22 Nov 2024 13:01:21 +1100 Subject: [PATCH] Fix incorrect binary names (#722) ... and a script to find mismatched binary names between benchmark sets. --- benchmark-sets/all/hermes.yaml | 2 +- benchmark-sets/all/sleuthkit.yaml | 2 +- helper/diff_target.py | 71 +++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 helper/diff_target.py diff --git a/benchmark-sets/all/hermes.yaml b/benchmark-sets/all/hermes.yaml index bec1dde2b8..1e23948622 100644 --- a/benchmark-sets/all/hermes.yaml +++ b/benchmark-sets/all/hermes.yaml @@ -59,5 +59,5 @@ "signature": "Value facebook::jsi::RuntimeDecorator::evaluatePreparedJavaScript(RuntimeDecorator *, const shared_ptr &)" "language": "c++" "project": "hermes" -"target_name": "/src/hermes/tools/fuzzers/libfuzzer/fuzzer-jsi-entry.cpp" +"target_name": "fuzzer-jsi-entry" "target_path": "/src/hermes/tools/fuzzers/libfuzzer/fuzzer-jsi-entry.cpp" diff --git a/benchmark-sets/all/sleuthkit.yaml b/benchmark-sets/all/sleuthkit.yaml index a6d197fbe9..1b2509dd0a 100644 --- a/benchmark-sets/all/sleuthkit.yaml +++ b/benchmark-sets/all/sleuthkit.yaml @@ -59,5 +59,5 @@ "signature": "TSK_RETVAL_ENUM ffs_dir_open_meta(TSK_FS_INFO *, TSK_FS_DIR **, TSK_INUM_T, int)" "language": "c++" "project": "sleuthkit" -"target_name": "sleuthkit_mmls_sun_fuzzer" +"target_name": "mmls_sun_fuzzer" "target_path": "/src/sleuthkit_mmls_fuzzer.cc" diff --git a/helper/diff_target.py b/helper/diff_target.py new file mode 100644 index 0000000000..9b90e12907 --- /dev/null +++ b/helper/diff_target.py @@ -0,0 +1,71 @@ +""" +Replace the fuzz target binary name (`target_name`) in all benchmark YAMLs of a +dir with the target name of the corresponding YAMLs of another dir. Target will +only be replaced if they share the same fuzz target source code path +(`target_path`). +Usage: python3 diff_target.py +""" +import argparse +import os + +import yaml + + +def load_yaml(file_path): + """Load a YAML file and return its contents.""" + try: + with open(file_path, 'r') as f: + return yaml.safe_load(f) + except Exception as e: + print(f"Error loading YAML file {file_path}: {e}") + return None + + +def save_yaml(file_path, data): + """Save a dictionary to a YAML file.""" + try: + with open(file_path, 'w') as f: + yaml.safe_dump(data, f) + except Exception as e: + print(f"Error saving YAML file {file_path}: {e}") + + +def overwrite_target_name(dir_a, dir_b): + """Overwrite target_name in A if target_path is the same and target_name differs.""" + common_files = set(os.listdir(dir_a)) & set(os.listdir(dir_b)) + updated_files = [] + + for file_name in common_files: + if not file_name.endswith('.yaml'): + continue + + file_a = os.path.join(dir_a, file_name) + file_b = os.path.join(dir_b, file_name) + + data_a = load_yaml(file_a) + data_b = load_yaml(file_b) + + if data_a and data_b: + if data_a.get('target_path') == data_b.get('target_path'): + if data_a.get('target_name') != data_b.get('target_name'): + data_a['target_name'] = data_b['target_name'] + save_yaml(file_a, data_a) + updated_files.append(file_name) + + return updated_files + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Update target_name in A if target_path matches B.") + parser.add_argument("dir_a", help="Path to directory A (to be updated)") + parser.add_argument("dir_b", help="Path to directory B (source of updates)") + args = parser.parse_args() + + updated_files = overwrite_target_name(args.dir_a, args.dir_b) + if updated_files: + print("The following files in A had target_name updated from B:") + for file_name in updated_files: + print(file_name) + else: + print("No updates were necessary.")