Skip to content

Commit

Permalink
Updated mutators
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnuram1999 committed Jan 24, 2024
1 parent 5fb5f69 commit 71f970f
Show file tree
Hide file tree
Showing 19 changed files with 25 additions and 62 deletions.
2 changes: 2 additions & 0 deletions slither/tools/mutator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

`slither-mutate <codebase> --test-cmd <test-command> <options>`

To view the list of mutators available `slither-mutate --list-mutators`

### CLI Interface

```
Expand Down
2 changes: 1 addition & 1 deletion slither/tools/mutator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ListMutators(argparse.Action): # pylint: disable=too-few-public-methods
def __call__(
self, parser: Any, *args: Any, **kwargs: Any
) -> None: # pylint: disable=signature-differs
checks = _get_mutators()
checks = _get_mutators(None)
output_mutators(checks)
parser.exit()

Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/AOR.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict
from slither.slithir.operations import Binary, BinaryType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
from slither.core.expressions.unary_operation import UnaryOperation

arithmetic_operators = [
Expand All @@ -15,7 +15,6 @@
class AOR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "AOR"
HELP = "Arithmetic operator replacement"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/ASOR.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
from slither.core.expressions.assignment_operation import AssignmentOperationType, AssignmentOperation

assignment_operators = [
Expand All @@ -20,7 +20,6 @@
class ASOR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "ASOR"
HELP = "Assignment Operator Replacement"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/BOR.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict
from slither.slithir.operations import Binary, BinaryType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator

bitwise_operators = [
BinaryType.AND,
Expand All @@ -14,7 +14,6 @@
class BOR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "BOR"
HELP = "Bitwise Operator Replacement"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/CR.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from typing import Dict
from slither.core.cfg.node import NodeType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator


class CR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "CR"
HELP = 'Comment Replacement'
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/FHR.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
import re

function_header_replacements = [
Expand All @@ -13,7 +13,6 @@
class FHR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "FHR"
HELP = 'Function Header Replacement'
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/LIR.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict
from slither.core.expressions import Literal
from slither.core.variables.variable import Variable
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.core.solidity_types import ElementaryType

Expand All @@ -10,7 +10,6 @@
class LIR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "LIR"
HELP = "Literal Interger Replacement"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/LOR.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict
from slither.slithir.operations import Binary, BinaryType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator

logical_operators = [
BinaryType.OROR,
Expand All @@ -11,7 +11,6 @@
class LOR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "LOR"
HELP = "Logical Operator Replacement"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/MIA.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from typing import Dict
from slither.core.cfg.node import NodeType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
from slither.core.expressions.unary_operation import UnaryOperationType, UnaryOperation

class MIA(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "MIA"
HELP = '"if" construct around statement'
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/MVIE.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from typing import Dict
from slither.core.expressions import Literal
from slither.core.variables.variable import Variable
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
from slither.tools.mutator.utils.patch import create_patch_with_line

class MVIE(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "MVIE"
HELP = "variable initialization using an expression"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/MVIV.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from typing import Dict
from slither.core.expressions import Literal
from slither.core.variables.variable import Variable
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
from slither.tools.mutator.utils.patch import create_patch_with_line

class MVIV(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "MVIV"
HELP = "variable initialization using a value"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/MWA.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from typing import Dict
from slither.core.cfg.node import NodeType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
from slither.core.expressions.unary_operation import UnaryOperationType, UnaryOperation

class MWA(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "MWA"
HELP = '"while" construct around statement'
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/ROR.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict
from slither.slithir.operations import Binary, BinaryType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator

relational_operators = [
BinaryType.LESS,
Expand All @@ -15,7 +15,6 @@
class ROR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "ROR"
HELP = "Relational Operator Replacement"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
4 changes: 1 addition & 3 deletions slither/tools/mutator/mutators/RR.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from typing import Dict
from slither.core.cfg.node import NodeType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature

from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator

class RR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "RR"
HELP = 'Revert Replacement'
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/SBR.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict
from slither.core.cfg.node import NodeType
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator
import re
from slither.core.variables.variable import Variable

Expand Down Expand Up @@ -47,7 +47,6 @@
class SBR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "SBR"
HELP = 'Solidity Based Replacement'
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:

Expand Down
3 changes: 1 addition & 2 deletions slither/tools/mutator/mutators/UOR.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict
from slither.core.expressions.unary_operation import UnaryOperationType, UnaryOperation
from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator

unary_operators = [
UnaryOperationType.PLUSPLUS_PRE,
Expand All @@ -14,7 +14,6 @@
class UOR(AbstractMutator): # pylint: disable=too-few-public-methods
NAME = "UOR"
HELP = "Unary Operator Replacement"
FAULTNATURE = FaultNature.Missing

def _mutate(self) -> Dict:
result: Dict = {}
Expand Down
24 changes: 1 addition & 23 deletions slither/tools/mutator/mutators/abstract_mutator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import abc
import logging
from enum import Enum
from typing import Optional, Dict, Tuple, List
from slither.core.compilation_unit import SlitherCompilationUnit
from slither.formatters.utils.patches import apply_patch, create_diff
Expand All @@ -12,22 +11,10 @@

class IncorrectMutatorInitialization(Exception):
pass

class FaultNature(Enum):
Missing = 0
Wrong = 1
Extraneous = 2
Undefined = 100

# not executed - can be detected by replacing with revert
# has no effect - can be detected by removing a line / comment
# can have valid mutant
# can't have valid mutant

class AbstractMutator(metaclass=abc.ABCMeta): # pylint: disable=too-few-public-methods
NAME = ""
HELP = ""
FAULTNATURE = FaultNature.Undefined
VALID_MUTANTS_COUNT = 0
INVALID_MUTANTS_COUNT = 0

Expand Down Expand Up @@ -69,11 +56,6 @@ def __init__(
f"HELP is not initialized {self.__class__.__name__}"
)

if self.FAULTNATURE == FaultNature.Undefined:
raise IncorrectMutatorInitialization(
f"FAULTNATURE is not initialized {self.__class__.__name__}"
)

if rate < 0 or rate > 100:
raise IncorrectMutatorInitialization(
f"rate must be between 0 and 100 {self.__class__.__name__}"
Expand Down Expand Up @@ -115,8 +97,4 @@ def mutate(self) -> Tuple[int, int, List[int]]:
# add valid mutant patches to a output file
with open(self.output_folder + "/patches_file.txt", 'a') as patches_file:
patches_file.write(diff + '\n')
return (self.VALID_MUTANTS_COUNT, self.INVALID_MUTANTS_COUNT, self.dont_mutate_line)




return (self.VALID_MUTANTS_COUNT, self.INVALID_MUTANTS_COUNT, self.dont_mutate_line)
13 changes: 6 additions & 7 deletions slither/tools/mutator/utils/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ def output_mutators(mutators_classes: List[Type[AbstractMutator]]) -> None:
for detector in mutators_classes:
argument = detector.NAME
help_info = detector.HELP
fault_nature = detector.FAULTNATURE.name
mutators_list.append((argument, help_info, fault_nature))
table = MyPrettyTable(["Num", "Name", "What it Does", "Fault Nature"])
mutators_list.append((argument, help_info))
table = MyPrettyTable(["Num", "Name", "What it Does"])

# Sort by class, nature, name
mutators_list = sorted(mutators_list, key=lambda element: (element[2], element[0]))
# Sort by class
mutators_list = sorted(mutators_list, key=lambda element: (element[0]))
idx = 1
for (argument, help_info, fault_nature) in mutators_list:
table.add_row([str(idx), argument, help_info, fault_nature])
for (argument, help_info) in mutators_list:
table.add_row([str(idx), argument, help_info])
idx = idx + 1
print(table)

0 comments on commit 71f970f

Please sign in to comment.