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

Adds a new printer for external calls. #2420

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open

Conversation

DarkaMaul
Copy link
Contributor

@DarkaMaul DarkaMaul commented Apr 11, 2024

Implements #2074

Sample output

For the contracts here

➜ slither --print external-calls tests/e2e/printers/test_data/test_external_calls/A.sol
'solc --version' running
'solc tests/e2e/printers/test_data/test_external_calls/A.sol --combined-json abi,ast,bin,bin-runtime,srcmap,srcmap-runtime,userdoc,devdoc,hashes --allow-paths .,/Users/dm/Projects/slither/tests/e2e/printers/test_data/test_external_calls' running
INFO:Printers:External calls
+---------------+------------------+------------+
| Source (Line) |      Destination |      Chain |
+---------------+------------------+------------+
| A.foo [10]    | IERC20.balanceOf |            |
| B.foo [10]    | IERC20.balanceOf | B.bar (19) |
| C.pop [28]    |            B.bar |            |
+---------------+------------------+------------+

This PR also has a partial fix for #2073 (only for Solidity)

Summary by CodeRabbit

  • New Features

    • Introduced ExternalCallPrinter to output detailed information on external calls in smart contracts.
    • Added a new Solidity test contract (A.sol) and ERC-20 interface (IERC20.sol) for enhanced testing scenarios.
  • Refactor

    • Improved the accuracy of external and internal call analysis in smart contracts.
  • Tests

    • Added tests for the new ExternalCallPrinter functionality to ensure reliable performance.

Copy link

coderabbitai bot commented Apr 11, 2024

Walkthrough

This update introduces a new feature to the Slither tool, allowing users to print a summary of all external calls made by functions in Solidity contracts. It adds the ExternalCallPrinter class to handle this functionality, updates the parsing logic for external calls, and includes new tests to ensure the feature works correctly.

Changes

Files/Modules Change Summary
slither/printers/all_printers.py Added import statement for ExternalCallPrinter.
slither/printers/summary/external_calls.py Introduced ExternalCallPrinter class to output information about external calls in contracts.
slither/solc_parsing/cfg/node.py Refactored the analyze_expressions method to improve differentiation between external and internal calls. Added import for SolidityVariable.
tests/e2e/printers/test_data/test_external_calls/A.sol Added Solidity contracts demonstrating functions for encoding data and calling internal functions.
tests/e2e/printers/test_data/test_external_calls/IERC20.sol Defined an interface for the ERC-20 standard, including functions for token transfers, approvals, and balance inquiries.
tests/e2e/printers/test_printers.py Added test_external_call_printers function to test the ExternalCallPrinter functionality.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant Slither
    participant ExternalCallPrinter
    participant SolidityContracts

    User->>Slither: Run Slither with --print external-calls
    Slither->>ExternalCallPrinter: Initialize ExternalCallPrinter
    ExternalCallPrinter->>SolidityContracts: Analyze contracts for external calls
    SolidityContracts->>ExternalCallPrinter: Return external call data
    ExternalCallPrinter->>Slither: Output external call summary
    Slither->>User: Display external call summary
Loading

Possibly related issues


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

def is_external_call(element: "Expression") -> bool:
if not isinstance(element.called, Identifier):
try:
return not isinstance(element.called.expression.value, SolidityVariable)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be able to be isinstance(element.called.value, SolidityVariable) and not require a try/except

Copy link
Contributor Author

@DarkaMaul DarkaMaul Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my initial try but the test failed on the CI :
https://github.com/crytic/slither/actions/runs/8649176961/job/23714782903

    def is_external_call(element: "Expression") -> bool:
        return not isinstance(element.called, Identifier) and not isinstance(
>           element.called.expression.value, SolidityVariable
        )
E       AttributeError: 'TypeConversion' object has no attribute 'value'

@DarkaMaul
Copy link
Contributor Author

After changes, the new output is as followed :

INFO:Printers:External calls
+----------------------------------------------------------------------------+------------------+--------------------+
| Source (Line)                                                              |      Destination |              Chain |
+----------------------------------------------------------------------------+------------------+--------------------+
| A.foo() tests/e2e/printers/test_data/test_external_calls/A.sol#10 (5 - 35) | IERC20.balanceOf |                    |
| A.foo() tests/e2e/printers/test_data/test_external_calls/A.sol#10 (5 - 35) | IERC20.balanceOf | B.bar() -> A.foo() |
| C.pop() tests/e2e/printers/test_data/test_external_calls/A.sol#28 (9 - 16) |            B.bar |                    |
+----------------------------------------------------------------------------+------------------+--------------------+

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (2)
slither/printers/summary/external_calls.py (1)

8-59: The implementation of ExternalCallPrinter looks robust and well-structured. Ensure that the performance implications of nested loops (lines 23-49) are acceptable given the expected size of the data.

tests/e2e/printers/test_printers.py (1)

Line range hint 5-63: The tests for ExternalCallPrinter are implemented correctly. Consider enhancing the test coverage to include more scenarios and edge cases.

Would you like me to help enhance the test scenarios for ExternalCallPrinter?

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b9a3ea6 and 56baa06.

Files selected for processing (6)
  • slither/printers/all_printers.py (1 hunks)
  • slither/printers/summary/external_calls.py (1 hunks)
  • slither/solc_parsing/cfg/node.py (3 hunks)
  • tests/e2e/printers/test_data/test_external_calls/A.sol (1 hunks)
  • tests/e2e/printers/test_data/test_external_calls/IERC20.sol (1 hunks)
  • tests/e2e/printers/test_printers.py (3 hunks)
Files skipped from review due to trivial changes (1)
  • tests/e2e/printers/test_data/test_external_calls/IERC20.sol
Additional context used
Ruff
slither/printers/all_printers.py

2-2: .summary.function.FunctionSummary imported but unused (F401)


3-3: .summary.contract.ContractSummary imported but unused (F401)


4-4: .summary.loc.LocPrinter imported but unused (F401)


5-5: .inheritance.inheritance.PrinterInheritance imported but unused (F401)


6-6: .inheritance.inheritance_graph.PrinterInheritanceGraph imported but unused (F401)


7-7: .call.call_graph.PrinterCallGraph imported but unused (F401)


8-8: .functions.authorization.PrinterWrittenVariablesAndAuthorization imported but unused (F401)


9-9: .summary.slithir.PrinterSlithIR imported but unused (F401)


10-10: .summary.slithir_ssa.PrinterSlithIRSSA imported but unused (F401)


11-11: .summary.human_summary.PrinterHumanSummary imported but unused (F401)


12-12: .summary.ck.CK imported but unused (F401)


13-13: .summary.halstead.Halstead imported but unused (F401)


14-14: .functions.cfg.CFG imported but unused (F401)


15-15: .summary.function_ids.FunctionIds imported but unused (F401)


16-16: .summary.variable_order.VariableOrder imported but unused (F401)


17-17: .summary.data_depenency.DataDependency imported but unused (F401)


18-18: .summary.modifier_calls.Modifiers imported but unused (F401)


19-19: .summary.require_calls.RequireOrAssert imported but unused (F401)


20-20: .summary.constructor_calls.ConstructorPrinter imported but unused (F401)


21-21: .guidance.echidna.Echidna imported but unused (F401)


22-22: .summary.evm.PrinterEVM imported but unused (F401)


23-23: .summary.when_not_paused.PrinterWhenNotPaused imported but unused (F401)


24-24: .summary.declaration.Declaration imported but unused (F401)


25-25: .functions.dominator.Dominator imported but unused (F401)


26-26: .summary.martin.Martin imported but unused (F401)


27-27: .summary.external_calls.ExternalCallPrinter imported but unused (F401)

Additional comments not posted (5)
tests/e2e/printers/test_data/test_external_calls/A.sol (3)

6-16: LGTM! Contract A is well-structured and follows Solidity best practices.


18-22: LGTM! Contract B correctly inherits from A and uses its internal function.


25-30: LGTM! Contract C is simple and correctly interacts with contract B.

slither/printers/all_printers.py (1)

27-27: Ensure that ExternalCallPrinter is utilized in the codebase to avoid unused import warnings.

Verification successful

The ExternalCallPrinter class is indeed utilized in the codebase, specifically in the tests/e2e/printers/test_printers.py file.

  • slither/printers/all_printers.py: Import statement.
  • slither/printers/summary/external_calls.py: Class definition.
  • tests/e2e/printers/test_printers.py: Import and usage.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify usage of ExternalCallPrinter in the codebase.

# Test: Search for the class usage. Expect: At least one occurrence.
rg --type python $'ExternalCallPrinter'

Length of output: 69



Script:

#!/bin/bash
# Description: Verify usage of ExternalCallPrinter in the codebase.

# Test: Search for the class usage. Expect: At least one occurrence.
rg --type py $'ExternalCallPrinter'

Length of output: 403

Tools
Ruff

27-27: .summary.external_calls.ExternalCallPrinter imported but unused (F401)

slither/solc_parsing/cfg/node.py (1)

70-84: The refactoring in analyze_expressions to differentiate external and internal calls is well-implemented. Ensure that the exception handling in is_external_call (lines 73-75) does not suppress other potential issues.

@DarkaMaul
Copy link
Contributor Author

Rebased to allow merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants