From 965513f239ec15ad63ec4d4b19f6d69ea15f2ff0 Mon Sep 17 00:00:00 2001 From: Nikhil Parasaram Date: Fri, 31 Dec 2021 19:23:26 +0000 Subject: [PATCH] Refactor code (#1577) * clean up code * Update lolcat --- mythril/analysis/call_helpers.py | 4 +- .../module/modules/dependence_on_origin.py | 2 +- .../modules/dependence_on_predictable_vars.py | 2 +- mythril/analysis/report.py | 2 +- mythril/analysis/solver.py | 1 - mythril/analysis/symbolic.py | 4 +- mythril/ethereum/util.py | 1 - mythril/exceptions.py | 7 ---- mythril/interfaces/cli.py | 1 - mythril/interfaces/epic.py | 42 +++++++++---------- mythril/laser/ethereum/call.py | 4 +- .../exponent_function_manager.py | 2 +- .../keccak_function_manager.py | 2 +- mythril/laser/ethereum/instruction_data.py | 2 +- mythril/laser/ethereum/instructions.py | 24 +++++------ mythril/laser/ethereum/state/calldata.py | 1 - mythril/laser/ethereum/state/world_state.py | 1 - mythril/laser/ethereum/svm.py | 4 +- .../transaction/transaction_models.py | 1 - mythril/laser/plugin/builder.py | 1 - mythril/laser/plugin/plugins/benchmark.py | 2 +- mythril/laser/smt/bool.py | 2 +- mythril/mythril/mythril_analyzer.py | 1 - mythril/mythril/mythril_config.py | 1 - mythril/mythril/mythril_disassembler.py | 3 -- mythril/plugin/interface.py | 2 +- mythril/plugin/loader.py | 3 +- mythril/support/opcodes.py | 2 +- mythril/support/signatures.py | 3 -- 29 files changed, 50 insertions(+), 77 deletions(-) diff --git a/mythril/analysis/call_helpers.py b/mythril/analysis/call_helpers.py index cb6f55d7..875bb365 100644 --- a/mythril/analysis/call_helpers.py +++ b/mythril/analysis/call_helpers.py @@ -19,7 +19,7 @@ def get_call_from_state(state: GlobalState) -> Union[Call, None]: stack = state.mstate.stack if op in ("CALL", "CALLCODE"): - gas, to, value, meminstart, meminsz, memoutstart, memoutsz = ( + gas, to, value, meminstart, meminsz, _, _ = ( get_variable(stack[-1]), get_variable(stack[-2]), get_variable(stack[-3]), @@ -47,7 +47,7 @@ def get_call_from_state(state: GlobalState) -> Union[Call, None]: return Call(state.node, state, None, op, to, gas, value) else: - gas, to, meminstart, meminsz, memoutstart, memoutsz = ( + gas, to, meminstart, meminsz, _, _ = ( get_variable(stack[-1]), get_variable(stack[-2]), get_variable(stack[-3]), diff --git a/mythril/analysis/module/modules/dependence_on_origin.py b/mythril/analysis/module/modules/dependence_on_origin.py index fcc1e3bd..4f43cc64 100644 --- a/mythril/analysis/module/modules/dependence_on_origin.py +++ b/mythril/analysis/module/modules/dependence_on_origin.py @@ -46,7 +46,7 @@ class TxOrigin(DetectionModule): self.issues.extend(issues) @staticmethod - def _analyze_state(state: GlobalState) -> list: + def _analyze_state(state: GlobalState) -> List[Issue]: """ :param state: diff --git a/mythril/analysis/module/modules/dependence_on_predictable_vars.py b/mythril/analysis/module/modules/dependence_on_predictable_vars.py index 3c58c806..4fabc337 100644 --- a/mythril/analysis/module/modules/dependence_on_predictable_vars.py +++ b/mythril/analysis/module/modules/dependence_on_predictable_vars.py @@ -60,7 +60,7 @@ class PredictableVariables(DetectionModule): self.issues.extend(issues) @staticmethod - def _analyze_state(state: GlobalState) -> list: + def _analyze_state(state: GlobalState) -> List[Issue]: """ :param state: diff --git a/mythril/analysis/report.py b/mythril/analysis/report.py index ac714dfd..b4fd950e 100644 --- a/mythril/analysis/report.py +++ b/mythril/analysis/report.py @@ -273,7 +273,7 @@ class Report: # Setup issues _issues = [] - for key, issue in self.issues.items(): + for _, issue in self.issues.items(): idx = self.source.get_source_index(issue.bytecode_hash) try: diff --git a/mythril/analysis/solver.py b/mythril/analysis/solver.py index 83f2deee..89b3db6e 100644 --- a/mythril/analysis/solver.py +++ b/mythril/analysis/solver.py @@ -7,7 +7,6 @@ from z3 import FuncInterp from mythril.exceptions import UnsatError from mythril.laser.ethereum.function_managers import ( - exponent_function_manager, keccak_function_manager, ) diff --git a/mythril/analysis/symbolic.py b/mythril/analysis/symbolic.py index b56a8501..3e5329e0 100644 --- a/mythril/analysis/symbolic.py +++ b/mythril/analysis/symbolic.py @@ -238,7 +238,7 @@ class SymExecWrapper: stack = state.mstate.stack if op in ("CALL", "CALLCODE"): - gas, to, value, meminstart, meminsz, memoutstart, memoutsz = ( + gas, to, value, meminstart, meminsz, _, _ = ( get_variable(stack[-1]), get_variable(stack[-2]), get_variable(stack[-3]), @@ -286,7 +286,7 @@ class SymExecWrapper: ) ) else: - gas, to, meminstart, meminsz, memoutstart, memoutsz = ( + gas, to, meminstart, meminsz, _, _ = ( get_variable(stack[-1]), get_variable(stack[-2]), get_variable(stack[-3]), diff --git a/mythril/ethereum/util.py b/mythril/ethereum/util.py index 13195ca0..1d0754af 100644 --- a/mythril/ethereum/util.py +++ b/mythril/ethereum/util.py @@ -17,7 +17,6 @@ from semantic_version import Version if sys.version_info[1] >= 6: import solcx - from solcx.exceptions import SolcNotInstalled log = logging.getLogger(__name__) diff --git a/mythril/exceptions.py b/mythril/exceptions.py index 2f6ad04f..3b3c968c 100644 --- a/mythril/exceptions.py +++ b/mythril/exceptions.py @@ -34,13 +34,6 @@ class CriticalError(MythrilBaseException): pass -class AddressNotFoundError(MythrilBaseException): - """A Mythril exception denoting the given smart contract address was not - found.""" - - pass - - class DetectorNotFoundError(MythrilBaseException): """A Mythril exception denoting attempted usage of a non-existant detection module.""" diff --git a/mythril/interfaces/cli.py b/mythril/interfaces/cli.py index c16ff23b..2edb1603 100644 --- a/mythril/interfaces/cli.py +++ b/mythril/interfaces/cli.py @@ -17,7 +17,6 @@ import traceback import mythril.support.signatures as sigs from argparse import ArgumentParser, Namespace, RawTextHelpFormatter from mythril.exceptions import ( - AddressNotFoundError, DetectorNotFoundError, CriticalError, ) diff --git a/mythril/interfaces/epic.py b/mythril/interfaces/epic.py index c18f984d..a8cc7b53 100755 --- a/mythril/interfaces/epic.py +++ b/mythril/interfaces/epic.py @@ -16,6 +16,7 @@ import random import re import sys import time +import argparse PY3 = sys.version_info >= (3,) @@ -165,7 +166,7 @@ class LolCat(object): if not s: return - for i in range(1, options.duration): + for _ in range(1, options.duration): self.output.write("\x1b[%dD" % (len(s),)) self.output.flush() options.os += options.spread @@ -208,30 +209,29 @@ def detect_mode(term_hint="xterm-256color"): def run(): """Main entry point.""" - import optparse - parser = optparse.OptionParser(usage=r"%prog [] [file ...]") - parser.add_option( - "-p", "--spread", type="float", default=3.0, help="Rainbow spread" + parser = argparse.ArgumentParser(usage=r"%prog [] [file ...]") + parser.add_argument( + "-p", "--spread", type=float, default=3.0, help="Rainbow spread" ) - parser.add_option( - "-F", "--freq", type="float", default=0.1, help="Rainbow frequency" + parser.add_argument( + "-F", "--freq", type=float, default=0.1, help="Rainbow frequency" ) - parser.add_option("-S", "--seed", type="int", default=0, help="Rainbow seed") - parser.add_option( + parser.add_argument("-S", "--seed", type=int, default=0, help="Rainbow seed") + parser.add_argument( "-a", "--animate", action="store_true", default=False, help="Enable psychedelics", ) - parser.add_option( - "-d", "--duration", type="int", default=12, help="Animation duration" + parser.add_argument( + "-d", "--duration", type=int, default=12, help="Animation duration" ) - parser.add_option( - "-s", "--speed", type="float", default=20.0, help="Animation speed" + parser.add_argument( + "-s", "--speed", type=float, default=20.0, help="Animation speed" ) - parser.add_option( + parser.add_argument( "-f", "--force", action="store_true", @@ -239,17 +239,17 @@ def run(): help="Force colour even when stdout is not a tty", ) - parser.add_option( + parser.add_argument( "-3", action="store_const", dest="mode", const=8, help="Force 3 bit colour mode" ) - parser.add_option( + parser.add_argument( "-4", action="store_const", dest="mode", const=16, help="Force 4 bit colour mode", ) - parser.add_option( + parser.add_argument( "-8", action="store_const", dest="mode", @@ -257,21 +257,19 @@ def run(): help="Force 8 bit colour mode", ) - parser.add_option( + parser.add_argument( "-c", "--charset-py2", default="utf-8", help="Manually set a charset to convert from, for python 2.7", ) - options, args = parser.parse_args() + options = parser.parse_args() options.os = random.randint(0, 256) if options.seed == 0 else options.seed options.mode = options.mode or detect_mode() lolcat = LolCat(mode=options.mode) - - if not args: - args = ["-"] + args = ["-"] for filename in args: if filename == "-": diff --git a/mythril/laser/ethereum/call.py b/mythril/laser/ethereum/call.py index 0db4e986..a3c359cb 100644 --- a/mythril/laser/ethereum/call.py +++ b/mythril/laser/ethereum/call.py @@ -4,7 +4,7 @@ parameters for the new global state.""" import logging import re -from typing import Union, List, cast, Callable, Optional +from typing import Union, List, cast, Optional from eth.constants import GAS_CALLSTIPEND import mythril.laser.ethereum.util as util @@ -18,7 +18,7 @@ from mythril.laser.ethereum.state.calldata import ( ConcreteCalldata, ) from mythril.laser.ethereum.state.global_state import GlobalState -from mythril.laser.smt import BitVec, is_true, If +from mythril.laser.smt import BitVec, If from mythril.laser.smt import simplify, Expression, symbol_factory from mythril.support.loader import DynLoader diff --git a/mythril/laser/ethereum/function_managers/exponent_function_manager.py b/mythril/laser/ethereum/function_managers/exponent_function_manager.py index 40742e28..fa7062da 100644 --- a/mythril/laser/ethereum/function_managers/exponent_function_manager.py +++ b/mythril/laser/ethereum/function_managers/exponent_function_manager.py @@ -1,5 +1,5 @@ import logging -from typing import Dict, List, Optional, Tuple +from typing import Tuple from mythril.laser.smt import And, BitVec, Bool, Function, URem, symbol_factory diff --git a/mythril/laser/ethereum/function_managers/keccak_function_manager.py b/mythril/laser/ethereum/function_managers/keccak_function_manager.py index 7e640a5a..8122b4c9 100644 --- a/mythril/laser/ethereum/function_managers/keccak_function_manager.py +++ b/mythril/laser/ethereum/function_managers/keccak_function_manager.py @@ -99,7 +99,7 @@ class KeccakFunctionManager: :return: Tuple of keccak and the condition it should satisfy """ length = data.size() - func, inverse = self.get_function(length) + func, _ = self.get_function(length) if data.symbolic is False: concrete_hash = self.find_concrete_keccak(data) diff --git a/mythril/laser/ethereum/instruction_data.py b/mythril/laser/ethereum/instruction_data.py index b0378586..074f4d85 100644 --- a/mythril/laser/ethereum/instruction_data.py +++ b/mythril/laser/ethereum/instruction_data.py @@ -1,5 +1,5 @@ from eth._utils.numeric import ceil32 -from typing import Callable, Dict, Tuple, Union +from typing import Tuple from mythril.support.opcodes import OPCODES, STACK, GAS from eth.constants import ( GAS_ECRECOVER, diff --git a/mythril/laser/ethereum/instructions.py b/mythril/laser/ethereum/instructions.py index ea031e73..86b235f4 100644 --- a/mythril/laser/ethereum/instructions.py +++ b/mythril/laser/ethereum/instructions.py @@ -8,7 +8,6 @@ from typing import cast, Callable, List, Union from mythril.laser.smt import ( Extract, Expression, - Function, UDiv, simplify, Concat, @@ -16,7 +15,6 @@ from mythril.laser.smt import ( UGT, BitVec, is_false, - is_true, URem, SRem, If, @@ -1705,9 +1703,9 @@ class Instruction: """ # TODO: implement me state = global_state.mstate - dpth = int(self.op_code[3:]) + depth = int(self.op_code[3:]) state.stack.pop(), state.stack.pop() - log_data = [state.stack.pop() for _ in range(dpth)] + _ = [state.stack.pop() for _ in range(depth)] # Not supported return [global_state] @@ -2117,10 +2115,10 @@ class Instruction: memory_out_size, memory_out_offset = global_state.mstate.stack[-7:-5] try: ( - callee_address, _, _, - value, + _, + _, _, memory_out_offset, memory_out_size, @@ -2261,10 +2259,10 @@ class Instruction: try: ( - callee_address, _, _, - value, + _, + _, _, memory_out_offset, memory_out_size, @@ -2405,11 +2403,11 @@ class Instruction: try: with_value = function_name != "staticcall" ( - callee_address, - callee_account, - call_data, - value, - gas, + _, + _, + _, + _, + _, memory_out_offset, memory_out_size, ) = get_call_parameters(global_state, self.dynamic_loader, with_value) diff --git a/mythril/laser/ethereum/state/calldata.py b/mythril/laser/ethereum/state/calldata.py index f7fa0036..016ee7d5 100644 --- a/mythril/laser/ethereum/state/calldata.py +++ b/mythril/laser/ethereum/state/calldata.py @@ -2,7 +2,6 @@ from typing import cast, Union, Tuple, List -from enum import Enum from typing import Any, Union from z3 import Model diff --git a/mythril/laser/ethereum/state/world_state.py b/mythril/laser/ethereum/state/world_state.py index 55bd84b9..017633cd 100644 --- a/mythril/laser/ethereum/state/world_state.py +++ b/mythril/laser/ethereum/state/world_state.py @@ -6,7 +6,6 @@ from eth._utils.address import generate_contract_address from mythril.support.loader import DynLoader from mythril.laser.smt import symbol_factory, Array, BitVec -from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.annotation import StateAnnotation from mythril.laser.ethereum.state.constraints import Constraints diff --git a/mythril/laser/ethereum/svm.py b/mythril/laser/ethereum/svm.py index e5876423..00213041 100644 --- a/mythril/laser/ethereum/svm.py +++ b/mythril/laser/ethereum/svm.py @@ -11,7 +11,7 @@ from mythril.laser.execution_info import ExecutionInfo from mythril.laser.ethereum.cfg import NodeFlags, Node, Edge, JumpType from mythril.laser.ethereum.evm_exceptions import StackUnderflowException from mythril.laser.ethereum.evm_exceptions import VmException -from mythril.laser.ethereum.instructions import Instruction, transfer_ether +from mythril.laser.ethereum.instructions import Instruction from mythril.laser.ethereum.instruction_data import get_required_stack_elements from mythril.laser.plugin.signals import PluginSkipWorldState, PluginSkipState from mythril.laser.ethereum.state.global_state import GlobalState @@ -287,7 +287,7 @@ class LaserEVM: def handle_vm_exception( self, global_state: GlobalState, op_code: str, error_msg: str ) -> List[GlobalState]: - transaction, return_global_state = global_state.transaction_stack.pop() + _, return_global_state = global_state.transaction_stack.pop() if return_global_state is None: # In this case we don't put an unmodified world state in the open_states list Since in the case of an diff --git a/mythril/laser/ethereum/transaction/transaction_models.py b/mythril/laser/ethereum/transaction/transaction_models.py index 21f2f7d4..ae8183dd 100644 --- a/mythril/laser/ethereum/transaction/transaction_models.py +++ b/mythril/laser/ethereum/transaction/transaction_models.py @@ -1,7 +1,6 @@ """This module contians the transaction models used throughout LASER's symbolic execution.""" -import array from copy import deepcopy from z3 import ExprRef from typing import Union, Optional diff --git a/mythril/laser/plugin/builder.py b/mythril/laser/plugin/builder.py index 51f8d546..5f37c1d2 100644 --- a/mythril/laser/plugin/builder.py +++ b/mythril/laser/plugin/builder.py @@ -1,7 +1,6 @@ from mythril.laser.plugin.interface import LaserPlugin from abc import ABC, abstractmethod -from typing import Optional class PluginBuilder(ABC): diff --git a/mythril/laser/plugin/plugins/benchmark.py b/mythril/laser/plugin/plugins/benchmark.py index 2950a343..c268ad6b 100644 --- a/mythril/laser/plugin/plugins/benchmark.py +++ b/mythril/laser/plugin/plugins/benchmark.py @@ -83,7 +83,7 @@ class BenchmarkPlugin(LaserPlugin): def _write_to_graph(self): """Write the coverage results to a graph""" traces = [] - for byte_code, trace_data in self.coverage.items(): + for _, trace_data in self.coverage.items(): traces += [list(trace_data.keys()), list(trace_data.values()), "r--"] plt.plot(*traces) diff --git a/mythril/laser/smt/bool.py b/mythril/laser/smt/bool.py index c09358d0..bc5f369a 100644 --- a/mythril/laser/smt/bool.py +++ b/mythril/laser/smt/bool.py @@ -1,7 +1,7 @@ """This module provides classes for an SMT abstraction of boolean expressions.""" -from typing import Union, cast, List, Set +from typing import Union, cast, Set import z3 diff --git a/mythril/mythril/mythril_analyzer.py b/mythril/mythril/mythril_analyzer.py index 799aab3c..e39b8445 100644 --- a/mythril/mythril/mythril_analyzer.py +++ b/mythril/mythril/mythril_analyzer.py @@ -5,7 +5,6 @@ import logging import traceback from typing import Optional, List -from mythril.laser.ethereum.iprof import InstructionProfiler from . import MythrilDisassembler from mythril.support.source_support import Source from mythril.support.loader import DynLoader diff --git a/mythril/mythril/mythril_config.py b/mythril/mythril/mythril_config.py index 8e7a4e63..430a2256 100644 --- a/mythril/mythril/mythril_config.py +++ b/mythril/mythril/mythril_config.py @@ -1,7 +1,6 @@ import codecs import logging import os -import platform import re from pathlib import Path diff --git a/mythril/mythril/mythril_disassembler.py b/mythril/mythril/mythril_disassembler.py index 4adcc779..2fbd3a67 100644 --- a/mythril/mythril/mythril_disassembler.py +++ b/mythril/mythril/mythril_disassembler.py @@ -3,7 +3,6 @@ import re import solc import sys import os -import platform from mythril.support.support_utils import sha3, zpad from typing import List, Tuple, Optional @@ -17,8 +16,6 @@ from mythril.ethereum.interface.rpc.exceptions import ConnectionError from mythril.solidity.soliditycontract import SolidityContract, get_contracts_from_file from eth_utils import int_to_big_endian -if sys.version_info[1] >= 6: - import solcx log = logging.getLogger(__name__) diff --git a/mythril/plugin/interface.py b/mythril/plugin/interface.py index 0184bff3..fca6387a 100644 --- a/mythril/plugin/interface.py +++ b/mythril/plugin/interface.py @@ -1,4 +1,4 @@ -from abc import ABC, abstractmethod +from abc import ABC from mythril.laser.plugin.builder import PluginBuilder as LaserPluginBuilder diff --git a/mythril/plugin/loader.py b/mythril/plugin/loader.py index 69a6e568..3b611e60 100644 --- a/mythril/plugin/loader.py +++ b/mythril/plugin/loader.py @@ -1,11 +1,10 @@ from mythril.analysis.module import DetectionModule -from mythril.plugin.interface import MythrilCLIPlugin, MythrilPlugin, MythrilLaserPlugin +from mythril.plugin.interface import MythrilPlugin, MythrilLaserPlugin from mythril.plugin.discovery import PluginDiscovery from mythril.support.support_utils import Singleton from mythril.analysis.module.loader import ModuleLoader -from mythril.laser.plugin.builder import PluginBuilder as LaserPluginBuilder from mythril.laser.plugin.loader import LaserPluginLoader from typing import Dict import logging diff --git a/mythril/support/opcodes.py b/mythril/support/opcodes.py index b863a72b..99b45283 100644 --- a/mythril/support/opcodes.py +++ b/mythril/support/opcodes.py @@ -1,4 +1,4 @@ -from typing import Dict, Tuple, Union +from typing import Dict Z_OPERATOR_TUPLE = (0, 1) diff --git a/mythril/support/signatures.py b/mythril/support/signatures.py index 3db0b3d9..1b9475e1 100644 --- a/mythril/support/signatures.py +++ b/mythril/support/signatures.py @@ -1,17 +1,14 @@ """The Mythril function signature database.""" import functools -import json import logging import multiprocessing import os import sqlite3 import time from collections import defaultdict -from subprocess import PIPE, Popen from typing import List, Set, DefaultDict, Dict from mythril.ethereum.util import get_solc_json -from mythril.exceptions import CompilerError log = logging.getLogger(__name__)