Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation repairs #2648

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions slither/tools/mutator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ def main() -> None: # pylint: disable=too-many-statements,too-many-branches,too
# get all the contracts as a list from given codebase
sol_file_list: List[str] = get_sol_file_list(Path(args.codebase), paths_to_ignore_list)

logger.info(blue("Preparing to mutate files:\n- " + "\n- ".join(sol_file_list)))
if not contract_names:
logger.info(blue("Preparing to mutate files:\n- " + "\n- ".join(sol_file_list)))
else:
logger.info(blue("Preparing to mutate contracts:\n- " + "\n- ".join(contract_names)))

# folder where backup files and uncaught mutants are saved
if output_dir is None:
Expand Down Expand Up @@ -240,7 +243,8 @@ def main() -> None: # pylint: disable=too-many-statements,too-many-branches,too

# perform mutations on {target_contract} in file {file_name}
# setup placeholder val to signal whether we need to skip if no target_contract is found
target_contract = "SLITHER_SKIP_MUTATIONS" if contract_names else ""
skip_flag = "SLITHER_SKIP_MUTATIONS"
target_contract = skip_flag if contract_names else ""
try:
# loop through all contracts in file_name
for compilation_unit_of_main_file in sl.compilation_units:
Expand All @@ -258,8 +262,7 @@ def main() -> None: # pylint: disable=too-many-statements,too-many-branches,too
)
continue

if target_contract == "SLITHER_SKIP_MUTATIONS":
logger.debug(f"Skipping mutations in {filename}")
if target_contract == skip_flag:
continue

# TODO: find a more specific way to omit interfaces
Expand Down Expand Up @@ -334,6 +337,10 @@ def main() -> None: # pylint: disable=too-many-statements,too-many-branches,too
# transfer and delete the backup files
transfer_and_delete(files_dict)

if target_contract == skip_flag:
logger.debug(f"No target contracts found in {filename}, skipping")
continue

# log results for this file
logger.info(blue(f"Done mutating {target_contract}."))
if total_mutant_counts[0] > 0:
Expand Down
8 changes: 5 additions & 3 deletions slither/tools/mutator/mutators/AOR.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ def _mutate(self) -> Dict:
# Get the string
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
# Replace the expression with true
new_str = f"{old_str.split(ir.type.value)[0]}{op.value}{old_str.split(ir.type.value)[1]}"
halves = old_str.split(ir.type.value)
if len(halves) != 2:
continue # skip if assembly
new_str = f"{halves[0]}{op.value}{halves[1]}"
create_patch_with_line(
result,
self.in_file,
Expand Down
2 changes: 1 addition & 1 deletion slither/tools/mutator/mutators/ASOR.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _mutate(self) -> Dict:
if op != ir.expression:
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
# Replace the expression with true
Expand Down
7 changes: 5 additions & 2 deletions slither/tools/mutator/mutators/BOR.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ def _mutate(self) -> Dict:
# Get the string
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
# Replace the expression with true
new_str = f"{old_str.split(ir.type.value)[0]}{op.value}{old_str.split(ir.type.value)[1]}"
halves = old_str.split(ir.type.value)
if len(halves) != 2:
continue # skip if assembly
new_str = f"{halves[0]}{op.value}{halves[1]}"
create_patch_with_line(
result,
self.in_file,
Expand Down
2 changes: 1 addition & 1 deletion slither/tools/mutator/mutators/CR.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _mutate(self) -> Dict:
# Get the string
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
new_str = "//" + old_str
Expand Down
2 changes: 1 addition & 1 deletion slither/tools/mutator/mutators/FHR.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _mutate(self) -> Dict:
for function in self.contract.functions_and_modifiers_declared:
start = function.source_mapping.start
stop = start + function.source_mapping.content.find("{")
old_str = self.in_file_str[start:stop]
old_str = function.source_mapping.content
line_no = function.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
for value in function_header_replacements:
Expand Down
4 changes: 2 additions & 2 deletions slither/tools/mutator/mutators/LIR.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _mutate(self) -> Dict: # pylint: disable=too-many-branches
# Get the string
start = variable.source_mapping.start
stop = start + variable.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = variable.source_mapping.content
line_no = variable.node_initialization.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
for value in literal_replacements:
Expand Down Expand Up @@ -67,7 +67,7 @@ def _mutate(self) -> Dict: # pylint: disable=too-many-branches
literal_replacements.append("-1")
start = variable.source_mapping.start
stop = start + variable.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = variable.source_mapping.content
line_no = variable.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
for new_value in literal_replacements:
Expand Down
2 changes: 1 addition & 1 deletion slither/tools/mutator/mutators/LOR.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _mutate(self) -> Dict:
# Get the string
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
# Replace the expression with true
Expand Down
2 changes: 1 addition & 1 deletion slither/tools/mutator/mutators/MIA.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def _mutate(self) -> Dict:
# Get the string
start = node.expression.source_mapping.start
stop = start + node.expression.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
# Replace the expression with true and false
Expand Down
4 changes: 2 additions & 2 deletions slither/tools/mutator/mutators/MVIE.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _mutate(self) -> Dict:
# Get the string
start = variable.source_mapping.start
stop = variable.expression.source_mapping.start
old_str = self.in_file_str[start:stop]
old_str = variable.source_mapping.content
new_str = old_str[: old_str.find("=")]
line_no = variable.node_initialization.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
Expand All @@ -44,7 +44,7 @@ def _mutate(self) -> Dict:
# Get the string
start = variable.source_mapping.start
stop = variable.expression.source_mapping.start
old_str = self.in_file_str[start:stop]
old_str = variable.source_mapping.content
new_str = old_str[: old_str.find("=")]
line_no = variable.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
Expand Down
4 changes: 2 additions & 2 deletions slither/tools/mutator/mutators/MVIV.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _mutate(self) -> Dict:
# Get the string
start = variable.source_mapping.start
stop = variable.expression.source_mapping.start
old_str = self.in_file_str[start:stop]
old_str = variable.source_mapping.content
new_str = old_str[: old_str.find("=")]
line_no = variable.node_initialization.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
Expand All @@ -43,7 +43,7 @@ def _mutate(self) -> Dict:
if variable.initialized and isinstance(variable.expression, Literal):
start = variable.source_mapping.start
stop = variable.expression.source_mapping.start
old_str = self.in_file_str[start:stop]
old_str = variable.source_mapping.content
new_str = old_str[: old_str.find("=")]
line_no = variable.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
Expand Down
2 changes: 1 addition & 1 deletion slither/tools/mutator/mutators/MWA.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _mutate(self) -> Dict:
# Get the string
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
if not isinstance(node.expression, UnaryOperation):
Expand Down
4 changes: 2 additions & 2 deletions slither/tools/mutator/mutators/ROR.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def _mutate(self) -> Dict:
# Get the string
start = ir.expression.source_mapping.start
stop = start + ir.expression.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
# Replace the expression with true
new_str = f"{old_str.split(ir.type.value)[0]} {op.value} {old_str.split(ir.type.value)[1]}"
new_str = f"{old_str.split(ir.type.value)[0]}{op.value}{old_str.split(ir.type.value)[1]}"
create_patch_with_line(
result,
self.in_file,
Expand Down
14 changes: 10 additions & 4 deletions slither/tools/mutator/mutators/RR.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ def _mutate(self) -> Dict:
for node in function.nodes:
if node.type not in (
NodeType.ENTRYPOINT,
NodeType.IF,
NodeType.ENDIF,
NodeType.ENDLOOP,
NodeType.PLACEHOLDER,
):
# Get the string
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
old_str = node.source_mapping.content
line_no = node.source_mapping.lines[0]
if not line_no in self.dont_mutate_line:
if node.type == NodeType.RETURN and not old_str.lstrip().startswith(
"return"
):
continue # skip the return declarations in fn signatures
if not old_str.lstrip().startswith("revert"):
new_str = "revert()"
create_patch_with_line(
Expand All @@ -33,6 +39,6 @@ def _mutate(self) -> Dict:
stop,
old_str,
new_str,
line_no[0],
line_no,
)
return result
4 changes: 2 additions & 2 deletions slither/tools/mutator/mutators/SBR.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _mutate(self) -> Dict:
# Get the string
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
for value in solidity_rules:
Expand All @@ -89,7 +89,7 @@ def _mutate(self) -> Dict:
if node:
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
for value in solidity_rules:
Expand Down
2 changes: 1 addition & 1 deletion slither/tools/mutator/mutators/UOR.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _mutate(self) -> Dict:
continue
start = node.source_mapping.start
stop = start + node.source_mapping.length
old_str = self.in_file_str[start:stop]
old_str = node.source_mapping.content
line_no = node.source_mapping.lines
if not line_no[0] in self.dont_mutate_line:
if (
Expand Down
12 changes: 9 additions & 3 deletions slither/tools/mutator/mutators/abstract_mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from slither.formatters.utils.patches import apply_patch, create_diff
from slither.tools.mutator.utils.testing_generated_mutant import test_patch
from slither.core.declarations import Contract
from slither.utils.colors import red

logger = logging.getLogger("Slither-Mutate")

Expand Down Expand Up @@ -70,13 +71,18 @@ def __init__( # pylint: disable=too-many-arguments

@abc.abstractmethod
def _mutate(self) -> Dict:
"""TODO Documentation"""
"""Abstract placeholder, will be overwritten by each mutator"""
return {}

# pylint: disable=too-many-branches
def mutate(self) -> Tuple[List[int], List[int], List[int]]:
# call _mutate function from different mutators
(all_patches) = self._mutate()
all_patches: Dict = {}
# pylint: disable=broad-exception-caught
try:
# call _mutate function from different mutators
(all_patches) = self._mutate()
except Exception as e:
logger.error(red("%s mutator failed in %s: %s"), self.NAME, self.contract.name, str(e))
if "patches" not in all_patches:
logger.debug("No patches found by %s", self.NAME)
return [0, 0, 0], [0, 0, 0], self.dont_mutate_line
Expand Down
4 changes: 1 addition & 3 deletions slither/tools/mutator/utils/testing_generated_mutant.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ def test_patch( # pylint: disable=too-many-arguments

create_mutant_file(output_folder, file, generator_name)
logger.info(
red(
f"[{generator_name}] Line {patch['line_number']}: '{patch['old_string']}' ==> '{patch['new_string']}' --> UNCAUGHT"
)
f"[{generator_name}] Line {patch['line_number']}: '{patch['old_string']}' ==> '{patch['new_string']}' --> UNCAUGHT"
)
reset_file(file)

Expand Down