Added contract_names arg

pull/2278/head
Vishnuram Rajkumar 10 months ago
parent 6ad193abd9
commit 96e8adc39b
  1. 16
      slither/tools/mutator/__main__.py
  2. 2
      slither/tools/mutator/mutators/FHR.py
  3. 28
      slither/tools/mutator/mutators/abstract_mutator.py
  4. 4
      slither/tools/mutator/utils/testing_generated_mutant.py

@ -176,7 +176,6 @@ def main() -> None:
for filename in sol_file_list:
contract_name = os.path.split(filename)[1].split('.sol')[0]
# TODO: user provides contract name
# slither object
sl = Slither(filename, **vars(args))
# create a backup files
@ -189,10 +188,17 @@ def main() -> None:
# mutation
try:
for compilation_unit_of_main_file in sl.compilation_units:
for M in mutators_list:
m = M(compilation_unit_of_main_file, int(timeout), test_command, test_directory, contract_name, solc_remappings, verbose, output_folder)
# check whether the contract instance exists or not
if m.get_exist_flag():
contract_instance = ''
for contract in compilation_unit_of_main_file.contracts:
if contract_names != None and contract.name in contract_names:
contract_instance = contract
elif str(contract.name).lower() == contract_name.lower():
contract_instance = contract
if contract_instance == '':
logger.error("Can't find the contract")
else:
for M in mutators_list:
m = M(compilation_unit_of_main_file, int(timeout), test_command, test_directory, contract_instance, solc_remappings, verbose, output_folder)
count_valid, count_invalid = m.mutate()
v_count += count_valid
total_count += count_valid + count_invalid

@ -3,8 +3,6 @@ from slither.tools.mutator.utils.patch import create_patch_with_line
from slither.tools.mutator.mutators.abstract_mutator import AbstractMutator, FaultNature
import re
# INFO: low severity
function_header_replacements = [
"pure ==> view",
"view ==> pure",

@ -7,6 +7,7 @@ from slither.core.compilation_unit import SlitherCompilationUnit
from slither.formatters.utils.patches import apply_patch, create_diff
from slither.tools.mutator.utils.testing_generated_mutant import test_patch
from slither.utils.colors import yellow
from slither.core.declarations import Contract
logger = logging.getLogger("Slither-Mutate")
@ -36,7 +37,7 @@ class AbstractMutator(metaclass=abc.ABCMeta): # pylint: disable=too-few-public-
timeout: int,
testing_command: str,
testing_directory: str,
contract_name: str,
contract_instance: Contract,
solc_remappings: str | None,
verbose: bool,
output_folder: str,
@ -50,10 +51,12 @@ class AbstractMutator(metaclass=abc.ABCMeta): # pylint: disable=too-few-public-
self.test_command = testing_command
self.test_directory = testing_directory
self.timeout = timeout
self.contract_exist = False
self.solc_remappings = solc_remappings
self.verbose = verbose
self.output_folder = output_folder
self.contract = contract_instance
self.in_file = self.contract.source_mapping.filename.absolute
self.in_file_str = self.contract.compilation_unit.core.source_code[self.in_file]
if not self.NAME:
raise IncorrectMutatorInitialization(
@ -74,27 +77,6 @@ class AbstractMutator(metaclass=abc.ABCMeta): # pylint: disable=too-few-public-
raise IncorrectMutatorInitialization(
f"rate must be between 0 and 100 {self.__class__.__name__}"
)
# identify the main contract, ignore the imports
for contract in self.slither.contracts:
# !limitation: what if the contract name is not same as file name
# !limitation: multi contract
if contract_name.lower() == str(contract.name).lower():
# contract
self.contract = contract
# Retrieve the file
self.in_file = self.contract.source_mapping.filename.absolute
# Retrieve the source code
self.in_file_str = self.contract.compilation_unit.core.source_code[self.in_file]
# flag contract existence
self.contract_exist = True
if not self.contract_exist:
self.contract_exist = False
logger.error(f"Contract name is not matching with the File name ({contract_name}). Please refer 'https://docs.soliditylang.org/en/latest/style-guide.html#contract-and-library-names')")
def get_exist_flag(self) -> bool:
return self.contract_exist
@abc.abstractmethod
def _mutate(self) -> Dict:

@ -9,7 +9,7 @@ from slither.tools.mutator.utils.file_handling import create_mutant_file, reset_
from slither.utils.colors import green, red
logger = logging.getLogger("Slither-Mutate")
# dont_mutate_line = {}
# function to compile the generated mutant
def compile_generated_mutant(file_path: str, mappings: str) -> bool:
try:
@ -58,6 +58,8 @@ def test_patch(file: str, patch: Dict, command: str, index: int, generator_name:
if(run_test_cmd(command, file, timeout)):
create_mutant_file(file, index, generator_name)
logger.info(green(f"String '{patch['old_string']}' replaced with '{patch['new_string']}' at line no. '{patch['line_number']}' ---> VALID\n"))
# if generator_name == 'RR':
# dont_mutate_line[patch['line_number']] = True
return True
reset_file(file)

Loading…
Cancel
Save