Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add crytic-compile support #1406

Merged
merged 25 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ca4cef1
Add crytic-compile support (WIP)
montyly Apr 18, 2019
9178a11
Add crytic-compile to travis
montyly Apr 18, 2019
9afe3ba
Fix incorrect indentation
montyly Apr 18, 2019
8077e67
Ethereum: Add suffix '.sol' to temporary file
montyly Apr 19, 2019
15f4deb
Improve libraries support (including with Solidity > 0.5)
montyly Apr 19, 2019
a82fb13
Update to crytic-compile 7dc9b7174d7c677d146f01771c974a560108e69e
montyly Apr 19, 2019
708c81a
Update to crytic-compile e2c9b45c8f08f4d46e71dd48d5af98c063fd68dd
montyly Apr 22, 2019
c4ec90c
Refactor _compile
montyly Apr 22, 2019
4afeff8
Merge all compilation options to crytic-compile options
montyly Apr 22, 2019
79b008c
Update to crytic-compile 6e39d46b93eb743316148d8ff80ec1d2a2045616
montyly Apr 22, 2019
8fa3f41
Fix incorrect kwargs parsing + handle relative working dir
montyly Apr 22, 2019
d1669e9
Minor fixes
montyly Apr 22, 2019
0d4246b
Fix codeclimate issues
montyly Apr 22, 2019
cca60af
Update to crytic-compile d1f58ebeea2493dfd1b47761603767ec05597b4f
montyly Apr 24, 2019
aef8fca
Minor commit (enable new travis)
montyly May 7, 2019
53ad6da
Merge branch 'master' into dev-crytic-compile
montyly May 23, 2019
4fcf297
Improve erroe message
montyly May 23, 2019
7e2b4ca
Merge branch 'master' into dev-crytic-compile
montyly Jun 27, 2019
8c19b7d
Fix merging issue
montyly Jun 27, 2019
e453900
Run black
montyly Jun 27, 2019
c8f26ee
Merge branch 'master' into dev-crytic-compile
montyly Jul 1, 2019
52a1447
Add truffle test
montyly Jul 2, 2019
3ca3ca8
Add missing call to run_truffle_tests
montyly Jul 2, 2019
736abfa
Add missing return statement
montyly Jul 2, 2019
5213781
Update travis test (consider missing EIP145 support https://github.co…
montyly Jul 2, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion manticore/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pkg_resources

from crytic_compile import is_supported, cryticparser
from .core.manticore import ManticoreBase, set_verbosity
from .ethereum.cli import ethereum_main
from .utils import config, log, install_helper
Expand Down Expand Up @@ -36,7 +37,7 @@ def main():

set_verbosity(args.v)

if args.argv[0].endswith(".sol"):
if args.argv[0].endswith(".sol") or is_supported(args.argv[0]):
ethereum_main(args, logger)
else:
install_helper.ensure_native_deps()
Expand All @@ -55,6 +56,11 @@ def positive(value):
prog="manticore",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)

# Add crytic compile arguments
# See https://github.com/crytic/crytic-compile/wiki/Configuration
cryticparser.init(parser)

parser.add_argument("--context", type=str, default=None, help=argparse.SUPPRESS)
parser.add_argument(
"--coverage", type=str, default="visited.txt", help="Where to write the coverage data"
Expand Down
1 change: 1 addition & 0 deletions manticore/ethereum/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def ethereum_main(args, logger):
tx_send_ether=not args.txnoether,
tx_account=args.txaccount,
tx_preconstrain=args.txpreconstrain,
crytic_compile_args=vars(args),
)

if not args.no_testcases:
Expand Down
183 changes: 104 additions & 79 deletions manticore/ethereum/manticore.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import sha3
import tempfile

from crytic_compile import CryticCompile, InvalidCompilation, is_supported

from ..core.manticore import ManticoreBase
from ..core.smtlib import (
ConstraintSet,
Expand All @@ -38,6 +40,7 @@
from ..utils.helpers import PickleSerializer

logger = logging.getLogger(__name__)
logging.getLogger("CryticCompile").setLevel(logging.ERROR)

cfg = config.get_group("evm")
cfg.add("defaultgas", 3000000, "Default gas value for ethereum transactions.")
Expand Down Expand Up @@ -194,16 +197,11 @@ def constrain(self, constraint):

@staticmethod
def compile(
source_code,
contract_name=None,
libraries=None,
runtime=False,
solc_bin=None,
solc_remaps=[],
source_code, contract_name=None, libraries=None, runtime=False, crytic_compile_args=dict()
):
""" Get initialization bytecode from a Solidity source code """
name, source_code, init_bytecode, runtime_bytecode, srcmap, srcmap_runtime, hashes, abi, warnings = ManticoreEVM._compile(
source_code, contract_name, libraries, solc_bin, solc_remaps
source_code, contract_name, libraries, crytic_compile_args
)
if runtime:
return runtime_bytecode
Expand Down Expand Up @@ -332,69 +330,95 @@ def _run_solc(source_file, solc_bin=None, solc_remaps=[], working_dir=None):
raise EthereumError("Solidity compilation error:\n\n{}".format(stderr))

@staticmethod
def _compile(
source_code, contract_name, libraries=None, solc_bin=None, solc_remaps=[], working_dir=None
):
def _compile_through_crytic_compile(filename, contract_name, libraries, crytic_compile_args):
"""
:param filename: filename to compile
:param contract_name: contract to extract
:param libraries: an itemizable of pairs (library_name, address)
:param crytic_compile_args: crytic compile options (https://github.com/crytic/crytic-compile/wiki/Configuration)
:type crytic_compile_args: dict
:return:
"""
try:

if crytic_compile_args:
crytic_compile = CryticCompile(filename, **crytic_compile_args)
else:
crytic_compile = CryticCompile(filename)

if not contract_name:
if len(crytic_compile.contracts_names_without_libraries) > 1:
raise EthereumError(
f"Solidity file must contain exactly one contract or you must use a `--contract` parameter to specify one. Contracts found: {', '.join(crytic_compile.contracts_names)}"
)
contract_name = list(crytic_compile.contracts_names_without_libraries)[0]

if contract_name not in crytic_compile.contracts_names:
raise ValueError(f"Specified contract not found: {contract_name}")

name = contract_name

libs = crytic_compile.libraries_names(name)
if libraries:
libs = [l for l in libs if l not in libraries]
if libs:
raise DependencyError(libs)

bytecode = bytes.fromhex(crytic_compile.bytecode_init(name, libraries))
runtime = bytes.fromhex(crytic_compile.bytecode_runtime(name, libraries))
srcmap = crytic_compile.srcmap_init(name)
srcmap_runtime = crytic_compile.srcmap_runtime(name)
hashes = crytic_compile.hashes(name)
abi = crytic_compile.abi(name)

filename = crytic_compile.filename_of_contract(name).absolute
with open(filename) as f:
source_code = f.read()

return name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi

except InvalidCompilation as e:
raise EthereumError(
f"Errors : {e}\n. Solidity failed to generate bytecode for your contract. Check if all the abstract functions are implemented. "
)

@staticmethod
def _compile(source_code, contract_name, libraries=None, crytic_compile_args=None):
""" Compile a Solidity contract, used internally

:param source_code: solidity source as either a string or a file handle
:param source_code: solidity source
:type source_code: string (filename, directory, etherscan address) or a file handle
:param contract_name: a string with the name of the contract to analyze
:param libraries: an itemizable of pairs (library_name, address)
:param solc_bin: path to solc binary
:param solc_remaps: solc import remaps
:param working_dir: working directory for solc compilation (defaults to current)
:param crytic_compile_args: crytic compile options (https://github.com/crytic/crytic-compile/wiki/Configuration)
:type crytic_compile_args: dict
:return: name, source_code, bytecode, srcmap, srcmap_runtime, hashes
:return: name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi, warnings
"""

if isinstance(source_code, str):
with tempfile.NamedTemporaryFile("w+") as temp:
crytic_compile_args = dict() if crytic_compile_args is None else crytic_compile_args

if isinstance(source_code, io.IOBase):
source_code = source_code.name

if isinstance(source_code, str) and not is_supported(source_code):
with tempfile.NamedTemporaryFile("w+", suffix=".sol") as temp:
temp.write(source_code)
temp.flush()
output, warnings = ManticoreEVM._run_solc(
temp, solc_bin, solc_remaps, working_dir=working_dir
compilation_result = ManticoreEVM._compile_through_crytic_compile(
temp.name, contract_name, libraries, crytic_compile_args
)
elif isinstance(source_code, io.IOBase):
output, warnings = ManticoreEVM._run_solc(
source_code, solc_bin, solc_remaps, working_dir=working_dir
)
source_code.seek(0)
source_code = source_code.read()
else:
raise TypeError(f"source code bad type: {type(source_code).__name__}")

contracts = output.get("contracts", [])
if len(contracts) != 1 and contract_name is None:
raise EthereumError(
f'Solidity file must contain exactly one contract or you must use a `--contract` parameter to specify one. Contracts found: {", ".join(contracts)}'
compilation_result = ManticoreEVM._compile_through_crytic_compile(
source_code, contract_name, libraries, crytic_compile_args
)

name, contract = None, None
if contract_name is None:
name, contract = list(contracts.items())[0]
else:
for n, c in contracts.items():
if n == contract_name or n.split(":")[1] == contract_name:
name, contract = n, c
break

if name is None:
raise ValueError(f"Specified contract not found: {contract_name}")

name = name.split(":")[1]

if contract["bin"] == "":
raise EthereumError(
"Solidity failed to generate bytecode for your contract. Check if all the abstract functions are implemented"
)
name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi = (
compilation_result
)
warnings = ""

bytecode = ManticoreEVM._link(contract["bin"], libraries)
srcmap = contract["srcmap"].split(";")
srcmap_runtime = contract["srcmap-runtime"].split(";")
hashes = {str(x): str(y) for x, y in contract["hashes"].items()}
abi = json.loads(contract["abi"])
runtime = ManticoreEVM._link(contract["bin-runtime"], libraries)
return (name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi, warnings)
return name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi, warnings

@property
def accounts(self):
Expand Down Expand Up @@ -604,7 +628,9 @@ def json_create_contract(
signature = SolidityMetadata.function_signature_for_name_and_inputs(
item["name"], item["inputs"]
)
hashes[signature] = sha3.keccak_256(signature.encode()).hexdigest()[:8]
hashes[signature] = int(
"0x" + sha3.keccak_256(signature.encode()).hexdigest()[:8], 16
)
if "signature" in item:
if item["signature"] != f"0x{hashes[signature]}":
raise Exception(
Expand Down Expand Up @@ -671,14 +697,13 @@ def solidity_create_contract(
balance=0,
address=None,
args=(),
solc_bin=None,
solc_remaps=[],
working_dir=None,
gas=None,
crytic_compile_args=None,
):
""" Creates a solidity contract and library dependencies

:param str source_code: solidity source code
:param source_code: solidity source code
:type source_code: string (filename, directory, etherscan address) or a file handle
:param owner: owner account (will be default caller in any transactions)
:type owner: int or EVMAccount
:param contract_name: Name of the contract to analyze (optional if there is a single one in the source code)
Expand All @@ -688,16 +713,15 @@ def solidity_create_contract(
:param address: the address for the new contract (optional)
:type address: int or EVMAccount
:param tuple args: constructor arguments
:param solc_bin: path to solc binary
:type solc_bin: str
:param solc_remaps: solc import remaps
:type solc_remaps: list of str
:param working_dir: working directory for solc compilation (defaults to current)
:type working_dir: str
:param crytic_compile_args: crytic compile options (https://github.com/crytic/crytic-compile/wiki/Configuration)
:type crytic_compile_args: dict
:param gas: gas budget for each contract creation needed (may be more than one if several related contracts defined in the solidity source)
:type gas: int
:rtype: EVMAccount
"""

crytic_compile_args = dict() if crytic_compile_args is None else crytic_compile_args

if libraries is None:
deps = {}
else:
Expand All @@ -711,9 +735,7 @@ def solidity_create_contract(
source_code,
contract_name_i,
libraries=deps,
solc_bin=solc_bin,
solc_remaps=solc_remaps,
working_dir=working_dir,
crytic_compile_args=crytic_compile_args,
)
md = SolidityMetadata(*compile_results)
if contract_name_i == contract_name:
Expand Down Expand Up @@ -755,12 +777,16 @@ def solidity_create_contract(
raise EthereumError("Failed to build contract %s" % contract_name_i)
self.metadata[int(contract_account)] = md

deps[contract_name_i] = contract_account
deps[contract_name_i] = int(contract_account)
except DependencyError as e:
contract_names.append(contract_name_i)
for lib_name in e.lib_names:
if lib_name not in deps:
contract_names.append(lib_name)
except EthereumError as e:
logger.error(e)
self.kill()
raise
except Exception as e:
self.kill()
raise
Expand Down Expand Up @@ -1130,28 +1156,27 @@ def preconstraint_for_call_transaction(
def multi_tx_analysis(
self,
solidity_filename,
working_dir=None,
contract_name=None,
tx_limit=None,
tx_use_coverage=True,
tx_send_ether=True,
tx_account="attacker",
tx_preconstrain=False,
args=None,
crytic_compile_args=dict(),
):
owner_account = self.create_account(balance=1000, name="owner")
attacker_account = self.create_account(balance=1000, name="attacker")
# Pretty print
logger.info("Starting symbolic create contract")

with open(solidity_filename) as f:
contract_account = self.solidity_create_contract(
f,
contract_name=contract_name,
owner=owner_account,
args=args,
working_dir=working_dir,
)
contract_account = self.solidity_create_contract(
solidity_filename,
contract_name=contract_name,
owner=owner_account,
args=args,
crytic_compile_args=crytic_compile_args,
)

if tx_account == "attacker":
tx_account = [attacker_account]
Expand Down
2 changes: 1 addition & 1 deletion manticore/ethereum/solidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
self._runtime_bytecode = runtime_bytecode

self._function_signatures_by_selector = {
bytes.fromhex(sel): sig for sig, sel in hashes.items()
bytes.fromhex("{:08x}".format(sel)): sig for sig, sel in hashes.items()
}

fallback_selector = b"\0\0\0\0"
Expand Down
25 changes: 24 additions & 1 deletion scripts/travis_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ make_vmtests(){
cd $DIR
}

install_truffle(){
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
source ~/.nvm/nvm.sh
nvm install --lts
nvm use --lts

npm install -g truffle
}

run_truffle_tests(){
mkdir truffle_tests
cd truffle_tests
truffle unbox metacoin
manticore . --contract MetaCoin --workspace output
if [ "$(ls output/*tx -l | wc -l)" != "41" ]; then
echo "Truffle test failed"
return 1
fi
cd ..
}

run_tests_from_dir() {
DIR=$1
coverage erase
Expand Down Expand Up @@ -108,9 +129,11 @@ run_examples() {
case $1 in
ethereum_vm)
make_vmtests
install_truffle
RV=$?
echo "Running only the tests from 'tests/$1' directory"
run_tests_from_dir $1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@montyly Are you missing a call to run_truffle_tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, my bad

RV=$?
RV=$(($RV + $?))
;;

native) ;& # Fallthrough
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def rtd_dependent_deps():
"pyevmasm==0.2.0",
"rlp",
"ply",
"crytic-compile>=0.1.1",
]
+ rtd_dependent_deps(),
extras_require=extra_require,
Expand Down
Loading