https://github.com/lind026/ownership-checker
https://github.com/lind026/ownership-checker
c safety
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lind026/ownership-checker
- Owner: linD026
- License: gpl-2.0
- Created: 2023-01-03T13:37:55.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-08-18T01:21:32.000Z (almost 2 years ago)
- Last Synced: 2025-05-14T09:44:46.770Z (about 1 year ago)
- Topics: c, safety
- Language: C
- Homepage:
- Size: 225 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ownership Checker for C Language
## Build
```bash
make # Build the program
make verbose=1 # Debug mode
make clean # Delete generated files
```
## Flags and Preprocessor
**WARNING** Now we use the compiler, like gcc or clang, to preprocess the C
source for helping us handle the header and macro. But in the future, we
will handle it by ourselves.
- `-P`: Analyzing the input file without preprocessing
- `-C `: The compiler for preprocessing, the default is `gcc`
- `-I `
## Example
```
$ make
OSC Analyzes file: tests/test_write.c
OSC ERROR: Return the dropped object
|-> tests/test_write.c:4:13
| return (b + 1);
| ^
+-> Dropped at tests/test_write.c:3:18
| function2(a, b, c);
| ^
OSC NOTE: The object is declared as function2 argument: int __mut *b
OSC ERROR: Don't write to the borrowed object
|-> tests/test_write.c:11:15
| a = *borrow = 3;
| ^
OSC NOTE: The object is declared as function argument: int __brw *borrow
OSC ERROR: Should release the end-of-life object
|-> tests/test_write.c:17:6
| }
| ^
+-> Set at tests/test_write.c:16:29
| int __mut *scoped_ptr = &a;
| ^
OSC NOTE: The object is declared as function scope: int __mut *scoped_ptr
OSC ERROR: Return the dropped object
|-> tests/test_write.c:19:18
| return mutable;
| ^
+-> Dropped at tests/test_write.c:13:24
| function2(a, mutable, borrow);
| ^
OSC NOTE: The object is declared as function argument: int __mut *mutable
```
Now, we only check the ownership of the pointer that has the `__mut` or `__brw` attribute.
## Sample
```cpp
// Warn on the pure pointer type in function prototype, i.e. `int *p` .
int function(int *move, int __clone *clone, int copy)
{
int *objA;
int /* __heap */ *objB_from_heap = malloc(); // obj, allocate memory from heap
// change ownership
objA = objB_from_heap;
// end of scope
// - end of lifetime: move, clone, copy
return *objA;
// pass value, don't have to consider the ownership
}
// Warning: don't have free() for obj, move, and clone
```
```cpp
int *funcA(const int /* __immutable */ *imt, int __mut *mut, int __brw *brw)
{
...
}
// Warning: don't have free() for imt, and mut
```