-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathREADME.md.bak
More file actions
314 lines (234 loc) Β· 8.68 KB
/
README.md.bak
File metadata and controls
314 lines (234 loc) Β· 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Rusty C++ Checker
A standalone static analyzer that enforces Rust-like ownership and borrowing rules for C++ code, bringing memory safety guarantees to existing C++ codebases without runtime overhead.
## π― Vision
This project aims to catch memory safety issues at compile-time by applying Rust's proven ownership model to C++ code. It helps prevent common bugs like use-after-move, double-free, and dangling references before they reach production.
## β¨ Features
### Core Capabilities
- **π Ownership Tracking**: Ensures single ownership of resources with move semantics
- **π Borrow Checking**: Enforces Rust's borrowing rules (multiple readers XOR single writer)
- **β³ Lifetime Analysis**: Validates that references don't outlive their data
- **π― Smart Pointer Support**: Special handling for `std::unique_ptr`, `std::shared_ptr`, and `std::weak_ptr`
- **π¨ Beautiful Diagnostics**: Clear, actionable error messages with source locations
### Detected Issues
- Use-after-move violations
- Multiple mutable borrows
- Dangling references
- Lifetime constraint violations
- RAII violations
- Data races (through borrow checking)
## π¦ Installation
### Prerequisites
- **Rust**: 1.70+ (for building the analyzer)
- **LLVM/Clang**: 14+ (for parsing C++)
- **Z3**: 4.8+ (for constraint solving)
### macOS
```bash
# Install dependencies
brew install llvm z3
# Clone the repository
git clone https://github.com/yourusername/rusty-cpp-checker
cd rusty-cpp-checker
# Build the project
cargo build --release
# Run tests
./run_tests.sh
# Add to PATH (optional)
export PATH="$PATH:$(pwd)/target/release"
```
**Note**: The project includes a `.cargo/config.toml` file that automatically sets the required environment variables for Z3. If you encounter build issues, you may need to adjust the paths in this file based on your system configuration.
### Linux (Ubuntu/Debian)
```bash
# Install dependencies
sudo apt-get update
sudo apt-get install llvm-14-dev libclang-14-dev libz3-dev
# Clone and build
git clone https://github.com/yourusername/rusty-cpp-checker
cd rusty-cpp-checker
cargo build --release
```
### Windows
```bash
# Install LLVM from https://releases.llvm.org/
# Install Z3 from https://github.com/Z3Prover/z3/releases
# Set environment variables:
set LIBCLANG_PATH=C:\Program Files\LLVM\lib
set Z3_SYS_Z3_HEADER=C:\z3\include\z3.h
# Build
cargo build --release
```
## π Usage
### Basic Usage
```bash
# Analyze a single file
rusty-cpp-checker path/to/file.cpp
# Analyze with verbose output
rusty-cpp-checker -vv path/to/file.cpp
# Output in JSON format (for IDE integration)
rusty-cpp-checker --format json path/to/file.cpp
```
### Standalone Binary (No Environment Variables Required)
For release distributions, we provide a standalone binary that doesn't require setting environment variables:
```bash
# Build standalone release
./build_release.sh
# Install from distribution
cd dist/rusty-cpp-checker-*/
./install.sh
# Or use directly
./rusty-cpp-checker-standalone file.cpp
```
See [RELEASE.md](RELEASE.md) for details on building and distributing standalone binaries.
### Environment Setup (macOS)
For convenience, add these to your shell profile:
```bash
# ~/.zshrc or ~/.bashrc
export Z3_SYS_Z3_HEADER=/opt/homebrew/opt/z3/include/z3.h
export DYLD_LIBRARY_PATH=/opt/homebrew/opt/llvm/lib:$DYLD_LIBRARY_PATH
```
## π‘οΈ Safety Annotations
The borrow checker uses a unified annotation system for gradual adoption in existing codebases:
### Unified Rule
`@safe` and `@unsafe` annotations attach to the **next** code element (namespace, function, or first statement).
```cpp
// Example 1: Namespace-level safety
// @safe
namespace myapp {
void func() { /* checked */ }
}
// Example 2: Function-level safety
// @safe
void checked_function() { /* checked */ }
void unchecked_function() { /* not checked - default is unsafe */ }
// Example 3: First-element rule
// @safe
int global = 42; // Makes entire file safe
// Example 4: Unsafe blocks within safe functions
// @safe
void mixed_safety() {
int value = 42;
int& ref1 = value;
// @unsafe
{
int& ref2 = value; // Not checked in unsafe block
}
// @endunsafe
}
```
### Default Behavior
- Files are **unsafe by default** (no checking) for backward compatibility
- Use `@safe` to opt into borrow checking
- Use `@unsafe` to explicitly disable checking
## π Examples
### Example 1: Use After Move
```cpp
#include <memory>
void bad_code() {
std::unique_ptr<int> ptr1 = std::make_unique<int>(42);
std::unique_ptr<int> ptr2 = std::move(ptr1);
*ptr1 = 10; // ERROR: Use after move!
}
```
**Output:**
```
error: use of moved value: `ptr1`
--> example.cpp:6:5
|
6 | *ptr1 = 10;
| ^^^^^ value used here after move
|
note: value moved here
--> example.cpp:5:34
|
5 | std::unique_ptr<int> ptr2 = std::move(ptr1);
| ^^^^^^^^^^^^^^
```
### Example 2: Multiple Mutable Borrows
```cpp
void bad_borrow() {
int value = 42;
int& ref1 = value;
int& ref2 = value; // ERROR: Cannot borrow as mutable twice
}
```
### Example 3: Lifetime Violation
```cpp
int& dangling_reference() {
int local = 42;
return local; // ERROR: Returning reference to local variable
}
```
## ποΈ Architecture
```
βββββββββββββββ ββββββββββββ ββββββββββ
β C++ Code ββββββΆβ Parser ββββββΆβ IR β
βββββββββββββββ ββββββββββββ ββββββββββ
β β
(libclang) βΌ
ββββββββββββββββ
βββββββββββββββ ββββββββββββ β Analysis β
β Diagnostics βββββββ Solver βββββ Engine β
βββββββββββββββ ββββββββββββ ββββββββββββββββ
β β
(Z3) (Ownership/Lifetime)
```
### Components
- **Parser** (`src/parser/`): Uses libclang to build C++ AST
- **IR** (`src/ir/`): Ownership-aware intermediate representation
- **Analysis** (`src/analysis/`): Core borrow checking algorithms
- **Solver** (`src/solver/`): Z3-based constraint solving for lifetimes
- **Diagnostics** (`src/diagnostics/`): User-friendly error reporting
## π― Roadmap
### Phase 1: Foundation (Current)
- [x] Basic project structure
- [x] Clang integration
- [x] Initial IR design
- [x] Simple ownership tracking
- [ ] Use-after-move detection
### Phase 2: Core Features
- [ ] Complete borrow checking
- [ ] Lifetime inference
- [ ] Smart pointer analysis
- [ ] Template support
- [ ] Multi-file analysis
### Phase 3: Production Ready
- [ ] IDE integration (VSCode, CLion)
- [ ] CI/CD integration
- [ ] Performance optimization
- [ ] Incremental analysis
- [ ] Fix suggestions
### Phase 4: Advanced
- [ ] Async/await support
- [ ] Thread safety analysis
- [ ] Custom annotations
- [ ] Auto-fixing capabilities
## π€ Contributing
We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
### Areas We Need Help
- Implementing more C++ AST patterns
- Improving error messages
- Writing test cases
- Documentation
- IDE plugins
## π Documentation
- [Architecture Overview](docs/ARCHITECTURE.md)
- [Borrow Checking Algorithm](docs/ALGORITHM.md)
- [Contributing Guide](CONTRIBUTING.md)
- [API Reference](docs/API.md)
## π¬ Research Papers
This project is inspired by:
- [Rust's Borrow Checker (Polonius)](https://github.com/rust-lang/polonius)
- [Linear Types for Safe Manual Memory Management](https://www.microsoft.com/en-us/research/publication/linear-types-for-safe-manual-memory-management/)
- [Region-Based Memory Management](https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-262.pdf)
## π License
MIT License - see [LICENSE](LICENSE) for details
## π Acknowledgments
- Rust team for the ownership model inspiration
- LLVM/Clang team for the excellent C++ parsing infrastructure
- Z3 team for the powerful constraint solver
- All contributors and early adopters
## π Contact
- **Issues**: [GitHub Issues](https://github.com/yourusername/cpp-borrow-checker/issues)
- **Discussions**: [GitHub Discussions](https://github.com/yourusername/cpp-borrow-checker/discussions)
- **Email**: your.email@example.com
---
**β οΈ Note**: This is an experimental tool. While it can catch many issues, it should not be the only safety measure in production code. Always use in conjunction with other testing and verification methods.