Skip to content

Commit

Permalink
fix: recursion error, better design
Browse files Browse the repository at this point in the history
  • Loading branch information
talfao committed Apr 14, 2024
1 parent f2b3346 commit 8b8a7bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
35 changes: 14 additions & 21 deletions slither/detectors/oracles/oracle_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@


class OracleDetector(AbstractDetector):

# Vars for start point and max number of recursive calls
INVESTIGATION_START = 1
MAX_INVESTIGATION_DEEP = 10

def __init__(self, compilation_unit, slither, logger):
super().__init__(compilation_unit, slither, logger)
self.oracles = []
Expand Down Expand Up @@ -166,19 +161,22 @@ def vars_in_conditions(self, oracle: Oracle):
for ir in node.irs:
if isinstance(ir, InternalCall):
self.investigate_internal_call(
ir.function, var, None, self.INVESTIGATION_START
oracle,
ir.function,
var,
)
oracle.remove_occurances_in_function()

if len(self.nodes_with_var) > 0:
oracle_vars.append(VarInCondition(var, self.nodes_with_var))
else:
if self.investigate_internal_call(
oracle.function, var, None, self.INVESTIGATION_START
):
if self.investigate_internal_call(oracle, oracle.function, var):
oracle_vars.append(VarInCondition(var, self.nodes_with_var))
oracle.remove_occurances_in_function()
elif nodes := self.investigate_on_return(oracle, var):
oracle_vars.append(VarInCondition(var, nodes))
oracle.out_of_function_checks.append((var, nodes))
oracle.remove_occurances_in_function()
else:
vars_not_in_condition.append(var)
oracle_vars.append(var)
Expand Down Expand Up @@ -207,8 +205,7 @@ def checks_performed_out_of_original_function(self, oracle, returned_var):
nodes = []
for node in nodes_of_call:
oracle.set_function(functions_of_call[i])
result = oracle.add_occurance_in_function(functions_of_call[i])
if not result:
if not oracle.add_occurance_in_function(functions_of_call[i]):
break
oracle.set_node(node)
new_vars = self.get_returned_variables_from_oracle(node)
Expand Down Expand Up @@ -248,13 +245,12 @@ def investigate_on_return(self, oracle, var) -> bool:
return False

# This function interates through all internal calls in function and checks if the var is used in condition any of them
def investigate_internal_call(
self, function: FunctionContract, var, original_function, depth
) -> bool:
def investigate_internal_call(self, oracle, function: FunctionContract, var) -> bool:
if function is None:
return False
if function == original_function or depth >= self.MAX_INVESTIGATION_DEEP:
print("Recursion limit reached")

result = oracle.add_occurance_in_function(function)
if not result:
return False

original_var_as_param = self.map_param_to_var(var, function)
Expand All @@ -280,12 +276,9 @@ def investigate_internal_call(
for node in function.nodes:
for ir in node.irs:
if isinstance(ir, InternalCall):
if self.investigate_internal_call(
ir.function, original_var_as_param, function, depth + 1
):
if self.investigate_internal_call(oracle, ir.function, original_var_as_param):
return True
elif depth >= self.INVESTIGATION_START:
return False

return False

def _detect(self):
Expand Down
3 changes: 3 additions & 0 deletions slither/detectors/oracles/supported_oracles/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def add_occurance_in_function(self, _function):
self.occured_in_functions.append(_function)
return True

def remove_occurances_in_function(self):
self.occured_in_functions = []

def get_calls(self):
return self.calls

Expand Down

0 comments on commit 8b8a7bb

Please sign in to comment.