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.37k stars 975 forks source link

slither fails to resolve imports in dependency due to issue in foundry #2483

Open alphastorm opened 5 months ago

alphastorm commented 5 months ago

Describe the issue:

forge is able to compile the contracts and run the test successfully, but slither fails to resolve the imports in the dependency even when the --ignore-compile flag is passed.

Code example to reproduce the issue:

Repo: https://github.com/alphastorm/slither-import-breaking

Steps:

Version:

0.10.3

Relevant log output:

    ~/Dev/slither-import-breaking    master  forge test                                                                                                 ✔  cove-contracts-boosties 3.9.17   01:59:22 PM 
[⠊] Compiling...
[⠃] Compiling 28 files with Solc 0.8.23
[⠊] Solc 0.8.23 finished in 869.49ms
Compiler run successful!

Ran 2 tests for test/Counter.t.sol:CounterTest
[PASS] testFuzz_SetNumber(uint256) (runs: 256, μ: 31154, ~: 31310)
[PASS] test_Increment() (gas: 31325)
Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 11.80ms (6.66ms CPU time)

Ran 1 test suite in 119.02ms (11.80ms CPU time): 2 tests passed, 0 failed, 0 skipped (2 total tests)
    ~/Dev/slither-import-breaking    master  forge clean                                                                                                ✔  cove-contracts-boosties 3.9.17   01:59:26 PM 
    ~/Dev/slither-import-breaking    master  forge build --build-info --skip '*/test/**' '*/script/**'                                                  ✔  cove-contracts-boosties 3.9.17   01:59:29 PM 
[⠊] Compiling...
[⠒] Compiling 5 files with Solc 0.8.23
[⠢] Solc 0.8.23 finished in 20.93ms
Compiler run successful!
    ~/Dev/slither-import-breaking    master  slither --version                                                                                          ✔  cove-contracts-boosties 3.9.17   01:59:33 PM 
0.10.3
    ~/Dev/slither-import-breaking    master  slither . --ignore-compile                                                                                 ✔  cove-contracts-boosties 3.9.17   01:59:38 PM 
--ignore-compile used, if something goes wrong, consider removing the ignore compile flag
Traceback (most recent call last):
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/bin/slither", line 8, in <module>
    sys.exit(main())
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/__main__.py", line 782, in main
    main_impl(all_detector_classes=detectors, all_printer_classes=printers)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/__main__.py", line 888, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/__main__.py", line 113, in process_all
    ) = process_single(compilation, args, detector_classes, printer_classes)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/__main__.py", line 80, in process_single
    slither = Slither(target, ast_format=ast, **vars(args))
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/slither.py", line 156, in __init__
    sol_parser.parse_top_level_items(ast, path)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 326, in parse_top_level_items
    get_imported_scope = self.compilation_unit.get_scope(import_directive.filename)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/core/compilation_unit.py", line 282, in get_scope
    filename = self._crytic_compile_compilation_unit.crytic_compile.filename_lookup(
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 283, in filename_lookup
    raise ValueError(f"{filename} does not exist")
ValueError: src/interfaces/IPriceOracle.sol does not exist
    ~/Dev/slither-import-breaking    master  slither .                                                                                                1 ✘  cove-contracts-boosties 3.9.17   01:59:43 PM 
'forge clean' running (wd: /Users/srs/Development/slither-import-breaking)
'forge config --json' running
'forge build --build-info --skip */test/** */script/** --force' running (wd: /Users/srs/Development/slither-import-breaking)
Traceback (most recent call last):
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/bin/slither", line 8, in <module>
    sys.exit(main())
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/__main__.py", line 782, in main
    main_impl(all_detector_classes=detectors, all_printer_classes=printers)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/__main__.py", line 888, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/__main__.py", line 113, in process_all
    ) = process_single(compilation, args, detector_classes, printer_classes)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/__main__.py", line 80, in process_single
    slither = Slither(target, ast_format=ast, **vars(args))
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/slither.py", line 156, in __init__
    sol_parser.parse_top_level_items(ast, path)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 326, in parse_top_level_items
    get_imported_scope = self.compilation_unit.get_scope(import_directive.filename)
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/slither/core/compilation_unit.py", line 282, in get_scope
    filename = self._crytic_compile_compilation_unit.crytic_compile.filename_lookup(
  File "/Users/srs/.pyenv/versions/cove-contracts-boosties/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 283, in filename_lookup
    raise ValueError(f"{filename} does not exist")
ValueError: src/interfaces/IPriceOracle.sol does not exist
DarkaMaul commented 5 months ago

Hi - thanks for your report.

I was able to reproduce the problem on my end.

It appears the problem comes from the {build_id}.json file:

While the input sources map in the document is correct :

 "input": {
    "language": "Solidity",
    "sources": {
      "lib/euler-price-oracle/src/adapter/BaseAdapter.sol": ... ,
      "lib/euler-price-oracle/src/interfaces/IPriceOracle.sol": ...,
      "lib/euler-price-oracle/src/lib/Errors.sol": ... , 
      "lib/forge-std/src/interfaces/IERC20.sol": ... ,
      "src/Counter.sol": ...
    },

The output/sources contains two more files whose ast are empty :

  "output": {
    "sources": {
      "lib/euler-price-oracle/src/adapter/BaseAdapter.sol": ...,
      "lib/euler-price-oracle/src/interfaces/IPriceOracle.sol": ...,
      "lib/euler-price-oracle/src/lib/Errors.sol": ...,
      "lib/forge-std/src/interfaces/IERC20.sol": ...,
      "src/Counter.sol":  ...,
      "src/interfaces/IPriceOracle.sol": {
        "id": 5,
        "ast": {}
      },
      "src/lib/Errors.sol": {
        "id": 6,
        "ast": {}
      }

Since those two files do not exist, slither fails to perform its analysis.

Remaining questions :

0xalpharush commented 5 months ago

I believe this is the same issue as reported in https://github.com/crytic/slither/issues/1622 and blocked on an issue in Foundry https://github.com/foundry-rs/foundry/issues/7591