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

AssertionError when setting type for UserDefinedType #1594

Closed
FredCoen opened this issue Jan 11, 2023 · 3 comments
Closed

AssertionError when setting type for UserDefinedType #1594

FredCoen opened this issue Jan 11, 2023 · 3 comments
Labels
bug Something isn't working High Priority

Comments

@FredCoen
Copy link

FredCoen commented Jan 11, 2023

Describe the issue:

After installing the latest slither version 0.9.2 I try to run slither . against my foundry project. unfrotunately I get the following trace below. Unfortunately the repo is currently closed source. Can I provide any more information?

Code example to reproduce the issue:
its a pretty standard foundry repo that cant be shared currently

Version:
0.9.2

Relevant log output:

'forge clean' running (wd: /Users/friedrichcoen/priv-contracts-v2)
'forge build --build-info --force' running
Compiling 196 files with 0.8.13
Solc 0.8.13 finished in 64.61s
Compiler run successful
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 834, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 98, in process_all
    ) = process_single(compilation, args, detector_classes, printer_classes)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 76, in process_single
    slither = Slither(target, ast_format=ast, **vars(args))
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slither.py", line 133, in init
    self._init_parsing_and_analyses(kwargs.get("skip_analyze", False))
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slither.py", line 153, in _init_parsing_and_analyses
    raise e
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slither.py", line 149, in _init_parsing_and_analyses
    parser.analyze_contracts()
  File "/opt/homebrew/lib/python3.9/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 529, in analyze_contracts
    self._convert_to_slithir()
  File "/opt/homebrew/lib/python3.9/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 740, in _convert_to_slithir
    func.generate_slithir_and_analyze()
  File "/opt/homebrew/lib/python3.9/site-packages/slither/core/declarations/function.py", line 1750, in generate_slithir_and_analyze
    node.slithir_generation()
  File "/opt/homebrew/lib/python3.9/site-packages/slither/core/cfg/node.py", line 719, in slithir_generation
    self._irs = convert_expression(expression, self)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slithir/convert.py", line 119, in convert_expression
    result = apply_ir_heuristics(result, node)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slithir/convert.py", line 1858, in apply_ir_heuristics
    irs = propagate_type_and_convert_call(irs, node)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slithir/convert.py", line 442, in propagate_type_and_convert_call
    new_ins = propagate_types(ins, node)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slithir/convert.py", line 774, in propagate_types
    ir.lvalue.set_type(UserDefinedType(contract))
  File "/opt/homebrew/lib/python3.9/site-packages/slither/core/solidity_types/user_defined_type.py", line 19, in init
    assert isinstance(t, (Contract, Enum, Structure))
AssertionError
Error in .
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 834, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 98, in process_all
    ) = process_single(compilation, args, detector_classes, printer_classes)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 76, in process_single
    slither = Slither(target, ast_format=ast, **vars(args))
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slither.py", line 133, in init
    self._init_parsing_and_analyses(kwargs.get("skip_analyze", False))
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slither.py", line 153, in _init_parsing_and_analyses
    raise e
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slither.py", line 149, in _init_parsing_and_analyses
    parser.analyze_contracts()
  File "/opt/homebrew/lib/python3.9/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 529, in analyze_contracts
    self._convert_to_slithir()
  File "/opt/homebrew/lib/python3.9/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 740, in _convert_to_slithir
    func.generate_slithir_and_analyze()
  File "/opt/homebrew/lib/python3.9/site-packages/slither/core/declarations/function.py", line 1750, in generate_slithir_and_analyze
    node.slithir_generation()
  File "/opt/homebrew/lib/python3.9/site-packages/slither/core/cfg/node.py", line 719, in slithir_generation

self._irs = convert_expression(expression, self)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slithir/convert.py", line 119, in convert_expression
    result = apply_ir_heuristics(result, node)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slithir/convert.py", line 1858, in apply_ir_heuristics
    irs = propagate_type_and_convert_call(irs, node)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slithir/convert.py", line 442, in propagate_type_and_convert_call
    new_ins = propagate_types(ins, node)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slithir/convert.py", line 774, in propagate_types
    ir.lvalue.set_type(UserDefinedType(contract))
  File "/opt/homebrew/lib/python3.9/site-packages/slither/core/solidity_types/user_defined_type.py", line 19, in init
    assert isinstance(t, (Contract, Enum, Structure))
AssertionError

@FredCoen FredCoen added the bug-candidate Bugs reports that are not yet confirmed label Jan 11, 2023
@0xalpharush 0xalpharush changed the title [Bug-Candidate]: AssertionError when setting type for UserDefinedType Jan 12, 2023
@0xalpharush 0xalpharush added bug Something isn't working and removed bug-candidate Bugs reports that are not yet confirmed labels Jan 12, 2023
@0xalpharush
Copy link
Contributor

0xalpharush commented Jan 12, 2023

Hey Fred, we chatted on telegram. Just leaving some notes... get_contract_from_name can return None, but that may mean something is off with scoping here... we will need to reproduce.

elif isinstance(ir, NewContract):
contract = node.file_scope.get_contract_from_name(ir.contract_name)
ir.lvalue.set_type(UserDefinedType(contract))

@0xalpharush
Copy link
Contributor

0xalpharush commented Jan 27, 2023

This is related to something like:

import {MyContract as MyAliasedContract} from "./MyContract.sol";

contract Test {
MyAliasedContract c;
    constructor {
        c = new MyAliasedContract();
    }
} 

I believe #1539 fixes this although I think we could improve alias support more broadly with a different approach than is currently implemented in Slither

@singhpreetileo90
Copy link

singhpreetileo90 commented Apr 3, 2023

Leaving some notes for #1594 and #1452 - I tried handling the the possibility of an AssertionError being raised during the creation of the user_defined_type object using try and except statement. and then I performed type checking and alias resolution on the t object as well as alias resolution for a given alias object and module as follows :

from typing import Union, TYPE_CHECKING, Tuple
import math
import ast
from slither.core.solidity_types.type import Type
from slither.exceptions import SlitherException
if TYPE_CHECKING:
from slither.core.declarations.structure import Structure
from slither.core.declarations.enum import Enum
from slither.core.declarations.contract import Contract

pylint: disable=import-outside-toplevel

class UserDefinedType(Type):
def init(self, t):
from slither.core.declarations.structure import Structure
from slither.core.declarations.enum import Enum
from slither.core.declarations.contract import Contract

    #assert isinstance(t, (Contract, Enum, Structure))
    #super().__init__()

    #handle the possibility of an 'AssertionError'
    try:
        class MyUserDefinedType(UserDefinedType):
            def __init__(self, x):
                if isinstance(x, (Contract, Enum, Structure)):
                    super().__init__(x)
                else:
                    self.x = t

        user_defined_type = MyUserDefinedType(t)

    except AssertionError:
        # Handle the error
        user_defined_type = None
    
    # Check if the given type is an alias
    type_check = str
    if hasattr(t, "name") and isinstance(t.name, type_check):
        print(t)
        alias_name = t.name
        for compilation_unit in t.contract.compilation_units:
            module = t.contract.resolver.resolve_unit(compilation_unit)
            alias = module.get_alias(alias_name)
            if alias is not None:
                t = alias.name

    print(f"t: {t}")
    self._type = t

@staticmethod
def alias_check(alias, module):
    if alias is None:
        return None

    if isinstance(alias, ast.Alias) and alias.kind == 'contract':
       instance = module.get_contract(alias.name)
    elif isinstance(alias, ast.Alias) and alias.kind == 'enum':
       instance = module.get_enum(alias.name)
    elif isinstance(alias, ast.Alias) and alias.kind == 'structure':
       instance = module.get_structure(alias.name)
    else:
       return None

    if instance is None:
       return None

    if isinstance(instance, ast.Alias):
       return UserDefinedType.alias_check(instance.alias, module)
    return instance.name

The output is :
t: None
Impossible to generate IR for Importer.constructor (Importer.sol#8-10):
'NoneType' object has no attribute 'references'

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
Projects
None yet
Development

No branches or pull requests

3 participants