Skip to content

Commit

Permalink
Implement user defined function call analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Rog3rSm1th committed Jul 27, 2023
1 parent 910e5ae commit 6593c99
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ The static analysis is performed using *analyzers* which can be either informati
|**Delegate calls detector**|`delegate_call`|Detect delegate calls|Informational|High|Security||✔️|
|**Dead code detector**|`dead_code`|Detect dead code|Informational|High|Security||✔️|
|**Unused arguments detector**|`unused_arguments`|Detect unused arguments|Informational|High|Security||✔️|
|**User defined function call**|`user_defined`|Detect calls of user defined functions|Informational|High|Security||✔️|

#### Run all the analyzers
``` python
Expand Down
6 changes: 5 additions & 1 deletion sierra/analyzer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from sierra.analyzer.functions.functions_analyzer import FunctionsAnalyzer
from sierra.analyzer.security.dead_code_analyzer import DeadCodeAnalyzer
from sierra.analyzer.security.delegate_call_analyzer import DelegateCallAnalyzer
from sierra.analyzer.security.user_defined_function_call_analyzer import (
UserDefinedFunctionCallAnalyzer,
)
from sierra.analyzer.security.usused_arguments_analyzer import UnusedArgumentsAnalyzer
from sierra.analyzer.statistics.statistics_analyzer import StatisticsAnalyzer
from sierra.analyzer.strings.strings_analyzer import StringsAnalyzer


all_analyzers = [
# Security
UserDefinedFunctionCallAnalyzer,
DeadCodeAnalyzer,
DelegateCallAnalyzer,
UnusedArgumentsAnalyzer,
# Informational
FunctionsAnalyzer,
StringsAnalyzer,
StatisticsAnalyzer,
StringsAnalyzer,
]
49 changes: 49 additions & 0 deletions sierra/analyzer/security/user_defined_function_call_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from sierra.analyzer.abstract_analyzer import (
AbstractAnalyzer,
CategoryClassification,
ImpactClassification,
PrecisionClassification,
)
from sierra.objects.objects import SierraConditionalBranch, SierraVariableAssignation


class UserDefinedFunctionCallAnalyzer(AbstractAnalyzer):
"""
Detect user defined function call
"""

NAME = "user defined function call"
ARGUMENT = "user_defined"
HELP = "Find user defined function calls"
IMPACT: ImpactClassification = ImpactClassification.MEDIUM
PRECISION: PrecisionClassification = PrecisionClassification.MEDIUM
CATEGORY: CategoryClassification = CategoryClassification.OPTIMIZATION

def _detect(self) -> bool:
for f in self.program.functions:
statements = f.statements

for statement in statements:
if isinstance(statement, SierraVariableAssignation):
libfunc_call = statement.function.id
elif isinstance(statement, SierraConditionalBranch):
libfunc_call = statement.function
else:
continue

# user defined function
if libfunc_call.startswith("function_call<user@") and libfunc_call.endswith(">"):
# Core functions
if libfunc_call.startswith("function_call<user@core"):
continue

# Remove function_call<> wrapper
function_name = libfunc_call[19:-1]

self.detected = True
self.result.append(
"User defined function %s called in %s"
% (function_name.split("::")[-1], f.id.split("::")[-1])
)

return self.detected

0 comments on commit 6593c99

Please sign in to comment.