Skip to content

Commit 50e1ded

Browse files
authored
Check versions before using integer module (#1613)
1 parent 042cc68 commit 50e1ded

File tree

8 files changed

+73
-4
lines changed

8 files changed

+73
-4
lines changed

mythril/analysis/module/loader.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mythril.analysis.module.base import DetectionModule, EntryPoint
22
from mythril.support.support_utils import Singleton
3+
from mythril.support.support_args import args
34

45
from mythril.analysis.module.modules.arbitrary_jump import ArbitraryJump
56
from mythril.analysis.module.modules.arbitrary_write import ArbitraryStorage
@@ -75,7 +76,12 @@ def get_detection_modules(
7576
result = [
7677
module for module in result if type(module).__name__ in white_list
7778
]
78-
79+
if args.use_integer_module is False:
80+
result = [
81+
module
82+
for module in result
83+
if type(module).__name__ != "IntegerArithmetics"
84+
]
7985
if entry_point:
8086
result = [module for module in result if module.entry_point == entry_point]
8187

mythril/ethereum/util.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
from typing import Optional
1414

1515
from json.decoder import JSONDecodeError
16-
from mythril.exceptions import CompilerError
1716
from semantic_version import Version, NpmSpec
1817

18+
from mythril.exceptions import CompilerError
19+
from mythril.support.support_args import args
20+
1921
import solcx
2022

2123
log = logging.getLogger(__name__)
@@ -158,6 +160,9 @@ def extract_version(file: str) -> Optional[str]:
158160
def extract_binary(file: str) -> str:
159161
with open(file) as f:
160162
version = extract_version(f.read())
163+
if version and NpmSpec("^0.8.0").match(Version(version)):
164+
args.use_integer_module = False
165+
161166
if version is None:
162167
return os.environ.get("SOLC") or "solc"
163168
return solc_exists(version)

mythril/mythril/mythril_disassembler.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import sys
55
import os
66

7-
from mythril.support.support_utils import sha3, zpad
7+
from semantic_version import Version, NpmSpec
88
from typing import List, Tuple, Optional
9+
10+
from mythril.support.support_utils import sha3, zpad
911
from mythril.ethereum import util
1012
from mythril.ethereum.interface.rpc.client import EthJsonRpc
1113
from mythril.exceptions import CriticalError, CompilerError, NoContractFoundError
1214
from mythril.support import signatures
1315
from mythril.support.support_utils import rzpad
16+
from mythril.support.support_args import args
1417
from mythril.ethereum.evmcontract import EVMContract
1518
from mythril.ethereum.interface.rpc.exceptions import ConnectionError
1619
from mythril.solidity.soliditycontract import SolidityContract, get_contracts_from_file
@@ -62,7 +65,8 @@ def _init_solc_binary(version: str) -> Optional[str]:
6265

6366
if version.startswith("v"):
6467
version = version[1:]
65-
68+
if version and NpmSpec("^0.8.0").match(Version(version)):
69+
args.use_integer_module = False
6670
if version == main_version_number:
6771
log.info("Given version matches installed version")
6872
solc_binary = os.environ.get("SOLC") or "solc"

mythril/support/support_args.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self):
1313
self.iprof = True
1414
self.solver_log = None
1515
self.transaction_sequences: List[List[str]] = None
16+
self.use_integer_module = True
1617

1718

1819
args = Args()
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
import json
3+
import sys
4+
5+
from subprocess import check_output
6+
from tests import PROJECT_DIR, TESTDATA
7+
8+
MYTH = str(PROJECT_DIR / "myth")
9+
test_data = (
10+
("version_contract.sol", "v0.7.0", True),
11+
("version_contract.sol", "v0.8.0", False),
12+
("version_contract_0.8.0.sol", None, False),
13+
("version_contract_0.7.0.sol", None, True),
14+
)
15+
16+
17+
@pytest.mark.parametrize("file_name, version, has_overflow", test_data)
18+
def test_analysis(file_name, version, has_overflow):
19+
file = str(TESTDATA / "input_contracts" / file_name)
20+
if version:
21+
command = f"python3 {MYTH} analyze {file} --solv {version}"
22+
else:
23+
command = f"python3 {MYTH} analyze {file}"
24+
output = check_output(command, shell=True).decode("UTF-8")
25+
if has_overflow:
26+
assert f"SWC ID: 101" in output
27+
else:
28+
assert (
29+
"The analysis was completed successfully. No issues were detected."
30+
in output
31+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
contract Test {
2+
uint256 input;
3+
function add(uint256 a, uint256 b) public {
4+
input = a + b;
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pragma solidity ^0.7.0;
2+
3+
contract Test {
4+
uint256 input;
5+
function add(uint256 a, uint256 b) public {
6+
input = a + b;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pragma solidity ^0.8.0;
2+
3+
contract Test {
4+
uint256 input;
5+
function add(uint256 a, uint256 b) public {
6+
input = a + b;
7+
}
8+
}

0 commit comments

Comments
 (0)