From 9e604a008a807d5faa08a850d4c229170946d3c5 Mon Sep 17 00:00:00 2001 From: step21 Date: Fri, 2 Feb 2018 02:45:46 +0100 Subject: [PATCH 1/5] solc versioning --- myth | 33 +++++++++++++++++++++++++++------ mythril/ether/util.py | 7 +++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/myth b/myth index bd1fbbf8..c5f818f5 100755 --- a/myth +++ b/myth @@ -22,6 +22,9 @@ from web3 import Web3 from ethereum import utils from pathlib import Path from json.decoder import JSONDecodeError +from solc.exceptions import SolcError +import semantic_version +import solc import logging import json import sys @@ -64,6 +67,7 @@ utils.add_argument('-d', '--disassemble', action='store_true', help='print disa utils.add_argument('--xrefs', action='store_true', help='get xrefs from a contract') utils.add_argument('--hash', help='calculate function signature hash', metavar='SIGNATURE') utils.add_argument('--storage', help='read state variables from storage index, use with -a', metavar='INDEX,NUM_SLOTS,[array]') +utils.add_argument('--solv', help='specify solidity compiler version. If not present, will try to install it (Experimental)', metavar='SOLV') options = parser.add_argument_group('options') options.add_argument('--sync-all', action='store_true', help='Also sync contracts with zero balance') @@ -87,12 +91,6 @@ try: except KeyError: mythril_dir = os.path.join(os.path.expanduser('~'), ".mythril") -try: - solc_binary = os.environ['SOLC'] -except KeyError: - solc_binary = 'solc' - - # Initialize data directry and singature database if not os.path.exists(mythril_dir): @@ -148,6 +146,29 @@ if args.truffle: sys.exit() +# Figure out solc binary and version +# Only proper versions are supported. No nightlies, commits etc (such as available in remix) + +if args.solv: + version = args.solv + #tried converting input to semver, seemed not necessary so just slicing for now + if version == str(solc.main.get_solc_version())[:6]: + print('Given version matches installed version') + try: + solc_binary = os.environ['SOLC'] + except KeyError: + solc_binary = 'solc' + else: + if util.solc_exists(version): + solc_binary = os.environ['HOME'] + "/.py-solc/solc-v" + version + "/bin/solc" + print("Setting the compiler to " + str(solc_binary)) + else: + try: + solc.install_solc('v' + version) + solc_binary = os.environ['HOME'] + "/.py-solc/solc-v" + version + "/bin/solc" + print("Setting the compiler to " + str(solc_binary)) + except SolcError: + print("There was an error when trying to install the specified solc version") # Establish RPC/IPC connection if necessary diff --git a/mythril/ether/util.py b/mythril/ether/util.py index 9ebc75d9..a132fca4 100644 --- a/mythril/ether/util.py +++ b/mythril/ether/util.py @@ -64,3 +64,10 @@ def get_random_address(): def get_indexed_address(index): return "0x" + (hex(index)[2:] * 40) +def solc_exists(version): + solc_binary = os.environ['HOME'] + "/.py-solc/solc-v" + version + "/bin/solc" + if os.path.exists(solc_binary): + return True + else: + return False + \ No newline at end of file From 97c33849430e97e784d6a31c3a122cfb74a0cfc9 Mon Sep 17 00:00:00 2001 From: step21 Date: Sun, 4 Feb 2018 09:26:02 +0100 Subject: [PATCH 2/5] added solc to requirements and setup.py --- requirements.txt | 1 + setup.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 7e302ad0..1e413531 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ web3 laser-ethereum==0.4.0 requests BTrees +py-solc diff --git a/setup.py b/setup.py index 43ac5499..dfde4af8 100755 --- a/setup.py +++ b/setup.py @@ -293,7 +293,8 @@ setup( 'z3-solver>=4.5', 'laser-ethereum==0.4.0', 'requests', - 'BTrees' + 'BTrees', + 'py-solc' ], python_requires='>=3.5', From b4968b97327fa5644f3e6b38a36e97c1381ff25b Mon Sep 17 00:00:00 2001 From: step21 Date: Sun, 4 Feb 2018 10:59:01 +0100 Subject: [PATCH 3/5] removed semver, used os-path.join, more correct and robust checking for compiler --- myth | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/myth b/myth index f21e4e43..9a10969b 100755 --- a/myth +++ b/myth @@ -23,7 +23,6 @@ from ethereum import utils from pathlib import Path from json.decoder import JSONDecodeError from solc.exceptions import SolcError -import semantic_version import solc import logging import json @@ -160,15 +159,23 @@ if args.solv: solc_binary = 'solc' else: if util.solc_exists(version): - solc_binary = os.environ['HOME'] + "/.py-solc/solc-v" + version + "/bin/solc" + solc_binary = os.join(os.environ['HOME'], "/.py-solc/solc-v", version, "/bin/solc") print("Setting the compiler to " + str(solc_binary)) else: try: solc.install_solc('v' + version) - solc_binary = os.environ['HOME'] + "/.py-solc/solc-v" + version + "/bin/solc" + solc_binary = os.join(os.environ['HOME'], "/.py-solc/solc-v", version, "/bin/solc") print("Setting the compiler to " + str(solc_binary)) except SolcError: - print("There was an error when trying to install the specified solc version") + exitWithError("There was an error when trying to install the specified solc version") +else: + try: + solc_binary = os.environ['SOLC'] + except KeyError: + try: + solc_binary = 'solc' + except: + exitWithError('No solidity compiler found, please make sure it is installed or specify it manually.') # Establish RPC/IPC connection if necessary From 56ca0ba4b43403907031e8b5cbaff71cb98c5324 Mon Sep 17 00:00:00 2001 From: step21 Date: Sun, 4 Feb 2018 11:08:34 +0100 Subject: [PATCH 4/5] used os.path.join in helper function --- mythril/ether/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mythril/ether/util.py b/mythril/ether/util.py index a132fca4..62f1890a 100644 --- a/mythril/ether/util.py +++ b/mythril/ether/util.py @@ -65,7 +65,7 @@ def get_indexed_address(index): return "0x" + (hex(index)[2:] * 40) def solc_exists(version): - solc_binary = os.environ['HOME'] + "/.py-solc/solc-v" + version + "/bin/solc" + solc_binary = os.join(os.environ['HOME'], "/.py-solc/solc-v", version, "/bin/solc") if os.path.exists(solc_binary): return True else: From 5403fc9148acf34fcd2e3780786879c739e24df3 Mon Sep 17 00:00:00 2001 From: step21 Date: Sun, 4 Feb 2018 11:23:41 +0100 Subject: [PATCH 5/5] fixed os.path.join --- myth | 18 ++++++++++-------- mythril/ether/util.py | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/myth b/myth index 9a10969b..e1dca48b 100755 --- a/myth +++ b/myth @@ -159,23 +159,25 @@ if args.solv: solc_binary = 'solc' else: if util.solc_exists(version): - solc_binary = os.join(os.environ['HOME'], "/.py-solc/solc-v", version, "/bin/solc") + print('Given version is already installed') + solc_binary = os.path.join(os.environ['HOME'], ".py-solc/solc-v" + version, "bin/solc") print("Setting the compiler to " + str(solc_binary)) else: try: solc.install_solc('v' + version) - solc_binary = os.join(os.environ['HOME'], "/.py-solc/solc-v", version, "/bin/solc") + solc_binary = os.path.join(os.environ['HOME'], ".py-solc/solc-v" + version, "bin/solc") print("Setting the compiler to " + str(solc_binary)) except SolcError: exitWithError("There was an error when trying to install the specified solc version") else: - try: - solc_binary = os.environ['SOLC'] - except KeyError: + if not args.solv: try: - solc_binary = 'solc' - except: - exitWithError('No solidity compiler found, please make sure it is installed or specify it manually.') + solc_binary = os.environ['SOLC'] + except KeyError: + try: + solc_binary = 'solc' + except: + exitWithError('No solidity compiler found, please make sure it is installed or specify it manually.') # Establish RPC/IPC connection if necessary diff --git a/mythril/ether/util.py b/mythril/ether/util.py index 62f1890a..693749bf 100644 --- a/mythril/ether/util.py +++ b/mythril/ether/util.py @@ -65,7 +65,7 @@ def get_indexed_address(index): return "0x" + (hex(index)[2:] * 40) def solc_exists(version): - solc_binary = os.join(os.environ['HOME'], "/.py-solc/solc-v", version, "/bin/solc") + solc_binary = os.path.join(os.environ['HOME'], ".py-solc/solc-v" + version, "bin/solc") if os.path.exists(solc_binary): return True else: