Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 32 additions & 0 deletions mythx_cli/analyze/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@
default=None,
help="Path to a custom solc executable",
)
@click.option(
"--solc-optimizer", type=click.BOOL, default=True, help="Enable the solc optimizer"
)
@click.option(
"--solc-optimizer-runs",
type=click.INT,
default=200,
help="Set the number of runs for the optimizer",
)
@click.option(
"--solc-via-ir",
type=click.BOOL,
default=False,
help="Set the solc compiler to run viaIR",
)
@click.option(
"--include",
type=click.STRING,
Expand Down Expand Up @@ -144,6 +159,9 @@ def analyze(
swc_whitelist: str,
solc_version: str,
solc_path: str,
solc_optimizer: bool,
solc_optimizer_runs: int,
solc_via_ir: bool,
include: Tuple[str],
remap_import: Tuple[str],
check_properties: bool,
Expand All @@ -167,6 +185,9 @@ def analyze(
:param swc_whitelist: A comma-separated list of SWC IDs to include
:param solc_version: The solc version to use for Solidity compilation
:param solc_path: The path to a custom solc executable
:param solc_optimizer: Enable the optimizer (default True)
:param solc_optimizer_runs: Set the number of runs for the optimizer (default 200)
:param solc_via_ir: Enable solc compilation viaIR (default False)
:param include: List of contract names to send - exclude everything else
:param remap_import: List of import remappings to pass on to solc
:param check_properties: Enable property verification mode
Expand All @@ -189,6 +210,11 @@ def analyze(
swc_blacklist = swc_blacklist or analyze_config.get("blacklist") or None
swc_whitelist = swc_whitelist or analyze_config.get("whitelist") or None
solc_version = solc_version or analyze_config.get("solc") or None
solc_optimizer = solc_optimizer or analyze_config.get("optimizer") or False
solc_optimizer_runs = (
solc_optimizer_runs or analyze_config.get("optimizer-runs") or 200
)
solc_via_ir = solc_via_ir or analyze_config.get("viaIR") or False
include = include or analyze_config.get("contracts") or []
remap_import = remap_import or analyze_config.get("remappings") or []
check_properties = (
Expand Down Expand Up @@ -235,6 +261,9 @@ def analyze(
SolidityJob.walk_solidity_files(
solc_version=solc_version,
solc_path=solc_path,
solc_optimizer=solc_optimizer,
solc_optimizer_runs=solc_optimizer_runs,
solc_via_ir=solc_via_ir,
base_path=element,
remappings=remap_import,
enable_scribble=enable_scribble,
Expand All @@ -252,6 +281,9 @@ def analyze(
job.generate_payloads(
version=solc_version,
solc_path=solc_path,
solc_optimizer=solc_optimizer,
solc_optimizer_runs=solc_optimizer_runs,
solc_via_ir=solc_via_ir,
contract=contract or None,
remappings=remap_import,
enable_scribble=enable_scribble,
Expand Down
27 changes: 26 additions & 1 deletion mythx_cli/analyze/solidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def solcx_compile(
enable_scribble: bool,
scribble_file: str = None,
solc_path: str = None,
solc_optimizer: bool = True,
solc_optimizer_runs: int = 200,
solc_via_ir: bool = False,
) -> Dict:
return solcx.compile_standard(
solc_binary=solc_path,
Expand Down Expand Up @@ -166,7 +169,11 @@ def solcx_compile(
"": ["ast"],
}
},
"optimizer": {"enabled": True, "runs": 200},
"optimizer": {
"enabled": solc_optimizer,
"runs": solc_optimizer_runs,
},
"viaIR": solc_via_ir,
},
},
# if scribble enabled, allow access to temporary file
Expand All @@ -177,6 +184,9 @@ def generate_payloads(
self,
version: Optional[str],
solc_path: Optional[str] = None,
solc_optimizer: Optional[bool] = True,
solc_optimizer_runs: Optional[int] = 200,
solc_via_ir: Optional[bool] = False,
contract: str = None,
remappings: Tuple[str] = None,
enable_scribble: bool = False,
Expand All @@ -201,6 +211,9 @@ def generate_payloads(

:param version: The solc version to use for compilation
:param solc_path: The path to a custom solc executable
:param solc_optimizer: Enable the optimizer (default True)
:param solc_optimizer_runs: Set the number of runs for the optimizer (default 200)
:param solc_via_ir: Enable solc compilation viaIR (default False)
:param contract: The contract name(s) to submit
:param remappings: Import remappings to pass to solcx
:param enable_scribble: Enable instrumentation with scribble
Expand Down Expand Up @@ -231,6 +244,9 @@ def generate_payloads(
remappings=remappings,
enable_scribble=enable_scribble,
solc_path=solc_path,
solc_optimizer=solc_optimizer,
solc_optimizer_runs=solc_optimizer_runs,
solc_via_ir=solc_via_ir,
)
except solcx.exceptions.SolcError as e:
raise click.exceptions.UsageError(
Expand Down Expand Up @@ -283,6 +299,9 @@ def walk_solidity_files(
cls,
solc_version: str,
solc_path: Optional[str] = None,
solc_optimizer: Optional[bool] = True,
solc_optimizer_runs: Optional[int] = 200,
solc_via_ir: Optional[bool] = False,
base_path: Optional[str] = None,
remappings: Tuple[str] = None,
enable_scribble: bool = False,
Expand All @@ -296,6 +315,9 @@ def walk_solidity_files(

:param solc_version: The solc version to use for Solidity compilation
:param solc_path: The path to a custom solc executable
:param solc_optimizer: Enable the optimizer (default True)
:param solc_optimizer_runs: Set the number of runs for the optimizer (default 200)
:param solc_via_ir: Enable solc compilation viaIR (default False)
:param base_path: The base path to walk through from
:param remappings: Import remappings to pass to solcx
:param enable_scribble: Enable instrumentation with scribble
Expand All @@ -321,6 +343,9 @@ def walk_solidity_files(
job.generate_payloads(
version=solc_version,
solc_path=solc_path,
solc_optimizer=solc_optimizer,
solc_optimizer_runs=solc_optimizer_runs,
solc_via_ir=solc_via_ir,
remappings=remappings,
enable_scribble=enable_scribble,
scribble_path=scribble_path,
Expand Down
46 changes: 46 additions & 0 deletions tests/test_analyze_solidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,52 @@ def test_config_solc_version(tmp_path):
assert result.exit_code == 0


def test_user_solc_optimizer(tmp_path):
setup_solidity_file(
tmp_path, name="outdated.sol", switch_dir=True, hide_pragma=True
)
runner = CliRunner()

with mock_context():
result = runner.invoke(
cli,
[
"--debug",
"analyze",
"--solc-version",
"0.4.13",
"--solc-optimizer",
"true",
"--solc-optimizer-runs",
"1000",
"--solc-via-ir",
"true",
"outdated.sol",
],
input="y\n",
)

assert ISSUES_TABLE in result.output
assert result.exit_code == 0


def test_config_solc_optimizer(tmp_path):
setup_solidity_file(
tmp_path, name="outdated.sol", switch_dir=True, hide_pragma=True
)
runner = CliRunner()
with open(".mythx.yml", "w+") as conf_f:
conf_f.write(
"analyze:\n solc: 0.4.13\n optimizer: true\n optimizer-runs: 1000\n viaIR: true\n"
)

with mock_context() as m:
result = runner.invoke(cli, ["--debug", "analyze", "outdated.sol"], input="y\n")

assert ISSUES_TABLE in result.output
assert result.exit_code == 0


def test_default_recursive_blacklist(tmp_path):
setup_solidity_file(tmp_path, name="outdated.sol", switch_dir=True)
runner = CliRunner()
Expand Down