Skip to content

Commit

Permalink
core: find where functions are declared in header files (#1624)
Browse files Browse the repository at this point in the history
core: find where functions are declared in headfiles

Signed-off-by: David Korczynski <david@adalogics.com>
  • Loading branch information
DavidKorczynski authored Jun 22, 2024
1 parent 60f3a7c commit 47b942b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/fuzz_introspector/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,17 @@ def correlate_introspector_func_to_debug_information(if_func,

def correlate_introspection_functions_to_debug_info(all_functions_json_report,
debug_all_functions,
proj_lang):
proj_lang,
report_dict=dict()):
"""Correlates function data collected by debug information to function
data collected by LLVMs module, and uses the correlated data to generate
function signatures for each function based on debug information."""

# Find header files
normalized_paths = set()
for header_file in report_dict.get('all_files_in_project', []):
normalized_paths.add(os.path.normpath(header_file['source_file']))

# A lot of look-ups are needed when matching LLVM functions to debug
# functions. Start with creating two indexes to make these look-ups
# faster.
Expand All @@ -924,6 +930,21 @@ def correlate_introspection_functions_to_debug_info(all_functions_json_report,
df['source']['source_file'] = os.path.normpath(df['source'].get(
'source_file', ''))

# Find the header file of this debug function.
possible_header_files = set()
for header_src_file in normalized_paths:
if not header_src_file.endswith(".h"):
continue
if not os.path.isfile(header_src_file):
continue
with open(header_src_file, 'r') as header_file_fd:
content = header_file_fd.read()
name = df.get('name', 'TOTALLYRANDOMNOTFUNCNAME123')
for line_idx, line in enumerate(content.split("\n")):
if f'{name}(' in line:
possible_header_files.add(header_src_file)
df['possible-header-files'] = list(possible_header_files)

# Append debug function to name-index.
entry_list1 = debug_dict_by_name.get(df.get('name', ''), [])
entry_list1.append(df)
Expand All @@ -939,6 +960,7 @@ def correlate_introspection_functions_to_debug_info(all_functions_json_report,
for dl3 in debug_dict_by_filename:
print("%s ------- %d" % (dl3, len(debug_dict_by_filename[dl3])))

# Now correlate signatures
for if_func in all_functions_json_report:
func_sig, correlated_debug_function = correlate_introspector_func_to_debug_information(
if_func, debug_all_functions, debug_dict_by_name,
Expand Down
1 change: 1 addition & 0 deletions src/fuzz_introspector/debug_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def load_debug_report(debug_files):
'all_global_variables': list(all_global_variables.values()),
'all_types': list(all_types.values())
}

return report_dict


Expand Down
2 changes: 1 addition & 1 deletion src/fuzz_introspector/html_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ def create_html_report(introspection_proj: analysis.IntrospectionProject,
# Correlate debug info to introspector functions
analysis.correlate_introspection_functions_to_debug_info(
all_functions_json_report, introspection_proj.debug_all_functions,
proj_profile.target_lang)
proj_profile.target_lang, introspection_proj.debug_report)

# Write various stats and all-functions data to summary.json
proj_profile.write_stats_to_summary_file()
Expand Down

0 comments on commit 47b942b

Please sign in to comment.