Skip to content

Commit c8f2470

Browse files
post-process: substitute exits for exceptions (#299)
* post-process: substitute exits for exceptions The post-processing logic is getting larger and makes sense to make it more Pythonian. Switching calls to exit for python exceptions to support this. * nit
1 parent 069d04a commit c8f2470

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

post-processing/exceptions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2022 Fuzz Introspector Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Exceptions used throughout the package."""
15+
16+
17+
class FuzzIntrospectorError(Exception):
18+
"""Base error"""
19+
20+
21+
class CalltreeError(FuzzIntrospectorError):
22+
"""Error for when dealing with calltrees"""
23+
24+
25+
class AnalysisError(FuzzIntrospectorError):
26+
"""Error for analysis logic"""
27+
28+
29+
class DataLoaderError(FuzzIntrospectorError):
30+
"""Error for handling data loader issues"""

post-processing/fuzz_analysis.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import fuzz_data_loader
3030
from enum import Enum
3131

32+
from exceptions import AnalysisError
33+
3234
logger = logging.getLogger(name=__name__)
3335
logger.setLevel(logging.INFO)
3436

@@ -142,7 +144,9 @@ def callstack_set_curr_node(n, name, c):
142144
if demangled_name != "LLVMFuzzerTestOneInput" and "TestOneInput" not in demangled_name:
143145
logger.info("Unexpected first node in the calltree.")
144146
logger.info(f"Found: {demangled_name}")
145-
exit(1)
147+
raise AnalysisError(
148+
"First node in calltree seems to be non-fuzzer function"
149+
)
146150
coverage_data = profile.coverage.get_hit_details("LLVMFuzzerTestOneInput")
147151
if len(coverage_data) == 0:
148152
logger.error("There is no coverage data (not even all negative).")
@@ -165,7 +169,9 @@ def callstack_set_curr_node(n, name, c):
165169
node.cov_parent = callstack_get_parent(node, callstack)
166170
else:
167171
logger.error("A node should either be the first or it must have a parent")
168-
exit(1)
172+
raise AnalysisError(
173+
"A node should either be the first or it must have a parent"
174+
)
169175
node.cov_hitcount = node_hitcount
170176

171177
# Map hitcount to color of target.

post-processing/fuzz_cfg_load.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Optional
2121
)
2222

23+
from exceptions import CalltreeError
24+
2325
logger = logging.getLogger(name=__name__)
2426

2527

@@ -72,7 +74,8 @@ def extract_all_callsites_recursive(
7274
def extract_all_callsites(calltree: Optional[CalltreeCallsite]) -> List[CalltreeCallsite]:
7375
if calltree is None:
7476
logger.error("Trying to extract from a None calltree")
75-
exit(1)
77+
raise CalltreeError("Calltree is None")
78+
7679
cs_list: List[CalltreeCallsite] = []
7780
extract_all_callsites_recursive(calltree, cs_list)
7881
return cs_list

post-processing/fuzz_data_loader.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import fuzz_cov_load
3333
import fuzz_utils
3434

35+
from exceptions import DataLoaderError
36+
3537
logger = logging.getLogger(name=__name__)
3638
logger.setLevel(logging.INFO)
3739

@@ -652,7 +654,9 @@ def add_func_to_reached_and_clone(merged_profile_old: MergedProjectProfile,
652654

653655
if merged_profile.all_functions[func_to_add.function_name].hitcount == 0:
654656
logger.info("Error. Hitcount did not get set for some reason. Exiting")
655-
exit(1)
657+
raise DataLoaderError(
658+
"Hitcount did not get set for some reason"
659+
)
656660

657661
return merged_profile
658662

0 commit comments

Comments
 (0)