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

slither-function-filter tool #2238

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

Conversation

shortdoom
Copy link

@shortdoom shortdoom commented Nov 18, 2023

reference: #2233

slither-function-filter tool. for the time being it supports basic flag-based filtering (no way to specify details of search, only true/false)

two example inputs:

slither-function-filter contract.sol --visibility external --modifiers --ext-calls or

slither-function-filter contract.sol --read-only etc.

so, it's possible to:

"Return all functions that are {visibility}, have/don't have internal OR external calls , have/don't have modifiers, are/aren't mutable (involve a potential state change)"

all allowed flags:

--contract-name (str)
--declared-only (bool)
--visibility (str)
--modifiers (bool)
--ext-calls (bool)
--int-calls (bool)
--state-change (bool)
--read-only (bool)
--contains-asm (bool)
--low-lvl-calls (bool)
--full-name (str)
--in-source (str)

every flag is optional, if none is specified, all contract functions will be returned, all flags are allowed to be mixed

implementation is focused around output of function.get_summary() but filter_functions() is easy to extend.

output is formatted in logger and with coloring.

image

@CLAassistant
Copy link

CLAassistant commented Nov 18, 2023

CLA assistant check
All committers have signed the CLA.

@0xalpharush
Copy link
Member

I had wanted something similar and made a note to myself:

We should expose a tool for semantic grepping with the ability to target contract/ function names, filter by visibility/purity,
Proposed CLI:

slither grep contracts/ [--contract|--function] [--pure,--nonpure] [--external-calls] [--unchecked] [--assembly]

I think that this is a decent start but I would imagine showing the source and line info so that it can be quickly examined or jumped to in the editor would be better. Wdyt?

@shortdoom
Copy link
Author

Some time passed from this PR but I remember that choosing exact Contract.sol as target to query is preferable over iterating the whole directory - avoids information overload in the cli output.

So you won't need this part of the command:

...contracts/ [--contract|--function]... just Contract.sol

Current version was designed as query contract to find function. It's an interesting direction to also allow some grepping at the level of Function itself (has_require, has_loop, etc.).

I noticed you also included --unchecked and --assembly, I would also add --low_lvl_calls.

I also agree on adding one more string input flag to allow to find a specific function by full_name.

Showing source code of a Function is trickier UX-wise. Some functions will be long and cli output will get cluttered. Hence why I resorted to displaying structured info (it tends to be more or less the same length). Displaying a line reference is a great idea, most IDEs will just allow to jump automatically and we could avoid displaying source code in terminal.

To have a full grep like tool we could allow to actually grep the function's source code without cluttering the cli though!

slither grep Contract.sol --match 'try:' - that would find all functions using try/catch block ("try:" string) in the function source code.

I can ofc bring this PR home.

@shortdoom
Copy link
Author

Also, wouldn't it be cool to have it as PythonAPI? This is how I actually implemented it for my own use, as an external class taking a list of Function to filter, returning Function(s) matching. I needed to resort to writing an external query class because there's no API (and this PR also doesn't propose it in the current state)

Copy link

coderabbitai bot commented Apr 11, 2024

Important

Auto Review Skipped

Auto reviews are disabled on base/target branches other than the default branch. Please add the base/target branch pattern to the list of additional branches to be reviewed in the settings.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.


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.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

@shortdoom
Copy link
Author

shortdoom commented Apr 11, 2024

Added lines reference to the output (allows to Jump-to-line)

Added more filters:

--contains-asm (bool)
--low-lvl-calls (bool)
--full-name (str)
--in-source (str)

full-name allows user to search inside the function.full_name string (not an exact match)

in-source will search inside of the functions source code (not an exact match)

slither-function-filter.webm

I think that covers most immediate needs we've seen mentioned by developers?

PS. It works best with the crytic-compile patch that's already merged but not yet released. If crytic.config.json is present in the working directory, it guarantees compilation and filtering.

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.

None yet

3 participants