Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,51 @@ awk 1 0 0 10
SUM: 2299 48299 38039 271157
--------------------------------------------------------------------------------
```

# Realtime Sanitizer (RTSan)

Allows detection of any non-deterministic behavior in a piece of code, such as dynamic allocations, mutex locks, and system calls.
It comes with `clang-20`, but with a bit of effort, it can be used with older versions and GCC using the standalone project (see ref below).
Basically, all you need to do is add the function attribute `[[clang::nonblocking]]` in your function and compile with `-fsanitize=realtime`.

The `Function Effect Analysis` is a sister project that can detect such effects at compile-time, whereas sanitizers are runtime-only.
To get compile-time analysis, compile with `-Wfunction-effects`.

## Basic exemple
`a.cpp`
```c++
void f() [[clang::nonblocking]] {
std::vector<int> v;
v.push_back(1);
}

int main() {
f();
return 0;
}
```
`CMakeLists.txt`
```cmake
cmake_minimum_required(VERSION 3.22)
project(a)
add_executable(a a.cpp)
target_compile_options(a PRIVATE -Werror=function-effects) # compile-time checks
target_compile_options(a PRIVATE -fsanitize=realtime) # runtime checks
target_link_options(a PRIVATE -fsanitize=realtime) # runtime checks
```

Try online: https://godbolt.org/z/ozYGc63rE

## References

LLVM’s Real-Time Safety Revolution:
https://www.youtube.com/watch?v=b_hd5FAv1dw

Standalone RTSan (devmode): https://github.com/realtime-sanitizer/rtsan

Official documentation:
https://clang.llvm.org/docs/RealtimeSanitizer.html

https://clang.llvm.org/docs/FunctionEffectAnalysis.html