Skip to content

Commit

Permalink
src: copy source files to report directory (#1384)
Browse files Browse the repository at this point in the history
* src: copy source files

Signed-off-by: David Korczynski <david@adalogics.com>

* fix style

Signed-off-by: David Korczynski <david@adalogics.com>

* nit

Signed-off-by: David Korczynski <david@adalogics.com>

---------

Signed-off-by: David Korczynski <david@adalogics.com>
  • Loading branch information
DavidKorczynski authored Jan 24, 2024
1 parent ea5a93b commit e2d56b8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/fuzz_introspector/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import multiprocessing
import os
import json
import shutil

from typing import (
Dict,
Expand Down Expand Up @@ -140,6 +141,15 @@ def dump_debug_files(self):
'source_file': split_line[-1],
'language': split_line[2]
}

# TODO: (David) remove this hack to frontend
# LLVM may combine two absolute paths, which causes the
# filepath to be erroneus.
# Fix this here
if '//' in file_dict['source_file']:
logger.info(line)
file_dict['source_file'] = '/' + file_dict[
'source_file'].split('//')[-1]
all_files_in_debug_info[
file_dict['source_file']] = file_dict
# Functions defined in the module
Expand Down Expand Up @@ -175,7 +185,10 @@ def dump_debug_files(self):
location = line.split(
"from")[-1].strip().split(" ")[0]
source_file = location.split(":")[0]
source_line = location.split(":")[1]
try:
source_line = location.split(":")[1]
except IndexError:
source_line = "-1"
current_struct = {
'type': 'struct',
'name': struct_name,
Expand All @@ -191,7 +204,10 @@ def dump_debug_files(self):
location = line.split(" from ")[-1].split(
" ")[0]
source_file = location.split(":")[0]
source_line = location.split(":")[1]
try:
source_line = location.split(":")[1]
except IndexError:
source_line = "-1"
current_type = {
'type': 'typedef',
'name': name,
Expand All @@ -212,7 +228,10 @@ def dump_debug_files(self):
location = line.split(
"from")[-1].strip().split(" ")[0]
source_file = location.split(":")[0]
source_line = location.split(":")[1]
try:
source_line = location.split(":")[1]
except IndexError:
source_line = "-1"

current_struct['elements'].append({
'name': elem_name,
Expand All @@ -228,7 +247,10 @@ def dump_debug_files(self):
global_variable_name = sline[0]
location = sline[-1]
source_file = location.split(":")[0]
source_line = location.split(":")[1]
try:
source_line = location.split(":")[1]
except IndexError:
source_line = "-1"
all_global_variables[source_file + source_line] = {
'name': global_variable_name,
'source': {
Expand All @@ -254,8 +276,11 @@ def dump_debug_files(self):
current_function['name'] = function_name
if ' from ' in line and ":" in line and "- Operand" not in line:
location = line.split(" from ")[-1]
source_file = line.split(":")[0]
source_line = line.split(":")[-1]
source_file = location.split(":")[0].strip()
try:
source_line = line.split(":")[-1]
except IndexError:
source_line = "-1"
current_function['source'] = {
'source_file': source_file,
'source_line': source_line,
Expand Down Expand Up @@ -298,6 +323,15 @@ def dump_debug_files(self):
'all_types': list(all_types.values())
}

# Extract all files
if not os.path.isdir(constants.SAVED_SOURCE_FOLDER):
os.mkdir(constants.SAVED_SOURCE_FOLDER)

for file_elem in report_dict['all_files_in_project']:
dst = constants.SAVED_SOURCE_FOLDER + '/' + file_elem['source_file']
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy(file_elem['source_file'], dst)

with open(constants.DEBUG_INFO_DUMP, 'w') as debug_dump:
debug_dump.write(json.dumps(report_dict))

Expand Down
2 changes: 2 additions & 0 deletions src/fuzz_introspector/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

DEBUG_INFO_DUMP = "all_debug_info.json"

SAVED_SOURCE_FOLDER = 'source-code'

# Holds data about all functions in javascript, to ease loading of static
# website.
ALL_FUNCTION_JS = "all_functions.js"
Expand Down

0 comments on commit e2d56b8

Please sign in to comment.