From fb964e7af7d2dddabec8dde05bd6125db57f96d5 Mon Sep 17 00:00:00 2001 From: alpharush <0xalpharush@protonmail.com> Date: Tue, 31 Jan 2023 11:41:06 -0600 Subject: [PATCH 1/2] use enum strings instead of impl. __str__ --- slither/core/cfg/node.py | 79 +++++++------------------ slither/core/declarations/function.py | 2 +- slither/slithir/operations/binary.py | 83 +++++++-------------------- slither/slithir/operations/unary.py | 14 +---- 4 files changed, 45 insertions(+), 133 deletions(-) diff --git a/slither/core/cfg/node.py b/slither/core/cfg/node.py index eec599bc42..80bc221ae7 100644 --- a/slither/core/cfg/node.py +++ b/slither/core/cfg/node.py @@ -66,80 +66,41 @@ class NodeType(Enum): - ENTRYPOINT = 0x0 # no expression + ENTRYPOINT = "ENTRY_POINT" # no expression - # Node with expression + # Nodes that may have an expression - EXPRESSION = 0x10 # normal case - RETURN = 0x11 # RETURN may contain an expression - IF = 0x12 - VARIABLE = 0x13 # Declaration of variable - ASSEMBLY = 0x14 - IFLOOP = 0x15 + EXPRESSION = "EXPRESSION" # normal case + RETURN = "RETURN" # RETURN may contain an expression + IF = "IF" + VARIABLE = "NEW VARIABLE" # Variable declaration + ASSEMBLY = "INLINE ASM" + IFLOOP = "IF_LOOP" - # Merging nodes + # Nodes where control flow merges # Can have phi IR operation - ENDIF = 0x50 # ENDIF node source mapping points to the if/else body - STARTLOOP = 0x51 # STARTLOOP node source mapping points to the entire loop body - ENDLOOP = 0x52 # ENDLOOP node source mapping points to the entire loop body + ENDIF = "END_IF" # ENDIF node source mapping points to the if/else "body" + STARTLOOP = "BEGIN_LOOP" # STARTLOOP node source mapping points to the entire loop "body" + ENDLOOP = "END_LOOP" # ENDLOOP node source mapping points to the entire loop "body" - # Below the nodes have no expression - # But are used to expression CFG structure + # Below the nodes do not have an expression but are used to expression CFG structure. # Absorbing node - THROW = 0x20 + THROW = "THROW" # Loop related nodes - BREAK = 0x31 - CONTINUE = 0x32 + BREAK = "BREAK" + CONTINUE = "CONTINUE" # Only modifier node - PLACEHOLDER = 0x40 + PLACEHOLDER = "_" - TRY = 0x41 - CATCH = 0x42 + TRY = "TRY" + CATCH = "CATCH" # Node not related to the CFG # Use for state variable declaration - OTHER_ENTRYPOINT = 0x60 - - # @staticmethod - def __str__(self): - if self == NodeType.ENTRYPOINT: - return "ENTRY_POINT" - if self == NodeType.EXPRESSION: - return "EXPRESSION" - if self == NodeType.RETURN: - return "RETURN" - if self == NodeType.IF: - return "IF" - if self == NodeType.VARIABLE: - return "NEW VARIABLE" - if self == NodeType.ASSEMBLY: - return "INLINE ASM" - if self == NodeType.IFLOOP: - return "IF_LOOP" - if self == NodeType.THROW: - return "THROW" - if self == NodeType.BREAK: - return "BREAK" - if self == NodeType.CONTINUE: - return "CONTINUE" - if self == NodeType.PLACEHOLDER: - return "_" - if self == NodeType.TRY: - return "TRY" - if self == NodeType.CATCH: - return "CATCH" - if self == NodeType.ENDIF: - return "END_IF" - if self == NodeType.STARTLOOP: - return "BEGIN_LOOP" - if self == NodeType.ENDLOOP: - return "END_LOOP" - if self == NodeType.OTHER_ENTRYPOINT: - return "OTHER_ENTRYPOINT" - return f"Unknown type {hex(self.value)}" + OTHER_ENTRYPOINT = "OTHER_ENTRYPOINT" # endregion diff --git a/slither/core/declarations/function.py b/slither/core/declarations/function.py index 2fdea72109..c4f4809f65 100644 --- a/slither/core/declarations/function.py +++ b/slither/core/declarations/function.py @@ -1378,7 +1378,7 @@ def slithir_cfg_to_dot_str(self, skip_expressions=False) -> str: content = "" content += "digraph{\n" for node in self.nodes: - label = f"Node Type: {str(node.type)} {node.node_id}\n" + label = f"Node Type: {node.type.value} {node.node_id}\n" if node.expression and not skip_expressions: label += f"\nEXPRESSION:\n{node.expression}\n" if node.irs and not skip_expressions: diff --git a/slither/slithir/operations/binary.py b/slither/slithir/operations/binary.py index d8844fa329..d416f3f904 100644 --- a/slither/slithir/operations/binary.py +++ b/slither/slithir/operations/binary.py @@ -12,25 +12,25 @@ class BinaryType(Enum): - POWER = 0 # ** - MULTIPLICATION = 1 # * - DIVISION = 2 # / - MODULO = 3 # % - ADDITION = 4 # + - SUBTRACTION = 5 # - - LEFT_SHIFT = 6 # << - RIGHT_SHIFT = 7 # >> - AND = 8 # & - CARET = 9 # ^ - OR = 10 # | - LESS = 11 # < - GREATER = 12 # > - LESS_EQUAL = 13 # <= - GREATER_EQUAL = 14 # >= - EQUAL = 15 # == - NOT_EQUAL = 16 # != - ANDAND = 17 # && - OROR = 18 # || + POWER = "**" + MULTIPLICATION = "*" + DIVISION = "/" + MODULO = "%" + ADDITION = "+" + SUBTRACTION = "-" + LEFT_SHIFT = "<<" + RIGHT_SHIFT = ">>" + AND = "&" + CARET = "^" + OR = "|" + LESS = "<" + GREATER = ">" + LESS_EQUAL = "<=" + GREATER_EQUAL = ">=" + EQUAL = "==" + NOT_EQUAL = "!=" + ANDAND = "&&" + OROR = "||" @staticmethod def return_bool(operation_type): @@ -98,47 +98,6 @@ def can_be_checked_for_overflow(self): BinaryType.DIVISION, ] - def __str__(self): # pylint: disable=too-many-branches - if self == BinaryType.POWER: - return "**" - if self == BinaryType.MULTIPLICATION: - return "*" - if self == BinaryType.DIVISION: - return "/" - if self == BinaryType.MODULO: - return "%" - if self == BinaryType.ADDITION: - return "+" - if self == BinaryType.SUBTRACTION: - return "-" - if self == BinaryType.LEFT_SHIFT: - return "<<" - if self == BinaryType.RIGHT_SHIFT: - return ">>" - if self == BinaryType.AND: - return "&" - if self == BinaryType.CARET: - return "^" - if self == BinaryType.OR: - return "|" - if self == BinaryType.LESS: - return "<" - if self == BinaryType.GREATER: - return ">" - if self == BinaryType.LESS_EQUAL: - return "<=" - if self == BinaryType.GREATER_EQUAL: - return ">=" - if self == BinaryType.EQUAL: - return "==" - if self == BinaryType.NOT_EQUAL: - return "!=" - if self == BinaryType.ANDAND: - return "&&" - if self == BinaryType.OROR: - return "||" - raise SlithIRError(f"str: Unknown operation type {self} {type(self)})") - class Binary(OperationWithLValue): def __init__(self, result, left_variable, right_variable, operation_type: BinaryType): @@ -178,8 +137,8 @@ def type(self): @property def type_str(self): if self.node.scope.is_checked and self._type.can_be_checked_for_overflow(): - return "(c)" + str(self._type) - return str(self._type) + return "(c)" + self._type.value + return self._type.value def __str__(self): if isinstance(self.lvalue, ReferenceVariable): diff --git a/slither/slithir/operations/unary.py b/slither/slithir/operations/unary.py index 7df55ad756..59b5bcbb0a 100644 --- a/slither/slithir/operations/unary.py +++ b/slither/slithir/operations/unary.py @@ -9,8 +9,8 @@ class UnaryType(Enum): - BANG = 0 # ! - TILD = 1 # ~ + BANG = "!" + TILD = "~" @staticmethod def get_type(operation_type, isprefix): @@ -21,14 +21,6 @@ def get_type(operation_type, isprefix): return UnaryType.TILD raise SlithIRError(f"get_type: Unknown operation type {operation_type}") - def __str__(self): - if self == UnaryType.BANG: - return "!" - if self == UnaryType.TILD: - return "~" - - raise SlithIRError(f"str: Unknown operation type {self}") - class Unary(OperationWithLValue): def __init__(self, result, variable, operation_type): @@ -53,7 +45,7 @@ def type(self): @property def type_str(self): - return str(self._type) + return self._type.value def __str__(self): return f"{self.lvalue} = {self.type_str} {self.rvalue} " From 6b50684d6194af853136d29ccfba6235ca91ceef Mon Sep 17 00:00:00 2001 From: alpharush <0xalpharush@protonmail.com> Date: Tue, 31 Jan 2023 11:58:03 -0600 Subject: [PATCH 2/2] use enum value --- slither/core/cfg/node.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slither/core/cfg/node.py b/slither/core/cfg/node.py index 80bc221ae7..f2af8dc57a 100644 --- a/slither/core/cfg/node.py +++ b/slither/core/cfg/node.py @@ -975,7 +975,7 @@ def __str__(self): additional_info += " " + str(self.expression) elif self.variable_declaration: additional_info += " " + str(self.variable_declaration) - txt = str(self._node_type) + additional_info + txt = self._node_type.value + additional_info return txt