Skip to content

Commit 9f72672

Browse files
frontends: python: add automatic package detection (#293)
1 parent 290051a commit 9f72672

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

frontends/python/main.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
import sys
1617
import json
1718
import argparse
1819

@@ -23,21 +24,58 @@
2324
def main():
2425
parser = argparse.ArgumentParser()
2526
parser.add_argument(
26-
"entry_point",
27-
nargs="*",
28-
help="Entry points to be processed"
27+
"--fuzzer",
28+
help="Fuzzer to be processed"
2929
)
3030
parser.add_argument(
3131
"--package",
3232
help="Package containing the code to be analyzed",
3333
default=None
3434
)
3535
args = parser.parse_args()
36-
run_fuzz_pass(args.package, args.entry_point)
36+
run_fuzz_pass(args.fuzzer, args.package)
3737

38-
def run_fuzz_pass(package, entry_point):
38+
def resolve_package(fuzzer_path):
39+
"""Resolves the package of a fuzzer"""
40+
print("Fuzzer path: %s"%(fuzzer_path))
41+
dirpath = os.path.dirname(fuzzer_path)
42+
43+
# sanity check one
44+
all_dirs = []
45+
for d in os.listdir(dirpath):
46+
if os.path.isdir(os.path.join(dirpath, d)):
47+
all_dirs.append(d)
48+
49+
# Read all potential imports in the fuzzer
50+
fuzz_content = ""
51+
with open(fuzzer_path, "r") as fp:
52+
fuzz_content = fp.read()
53+
54+
# Now go through each of the directories and check if any dir is in the fuzzer
55+
imported_dirs = []
56+
for d in all_dirs:
57+
if d in fuzz_content:
58+
print("Directory: %s"%(d))
59+
imported_dirs.append(d)
60+
61+
if len(imported_dirs) > 0:
62+
print("Package path: %s"%(dirpath))
63+
return dirpath + "/"
64+
65+
print("Could not identify the package")
66+
return None
67+
68+
def run_fuzz_pass(fuzzer, package):
69+
if package is None:
70+
package = resolve_package(fuzzer)
71+
if package is None:
72+
print("No package. Exiting early now as the results will not be good")
73+
sys.exit(1)
74+
75+
print("Fuzzer: %s"%(fuzzer))
76+
print("Package: %s"%(package))
3977
cg = CallGraphGenerator(
40-
entry_point,
78+
[ fuzzer ],
4179
package,
4280
-1,
4381
CALL_GRAPH_OP
@@ -60,11 +98,7 @@ def convert_to_fuzzing_cfg(cg_extended):
6098
# Extract fuzzer entrypoint and print calltree.
6199
ep_key = cg_extended['ep']['mod'] + "." + cg_extended['ep']['name']
62100
ep_node = cg_extended['cg'][ep_key]
63-
64-
# Dump the full cg to json. This includes information about each function.
65101
print(json.dumps(cg_extended, indent=4))
66-
67-
# Print the calltree for the given fuzzer
68102
print_calltree(cg_extended['cg'], ep_key, set())
69103

70104
def print_calltree(cg_extended, k, s1, depth=0, lineno=-1, themod="", ext_mod=""):

0 commit comments

Comments
 (0)