Skip to content

Commit 3e52dc4

Browse files
Merge pull request #63 from FuzzingLabs/feat/cli_ast_cmd
Feat/cli ast cmd
2 parents 5f0507e + b2d6ea8 commit 3e52dc4

63 files changed

Lines changed: 1502 additions & 923 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Deploy mdBook to GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
steps:
14+
- uses: actions/checkout@v3
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Install mdBook
19+
run: |
20+
mkdir -p $HOME/.cargo/bin
21+
curl -L https://github.com/rust-lang/mdBook/releases/download/v0.4.30/mdbook-v0.4.30-x86_64-unknown-linux-gnu.tar.gz | tar xvz -C $HOME/.cargo/bin
22+
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
23+
24+
- name: Build mdBook
25+
run: mdbook build docs
26+
27+
- name: Deploy to GitHub Pages
28+
uses: JamesIves/github-pages-deploy-action@v4
29+
with:
30+
folder: docs/book # Default output folder for mdBook
31+
branch: gh-pages

GEMINI.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# GEMINI AI Agent: Contributor Guidelines for sol-azy
2+
3+
This document outlines the operational guidelines, development workflow, and quality standards for a GEMINI AI agent contributing to the `sol-azy` project. The agent is expected to function as a senior Rust engineer, delivering high-quality, idiomatic, and thoroughly tested code.
4+
5+
## 1. Core Principles
6+
7+
As a senior engineer on this project, you are expected to embody the following principles:
8+
9+
* **Quality First:** All contributions must be well-designed, robust, and maintainable. Code should be clean, idiomatic, and adhere to Rust best practices.
10+
* **Deep Understanding:** Do not rush into coding. Take the time to understand the project's architecture, existing patterns, and the specific requirements of the task.
11+
* **Proactive Ownership:** Independently identify challenges, research solutions, and take full ownership of the development lifecycle, from understanding the task to ensuring its successful integration.
12+
13+
## 2. Project Overview
14+
15+
* **Purpose:** `sol-azy` is a CLI tool for Solana auditors, focusing on reverse engineering and static analysis.
16+
* **Tech Stack:**
17+
* **Language:** 100% Rust.
18+
* **Documentation:** `mdbook` in the `docs/` directory.
19+
* **Scripting/Rules Engine:** `starlark-rs` for defining analysis rules with Python-like syntax.
20+
21+
## 3. Onboarding Checklist
22+
23+
Before your first contribution, complete the following steps to familiarize yourself with the project:
24+
25+
1. [ ] **Read the Introduction:** Start with `docs/src/introduction.md`.
26+
2. [ ] **Understand the Architecture:** Review `docs/src/architecture.md` for a high-level system design.
27+
3. [ ] **Learn CLI Usage:** Read `docs/src/cli_usage.md` to understand the tool's user-facing functionality.
28+
4. [ ] **Study Rule Development:** The `docs/src/rules/` directory is critical. Understand the Starlark rule format, available libraries, and how rules are written.
29+
5. [ ] **Explore the Codebase:** Skim the `src/` directory, paying special attention to `src/engines/starlark_engine.rs` to see how Starlark integrates with the Rust code.
30+
31+
## 4. The Development Workflow
32+
33+
Follow this structured workflow for every task or feature.
34+
35+
### Step 1: Understand the Task
36+
- Deconstruct the request. If anything is unclear, ask for clarification.
37+
- Identify the core problem you need to solve.
38+
39+
### Step 2: Explore & Research
40+
- **Documentation First:** Always begin by searching the project's `docs/` for relevant information. The documentation is the primary source of truth.
41+
- **Codebase Exploration:** Use `list_directory`, `glob`, and `read_file` to study existing modules, data structures, and functions. Look for similar implementations to maintain consistency.
42+
- **External Research:** If the project's resources are insufficient, use `search_web` to find information on:
43+
- Rust language features or library usage.
44+
- `starlark-rs` API and patterns.
45+
- Relevant Solana development concepts.
46+
47+
### Step 3: Implement
48+
- **Write Idiomatic Rust:** Adhere to Rust best practices, focusing on safety, performance, and clarity.
49+
- **Follow Existing Patterns:** Ensure your code is consistent with the surrounding codebase.
50+
- **Develop Starlark Rules:** When writing Starlark rules, refer to `docs/src/rules/` and existing rules in `rules/syn_ast/`. Use the provided Starlark libraries correctly.
51+
52+
### Step 4: Verify & Test
53+
This is a non-negotiable step. High-quality code is tested code.
54+
55+
1. **Format:** Run `cargo fmt` to ensure consistent code style.
56+
2. **Check:** Run `cargo check` for fast compilation checks.
57+
3. **Lint:** Run `cargo clippy` to catch common mistakes and style issues. Address all warnings.
58+
4. **Test:**
59+
* Write **unit tests** for new logic within the relevant module.
60+
* Add **integration tests** in the `tests/` directory for end-to-end functionality.
61+
* Run `cargo test` to execute the full test suite and ensure no regressions were introduced.
62+
63+
### Step 5: Document & Commit
64+
- **Update Documentation:** If your changes affect user-facing functionality, the architecture, or the rule system, update the corresponding `mdbook` documentation in the `docs/` directory.
65+
- **Write Meaningful Commit Messages:** Follow the **Conventional Commits** specification. This helps maintain a clear and automated version history.
66+
- **Format:** `<type>[optional scope]: <description>`
67+
- **Example:** `feat(analysis): add new detector for insecure delegatecall`
68+
- **Common types:** `feat`, `fix`, `docs`, `style`, `refactor`, `test`, `chore`.
69+
70+
## 5. Debugging Guide
71+
72+
If you encounter an error or unexpected behavior:
73+
74+
1. **Read the Error:** Carefully analyze the full error message and stack trace.
75+
2. **Reproduce Systematically:** Isolate the smallest possible case that triggers the error.
76+
3. **Hypothesize:** Form a hypothesis about the root cause.
77+
4. **Test Hypothesis:** Use logging, unit tests, or focused code modifications to prove or disprove your hypothesis. Iterate until the issue is resolved.
78+
79+
By adhering to these guidelines, you will contribute effectively to `sol-azy` and uphold the standards of a senior Rust engineer.

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ cargo run -- sast --target-dir ./my_project --rules-dir ./rules/
6161
cargo run -- reverse --mode both --out-dir ./out --bytecodes-file ./program.so --labeling --reduced
6262
```
6363

64-
### Fetch deployed bytecode from mainnet
64+
### Fetch deployed bytecode from the mainnet
6565

6666
```bash
6767
cargo run -- fetcher --program-id <PROGRAM_ID> --out-dir ./out/
@@ -77,13 +77,22 @@ cargo run -- dotting -c functions.json -r reduced.dot -f full.dot
7777

7878
## Documentation
7979

80-
The project includes a full [mdBook documentation](https://github.com/FuzzingLabs/sol-azy/tree/master/docs):
80+
The project includes full [mdBook documentation](https://github.com/FuzzingLabs/sol-azy/tree/master/docs):
81+
82+
### Serve the docs
8183

8284
```bash
8385
cd book
8486
mdbook serve
8587
```
8688

89+
### Build the docs
90+
91+
```bash
92+
cd book
93+
mdbook build
94+
```
95+
8796
---
8897

8998
## License
@@ -98,8 +107,8 @@ If you have any questions, suggestions, or need support:
98107

99108
- Feel free to [open an issue](https://github.com/FuzzingLabs/sol-azy/issues).
100109
- You can also reach out directly to _(responses may take longer than issue depending on individual availability)_:
101-
- [FuzzingLabs](https://x.com/FuzzingLabs)
102-
- [MohaFuzzingLabs](https://github.com/MohaFuzzingLabs)
103-
- [Ectario](https://x.com/Ectari0)
110+
- [FuzzingLabs](https://x.com/FuzzingLabs)
111+
- [MohaFuzzingLabs](https://github.com/MohaFuzzingLabs)
112+
- [Ectario](https://x.com/Ectari0)
104113

105114
We're happy to help and value community engagement!

docs/src/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Rule Format](rules/format.md)
1212
- [Writing Templates](rules/templates.md)
1313
- [Detection Example](rules/example.md)
14+
- [Starlark references](rules/starlark_libs.md)
1415
- [Reverse Engineering](reverse.md)
1516
- [Overview](reverse/overview.md)
1617
- [Disassembly](reverse/disassembly.md)
@@ -20,3 +21,5 @@
2021
- [Manual CFG Editing (Dotting)](reverse/dotting.md)
2122
- [Architecture](architecture.md)
2223
- [App State](architecture/app_state.md)
24+
- [SAST Engine](architecture/sast_engine.md)
25+
- [Evolution of the tool](future.md)

docs/src/architecture.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ This feature is useful for audits where source code is unavailable or unverifiab
170170

171171
## External Dependencies
172172

173-
- [sbpf-solana (anza-xyz)](https://github.com/anza-xyz/sbpf-solana): Disassembly / Analysis core
173+
- [`sbpf-solana (anza-xyz)`](https://github.com/anza-xyz/sbpf-solana): Disassembly / Analysis core
174174
- [`syn`](https://docs.rs/syn): Source AST parsing
175175
- [`starlark-rust`](https://github.com/facebook/starlark-rust): Rule evaluation engine
176176

docs/src/architecture/app_state.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ It coordinates the execution of commands like `build`, `sast` and `reverse`, and
77

88
## Where It Lives
99

10-
File: `src/state/app_state.rs`]
10+
File: `src/state/app_state.rs`
1111

1212
---
1313

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# `SastEngine` Architecture

docs/src/future.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Evolution of sol-azy
2+
3+
**Sol-azy** is currently in development, and we have few ideas for new features.
4+
5+
## Static analysis
6+
7+
### Enhance Starlark libraries
8+
9+
### Support MIR
10+
11+

docs/src/rules/starlark_libs.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Starlark Libraries References
2+
3+
## AST Node Structure
4+
5+
An AST node represents a single element in the Abstract Syntax Tree with a standardized structure. Each node contains: a
6+
`raw_node` with the original AST data, an `access_path` string representing the node's location in the tree, `metadata`
7+
for additional information like position and mutability flags, `children` and `parent` references for tree navigation,
8+
`ident` for the node's identifier, and optional fields like `args` for function arguments and `root` to indicate if it's
9+
a root node. This consistent structure facilitates tree traversal and pattern matching operations throughout the
10+
codebase.
11+
12+
```python
13+
{
14+
"raw_node": {}, # Original AST data from parser
15+
"access_path": "",# Path string showing location in tree (e.g. "root.expr.binary.left")
16+
"metadata": {}, # Additional data like position and mutability flags
17+
"children": [], # List of child nodes
18+
"parent": {}, # Reference to parent node
19+
"root": False, # Boolean indicating if this is a root node
20+
"args": [], # Function arguments (if applicable)
21+
"ident": "" # Node identifier/name
22+
}
23+
```
24+
25+
## Syn AST Utilities
26+
27+
The Syn AST utilities module provides functions for working with Rust's syntactic ASTs, particularly for security
28+
analysis.
29+
30+
### Core Components
31+
32+
#### Constants
33+
34+
- `EMPTY_ACCESS_PATH`, `EMPTY_IDENT`, `EMPTY_METADATA`, `EMPTY_NODE`: Default values for empty nodes
35+
36+
#### Node Management
37+
38+
- `new_ast_node(syn_ast_node, metadata, access_path)`: Creates a new AST node
39+
- `ast_node_add_child(node, child)`: Adds a child to an AST node
40+
- `ast_node_add_children(node, children)`: Adds multiple children to an AST node
41+
- `to_result(node)`: Converts a node to a result format
42+
- `filter_result(result)`: Filters duplicate results
43+
44+
#### Tree Traversal
45+
46+
- `traverse_tree(node, collector)`: Traverses a tree with a collector function
47+
- `flatten_tree(root)`: Flattens a tree into a list of nodes
48+
- `first(nodes)`: Returns the first node from a list
49+
50+
#### Node Finding Functions
51+
52+
- `find_by_child(self, child_ident)`: Finds nodes by child identifier
53+
- `find_chained_calls(self, *idents)`: Finds chained method calls
54+
- `find_macro_attribute_by_names(self, *idents)`: Finds macro attributes by name
55+
- `find_by_similar_access_path(self, access_path, stop_keyword)`: Finds nodes with similar access paths
56+
- `find_comparisons(self, ident1, ident2)`: Finds comparisons between two identifiers
57+
- `find_comparison_to_any(self, ident)`: Finds comparisons involving a specific identifier
58+
- `find_functions_by_names(self, *function_names)`: Finds functions by name
59+
- `find_by_names(self, *idents)`: Finds nodes by identifier names
60+
- `find_method_calls(self, caller, method)`: Finds method calls on a specific caller
61+
- `find_assignments(self, ident, value_ident)`: Finds assignment operations
62+
- `find_mutables(self)`: Finds mutable variables
63+
- `find_account_typed_nodes(self, ident)`: Finds account-typed nodes
64+
- `find_member_accesses(self, ident)`: Finds member accesses for a specific identifier
65+
66+
#### AST Preparation
67+
68+
- `find_ident_src_node(sub_data, sub_access_path, metadata)`: Finds identifier source nodes
69+
- `find_fn_names(node)`: Extracts function names from an AST
70+
- `find_raw_nodes_by_fn_names(node, func_names)`: Finds raw nodes by function names
71+
- `find_raw_nodes(ast)`: Finds all raw nodes in an AST
72+
- `prepare_syn_ast(ast, access_path, parent)`: Prepares a Syn AST for analysis
73+
- `prepare_ast(ast)`: Main function to prepare an AST for analysis
74+
75+
### Usage Examples
76+
77+
For finding specific code patterns:
78+
79+
```python
80+
```
81+
82+
This documentation provides an overview of the functionality available in these Starlark libraries, which are designed
83+
for security analysis of Solana programs by detecting specific code patterns in their AST representations.

docs/src/static_analysis.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ The rule engine gives you access to:
3939
## Related Pages
4040

4141
- 📘 [How to write Rules](rules/format.md)
42-
-[Use case example](rules/example.md)
42+
-[Use case example](rules/example.md)
43+
44+
## Note
45+
46+
The sast engineering core in Sol-azy is based on the excellent open-source project
47+
[`radar`](https://github.com/Auditware/radar) by [Auditware)](https://github.com/Auditware).
48+
49+
We've been heavily inspired by their approach and wanted a standalone binary capable of it.

0 commit comments

Comments
 (0)