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

Tracking: support for imports with alias (either symbol alias or unit alias) #1452

Closed
SheldonHolmgren opened this issue Nov 4, 2022 · 8 comments
Assignees
Labels
bug Something isn't working High Priority tracking

Comments

@SheldonHolmgren
Copy link
Contributor

Describe the issue:

See the linked repo with a minimal reproducible example. Running slither . --compile-force-framework foundry leads to the crash with a backtrace pasted as the log output below. This is because Slither cannot find a contract named c.Counter (as referenced from src/Importer.sol).

If I edit Importer.sol in the following way:

diff --git a/src/Importer.sol b/src/Importer.sol
index 316a7e3..5fbc2fe 100644
--- a/src/Importer.sol
+++ b/src/Importer.sol
@@ -2,10 +2,10 @@
 // SPDX-License-Identifier: UNLICENSED
 pragma solidity ^0.8.13;
 
-import "src/Counter.sol" as c;
+import "src/Counter.sol";
 
 contract Importer {
     constructor() {
-        new c.Counter();
+        new Counter();
     }
 }

slither runs without problem.

Code example to reproduce the issue:

https://github.com/SheldonHolmgren/slither_bug_example

Version:

0.9.0

Relevant log output:

'forge build --extra-output abi --extra-output userdoc --extra-output devdoc --extra-output evm.methodIdentifiers --force' running
Compiling 19 files with 0.8.16
Solc 0.8.16 finished in 1.52s
Compiler run successful

Traceback (most recent call last):
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/__main__.py", line 826, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/__main__.py", line 97, in process_all
    ) = process_single(compilation, args, detector_classes, printer_classes)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/__main__.py", line 75, in process_single
    slither = Slither(target, ast_format=ast, **vars(args))
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slither.py", line 130, in __init__
    parser.analyze_contracts()
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 503, in analyze_contracts
    self._convert_to_slithir()
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 688, in _convert_to_slithir
    func.generate_slithir_and_analyze()
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/core/declarations/function.py", line 1705, in generate_slithir_and_analyze
    node.slithir_generation()
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/core/cfg/node.py", line 719, in slithir_generation
    self._irs = convert_expression(expression, self)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slithir/convert.py", line 119, in convert_expression
    result = apply_ir_heuristics(result, node)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slithir/convert.py", line 1790, in apply_ir_heuristics
    irs = propagate_type_and_convert_call(irs, node)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slithir/convert.py", line 438, in propagate_type_and_convert_call
    new_ins = propagate_types(ins, node)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slithir/convert.py", line 743, in propagate_types
    ir.lvalue.set_type(UserDefinedType(contract))
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/core/solidity_types/user_defined_type.py", line 24, in __init__
    assert isinstance(t, (Contract, Enum, Structure))
AssertionError
Error in .
Traceback (most recent call last):
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/__main__.py", line 826, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/__main__.py", line 97, in process_all
    ) = process_single(compilation, args, detector_classes, printer_classes)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/__main__.py", line 75, in process_single
    slither = Slither(target, ast_format=ast, **vars(args))
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slither.py", line 130, in __init__
    parser.analyze_contracts()
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 503, in analyze_contracts
    self._convert_to_slithir()
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 688, in _convert_to_slithir
    func.generate_slithir_and_analyze()
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/core/declarations/function.py", line 1705, in generate_slithir_and_analyze
    node.slithir_generation()
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/core/cfg/node.py", line 719, in slithir_generation
    self._irs = convert_expression(expression, self)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slithir/convert.py", line 119, in convert_expression
    result = apply_ir_heuristics(result, node)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slithir/convert.py", line 1790, in apply_ir_heuristics
    irs = propagate_type_and_convert_call(irs, node)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slithir/convert.py", line 438, in propagate_type_and_convert_call
    new_ins = propagate_types(ins, node)
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/slithir/convert.py", line 743, in propagate_types
    ir.lvalue.set_type(UserDefinedType(contract))
  File "/home/holmgren/.local/lib/python3.10/site-packages/slither/core/solidity_types/user_defined_type.py", line 24, in __init__
    assert isinstance(t, (Contract, Enum, Structure))
AssertionError
@SheldonHolmgren SheldonHolmgren added the bug-candidate Bugs reports that are not yet confirmed label Nov 4, 2022
@0xalpharush
Copy link
Member

Thanks for creating a repo for reproducing this. We also need to address #1364

@0xalpharush
Copy link
Member

Example from here #1536 (comment)

@0xalpharush 0xalpharush changed the title [Bug-Candidate]: import "filename" as someAlias; doesn't work [Bug]: import "filename" as someAlias; doesn't work Feb 1, 2023
@0xalpharush 0xalpharush changed the title [Bug]: import "filename" as someAlias; doesn't work Tracking: support for imports with alias (either symbol alias or unit alias) Feb 1, 2023
@0xalpharush
Copy link
Member

somewhat related #1809

@singhpreetileo90
Copy link

Made certain changes to address the issue :

Changes to solc_parsing/declarations/convert.py : still getting error -

ERROR:ContractSolcParsing:Impossible to generate IR for Importer.constructor (Importer.sol#9-12):
'NoneType' object has no attribute 'references'

# Provide an additional function for handling 'import ... as ...' 
def parse_import_alias(self, stmt):
    import_alias_node = ast.AST.ImportAlias()
    import_alias_node.name = stmt['name']
    import_alias_node.alias = stmt['alias']
    return import_alias_node

# Use the new function parse_import to check for the 'as' keyword
def parse_import(self, stmt):
    import_node = ast.AST.Import()
    import_node.path = stmt['path']
    if 'as' in stmt:
        import_node.alias = self.parse_import_alias(stmt['as'])
    return import_node

@0xAurelou
Copy link

Any work on going on this PR ?

@0xalpharush
Copy link
Member

Some work was started here but it is a large undertaking to get right and requires more testing #2250

@0xalpharush
Copy link
Member

closed by #2403

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working High Priority tracking
Projects
None yet
Development

No branches or pull requests

4 participants