crytic / slither

Static Analyzer for Solidity and Vyper
https://blog.trailofbits.com/2018/10/19/slither-a-solidity-static-analysis-framework/
GNU Affero General Public License v3.0
5.34k stars 969 forks source link

InvalidCompilation errors are verbose and sometimes obscure the underlying issue #2011

Open 0xalpharush opened 1 year ago

0xalpharush commented 1 year ago

I think we need to rethink this because it causes stuff like parser errors, wrong solc version, or any error in crytic-compile to be shown twice. It's very verbose and obfuscates the issue (e.g. it looks like it's a slither or crytic-compile error when in fact it may have to do with a missing comma in Solidity) https://github.com/crytic/slither/blob/3d4f934d3228f072b7df2c5e7252c64df4601bc8/slither/__main__.py#L873-L877

For example

Traceback (most recent call last):
  File "/Users/alpharush/env/lib/python3.9/site-packages/slither/__main__.py", line 814, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/Users/alpharush/env/lib/python3.9/site-packages/slither/__main__.py", line 91, in process_all
    compilations = compile_all(target, **vars(args))
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 646, in compile_all
    compilations.append(CryticCompile(target, **kwargs))
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 131, in __init__
    self._compile(**kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 553, in _compile
    self._platform.compile(self, **kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 149, in compile
    targets_json = _get_targets_json(compilation_unit, self._target, **kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 278, in _get_targets_json
    return _run_solc(
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 495, in _run_solc
    compiler="solc", version=get_version(solc, env), optimized=is_optimized(solc_arguments)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 393, in get_version
    raise InvalidCompilation(
crytic_compile.platform.exceptions.InvalidCompilation: 
Solidity version not found:
STDOUT:

STDERR:
Traceback (most recent call last):
  File "/Users/alpharush/env/bin/solc", line 8, in <module>
    sys.exit(solc())
  File "/Users/alpharush/env/lib/python3.9/site-packages/solc_select/__main__.py", line 80, in solc
    res = current_version()
  File "/Users/alpharush/env/lib/python3.9/site-packages/solc_select/solc_select.py", line 62, in current_version
    raise argparse.ArgumentTypeError(
argparse.ArgumentTypeError: No solc version set. Run `solc-select use VERSION` or set SOLC_VERSION environment variable.

ERROR:root:Error in test3.sol
ERROR:root:Traceback (most recent call last):
  File "/Users/alpharush/env/lib/python3.9/site-packages/slither/__main__.py", line 814, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/Users/alpharush/env/lib/python3.9/site-packages/slither/__main__.py", line 91, in process_all
    compilations = compile_all(target, **vars(args))
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 646, in compile_all
    compilations.append(CryticCompile(target, **kwargs))
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 131, in __init__
    self._compile(**kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 553, in _compile
    self._platform.compile(self, **kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 149, in compile
    targets_json = _get_targets_json(compilation_unit, self._target, **kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 278, in _get_targets_json
    return _run_solc(
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 495, in _run_solc
    compiler="solc", version=get_version(solc, env), optimized=is_optimized(solc_arguments)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 393, in get_version
    raise InvalidCompilation(
crytic_compile.platform.exceptions.InvalidCompilation: 
Solidity version not found:
STDOUT:

STDERR:
Traceback (most recent call last):
  File "/Users/alpharush/env/bin/solc", line 8, in <module>
    sys.exit(solc())
  File "/Users/alpharush/env/lib/python3.9/site-packages/solc_select/__main__.py", line 80, in solc
    res = current_version()
  File "/Users/alpharush/env/lib/python3.9/site-packages/solc_select/solc_select.py", line 62, in current_version
    raise argparse.ArgumentTypeError(
argparse.ArgumentTypeError: No solc version set. Run `solc-select use VERSION` or set SOLC_VERSION environment variable.
0xalpharush commented 1 year ago

Another example

'solc --version' running
'solc test3.sol --combined-json abi,ast,bin,bin-runtime,srcmap,srcmap-runtime,userdoc,devdoc,hashes --allow-paths .,/Users/alpharush' running
Compilation warnings/errors on test3.sol:
Error: Source file requires different compiler version (current compiler is 0.8.20+commit.a1b79de6.Darwin.appleclang) - note that nightly builds are considered to be strictly less than the released version
 --> test3.sol:1:1:
  |
1 | pragma solidity >=0.4.0 <0.7.0;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Traceback (most recent call last):
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 575, in _run_solc
    ret: Dict = json.loads(stdout)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/alpharush/env/lib/python3.9/site-packages/slither/__main__.py", line 814, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/Users/alpharush/env/lib/python3.9/site-packages/slither/__main__.py", line 91, in process_all
    compilations = compile_all(target, **vars(args))
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 646, in compile_all
    compilations.append(CryticCompile(target, **kwargs))
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 131, in __init__
    self._compile(**kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 553, in _compile
    self._platform.compile(self, **kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 149, in compile
    targets_json = _get_targets_json(compilation_unit, self._target, **kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 278, in _get_targets_json
    return _run_solc(
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 579, in _run_solc
    raise InvalidCompilation(f"Invalid solc compilation {stderr}")
crytic_compile.platform.exceptions.InvalidCompilation: Invalid solc compilation Error: Source file requires different compiler version (current compiler is 0.8.20+commit.a1b79de6.Darwin.appleclang) - note that nightly builds are considered to be strictly less than the released version
 --> test3.sol:1:1:
  |
1 | pragma solidity >=0.4.0 <0.7.0;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ERROR:root:Error in test3.sol
ERROR:root:Traceback (most recent call last):
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 575, in _run_solc
    ret: Dict = json.loads(stdout)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/alpharush/env/lib/python3.9/site-packages/slither/__main__.py", line 814, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/Users/alpharush/env/lib/python3.9/site-packages/slither/__main__.py", line 91, in process_all
    compilations = compile_all(target, **vars(args))
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 646, in compile_all
    compilations.append(CryticCompile(target, **kwargs))
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 131, in __init__
    self._compile(**kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 553, in _compile
    self._platform.compile(self, **kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 149, in compile
    targets_json = _get_targets_json(compilation_unit, self._target, **kwargs)
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 278, in _get_targets_json
    return _run_solc(
  File "/Users/alpharush/env/lib/python3.9/site-packages/crytic_compile/platform/solc.py", line 579, in _run_solc
    raise InvalidCompilation(f"Invalid solc compilation {stderr}")
crytic_compile.platform.exceptions.InvalidCompilation: Invalid solc compilation Error: Source file requires different compiler version (current compiler is 0.8.20+commit.a1b79de6.Darwin.appleclang) - note that nightly builds are considered to be strictly less than the released version
 --> test3.sol:1:1:
  |
1 | pragma solidity >=0.4.0 <0.7.0;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0xalpharush commented 2 months ago

The PR removed too much info and impaired users debugging and reporting issues https://github.com/crytic/slither/issues/2525