From 25de4e7cc3a81ea08be5e7333d0a46bec537168a Mon Sep 17 00:00:00 2001 From: Daniel Bast <2790401+dbast@users.noreply.github.com> Date: Wed, 7 Aug 2024 15:12:46 +0200 Subject: [PATCH] Run black + ruff via pre-commit, fix findings to reduce requirements.txt (#1865) * Run black via pre-commit + Github Action workflow, update black * Apply black via pre-commit * Enable ruff + rulset "F" via pre-commit * Apply ruff via pre-commit * Manual lint fixes * Apply review comments --------- Co-authored-by: Nikhil Parasaram --- .circleci/config.yml | 6 -- .github/workflows/pre-commit.yml | 29 ++++++++++ .pre-commit-config.yaml | 30 +++++++++- mypy-stubs/z3/__init__.pyi | 2 + myth | 1 - mythril/__init__.py | 2 +- mythril/analysis/call_helpers.py | 1 + mythril/analysis/issue_annotation.py | 2 +- mythril/analysis/module/base.py | 1 + mythril/analysis/module/loader.py | 1 - .../analysis/module/modules/arbitrary_jump.py | 3 +- .../module/modules/arbitrary_write.py | 1 + .../analysis/module/modules/delegatecall.py | 3 +- .../module/modules/dependence_on_origin.py | 1 + .../modules/dependence_on_predictable_vars.py | 1 + .../analysis/module/modules/ether_thief.py | 1 + mythril/analysis/module/modules/exceptions.py | 1 + mythril/analysis/module/modules/integer.py | 2 +- .../analysis/module/modules/multiple_sends.py | 1 + .../module/modules/requirements_violation.py | 1 + mythril/analysis/module/modules/suicide.py | 1 - .../modules/transaction_order_dependence.py | 2 +- .../module/modules/unchecked_retval.py | 1 + .../module/modules/unexpected_ether.py | 1 + mythril/analysis/ops.py | 1 + mythril/analysis/report.py | 4 +- mythril/analysis/symbolic.py | 7 +-- mythril/analysis/traceexplore.py | 1 + mythril/concolic/concolic_execution.py | 9 +-- mythril/concolic/find_trace.py | 6 +- mythril/disassembler/disassembly.py | 1 + mythril/ethereum/evmcontract.py | 1 + mythril/ethereum/interface/rpc/client.py | 1 + mythril/ethereum/interface/rpc/constants.py | 1 + mythril/ethereum/interface/rpc/utils.py | 1 + mythril/ethereum/util.py | 9 +-- mythril/interfaces/cli.py | 24 ++++---- mythril/interfaces/epic.py | 2 + mythril/laser/ethereum/call.py | 1 - mythril/laser/ethereum/cfg.py | 1 + mythril/laser/ethereum/cheat_code.py | 17 +----- .../exponent_function_manager.py | 9 ++- .../keccak_function_manager.py | 2 +- mythril/laser/ethereum/instructions.py | 55 ++++++++++--------- mythril/laser/ethereum/state/account.py | 9 +-- mythril/laser/ethereum/state/calldata.py | 5 +- mythril/laser/ethereum/state/constraints.py | 11 ++-- mythril/laser/ethereum/state/environment.py | 1 + mythril/laser/ethereum/state/global_state.py | 1 + mythril/laser/ethereum/state/machine_state.py | 3 +- mythril/laser/ethereum/state/memory.py | 9 ++- mythril/laser/ethereum/state/return_data.py | 1 + mythril/laser/ethereum/state/world_state.py | 2 +- mythril/laser/ethereum/strategy/basic.py | 2 +- mythril/laser/ethereum/strategy/beam.py | 2 - mythril/laser/ethereum/strategy/concolic.py | 3 - .../ethereum/strategy/constraint_strategy.py | 9 --- mythril/laser/ethereum/svm.py | 3 +- .../laser/ethereum/transaction/concolic.py | 3 +- .../laser/ethereum/transaction/symbolic.py | 8 +-- .../ethereum/tx_prioritiser/rf_prioritiser.py | 2 - mythril/laser/ethereum/util.py | 10 +++- mythril/laser/plugin/plugins/__init__.py | 1 + .../plugins/coverage_metrics/coverage_data.py | 1 - .../coverage_metrics/metrics_plugin.py | 1 - .../plugin/plugins/instruction_profiler.py | 1 - .../plugins/state_merge/merge_states.py | 2 +- .../plugins/state_merge/state_merge_plugin.py | 1 - .../plugin/plugins/summary/annotations.py | 4 +- mythril/laser/plugin/plugins/summary/core.py | 17 ++---- .../laser/plugin/plugins/summary/summary.py | 2 +- mythril/laser/smt/__init__.py | 3 +- mythril/laser/smt/bitvec_helper.py | 14 ++--- mythril/laser/smt/expression.py | 1 + mythril/laser/smt/solver/solver.py | 1 + mythril/mythril/mythril_config.py | 1 - mythril/mythril/mythril_disassembler.py | 8 +-- mythril/solidity/soliditycontract.py | 1 + mythril/support/loader.py | 1 + mythril/support/lock.py | 2 +- mythril/support/model.py | 7 +-- mythril/support/signatures.py | 6 +- mythril/support/support_utils.py | 2 +- pyproject.toml | 15 +++++ requirements.txt | 1 - setup.py | 2 +- tests/__init__.py | 4 +- tests/analysis/abi_decode_test.py | 9 --- tests/analysis/arbitrary_jump_test.py | 5 +- tests/cmd_line_test.py | 3 +- tests/concolic/concolic_tests.py | 11 +--- tests/disassembler/asm_test.py | 7 ++- tests/disassembler/disassembly_test.py | 2 +- tests/disassembler_test.py | 8 ++- tests/features_test.py | 2 +- tests/graph_test.py | 1 + tests/instructions/codecopy_test.py | 1 - tests/instructions/extcodehash_test.py | 1 - tests/instructions/push_test.py | 2 - tests/instructions/sar_test.py | 2 - tests/instructions/shl_test.py | 1 - tests/instructions/shr_test.py | 1 - tests/integration_tests/analysis_tests.py | 1 - tests/integration_tests/old_version_test.py | 1 - .../integration_tests/safe_functions_test.py | 2 - tests/integration_tests/solc_settings_test.py | 5 -- tests/integration_tests/src_mapping_test.py | 4 -- tests/integration_tests/summary_test.py | 2 - .../transient_storage_test.py | 4 -- tests/integration_tests/version_test.py | 4 +- tests/laser/Precompiles/blake2_test.py | 1 + tests/laser/Precompiles/ecrecover_test.py | 2 - tests/laser/Precompiles/identity_test.py | 5 +- tests/laser/Precompiles/ripemd_test.py | 2 - tests/laser/Precompiles/sha256_test.py | 2 - tests/laser/evm_testsuite/evm_test.py | 5 +- tests/laser/keccak_tests.py | 2 +- tests/laser/state/calldata_test.py | 4 +- tests/laser/state/mstack_test.py | 6 +- .../world_state_account_exist_load_test.py | 12 ---- tests/laser/transaction_test.py | 2 - tests/mythril/mythril_config_test.py | 1 - tests/mythril/mythril_disassembler_test.py | 4 +- tests/rpc_test.py | 1 - 124 files changed, 268 insertions(+), 291 deletions(-) create mode 100644 .github/workflows/pre-commit.yml create mode 100644 pyproject.toml diff --git a/.circleci/config.yml b/.circleci/config.yml index 565a6a137..e3c11633e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -37,12 +37,6 @@ jobs: paths: - .tox/py* - /root/.cache/pip/wheels/ - # TODO: Remove all --break-system-packages here in and mythril-ci image - - run: - name: Black style check - command: | - pip3 install --user black==22.3.0 --break-system-packages - python3 -m black --check /home/mythril/ - run: background: true diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 000000000..6c9c33eba --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,29 @@ +on: + push: + branches: + - master + - develop + tags: + - '*' + pull_request: + +name: pre-commit + +concurrency: + # Concurrency group that uses the workflow name and PR number if available + # or commit SHA as a fallback. If a new build is triggered under that + # concurrency group while a previous build is running it will be canceled. + # Repeated pushes to a PR will cancel all previous builds, while multiple + # merges to a branch will not cancel. + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} + cancel-in-progress: true + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.9' + - uses: pre-commit/action@v3.0.1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0807b4df4..3df7e7e0b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,35 @@ # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks +# Apply to all files without commiting: +# pre-commit run --all-files +# Update this file: +# pre-commit autoupdate repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: check-toml + - id: check-yaml - repo: https://github.com/psf/black - rev: 21.12b0 + rev: 24.8.0 hooks: - id: black +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.5.6 + hooks: + - id: ruff + args: [--fix, --show-fixes] +- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks + rev: v2.14.0 + hooks: + - id: pretty-format-toml + args: [--autofix] +- repo: https://github.com/python-jsonschema/check-jsonschema + rev: 0.29.1 + hooks: + - id: check-circle-ci + - id: check-github-workflows +- repo: meta + hooks: + - id: check-hooks-apply + - id: check-useless-excludes diff --git a/mypy-stubs/z3/__init__.pyi b/mypy-stubs/z3/__init__.pyi index 08e6456d7..9888be7b7 100644 --- a/mypy-stubs/z3/__init__.pyi +++ b/mypy-stubs/z3/__init__.pyi @@ -20,10 +20,12 @@ class AstRef(Z3PPObject): def __init__(self, ast: Ast, ctx: Context) -> None: self.ast: Ast = ... self.ctx: Context = ... + @overload def __init__(self, ast: Ast) -> None: self.ast: Ast = ... self.ctx: Context = ... + def ctx_ref(self) -> ContextObj: ... def as_ast(self) -> Ast: ... def children(self) -> List[AstRef]: ... diff --git a/myth b/myth index 1eeb58da3..c2f1eca16 100755 --- a/myth +++ b/myth @@ -10,4 +10,3 @@ import mythril.interfaces.cli if __name__ == "__main__": mythril.interfaces.cli.main() exit() - diff --git a/mythril/__init__.py b/mythril/__init__.py index 9b018ae65..881cc7201 100644 --- a/mythril/__init__.py +++ b/mythril/__init__.py @@ -4,7 +4,7 @@ import logging # Accept mythril.VERSION to get mythril's current version number -from .__version__ import __version__ as VERSION # NOQA +from .__version__ import __version__ as VERSION from mythril.plugin.loader import MythrilPluginLoader log = logging.getLogger(__name__) diff --git a/mythril/analysis/call_helpers.py b/mythril/analysis/call_helpers.py index 875bb365e..5cd75232a 100644 --- a/mythril/analysis/call_helpers.py +++ b/mythril/analysis/call_helpers.py @@ -1,5 +1,6 @@ """This module provides helper functions for the analysis modules to deal with call functionality.""" + from typing import Union from mythril.analysis.ops import VarType, Call, get_variable diff --git a/mythril/analysis/issue_annotation.py b/mythril/analysis/issue_annotation.py index 6e741de62..17132dd97 100644 --- a/mythril/analysis/issue_annotation.py +++ b/mythril/analysis/issue_annotation.py @@ -2,7 +2,7 @@ from mythril.analysis.report import Issue from mythril.laser.ethereum.state.annotation import StateAnnotation -from mythril.laser.smt import Bool +from mythril.laser.smt import SMTBool as Bool from copy import deepcopy diff --git a/mythril/analysis/module/base.py b/mythril/analysis/module/base.py index a39de8214..0d77592e9 100644 --- a/mythril/analysis/module/base.py +++ b/mythril/analysis/module/base.py @@ -3,6 +3,7 @@ This module includes an definition of the DetectionModule interface. DetectionModules implement different analysis rules to find weaknesses and vulnerabilities. """ + import logging from typing import List, Set, Optional, Tuple diff --git a/mythril/analysis/module/loader.py b/mythril/analysis/module/loader.py index d75469908..a3816a5d4 100644 --- a/mythril/analysis/module/loader.py +++ b/mythril/analysis/module/loader.py @@ -26,7 +26,6 @@ from mythril.analysis.module.modules.user_assertions import UserAssertions from mythril.analysis.module.modules.unexpected_ether import UnexpectedEther -from mythril.analysis.module.base import EntryPoint from mythril.exceptions import DetectorNotFoundError diff --git a/mythril/analysis/module/modules/arbitrary_jump.py b/mythril/analysis/module/modules/arbitrary_jump.py index 3f0fa7a89..0927b574a 100644 --- a/mythril/analysis/module/modules/arbitrary_jump.py +++ b/mythril/analysis/module/modules/arbitrary_jump.py @@ -1,7 +1,6 @@ """This module contains the detection code for Arbitrary jumps.""" -import logging -from mythril.exceptions import UnsatError +import logging from mythril.analysis.solver import get_transaction_sequence, UnsatError from mythril.analysis.issue_annotation import IssueAnnotation diff --git a/mythril/analysis/module/modules/arbitrary_write.py b/mythril/analysis/module/modules/arbitrary_write.py index edceaf70c..02a95f3d7 100644 --- a/mythril/analysis/module/modules/arbitrary_write.py +++ b/mythril/analysis/module/modules/arbitrary_write.py @@ -1,4 +1,5 @@ """This module contains the detection code for arbitrary storage write.""" + import logging from mythril.analysis.module.base import DetectionModule, EntryPoint from mythril.analysis.potential_issues import ( diff --git a/mythril/analysis/module/modules/delegatecall.py b/mythril/analysis/module/modules/delegatecall.py index b0699a9c9..ab49a68e5 100644 --- a/mythril/analysis/module/modules/delegatecall.py +++ b/mythril/analysis/module/modules/delegatecall.py @@ -1,4 +1,5 @@ """This module contains the detection code for insecure delegate call usage.""" + import logging from typing import List @@ -14,7 +15,7 @@ from mythril.analysis.module.base import DetectionModule, EntryPoint from mythril.exceptions import UnsatError from mythril.laser.ethereum.state.global_state import GlobalState -from mythril.laser.smt import symbol_factory, UGT, Bool +from mythril.laser.smt import symbol_factory, UGT log = logging.getLogger(__name__) diff --git a/mythril/analysis/module/modules/dependence_on_origin.py b/mythril/analysis/module/modules/dependence_on_origin.py index 7e9b575f9..eadf0a3ba 100644 --- a/mythril/analysis/module/modules/dependence_on_origin.py +++ b/mythril/analysis/module/modules/dependence_on_origin.py @@ -1,5 +1,6 @@ """This module contains the detection code for predictable variable dependence.""" + import logging from copy import copy from mythril.analysis.issue_annotation import IssueAnnotation diff --git a/mythril/analysis/module/modules/dependence_on_predictable_vars.py b/mythril/analysis/module/modules/dependence_on_predictable_vars.py index c57397165..1ac8a1208 100644 --- a/mythril/analysis/module/modules/dependence_on_predictable_vars.py +++ b/mythril/analysis/module/modules/dependence_on_predictable_vars.py @@ -1,5 +1,6 @@ """This module contains the detection code for predictable variable dependence.""" + import logging from mythril.analysis.issue_annotation import IssueAnnotation diff --git a/mythril/analysis/module/modules/ether_thief.py b/mythril/analysis/module/modules/ether_thief.py index 0cc6436de..9850111c6 100644 --- a/mythril/analysis/module/modules/ether_thief.py +++ b/mythril/analysis/module/modules/ether_thief.py @@ -1,5 +1,6 @@ """This module contains the detection code for unauthorized ether withdrawal.""" + import logging from copy import copy diff --git a/mythril/analysis/module/modules/exceptions.py b/mythril/analysis/module/modules/exceptions.py index 5e2d1c8ef..d5623d3f3 100644 --- a/mythril/analysis/module/modules/exceptions.py +++ b/mythril/analysis/module/modules/exceptions.py @@ -1,4 +1,5 @@ """This module contains the detection code for reachable exceptions.""" + import logging from typing import cast, List, Optional diff --git a/mythril/analysis/module/modules/integer.py b/mythril/analysis/module/modules/integer.py index 5afacda4e..912d847f9 100644 --- a/mythril/analysis/module/modules/integer.py +++ b/mythril/analysis/module/modules/integer.py @@ -22,7 +22,7 @@ symbol_factory, Not, Expression, - Bool, + SMTBool as Bool, And, ) diff --git a/mythril/analysis/module/modules/multiple_sends.py b/mythril/analysis/module/modules/multiple_sends.py index c46bd0775..3e73b1c2e 100644 --- a/mythril/analysis/module/modules/multiple_sends.py +++ b/mythril/analysis/module/modules/multiple_sends.py @@ -1,5 +1,6 @@ """This module contains the detection code to find multiple sends occurring in a single transaction.""" + from copy import copy from typing import cast, List from mythril.analysis.issue_annotation import IssueAnnotation diff --git a/mythril/analysis/module/modules/requirements_violation.py b/mythril/analysis/module/modules/requirements_violation.py index ac7be628d..a98004b46 100644 --- a/mythril/analysis/module/modules/requirements_violation.py +++ b/mythril/analysis/module/modules/requirements_violation.py @@ -1,4 +1,5 @@ """This module contains the detection code for requirement violations in a call""" + import logging from mythril.analysis import solver diff --git a/mythril/analysis/module/modules/suicide.py b/mythril/analysis/module/modules/suicide.py index 9f61164ff..7dae7f53c 100644 --- a/mythril/analysis/module/modules/suicide.py +++ b/mythril/analysis/module/modules/suicide.py @@ -11,7 +11,6 @@ ContractCreationTransaction, ) import logging -from mythril.laser.ethereum.function_managers import keccak_function_manager log = logging.getLogger(__name__) diff --git a/mythril/analysis/module/modules/transaction_order_dependence.py b/mythril/analysis/module/modules/transaction_order_dependence.py index 9c37c0aa2..0ff215f7f 100644 --- a/mythril/analysis/module/modules/transaction_order_dependence.py +++ b/mythril/analysis/module/modules/transaction_order_dependence.py @@ -8,7 +8,7 @@ from mythril.analysis.swc_data import TX_ORDER_DEPENDENCE from mythril.laser.ethereum.transaction.symbolic import ACTORS from mythril.analysis.module.base import DetectionModule -from mythril.laser.smt import Or, Bool +from mythril.laser.smt import Or, SMTBool as Bool from mythril.laser.ethereum.state.global_state import GlobalState from mythril.exceptions import UnsatError import logging diff --git a/mythril/analysis/module/modules/unchecked_retval.py b/mythril/analysis/module/modules/unchecked_retval.py index b6a1e0ecf..22bb3eca3 100644 --- a/mythril/analysis/module/modules/unchecked_retval.py +++ b/mythril/analysis/module/modules/unchecked_retval.py @@ -1,5 +1,6 @@ """This module contains detection code to find occurrences of calls whose return value remains unchecked.""" + from copy import copy from typing import cast, List diff --git a/mythril/analysis/module/modules/unexpected_ether.py b/mythril/analysis/module/modules/unexpected_ether.py index 6f5acbd47..d2ab2154e 100644 --- a/mythril/analysis/module/modules/unexpected_ether.py +++ b/mythril/analysis/module/modules/unexpected_ether.py @@ -1,4 +1,5 @@ """This module contains the detection code for unexpected ether balance.""" + from mythril.analysis.report import Issue from mythril.analysis.issue_annotation import IssueAnnotation from mythril.analysis.swc_data import UNEXPECTED_ETHER_BALANCE diff --git a/mythril/analysis/ops.py b/mythril/analysis/ops.py index c4f6ae17d..f8773370f 100644 --- a/mythril/analysis/ops.py +++ b/mythril/analysis/ops.py @@ -1,5 +1,6 @@ """This module contains various helper methods for dealing with EVM operations.""" + from enum import Enum from mythril.laser.ethereum import util diff --git a/mythril/analysis/report.py b/mythril/analysis/report.py index e3ed0d3f1..c9e74ec5a 100644 --- a/mythril/analysis/report.py +++ b/mythril/analysis/report.py @@ -1,5 +1,5 @@ """This module provides classes that make up an issue report.""" -import base64 + import logging import re import json @@ -242,7 +242,7 @@ def resolve_input(data, function_name): for item in decoded_output ) return decoded_output - except Exception as e: + except Exception: return None diff --git a/mythril/analysis/symbolic.py b/mythril/analysis/symbolic.py index 318472920..232ecf239 100644 --- a/mythril/analysis/symbolic.py +++ b/mythril/analysis/symbolic.py @@ -33,7 +33,6 @@ from mythril.laser.ethereum.strategy.extensions.bounded_loops import ( BoundedLoopsStrategy, ) -from mythril.laser.plugin.plugins.state_merge.state_merge_plugin import StateMergePlugin from mythril.laser.smt import symbol_factory, BitVec from mythril.support.support_args import args from typing import Union, List, Type, Optional @@ -206,9 +205,9 @@ def __init__( dynamic_loader=dynloader, contract_name=contract.name, balances=world_state.balances, - concrete_storage=True - if (dynloader is not None and dynloader.active) - else False, + concrete_storage=( + True if (dynloader is not None and dynloader.active) else False + ), ) # concrete_storage can get overridden by global args if dynloader is not None: diff --git a/mythril/analysis/traceexplore.py b/mythril/analysis/traceexplore.py index fbc5befc2..55e83146f 100644 --- a/mythril/analysis/traceexplore.py +++ b/mythril/analysis/traceexplore.py @@ -1,5 +1,6 @@ """This module provides a function to convert a state space into a set of state nodes and transition edges.""" + from z3 import Z3Exception from mythril.laser.smt import simplify from mythril.laser.ethereum.svm import NodeFlags diff --git a/mythril/concolic/concolic_execution.py b/mythril/concolic/concolic_execution.py index 4dfed8732..9ce5fa25d 100644 --- a/mythril/concolic/concolic_execution.py +++ b/mythril/concolic/concolic_execution.py @@ -1,20 +1,15 @@ -import json -import binascii - -from datetime import datetime, timedelta +from datetime import datetime from typing import Dict, List, Any from copy import deepcopy from mythril.concolic.concrete_data import ConcreteData from mythril.concolic.find_trace import concrete_execution -from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.strategy.concolic import ConcolicStrategy from mythril.laser.ethereum.svm import LaserEVM from mythril.laser.ethereum.state.world_state import WorldState -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.transaction.symbolic import execute_transaction from mythril.laser.ethereum.transaction.transaction_models import tx_id_manager -from mythril.laser.smt import Expression, BitVec, symbol_factory +from mythril.laser.smt import symbol_factory from mythril.laser.ethereum.time_handler import time_handler from mythril.support.support_args import args diff --git a/mythril/concolic/find_trace.py b/mythril/concolic/find_trace.py index 5e32c8bca..537efeff5 100644 --- a/mythril/concolic/find_trace.py +++ b/mythril/concolic/find_trace.py @@ -1,9 +1,8 @@ -import json import binascii from copy import deepcopy from datetime import datetime -from typing import Dict, List, Tuple +from typing import List, Tuple from mythril.concolic.concrete_data import ConcreteData @@ -15,9 +14,8 @@ from mythril.laser.plugin.plugins import TraceFinderBuilder from mythril.laser.ethereum.transaction.concolic import execute_transaction from mythril.laser.plugin.loader import LaserPluginLoader -from mythril.laser.smt import Expression, BitVec, symbol_factory +from mythril.laser.smt import symbol_factory from mythril.laser.ethereum.transaction.transaction_models import tx_id_manager -from mythril.plugin.discovery import PluginDiscovery from mythril.support.support_args import args diff --git a/mythril/disassembler/disassembly.py b/mythril/disassembler/disassembly.py index 7dda95ac3..dd5984f25 100644 --- a/mythril/disassembler/disassembly.py +++ b/mythril/disassembler/disassembly.py @@ -1,4 +1,5 @@ """This module contains the class used to represent disassembly code.""" + from mythril.ethereum import util from mythril.disassembler import asm from mythril.support.signatures import SignatureDB diff --git a/mythril/ethereum/evmcontract.py b/mythril/ethereum/evmcontract.py index 538a40a86..600c476eb 100644 --- a/mythril/ethereum/evmcontract.py +++ b/mythril/ethereum/evmcontract.py @@ -1,5 +1,6 @@ """This module contains the class representing EVM contracts, aka Smart Contracts.""" + import re import logging import persistent diff --git a/mythril/ethereum/interface/rpc/client.py b/mythril/ethereum/interface/rpc/client.py index 65864d21e..809b81fed 100644 --- a/mythril/ethereum/interface/rpc/client.py +++ b/mythril/ethereum/interface/rpc/client.py @@ -2,6 +2,7 @@ This code is adapted from: https://github.com/ConsenSys/ethjsonrpc """ + import json import logging diff --git a/mythril/ethereum/interface/rpc/constants.py b/mythril/ethereum/interface/rpc/constants.py index 183bd1d39..87f6a467f 100644 --- a/mythril/ethereum/interface/rpc/constants.py +++ b/mythril/ethereum/interface/rpc/constants.py @@ -1,5 +1,6 @@ """This file contains constants used used by the Ethereum JSON RPC interface.""" + BLOCK_TAG_EARLIEST = "earliest" BLOCK_TAG_LATEST = "latest" BLOCK_TAG_PENDING = "pending" diff --git a/mythril/ethereum/interface/rpc/utils.py b/mythril/ethereum/interface/rpc/utils.py index c9a4e380e..fff1d3e15 100644 --- a/mythril/ethereum/interface/rpc/utils.py +++ b/mythril/ethereum/interface/rpc/utils.py @@ -1,5 +1,6 @@ """This module contains various utility functions regarding the RPC data format and validation.""" + from .constants import BLOCK_TAGS diff --git a/mythril/ethereum/util.py b/mythril/ethereum/util.py index f5d0383b3..9c47a2872 100644 --- a/mythril/ethereum/util.py +++ b/mythril/ethereum/util.py @@ -1,24 +1,20 @@ """This module contains various utility functions regarding unit conversion and solc integration.""" + import binascii import json -import sys import os import platform import logging -import solc -import re import typing -from pathlib import Path from requests.exceptions import ConnectionError from subprocess import PIPE, Popen from typing import Tuple from json.decoder import JSONDecodeError import semantic_version as semver -from semantic_version import Version, NpmSpec -from pyparsing import Word, Group, Optional, ZeroOrMore, oneOf, Regex, Combine +from pyparsing import Word, Optional, Regex, Combine from mythril.exceptions import CompilerError from mythril.support.support_args import args @@ -134,7 +130,6 @@ def solc_exists(version): :return: """ - default_binary = "/usr/bin/solc" if platform.system() == "Darwin": solcx.import_installed_solc() solcx.install_solc("v" + version) diff --git a/mythril/interfaces/cli.py b/mythril/interfaces/cli.py index a52f254ce..9af39c91a 100644 --- a/mythril/interfaces/cli.py +++ b/mythril/interfaces/cli.py @@ -15,7 +15,6 @@ import traceback from ast import literal_eval -import mythril.support.signatures as sigs from argparse import ArgumentParser, Namespace, RawTextHelpFormatter from mythril.concolic import concolic_execution from mythril.exceptions import ( @@ -23,7 +22,6 @@ CriticalError, ) from mythril.laser.ethereum.transaction.symbolic import ACTORS -from mythril.plugin.discovery import PluginDiscovery from mythril.plugin.loader import MythrilPluginLoader from mythril.mythril import MythrilAnalyzer, MythrilDisassembler, MythrilConfig @@ -786,18 +784,20 @@ def execute_command( print("Disassembly: \n" + disassembler.contracts[0].get_creation_easm()) elif args.command == SAFE_FUNCTIONS_COMMAND: - args.no_onchain_data = ( - args.disable_dependency_pruning - ) = args.unconstrained_storage = True + args.no_onchain_data = args.disable_dependency_pruning = ( + args.unconstrained_storage + ) = True args.pruning_factor = 1 function_analyzer = MythrilAnalyzer( strategy=strategy, disassembler=disassembler, address=address, cmd_args=args ) try: report = function_analyzer.fire_lasers( - modules=[m.strip() for m in args.modules.strip().split(",")] - if args.modules - else None, + modules=( + [m.strip() for m in args.modules.strip().split(",")] + if args.modules + else None + ), transaction_count=1, ) print_function_report(disassembler, report) @@ -860,9 +860,11 @@ def execute_command( else: try: report = analyzer.fire_lasers( - modules=[m.strip() for m in args.modules.strip().split(",")] - if args.modules - else None, + modules=( + [m.strip() for m in args.modules.strip().split(",")] + if args.modules + else None + ), transaction_count=args.transaction_count, ) diff --git a/mythril/interfaces/epic.py b/mythril/interfaces/epic.py index a8cc7b53b..deba32524 100755 --- a/mythril/interfaces/epic.py +++ b/mythril/interfaces/epic.py @@ -1,4 +1,5 @@ """Don't ask.""" + #!/usr/bin/env python # # "THE BEER-WARE LICENSE" (Revision 43~maze) @@ -20,6 +21,7 @@ PY3 = sys.version_info >= (3,) + # Reset terminal colors at exit def reset(): """""" diff --git a/mythril/laser/ethereum/call.py b/mythril/laser/ethereum/call.py index 8f951549b..bd4d3e506 100644 --- a/mythril/laser/ethereum/call.py +++ b/mythril/laser/ethereum/call.py @@ -10,7 +10,6 @@ import mythril.laser.ethereum.util as util from mythril.laser.ethereum.util import insert_ret_val from mythril.laser.ethereum import natives -from mythril.laser.ethereum.cheat_code import handle_cheat_codes, hevm_cheat_code from mythril.laser.ethereum.instruction_data import calculate_native_gas from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.natives import PRECOMPILE_COUNT, PRECOMPILE_FUNCTIONS diff --git a/mythril/laser/ethereum/cfg.py b/mythril/laser/ethereum/cfg.py index 2317b5d31..c5e96e4ab 100644 --- a/mythril/laser/ethereum/cfg.py +++ b/mythril/laser/ethereum/cfg.py @@ -1,4 +1,5 @@ """This module.""" + from enum import Enum from typing import Dict, List, TYPE_CHECKING diff --git a/mythril/laser/ethereum/cheat_code.py b/mythril/laser/ethereum/cheat_code.py index be1205853..d6355c884 100644 --- a/mythril/laser/ethereum/cheat_code.py +++ b/mythril/laser/ethereum/cheat_code.py @@ -1,23 +1,12 @@ -import logging -import re -from typing import Union, List, cast, Optional -from eth.constants import GAS_CALLSTIPEND +from typing import Union -import mythril.laser.ethereum.util as util from mythril.laser.ethereum.util import insert_ret_val -from mythril.laser.ethereum import natives -from mythril.laser.ethereum.instruction_data import calculate_native_gas -from mythril.laser.ethereum.state.account import Account -from mythril.laser.ethereum.natives import PRECOMPILE_COUNT, PRECOMPILE_FUNCTIONS from mythril.laser.ethereum.state.calldata import ( BaseCalldata, - SymbolicCalldata, - ConcreteCalldata, ) from mythril.laser.ethereum.state.global_state import GlobalState -from mythril.laser.smt import BitVec, If -from mythril.laser.smt import simplify, Expression, symbol_factory -from mythril.support.loader import DynLoader +from mythril.laser.smt import BitVec +from mythril.laser.smt import Expression class hevm_cheat_code: diff --git a/mythril/laser/ethereum/function_managers/exponent_function_manager.py b/mythril/laser/ethereum/function_managers/exponent_function_manager.py index d88aa47dc..e9e3f8b13 100644 --- a/mythril/laser/ethereum/function_managers/exponent_function_manager.py +++ b/mythril/laser/ethereum/function_managers/exponent_function_manager.py @@ -2,7 +2,14 @@ from typing import Tuple -from mythril.laser.smt import And, BitVec, Bool, Function, URem, symbol_factory +from mythril.laser.smt import ( + And, + BitVec, + SMTBool as Bool, + Function, + URem, + symbol_factory, +) log = logging.getLogger(__name__) diff --git a/mythril/laser/ethereum/function_managers/keccak_function_manager.py b/mythril/laser/ethereum/function_managers/keccak_function_manager.py index a04fad10e..6a45714bc 100644 --- a/mythril/laser/ethereum/function_managers/keccak_function_manager.py +++ b/mythril/laser/ethereum/function_managers/keccak_function_manager.py @@ -6,7 +6,7 @@ ULE, And, ULT, - Bool, + SMTBool as Bool, Or, ) diff --git a/mythril/laser/ethereum/instructions.py b/mythril/laser/ethereum/instructions.py index dba8fe0e2..f90d54738 100644 --- a/mythril/laser/ethereum/instructions.py +++ b/mythril/laser/ethereum/instructions.py @@ -1,5 +1,6 @@ """This module contains a representation class for EVM instructions and transitions between them.""" + import logging from copy import copy, deepcopy @@ -19,7 +20,7 @@ URem, SRem, If, - Bool, + SMTBool as Bool, Not, LShR, UGE, @@ -1205,13 +1206,13 @@ def _code_copy_helper( except TypeError: # except both attribute error and Exception global_state.mstate.mem_extend(concrete_memory_offset, 1) - global_state.mstate.memory[ - concrete_memory_offset - ] = global_state.new_bitvec( - "code({})".format( - global_state.environment.active_account.contract_name - ), - 8, + global_state.mstate.memory[concrete_memory_offset] = ( + global_state.new_bitvec( + "code({})".format( + global_state.environment.active_account.contract_name + ), + 8, + ) ) return [global_state] @@ -1221,13 +1222,13 @@ def _code_copy_helper( log.debug("Unsupported symbolic code offset in {}".format(op)) global_state.mstate.mem_extend(concrete_memory_offset, concrete_size) for i in range(concrete_size): - global_state.mstate.memory[ - concrete_memory_offset + i - ] = global_state.new_bitvec( - "code({})".format( - global_state.environment.active_account.contract_name - ), - 8, + global_state.mstate.memory[concrete_memory_offset + i] = ( + global_state.new_bitvec( + "code({})".format( + global_state.environment.active_account.contract_name + ), + 8, + ) ) return [global_state] @@ -1897,9 +1898,9 @@ def selfdestruct_(self, global_state: GlobalState): global_state.environment.active_account = deepcopy( global_state.environment.active_account ) - global_state.accounts[ - global_state.environment.active_account.address.value - ] = global_state.environment.active_account + global_state.accounts[global_state.environment.active_account.address.value] = ( + global_state.environment.active_account + ) global_state.environment.active_account.set_balance(0) global_state.environment.active_account.deleted = True @@ -2239,9 +2240,9 @@ def callcode_post(self, global_state: GlobalState) -> List[GlobalState]: else: ret_size = global_state.last_return_data.size.value for i in range(min(memory_out_size, ret_size)): - global_state.mstate.memory[ - i + memory_out_offset - ] = global_state.last_return_data[i] + global_state.mstate.memory[i + memory_out_offset] = ( + global_state.last_return_data[i] + ) # Put return value on stack return_value = global_state.new_bitvec("retval_" + str(instr["address"]), 256) @@ -2387,9 +2388,9 @@ def delegatecall_post(self, global_state: GlobalState) -> List[GlobalState]: else: ret_size = global_state.last_return_data.size.value for i in range(min(memory_out_size, ret_size)): - global_state.mstate.memory[ - i + memory_out_offset - ] = global_state.last_return_data[i] + global_state.mstate.memory[i + memory_out_offset] = ( + global_state.last_return_data[i] + ) # Put return value on stack return_value = global_state.new_bitvec("retval_" + str(instr["address"]), 256) @@ -2538,9 +2539,9 @@ def post_handler(self, global_state, function_name: str): ret_size = global_state.last_return_data.size.value for i in range(min(memory_out_size, ret_size)): - global_state.mstate.memory[ - i + memory_out_offset - ] = global_state.last_return_data[i] + global_state.mstate.memory[i + memory_out_offset] = ( + global_state.last_return_data[i] + ) # Put return value on stack return_value = global_state.new_bitvec( diff --git a/mythril/laser/ethereum/state/account.py b/mythril/laser/ethereum/state/account.py index 481d6c931..33f6b48b0 100644 --- a/mythril/laser/ethereum/state/account.py +++ b/mythril/laser/ethereum/state/account.py @@ -2,12 +2,13 @@ This includes classes representing accounts and their storage. """ + import logging from copy import copy, deepcopy from typing import Any, Dict, Union, Set -from mythril.laser.smt import Array, K, BitVec, simplify, BaseArray, If, Bool +from mythril.laser.smt import Array, K, BitVec, simplify, BaseArray, If, SMTBool as Bool from mythril.disassembler.disassembly import Disassembly from mythril.laser.smt import symbol_factory from mythril.support.support_args import args @@ -174,9 +175,9 @@ def set_storage(self, storage: Dict): """ for key, value in storage.items(): concrete_key, concrete_value = int(key, 16), int(value, 16) - self.storage[ - symbol_factory.BitVecVal(concrete_key, 256) - ] = symbol_factory.BitVecVal(concrete_value, 256) + self.storage[symbol_factory.BitVecVal(concrete_key, 256)] = ( + symbol_factory.BitVecVal(concrete_value, 256) + ) def add_balance(self, balance: Union[int, BitVec]) -> None: """ diff --git a/mythril/laser/ethereum/state/calldata.py b/mythril/laser/ethereum/state/calldata.py index 0f774a34d..012b98470 100644 --- a/mythril/laser/ethereum/state/calldata.py +++ b/mythril/laser/ethereum/state/calldata.py @@ -1,8 +1,9 @@ """This module declares classes to represent call data.""" + from typing import cast, Union, Tuple, List -from typing import Any, Union +from typing import Any from z3 import Model, unsat, unknown from z3.z3types import Z3Exception @@ -12,7 +13,7 @@ from mythril.laser.smt import ( Array, BitVec, - Bool, + SMTBool as Bool, Concat, Expression, If, diff --git a/mythril/laser/ethereum/state/constraints.py b/mythril/laser/ethereum/state/constraints.py index 9afa5c76a..fb0c28b84 100644 --- a/mythril/laser/ethereum/state/constraints.py +++ b/mythril/laser/ethereum/state/constraints.py @@ -1,7 +1,8 @@ """This module contains the class used to represent state-change constraints in the call graph.""" + from mythril.exceptions import UnsatError, SolverTimeOutException -from mythril.laser.smt import symbol_factory, simplify, Bool +from mythril.laser.smt import symbol_factory, simplify, SMTBool as Bool from mythril.support.model import get_model from mythril.laser.ethereum.function_managers import keccak_function_manager from mythril.laser.smt.model import Model @@ -118,9 +119,11 @@ def __iadd__(self, constraints: Iterable[Union[bool, Bool]]) -> "Constraints": @staticmethod def _get_smt_bool_list(constraints: Iterable[Union[bool, Bool]]) -> List[Bool]: return [ - constraint - if isinstance(constraint, Bool) - else symbol_factory.Bool(constraint) + ( + constraint + if isinstance(constraint, Bool) + else symbol_factory.Bool(constraint) + ) for constraint in constraints ] diff --git a/mythril/laser/ethereum/state/environment.py b/mythril/laser/ethereum/state/environment.py index 9a5837adc..eac56ac2c 100644 --- a/mythril/laser/ethereum/state/environment.py +++ b/mythril/laser/ethereum/state/environment.py @@ -1,5 +1,6 @@ """This module contains the representation for an execution state's environment.""" + from typing import Dict from z3 import ExprRef diff --git a/mythril/laser/ethereum/state/global_state.py b/mythril/laser/ethereum/state/global_state.py index 0965bdab6..879e43107 100644 --- a/mythril/laser/ethereum/state/global_state.py +++ b/mythril/laser/ethereum/state/global_state.py @@ -1,4 +1,5 @@ """This module contains a representation of the global execution state.""" + from typing import Dict, Union, List, Iterable, TYPE_CHECKING from copy import copy, deepcopy diff --git a/mythril/laser/ethereum/state/machine_state.py b/mythril/laser/ethereum/state/machine_state.py index 3667bac3d..d63f9d6d3 100644 --- a/mythril/laser/ethereum/state/machine_state.py +++ b/mythril/laser/ethereum/state/machine_state.py @@ -1,9 +1,10 @@ """This module contains a representation of the EVM's machine state and its stack.""" + from copy import copy from typing import cast, Sized, Union, Any, List, Dict, Optional -from mythril.laser.smt import BitVec, Bool, If, Expression, symbol_factory +from mythril.laser.smt import BitVec, SMTBool as Bool, If, Expression, symbol_factory from eth._utils.numeric import ceil32 from eth.constants import GAS_MEMORY, GAS_MEMORY_QUADRATIC_DENOMINATOR diff --git a/mythril/laser/ethereum/state/memory.py b/mythril/laser/ethereum/state/memory.py index f725e2aea..a1492bcfc 100644 --- a/mythril/laser/ethereum/state/memory.py +++ b/mythril/laser/ethereum/state/memory.py @@ -1,4 +1,5 @@ """This module contains a representation of a smart contract's memory.""" + from copy import copy from typing import cast, Dict, List, Union, overload from z3 import Z3Exception @@ -6,7 +7,7 @@ from mythril.laser.ethereum import util from mythril.laser.smt import ( BitVec, - Bool, + SMTBool as Bool, Concat, Extract, If, @@ -115,12 +116,10 @@ def write_word_at(self, index: int, value: Union[int, BitVec, bool, Bool]) -> No self[index + 31 - (i // 8)] = Extract(i + 7, i, value_to_write) @overload - def __getitem__(self, item: BitVec) -> Union[int, BitVec]: - ... + def __getitem__(self, item: BitVec) -> Union[int, BitVec]: ... @overload - def __getitem__(self, item: slice) -> List[Union[int, BitVec]]: - ... + def __getitem__(self, item: slice) -> List[Union[int, BitVec]]: ... def __getitem__( self, item: Union[BitVec, slice] diff --git a/mythril/laser/ethereum/state/return_data.py b/mythril/laser/ethereum/state/return_data.py index ce4f930c8..aa4250f55 100644 --- a/mythril/laser/ethereum/state/return_data.py +++ b/mythril/laser/ethereum/state/return_data.py @@ -1,4 +1,5 @@ """This module declares classes to represent call data.""" + from typing import List from mythril.laser.smt import ( diff --git a/mythril/laser/ethereum/state/world_state.py b/mythril/laser/ethereum/state/world_state.py index 7662a6126..e1dd49b66 100644 --- a/mythril/laser/ethereum/state/world_state.py +++ b/mythril/laser/ethereum/state/world_state.py @@ -1,4 +1,5 @@ """This module contains a representation of the EVM's world state.""" + from copy import copy, deepcopy from random import randint from typing import Dict, List, Iterator, Optional, TYPE_CHECKING @@ -10,7 +11,6 @@ from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.annotation import StateAnnotation from mythril.laser.ethereum.state.constraints import Constraints -from mythril.laser.ethereum.state.transient_storage import TransientStorage if TYPE_CHECKING: from mythril.laser.ethereum.cfg import Node diff --git a/mythril/laser/ethereum/strategy/basic.py b/mythril/laser/ethereum/strategy/basic.py index ecd81ff76..93fe4be50 100644 --- a/mythril/laser/ethereum/strategy/basic.py +++ b/mythril/laser/ethereum/strategy/basic.py @@ -1,6 +1,6 @@ """This module implements basic symbolic execution search strategies.""" + from random import randrange -from typing import List from mythril.laser.ethereum.state.global_state import GlobalState from . import BasicSearchStrategy diff --git a/mythril/laser/ethereum/strategy/beam.py b/mythril/laser/ethereum/strategy/beam.py index 244081050..255144c67 100644 --- a/mythril/laser/ethereum/strategy/beam.py +++ b/mythril/laser/ethereum/strategy/beam.py @@ -1,5 +1,3 @@ -from typing import List - from mythril.laser.ethereum.state.global_state import GlobalState from . import BasicSearchStrategy diff --git a/mythril/laser/ethereum/strategy/concolic.py b/mythril/laser/ethereum/strategy/concolic.py index d3b5e9cc4..7eb3378b5 100644 --- a/mythril/laser/ethereum/strategy/concolic.py +++ b/mythril/laser/ethereum/strategy/concolic.py @@ -1,9 +1,6 @@ from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.constraints import Constraints -from mythril.laser.ethereum.strategy.basic import BasicSearchStrategy from mythril.laser.ethereum.state.annotation import StateAnnotation -from mythril.laser.ethereum.transaction import ContractCreationTransaction -from mythril.laser.ethereum.util import get_instruction_index from mythril.analysis.solver import get_transaction_sequence from mythril.laser.smt import Not from mythril.exceptions import UnsatError diff --git a/mythril/laser/ethereum/strategy/constraint_strategy.py b/mythril/laser/ethereum/strategy/constraint_strategy.py index b350cb251..9dc6a7430 100644 --- a/mythril/laser/ethereum/strategy/constraint_strategy.py +++ b/mythril/laser/ethereum/strategy/constraint_strategy.py @@ -1,17 +1,8 @@ from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.strategy.basic import BasicSearchStrategy -from mythril.laser.ethereum.state.annotation import StateAnnotation -from mythril.laser.ethereum.transaction import ContractCreationTransaction -from mythril.laser.smt import And, simplify from mythril.support.support_utils import ModelCache -from typing import Dict, cast, List -from collections import OrderedDict -from copy import copy, deepcopy -from functools import lru_cache import logging -import z3 -from time import time log = logging.getLogger(__name__) diff --git a/mythril/laser/ethereum/svm.py b/mythril/laser/ethereum/svm.py index 00119d068..a173e124c 100644 --- a/mythril/laser/ethereum/svm.py +++ b/mythril/laser/ethereum/svm.py @@ -1,4 +1,5 @@ """This module implements the main symbolic execution engine.""" + import logging from collections import defaultdict from copy import copy @@ -247,7 +248,7 @@ def _execute_transactions_non_ordered(self, address): """ for txs in self.strategy: log.info(f"Executing the sequence: {txs}") - self._execute_transactions_incremental(address, txs=tx) + self._execute_transactions_incremental(address, txs=txs) def _execute_transactions_incremental(self, address, txs=None): """This function executes multiple transactions incrementally on the address diff --git a/mythril/laser/ethereum/transaction/concolic.py b/mythril/laser/ethereum/transaction/concolic.py index dfc22d1d5..10518eb22 100644 --- a/mythril/laser/ethereum/transaction/concolic.py +++ b/mythril/laser/ethereum/transaction/concolic.py @@ -1,15 +1,14 @@ """This module contains functions to set up and execute concolic message calls.""" + import binascii from typing import List, Union -from copy import deepcopy from mythril.exceptions import IllegalArgumentError from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.cfg import Node, Edge, JumpType from mythril.laser.smt import symbol_factory -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.calldata import ConcreteCalldata from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.world_state import WorldState diff --git a/mythril/laser/ethereum/transaction/symbolic.py b/mythril/laser/ethereum/transaction/symbolic.py index e339d3ba4..c995e410c 100644 --- a/mythril/laser/ethereum/transaction/symbolic.py +++ b/mythril/laser/ethereum/transaction/symbolic.py @@ -1,15 +1,14 @@ """This module contains functions setting up and executing transactions with symbolic values.""" + import logging -from typing import Optional, List, Union -from copy import deepcopy +from typing import Optional, List from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.cfg import Node, Edge, JumpType from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.calldata import SymbolicCalldata -from mythril.laser.ethereum.state.constraints import Constraints from mythril.laser.ethereum.state.world_state import WorldState from mythril.laser.ethereum.transaction.transaction_models import ( MessageCallTransaction, @@ -17,8 +16,7 @@ tx_id_manager, BaseTransaction, ) -from mythril.laser.smt import symbol_factory, Or, Bool, BitVec -from mythril.support.support_args import args as cmd_args +from mythril.laser.smt import symbol_factory, Or, SMTBool as Bool, BitVec FUNCTION_HASH_BYTE_LENGTH = 4 diff --git a/mythril/laser/ethereum/tx_prioritiser/rf_prioritiser.py b/mythril/laser/ethereum/tx_prioritiser/rf_prioritiser.py index 0820e62c6..442e69ee3 100644 --- a/mythril/laser/ethereum/tx_prioritiser/rf_prioritiser.py +++ b/mythril/laser/ethereum/tx_prioritiser/rf_prioritiser.py @@ -1,7 +1,5 @@ import pickle -from sklearn.ensemble import RandomForestClassifier import numpy as np -from itertools import chain import logging diff --git a/mythril/laser/ethereum/util.py b/mythril/laser/ethereum/util.py index d0be0d948..5bed920ee 100644 --- a/mythril/laser/ethereum/util.py +++ b/mythril/laser/ethereum/util.py @@ -1,12 +1,20 @@ """This module contains various utility conversion functions and constants for LASER.""" + import re from typing import Dict, List, Union, TYPE_CHECKING, cast if TYPE_CHECKING: from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.state.global_state import GlobalState -from mythril.laser.smt import BitVec, Bool, Expression, If, simplify, symbol_factory +from mythril.laser.smt import ( + BitVec, + SMTBool as Bool, + Expression, + If, + simplify, + symbol_factory, +) TT256 = 2**256 TT256M1 = 2**256 - 1 diff --git a/mythril/laser/plugin/plugins/__init__.py b/mythril/laser/plugin/plugins/__init__.py index b5c577f00..463d66c5d 100644 --- a/mythril/laser/plugin/plugins/__init__.py +++ b/mythril/laser/plugin/plugins/__init__.py @@ -5,6 +5,7 @@ - benchmarking - pruning """ + from mythril.laser.plugin.plugins.benchmark import BenchmarkPluginBuilder from mythril.laser.plugin.plugins.call_depth_limiter import CallDepthLimitBuilder from mythril.laser.plugin.plugins.coverage.coverage_plugin import CoveragePluginBuilder diff --git a/mythril/laser/plugin/plugins/coverage_metrics/coverage_data.py b/mythril/laser/plugin/plugins/coverage_metrics/coverage_data.py index 491c141fa..be4c5206f 100644 --- a/mythril/laser/plugin/plugins/coverage_metrics/coverage_data.py +++ b/mythril/laser/plugin/plugins/coverage_metrics/coverage_data.py @@ -1,5 +1,4 @@ import json -from typing import Dict, List from mythril.support.support_utils import get_code_hash from mythril.laser.execution_info import ExecutionInfo diff --git a/mythril/laser/plugin/plugins/coverage_metrics/metrics_plugin.py b/mythril/laser/plugin/plugins/coverage_metrics/metrics_plugin.py index 7b4bdbf06..4062a44a1 100644 --- a/mythril/laser/plugin/plugins/coverage_metrics/metrics_plugin.py +++ b/mythril/laser/plugin/plugins/coverage_metrics/metrics_plugin.py @@ -7,7 +7,6 @@ InstructionCoverageInfo, ) from .constants import BATCH_OF_STATES -from typing import Dict, Tuple, List import time import logging diff --git a/mythril/laser/plugin/plugins/instruction_profiler.py b/mythril/laser/plugin/plugins/instruction_profiler.py index d5034fda9..a22d3c1dd 100644 --- a/mythril/laser/plugin/plugins/instruction_profiler.py +++ b/mythril/laser/plugin/plugins/instruction_profiler.py @@ -5,7 +5,6 @@ from mythril.laser.plugin.interface import LaserPlugin from mythril.laser.ethereum.svm import LaserEVM from mythril.laser.ethereum.state.global_state import GlobalState -from datetime import datetime import logging # Type annotations: diff --git a/mythril/laser/plugin/plugins/state_merge/merge_states.py b/mythril/laser/plugin/plugins/state_merge/merge_states.py index 2b78295a1..c828a39c2 100644 --- a/mythril/laser/plugin/plugins/state_merge/merge_states.py +++ b/mythril/laser/plugin/plugins/state_merge/merge_states.py @@ -5,7 +5,7 @@ from mythril.laser.ethereum.state.world_state import WorldState from mythril.laser.ethereum.state.account import Account, Storage from mythril.laser.ethereum.state.constraints import Constraints -from mythril.laser.smt import symbol_factory, Array, If, Or, And, Not, Bool +from mythril.laser.smt import symbol_factory, Array, If, Or, And, Not, SMTBool as Bool log = logging.getLogger(__name__) diff --git a/mythril/laser/plugin/plugins/state_merge/state_merge_plugin.py b/mythril/laser/plugin/plugins/state_merge/state_merge_plugin.py index 0d6a28837..f9a12ff7d 100644 --- a/mythril/laser/plugin/plugins/state_merge/state_merge_plugin.py +++ b/mythril/laser/plugin/plugins/state_merge/state_merge_plugin.py @@ -6,7 +6,6 @@ from .check_mergeability import check_ws_merge_condition from mythril.laser.ethereum.state.world_state import WorldState from mythril.laser.ethereum.state.annotation import StateAnnotation -from mythril.laser.plugin.interface import LaserPlugin import logging log = logging.getLogger(__name__) diff --git a/mythril/laser/plugin/plugins/summary/annotations.py b/mythril/laser/plugin/plugins/summary/annotations.py index 8b3b136f0..fdd178d79 100644 --- a/mythril/laser/plugin/plugins/summary/annotations.py +++ b/mythril/laser/plugin/plugins/summary/annotations.py @@ -1,10 +1,10 @@ from mythril.laser.ethereum.state.annotation import StateAnnotation from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.environment import Environment -from mythril.laser.smt import Bool, BaseArray +from mythril.laser.smt import SMTBool as Bool, BaseArray from typing import List, Tuple -from copy import deepcopy, copy +from copy import deepcopy class SummaryTrackingAnnotation(StateAnnotation): diff --git a/mythril/laser/plugin/plugins/summary/core.py b/mythril/laser/plugin/plugins/summary/core.py index b5bd1a179..b9941fbf9 100644 --- a/mythril/laser/plugin/plugins/summary/core.py +++ b/mythril/laser/plugin/plugins/summary/core.py @@ -2,7 +2,6 @@ from .annotations import SummaryTrackingAnnotation from mythril.analysis.issue_annotation import IssueAnnotation from mythril.analysis.potential_issues import check_potential_issues -from mythril.analysis import solver from mythril.analysis.solver import get_transaction_sequence from mythril.exceptions import UnsatError @@ -18,31 +17,23 @@ from mythril.support.support_utils import get_code_hash from mythril.laser.ethereum.function_managers import ( keccak_function_manager, - KeccakFunctionManager, ) from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.constraints import Constraints from mythril.laser.ethereum.state.environment import Environment from mythril.laser.ethereum.state.calldata import SymbolicCalldata -from mythril.laser.ethereum.state.account import Account from mythril.laser.smt import ( - K, Array, - BaseArray, - Bool, - simplify, + SMTBool as Bool, Solver, - Not, - Or, symbol_factory, Expression, ) from mythril.support.support_args import args import z3 -from typing import Dict, Tuple, List, Optional, Set +from typing import Tuple, List, Optional, Set from copy import copy, deepcopy -from mythril.support.model import get_model import logging @@ -399,12 +390,12 @@ def _record_symbolic_summary( exit=global_state.mstate.pc, trace=tracking_annotation.trace, code=tracking_annotation.code, - issues=list(global_state.get_annotations(IssueAnnotation)), + issues=annotations, revert=revert, get_map=get_map, set_map=set_map, ) - log.debug(list(global_state.get_annotations(IssueAnnotation))) + log.debug(annotations) # Calculate issues for the first transaction if len(global_state.world_state.transaction_sequence) == 2: for state in self.init_save_states: diff --git a/mythril/laser/plugin/plugins/summary/summary.py b/mythril/laser/plugin/plugins/summary/summary.py index e197a3432..c3726a6a4 100644 --- a/mythril/laser/plugin/plugins/summary/summary.py +++ b/mythril/laser/plugin/plugins/summary/summary.py @@ -1,4 +1,4 @@ -from mythril.laser.smt import BaseArray, Array, Solver, symbol_factory +from mythril.laser.smt import Array, Solver, symbol_factory from mythril.support.support_args import args from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.plugin.plugins.plugin_annotations import MutationAnnotation diff --git a/mythril/laser/smt/__init__.py b/mythril/laser/smt/__init__.py index 09e5b243e..c6876915a 100644 --- a/mythril/laser/smt/__init__.py +++ b/mythril/laser/smt/__init__.py @@ -19,12 +19,11 @@ ) from mythril.laser.smt.expression import Expression, simplify -from mythril.laser.smt.bool import Bool, is_true, is_false, Or, Not, And +from mythril.laser.smt.bool import Bool as SMTBool, is_true, is_false, Or, Not, And from mythril.laser.smt.array import K, Array, BaseArray from mythril.laser.smt.function import Function from mythril.laser.smt.solver import Solver, Optimize, SolverStatistics from mythril.laser.smt.model import Model -from mythril.laser.smt.bool import Bool as SMTBool from typing import Union, Any, Optional, Set, TypeVar, Generic import z3 diff --git a/mythril/laser/smt/bitvec_helper.py b/mythril/laser/smt/bitvec_helper.py index aadcefad0..8cada6a5d 100644 --- a/mythril/laser/smt/bitvec_helper.py +++ b/mythril/laser/smt/bitvec_helper.py @@ -32,13 +32,13 @@ def LShR(a: BitVec, b: BitVec): @overload -def If(a: Union[Bool, bool], b: Union[BitVec, int], c: Union[BitVec, int]) -> BitVec: - ... +def If( + a: Union[Bool, bool], b: Union[BitVec, int], c: Union[BitVec, int] +) -> BitVec: ... @overload -def If(a: Union[Bool, bool], b: BaseArray, c: BaseArray) -> BaseArray: - ... +def If(a: Union[Bool, bool], b: BaseArray, c: BaseArray) -> BaseArray: ... def If( @@ -113,13 +113,11 @@ def ULE(a: BitVec, b: BitVec) -> Bool: @overload -def Concat(*args: List[BitVec]) -> BitVec: - ... +def Concat(*args: List[BitVec]) -> BitVec: ... @overload -def Concat(*args: BitVec) -> BitVec: - ... +def Concat(*args: BitVec) -> BitVec: ... def Concat(*args: Union[BitVec, List[BitVec]]) -> BitVec: diff --git a/mythril/laser/smt/expression.py b/mythril/laser/smt/expression.py index ea1bdad53..b2b9fdd32 100644 --- a/mythril/laser/smt/expression.py +++ b/mythril/laser/smt/expression.py @@ -1,4 +1,5 @@ """This module contains the SMT abstraction for a basic symbol expression.""" + from typing import Optional, Set, Any, TypeVar, Generic, cast import z3 diff --git a/mythril/laser/smt/solver/solver.py b/mythril/laser/smt/solver/solver.py index 6237dbe31..65150e42c 100644 --- a/mythril/laser/smt/solver/solver.py +++ b/mythril/laser/smt/solver/solver.py @@ -1,4 +1,5 @@ """This module contains an abstract SMT representation of an SMT solver.""" + import logging import os import sys diff --git a/mythril/mythril/mythril_config.py b/mythril/mythril/mythril_config.py index 14b3bc362..10c1befce 100644 --- a/mythril/mythril/mythril_config.py +++ b/mythril/mythril/mythril_config.py @@ -1,7 +1,6 @@ import codecs import logging import os -import re from pathlib import Path from shutil import copyfile diff --git a/mythril/mythril/mythril_disassembler.py b/mythril/mythril/mythril_disassembler.py index 1af1ee816..32988fcd0 100644 --- a/mythril/mythril/mythril_disassembler.py +++ b/mythril/mythril/mythril_disassembler.py @@ -6,12 +6,11 @@ import shutil import solc import subprocess -import sys import warnings from eth_utils import int_to_big_endian from semantic_version import Version, NpmSpec -from typing import List, Tuple, Optional, TYPE_CHECKING +from typing import List, Tuple, Optional from mythril.support.support_utils import sha3, zpad from mythril.ethereum import util @@ -27,7 +26,6 @@ get_contracts_from_file, get_contracts_from_foundry, ) -from mythril.support.support_args import args def format_warning(message, category, filename, lineno, line=""): @@ -196,17 +194,13 @@ def load_from_foundry(self): for file in files: build_info = Path(build_dir, file) - uniq_id = file if ".json" not in file else file[0:-5] - with open(build_info, encoding="utf8") as file_desc: loaded_json = json.load(file_desc) targets_json = loaded_json["output"] - version_from_config = loaded_json["solcVersion"] input_json = loaded_json["input"] compiler = "solc" if input_json["language"] == "Solidity" else "vyper" - optimizer = input_json["settings"]["optimizer"]["enabled"] if compiler == "vyper": raise NotImplementedError("Support for Vyper is not implemented.") diff --git a/mythril/solidity/soliditycontract.py b/mythril/solidity/soliditycontract.py index a7f368ff2..308e9319e 100644 --- a/mythril/solidity/soliditycontract.py +++ b/mythril/solidity/soliditycontract.py @@ -1,5 +1,6 @@ """This module contains representation classes for Solidity files, contracts and source mappings.""" + from typing import Dict, Set import logging diff --git a/mythril/support/loader.py b/mythril/support/loader.py index d8a457365..06f5c2d4d 100644 --- a/mythril/support/loader.py +++ b/mythril/support/loader.py @@ -1,5 +1,6 @@ """This module contains the dynamic loader logic to get on-chain storage data and dependencies.""" + from mythril.disassembler.disassembly import Disassembly import logging import re diff --git a/mythril/support/lock.py b/mythril/support/lock.py index 888b1bf40..3521a025f 100644 --- a/mythril/support/lock.py +++ b/mythril/support/lock.py @@ -42,7 +42,7 @@ def acquire(self): if e.errno != errno.EEXIST: raise if (time.time() - start_time) >= self.timeout: - raise FileLockException( + raise LockFileException( f"Stuck for more than {self.timeout} seconds waiting to unlock the file {self.filename}." ) time.sleep(self.delay) diff --git a/mythril/support/model.py b/mythril/support/model.py index 6dff04842..d71d90bdb 100644 --- a/mythril/support/model.py +++ b/mythril/support/model.py @@ -6,18 +6,13 @@ import logging import os -import signal import sys -import threading -from collections import OrderedDict -from copy import deepcopy from functools import lru_cache from multiprocessing.pool import ThreadPool from multiprocessing import TimeoutError from pathlib import Path -from time import time -from z3 import sat, unknown, is_true +from z3 import sat, unknown log = logging.getLogger(__name__) diff --git a/mythril/support/signatures.py b/mythril/support/signatures.py index a14b52d9a..45faf34d5 100644 --- a/mythril/support/signatures.py +++ b/mythril/support/signatures.py @@ -1,12 +1,12 @@ """The Mythril function signature database.""" + import functools import logging import multiprocessing import os import sqlite3 -import time from collections import defaultdict -from typing import List, Set, DefaultDict, Dict +from typing import List, DefaultDict, Dict from mythril.ethereum.util import get_solc_json @@ -85,7 +85,7 @@ def __enter__(self): """ try: self.conn = sqlite3.connect(self.path) - except sqlite3.OperationalError as e: + except sqlite3.OperationalError: raise sqlite3.OperationalError(f"Unable to Connect to path {self.path}") self.cursor = self.conn.cursor() return self.cursor diff --git a/mythril/support/support_utils.py b/mythril/support/support_utils.py index 92d62f3e8..0c30f08ec 100644 --- a/mythril/support/support_utils.py +++ b/mythril/support/support_utils.py @@ -5,7 +5,7 @@ from eth_hash.auto import keccak from functools import lru_cache from typing import Dict -from z3 import is_true, simplify, And +from z3 import is_true import logging log = logging.getLogger(__name__) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..8bb76ff42 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,15 @@ +[tool.ruff] +target-version = "py37" + +[tool.ruff.lint] +fixable = ["ALL"] +# List of rules https://docs.astral.sh/ruff/rules/ +select = ["F"] + +[tool.ruff.lint.per-file-ignores] +# Fine for __init__.py files: +# * F401: Module imported but unused +# * F403: 'from .module import *' +# (resolvable via __all__ list, +# see https://docs.python.org/3/tutorial/modules.html#importing-from-a-package) +"mythril/**/__init__.py" = ["F401", "F403"] diff --git a/requirements.txt b/requirements.txt index 732348316..d2fdb6c19 100644 --- a/requirements.txt +++ b/requirements.txt @@ -29,7 +29,6 @@ pytest_mock requests rlp<4,>=3 semantic_version -scikit-learn transaction>=2.2.1 z3-solver<=4.12.5.0,>=4.8.8.0 matplotlib diff --git a/setup.py b/setup.py index 62f6bacd3..421cded95 100755 --- a/setup.py +++ b/setup.py @@ -9,7 +9,6 @@ """ from setuptools import setup, find_packages from setuptools.command.install import install as _install -from subprocess import check_call import sys import os @@ -25,6 +24,7 @@ REQUIRES_PYTHON = ">=3.7.0" here = os.path.abspath(os.path.dirname(__file__)) + # What packages are required for this module to be executed? def get_requirements(): """ diff --git a/tests/__init__.py b/tests/__init__.py index 02403427e..e40e7cd96 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,7 +1,5 @@ from pathlib import Path from unittest import TestCase -import os -import shutil TESTS_DIR = Path(__file__).parent PROJECT_DIR = TESTS_DIR.parent @@ -30,7 +28,7 @@ def compare_files_error_message(self): """ message = "Following output files are changed, compare them manually to see differences: \n" - for (input_file, expected, current) in self.changed_files: + for input_file, expected, current in self.changed_files: message += "{}:\n".format(input_file.name) message += "- {}\n".format(str(expected)) message += "- {}\n".format(str(current)) diff --git a/tests/analysis/abi_decode_test.py b/tests/analysis/abi_decode_test.py index 0b38896ab..814ac137c 100644 --- a/tests/analysis/abi_decode_test.py +++ b/tests/analysis/abi_decode_test.py @@ -1,15 +1,6 @@ import pytest from mythril.analysis.report import Issue -from mythril.disassembler.disassembly import Disassembly -from mythril.laser.ethereum.state.environment import Environment -from mythril.laser.ethereum.state.account import Account -from mythril.laser.ethereum.state.machine_state import MachineState -from mythril.laser.ethereum.state.global_state import GlobalState -from mythril.laser.ethereum.state.world_state import WorldState -from mythril.laser.ethereum.instructions import Instruction -from mythril.laser.ethereum.transaction.transaction_models import MessageCallTransaction -from mythril.laser.smt import symbol_factory, simplify, LShR test_data = ( diff --git a/tests/analysis/arbitrary_jump_test.py b/tests/analysis/arbitrary_jump_test.py index b6919fb10..bd8463f21 100644 --- a/tests/analysis/arbitrary_jump_test.py +++ b/tests/analysis/arbitrary_jump_test.py @@ -1,19 +1,16 @@ import pytest -from unittest.mock import patch from mythril.disassembler.disassembly import Disassembly -from mythril.laser.smt import symbol_factory, BitVec +from mythril.laser.smt import symbol_factory from mythril.laser.ethereum.state.environment import Environment from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.state.constraints import Constraints from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.world_state import WorldState -from mythril.laser.ethereum.instructions import Instruction from mythril.laser.ethereum.transaction.symbolic import ACTORS from mythril.laser.ethereum.transaction.transaction_models import MessageCallTransaction from mythril.laser.ethereum.call import SymbolicCalldata -from mythril.laser.ethereum.transaction import TransactionStartSignal from mythril.analysis.module.modules.arbitrary_jump import ( is_unique_jumpdest, ArbitraryJump, diff --git a/tests/cmd_line_test.py b/tests/cmd_line_test.py index 276c78aca..4d03a6b6f 100644 --- a/tests/cmd_line_test.py +++ b/tests/cmd_line_test.py @@ -1,6 +1,5 @@ from subprocess import check_output, CalledProcessError, STDOUT -from tests import BaseTestCase, TESTDATA, PROJECT_DIR, TESTS_DIR -from unittest.mock import patch +from tests import BaseTestCase, TESTDATA, PROJECT_DIR MYTH = str(PROJECT_DIR / "myth") diff --git a/tests/concolic/concolic_tests.py b/tests/concolic/concolic_tests.py index 8ace162e1..da090333e 100644 --- a/tests/concolic/concolic_tests.py +++ b/tests/concolic/concolic_tests.py @@ -1,29 +1,22 @@ import binascii import json -import pathlib import pytest import subprocess from copy import deepcopy from datetime import datetime -from unittest.mock import patch from subprocess import check_output, CalledProcessError -from tests import BaseTestCase, PROJECT_DIR, TESTDATA +from tests import PROJECT_DIR, TESTDATA from mythril.concolic import concrete_execution from mythril.concolic.find_trace import setup_concrete_initial_state -from mythril.disassembler.asm import disassemble -from mythril.concolic.concrete_data import ConcreteData from mythril.laser.ethereum import util -from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.svm import LaserEVM -from mythril.laser.ethereum.state.world_state import WorldState -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.time_handler import time_handler from mythril.laser.ethereum.transaction.concolic import execute_transaction from mythril.laser.plugin.loader import LaserPluginLoader -from mythril.laser.smt import Expression, BitVec, symbol_factory +from mythril.laser.smt import symbol_factory from mythril.laser.plugin.plugins import TraceFinderBuilder MYTH = str(PROJECT_DIR / "myth") diff --git a/tests/disassembler/asm_test.py b/tests/disassembler/asm_test.py index 0959c5b44..f580a8266 100644 --- a/tests/disassembler/asm_test.py +++ b/tests/disassembler/asm_test.py @@ -1,4 +1,9 @@ -from mythril.disassembler.asm import * +from mythril.disassembler.asm import ( + disassemble, + find_op_code_sequence, + is_sequence_match, + get_opcode_from_name, +) import pytest valid_names = [("PUSH1", 0x60), ("STOP", 0x0), ("RETURN", 0xF3)] diff --git a/tests/disassembler/disassembly_test.py b/tests/disassembler/disassembly_test.py index 85c006b77..fe38d3946 100644 --- a/tests/disassembler/disassembly_test.py +++ b/tests/disassembler/disassembly_test.py @@ -1,4 +1,4 @@ -from mythril.disassembler.disassembly import * +from mythril.disassembler.disassembly import get_function_info, SignatureDB instruction_list = [ {"opcode": "PUSH4", "argument": "0x10203040"}, diff --git a/tests/disassembler_test.py b/tests/disassembler_test.py index 4654f3ac1..3338d345c 100644 --- a/tests/disassembler_test.py +++ b/tests/disassembler_test.py @@ -1,6 +1,10 @@ from mythril.disassembler.disassembly import Disassembly -from mythril.ethereum import util -from tests import * +from tests import ( + BaseTestCase, + TESTDATA_INPUTS, + TESTDATA_OUTPUTS_EXPECTED, + TESTDATA_OUTPUTS_CURRENT, +) import os diff --git a/tests/features_test.py b/tests/features_test.py index a4bda13e5..f0e5261f8 100644 --- a/tests/features_test.py +++ b/tests/features_test.py @@ -2,7 +2,7 @@ from pathlib import Path from mythril.mythril import MythrilDisassembler from mythril.solidity.features import SolidityFeatureExtractor -from mythril.solidity.soliditycontract import SolidityContract, SolcAST +from mythril.solidity.soliditycontract import SolidityContract TEST_FILES = Path(__file__).parent / "testdata/input_contracts" solc_binary_5 = MythrilDisassembler._init_solc_binary("v0.5.0") diff --git a/tests/graph_test.py b/tests/graph_test.py index 13935631a..8dd222780 100644 --- a/tests/graph_test.py +++ b/tests/graph_test.py @@ -1,6 +1,7 @@ """ This test only checks whether dumping is successful, not whether the dumped state space makes sense """ + from mythril.mythril import MythrilAnalyzer, MythrilDisassembler from mythril.ethereum import util from mythril.solidity.soliditycontract import EVMContract diff --git a/tests/instructions/codecopy_test.py b/tests/instructions/codecopy_test.py index b7ed02aa6..c45ebb715 100644 --- a/tests/instructions/codecopy_test.py +++ b/tests/instructions/codecopy_test.py @@ -1,6 +1,5 @@ from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.state.environment import Environment -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.world_state import WorldState diff --git a/tests/instructions/extcodehash_test.py b/tests/instructions/extcodehash_test.py index 00dbce726..59c6ef21d 100644 --- a/tests/instructions/extcodehash_test.py +++ b/tests/instructions/extcodehash_test.py @@ -1,6 +1,5 @@ from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.state.environment import Environment -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.world_state import WorldState diff --git a/tests/instructions/push_test.py b/tests/instructions/push_test.py index 13f26648e..1ea9c4efb 100644 --- a/tests/instructions/push_test.py +++ b/tests/instructions/push_test.py @@ -3,10 +3,8 @@ from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.state.environment import Environment from mythril.laser.ethereum.state.world_state import WorldState -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.state.global_state import GlobalState -from mythril.laser.ethereum.state.world_state import WorldState from mythril.laser.ethereum.instructions import Instruction from mythril.laser.ethereum.transaction.transaction_models import MessageCallTransaction from mythril.laser.smt import symbol_factory, simplify diff --git a/tests/instructions/sar_test.py b/tests/instructions/sar_test.py index 7b8c773f3..afbed8414 100644 --- a/tests/instructions/sar_test.py +++ b/tests/instructions/sar_test.py @@ -3,10 +3,8 @@ from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.state.environment import Environment from mythril.laser.ethereum.state.world_state import WorldState -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.state.global_state import GlobalState -from mythril.laser.ethereum.state.world_state import WorldState from mythril.laser.ethereum.instructions import Instruction from mythril.laser.ethereum.transaction.transaction_models import MessageCallTransaction from mythril.laser.smt import symbol_factory, simplify diff --git a/tests/instructions/shl_test.py b/tests/instructions/shl_test.py index 5233ca6cb..44c48538d 100644 --- a/tests/instructions/shl_test.py +++ b/tests/instructions/shl_test.py @@ -2,7 +2,6 @@ from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.state.environment import Environment -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.world_state import WorldState diff --git a/tests/instructions/shr_test.py b/tests/instructions/shr_test.py index e6840d086..5629dfae0 100644 --- a/tests/instructions/shr_test.py +++ b/tests/instructions/shr_test.py @@ -2,7 +2,6 @@ from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.state.environment import Environment -from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.world_state import WorldState diff --git a/tests/integration_tests/analysis_tests.py b/tests/integration_tests/analysis_tests.py index 078b73263..de14f3b73 100644 --- a/tests/integration_tests/analysis_tests.py +++ b/tests/integration_tests/analysis_tests.py @@ -1,6 +1,5 @@ import pytest import json -import sys from utils import output_of from tests import PROJECT_DIR, TESTDATA diff --git a/tests/integration_tests/old_version_test.py b/tests/integration_tests/old_version_test.py index 1abfbd9d5..e326ce6c6 100644 --- a/tests/integration_tests/old_version_test.py +++ b/tests/integration_tests/old_version_test.py @@ -1,6 +1,5 @@ import pytest import json -import sys from tests import PROJECT_DIR, TESTDATA from utils import output_of diff --git a/tests/integration_tests/safe_functions_test.py b/tests/integration_tests/safe_functions_test.py index 65d506035..870b085c9 100644 --- a/tests/integration_tests/safe_functions_test.py +++ b/tests/integration_tests/safe_functions_test.py @@ -1,6 +1,4 @@ import pytest -import json -import sys from utils import output_of from tests import PROJECT_DIR, TESTDATA diff --git a/tests/integration_tests/solc_settings_test.py b/tests/integration_tests/solc_settings_test.py index 0478290a6..62c7da5d5 100644 --- a/tests/integration_tests/solc_settings_test.py +++ b/tests/integration_tests/solc_settings_test.py @@ -1,7 +1,3 @@ -import pytest -import json -import sys - from subprocess import check_output, STDOUT from tests import PROJECT_DIR, TESTDATA @@ -21,7 +17,6 @@ def test_positive_solc_settings(): def test_positive_solc_args(): base_path = str(TESTDATA) file_dir = str(TESTDATA / "json_test_dir" / "dir_a") - json_file_path = str(TESTDATA / "json_test_dir" / "test_file.json") file_path = file_dir + "/input_file_args.sol" command = f"""cd {file_dir} && python3 {MYTH} analyze {file_path} --solc-args "--allow-paths {base_path}" --solv 0.8.0""" diff --git a/tests/integration_tests/src_mapping_test.py b/tests/integration_tests/src_mapping_test.py index 57186ec82..615477533 100644 --- a/tests/integration_tests/src_mapping_test.py +++ b/tests/integration_tests/src_mapping_test.py @@ -1,7 +1,3 @@ -import pytest -import json -import sys - from subprocess import STDOUT from tests import PROJECT_DIR, TESTDATA from utils import output_of diff --git a/tests/integration_tests/summary_test.py b/tests/integration_tests/summary_test.py index ce8dcc5be..3307123eb 100644 --- a/tests/integration_tests/summary_test.py +++ b/tests/integration_tests/summary_test.py @@ -1,7 +1,5 @@ import pytest import json -import sys -import os from tests import PROJECT_DIR, TESTDATA from utils import output_of diff --git a/tests/integration_tests/transient_storage_test.py b/tests/integration_tests/transient_storage_test.py index be0183eb4..84955b6a4 100644 --- a/tests/integration_tests/transient_storage_test.py +++ b/tests/integration_tests/transient_storage_test.py @@ -1,9 +1,5 @@ import pytest -from unittest.mock import patch, MagicMock -import json -import sys -from subprocess import STDOUT from tests import PROJECT_DIR, TESTDATA from utils import output_of diff --git a/tests/integration_tests/version_test.py b/tests/integration_tests/version_test.py index 6b00b3c6a..df7973855 100644 --- a/tests/integration_tests/version_test.py +++ b/tests/integration_tests/version_test.py @@ -1,6 +1,4 @@ import pytest -import json -import sys from tests import PROJECT_DIR, TESTDATA from utils import output_of @@ -29,7 +27,7 @@ def test_analysis(file_name, version, has_overflow): command = f"python3 {MYTH} analyze {file}" output = output_of(command) if has_overflow: - assert f"SWC ID: 101" in output + assert "SWC ID: 101" in output else: assert ( "The analysis was completed successfully. No issues were detected." diff --git a/tests/laser/Precompiles/blake2_test.py b/tests/laser/Precompiles/blake2_test.py index 5f400e38f..3eafade78 100644 --- a/tests/laser/Precompiles/blake2_test.py +++ b/tests/laser/Precompiles/blake2_test.py @@ -1,6 +1,7 @@ import pytest from mythril.laser.ethereum.natives import blake2b_fcompress + # Test cases taken from https://eips.ethereum.org/EIPS/eip-152. # One of the test case is expected to take a few hrs so I ignored it @pytest.mark.parametrize( diff --git a/tests/laser/Precompiles/ecrecover_test.py b/tests/laser/Precompiles/ecrecover_test.py index c9bea5aba..5c6e5e94a 100644 --- a/tests/laser/Precompiles/ecrecover_test.py +++ b/tests/laser/Precompiles/ecrecover_test.py @@ -1,7 +1,5 @@ import pytest -from unittest.mock import patch -from eth_utils import decode_hex from mythril.laser.ethereum.natives import ecrecover, NativeContractException from mythril.laser.smt import symbol_factory diff --git a/tests/laser/Precompiles/identity_test.py b/tests/laser/Precompiles/identity_test.py index 0ad9e453b..0374a5857 100644 --- a/tests/laser/Precompiles/identity_test.py +++ b/tests/laser/Precompiles/identity_test.py @@ -1,9 +1,6 @@ import pytest -from unittest.mock import patch -from eth_utils import decode_hex -from mythril.laser.ethereum.natives import identity, NativeContractException -from mythril.laser.smt import symbol_factory +from mythril.laser.ethereum.natives import identity @pytest.mark.parametrize( diff --git a/tests/laser/Precompiles/ripemd_test.py b/tests/laser/Precompiles/ripemd_test.py index 13bbbda3c..62272c9d7 100644 --- a/tests/laser/Precompiles/ripemd_test.py +++ b/tests/laser/Precompiles/ripemd_test.py @@ -1,7 +1,5 @@ import pytest -from unittest.mock import patch -from eth_utils import decode_hex from mythril.laser.ethereum.natives import ripemd160, NativeContractException from mythril.laser.smt import symbol_factory diff --git a/tests/laser/Precompiles/sha256_test.py b/tests/laser/Precompiles/sha256_test.py index c85cddd26..83561ade0 100644 --- a/tests/laser/Precompiles/sha256_test.py +++ b/tests/laser/Precompiles/sha256_test.py @@ -1,7 +1,5 @@ import pytest -from unittest.mock import patch -from eth_utils import decode_hex from mythril.laser.ethereum.natives import sha256, NativeContractException from mythril.laser.smt import symbol_factory diff --git a/tests/laser/evm_testsuite/evm_test.py b/tests/laser/evm_testsuite/evm_test.py index 6f7fe2c4e..5bd201332 100644 --- a/tests/laser/evm_testsuite/evm_test.py +++ b/tests/laser/evm_testsuite/evm_test.py @@ -2,19 +2,16 @@ from mythril.laser.ethereum.state.account import Account from mythril.laser.ethereum.state.world_state import WorldState from mythril.laser.ethereum.time_handler import time_handler -from mythril.laser.ethereum.function_managers import keccak_function_manager from mythril.disassembler.disassembly import Disassembly from mythril.laser.ethereum.transaction.concolic import execute_message_call -from mythril.laser.smt import Expression, BitVec, symbol_factory +from mythril.laser.smt import Expression, symbol_factory from mythril.support.support_args import args from datetime import datetime import binascii import json -import os from pathlib import Path import pytest -from z3 import ExprRef, simplify evm_test_dir = Path(__file__).parent / "VMTests" diff --git a/tests/laser/keccak_tests.py b/tests/laser/keccak_tests.py index 6dabc37a3..89c9a919e 100644 --- a/tests/laser/keccak_tests.py +++ b/tests/laser/keccak_tests.py @@ -1,4 +1,4 @@ -from mythril.laser.smt import Solver, symbol_factory, And +from mythril.laser.smt import Solver, symbol_factory from mythril.laser.ethereum.function_managers import keccak_function_manager import z3 import pytest diff --git a/tests/laser/state/calldata_test.py b/tests/laser/state/calldata_test.py index c94aab9c8..a6c0de795 100644 --- a/tests/laser/state/calldata_test.py +++ b/tests/laser/state/calldata_test.py @@ -1,9 +1,7 @@ import pytest from mythril.laser.ethereum.state.calldata import ConcreteCalldata, SymbolicCalldata from mythril.laser.smt import Solver, symbol_factory -from z3 import sat, unsat -from z3.z3types import Z3Exception -from unittest.mock import MagicMock +from z3 import unsat uninitialized_test_data = [ ([]), # Empty concrete calldata diff --git a/tests/laser/state/mstack_test.py b/tests/laser/state/mstack_test.py index 2a3cfb01e..ad01e3736 100644 --- a/tests/laser/state/mstack_test.py +++ b/tests/laser/state/mstack_test.py @@ -1,7 +1,11 @@ import pytest from mythril.laser.ethereum.state.machine_state import MachineStack -from mythril.laser.ethereum.evm_exceptions import * + +from mythril.laser.ethereum.evm_exceptions import ( + StackOverflowException, + StackUnderflowException, +) from tests import BaseTestCase diff --git a/tests/laser/state/world_state_account_exist_load_test.py b/tests/laser/state/world_state_account_exist_load_test.py index ef4256074..4e69de10e 100644 --- a/tests/laser/state/world_state_account_exist_load_test.py +++ b/tests/laser/state/world_state_account_exist_load_test.py @@ -1,15 +1,3 @@ -import pytest - -from mythril.disassembler.disassembly import Disassembly -from mythril.laser.ethereum.state.environment import Environment -from mythril.laser.ethereum.state.account import Account -from mythril.laser.ethereum.state.machine_state import MachineState -from mythril.laser.ethereum.state.global_state import GlobalState -from mythril.laser.ethereum.state.world_state import WorldState -from mythril.support.loader import DynLoader -from mythril.ethereum.interface.rpc.client import EthJsonRpc -from mythril.laser.ethereum.instructions import Instruction - """ def _get_global_state(): active_account = Account("0x0", code=Disassembly("60606040")) diff --git a/tests/laser/transaction_test.py b/tests/laser/transaction_test.py index f66d9b9c0..f0d06da39 100644 --- a/tests/laser/transaction_test.py +++ b/tests/laser/transaction_test.py @@ -4,8 +4,6 @@ from mythril.laser.ethereum.state.world_state import WorldState from mythril.support.support_args import args -import mythril.laser.ethereum.cfg as cfg - def test_intercontract_call(): # Arrange diff --git a/tests/mythril/mythril_config_test.py b/tests/mythril/mythril_config_test.py index afe27682f..883d7d359 100644 --- a/tests/mythril/mythril_config_test.py +++ b/tests/mythril/mythril_config_test.py @@ -1,5 +1,4 @@ import pytest -import os from configparser import ConfigParser from pathlib import Path diff --git a/tests/mythril/mythril_disassembler_test.py b/tests/mythril/mythril_disassembler_test.py index 79ae3f939..5a95afff9 100644 --- a/tests/mythril/mythril_disassembler_test.py +++ b/tests/mythril/mythril_disassembler_test.py @@ -1,6 +1,4 @@ -import pytest -from mythril.mythril import MythrilConfig, MythrilDisassembler -from mythril.exceptions import CriticalError +from mythril.mythril import MythrilDisassembler storage_test = [ ( diff --git a/tests/rpc_test.py b/tests/rpc_test.py index de899f1f0..00ca81a77 100644 --- a/tests/rpc_test.py +++ b/tests/rpc_test.py @@ -1,4 +1,3 @@ -from unittest import TestCase from tests import BaseTestCase from mythril.ethereum.interface.rpc.client import EthJsonRpc