-
Notifications
You must be signed in to change notification settings - Fork 996
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2367 from crytic/dev-fix-toplevelfn-ir
Fix IR for top level functions with using-for
- Loading branch information
Showing
8 changed files
with
116 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Dict, List, TYPE_CHECKING, Union | ||
from slither.core.solidity_types.type import Type | ||
|
||
if TYPE_CHECKING: | ||
from slither.core.declarations import Function | ||
|
||
USING_FOR_KEY = Union[str, Type] | ||
USING_FOR_ITEM = List[Union[Type, "Function"]] | ||
USING_FOR = Dict[USING_FOR_KEY, USING_FOR_ITEM] | ||
|
||
|
||
def _merge_using_for(uf1: USING_FOR, uf2: USING_FOR) -> USING_FOR: | ||
result = {**uf1, **uf2} | ||
for key, value in result.items(): | ||
if key in uf1 and key in uf2: | ||
result[key] = value + uf1[key] | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
pragma solidity 0.8.24; | ||
|
||
library Lib { | ||
function a(uint q) public {} | ||
} | ||
function c(uint z) {} | ||
|
||
using {Lib.a} for uint; | ||
using {c} for uint; | ||
|
||
function b(uint y) { | ||
Lib.a(4); | ||
y.c(); | ||
y.a(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from pathlib import Path | ||
from slither import Slither | ||
from slither.core.declarations.contract import Contract | ||
from slither.slithir.operations import LibraryCall, InternalCall | ||
|
||
TEST_DATA_DIR = Path(__file__).resolve().parent / "test_data" | ||
|
||
|
||
def test_top_level_using_for(solc_binary_path) -> None: | ||
solc_path = solc_binary_path("0.8.24") | ||
slither = Slither(Path(TEST_DATA_DIR, "top_level_using_for.sol").as_posix(), solc=solc_path) | ||
|
||
function = slither.compilation_units[0].functions_top_level[1] | ||
assert function.name == "b" | ||
|
||
# LIBRARY_CALL, dest:Lib, function:Lib.a(uint256), arguments:['4'] | ||
first_ir = function.slithir_operations[0] | ||
assert ( | ||
isinstance(first_ir, LibraryCall) | ||
and isinstance(first_ir.destination, Contract) | ||
and first_ir.destination.name == "Lib" | ||
and first_ir.function_name == "a" | ||
and len(first_ir.arguments) == 1 | ||
) | ||
|
||
# INTERNAL_CALL, c(uint256)(y) | ||
second_ir = function.slithir_operations[1] | ||
assert ( | ||
isinstance(second_ir, InternalCall) | ||
and second_ir.function_name == "c" | ||
and len(second_ir.arguments) == 1 | ||
and second_ir.arguments[0].name == "y" | ||
) | ||
|
||
# LIBRARY_CALL, dest:Lib, function:Lib.a(uint256), arguments:['y'] | ||
third_ir = function.slithir_operations[2] | ||
assert ( | ||
isinstance(third_ir, LibraryCall) | ||
and isinstance(third_ir.destination, Contract) | ||
and third_ir.destination.name == "Lib" | ||
and third_ir.function_name == "a" | ||
and len(third_ir.arguments) == 1 | ||
and third_ir.arguments[0].name == "y" | ||
) |