Skip to content

Commit e047ca6

Browse files
committed
Documentation draft 1
1 parent 57de561 commit e047ca6

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

clang/docs/RealtimeSanitizer.rst

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
=================
2+
RealtimeSanitizer
3+
=================
4+
5+
.. contents::
6+
:local:
7+
8+
Introduction
9+
============
10+
RealtimeSanitizer (a.k.a. RTSan) is a real-time safety testing tool for C and
11+
C++ projects. RTSan can be used to detect real-time violations,such as calls to
12+
methods that are not safe for use in functions with deterministic runtime
13+
requirements.
14+
15+
The tool can detect the following types of real-time violations:
16+
17+
* System calls
18+
* Allocations
19+
* Exceptions
20+
21+
These checks are put in place when compiling with the
22+
``-fsanitize=realtime`` flag, for functions marked with
23+
``[[clang::nonblocking]]``.
24+
25+
.. code-block:: c
26+
void process_audio(float* buffer) [[clang::nonblocking]] {
27+
...
28+
}
29+
30+
The runtime slowdown introduced by RealtimeSanitizer is trivial. Code in
31+
real-time contexts without real-time safety violations have no slowdown.
32+
33+
How to build
34+
============
35+
36+
Build LLVM/Clang with `CMake <https://llvm.org/docs/CMake.html>` and enable the
37+
``compiler-rt`` runtime. An example CMake configuration that will allow for the
38+
use/testing of RealtimeSanitizer:
39+
40+
.. code-block:: console
41+
42+
$ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="compiler-rt" <path to source>/llvm
43+
44+
Usage
45+
=====
46+
47+
There are two requirements:
48+
1. The code must be compiled with the ``-fsanitize=realtime`` flag.
49+
2. Functions that are subject to real-time constraints must be
50+
marked with the ``[[clang::nonblocking]]`` attribute.
51+
52+
Typically, these attributes should be added onto the functions that are entry
53+
points for threads with real-time priority. These threads are subject to a fixed
54+
callback time, such as audio callback threads or rendering loops in video game
55+
code.
56+
57+
.. code-block:: console
58+
% cat example_realtime_violation.cpp
59+
int main() [[clang::nonblocking]] {
60+
int* p = new int;
61+
return 0;
62+
}
63+
64+
# Compile and link
65+
% clang -fsanitize=realtime -g example_realtime_violation.cpp
66+
67+
If a real-time safety violation is detected in a ``[[clang::nonblocking]]``
68+
context, or any function invoked by that function, the program will exit with a
69+
non-zero exit code.
70+
71+
.. code-block:: console
72+
% clang -fsanitize=realtime -g example_realtime_violation.cpp
73+
% ./a.out
74+
Real-time violation: intercepted call to real-time unsafe function `malloc` in real-time context! Stack trace:
75+
#0 0x00010065ad9c in __rtsan::PrintStackTrace() rtsan_stack.cpp:45
76+
#1 0x00010065abcc in __rtsan::Context::ExpectNotRealtime(char const*) rtsan_context.cpp:78
77+
#2 0x00010065b8d0 in malloc rtsan_interceptors.cpp:289
78+
#3 0x000195bd7bd0 in operator new(unsigned long)+0x1c (libc++abi.dylib:arm64+0x16bd0)
79+
#4 0xb338001000dbf68 (<unknown module>)
80+
#5 0x0001958960dc (<unknown module>)
81+
#6 0x45737ffffffffffc (<unknown module>)

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ Moved checkers
349349

350350
Sanitizers
351351
----------
352+
- Introduced Realtime Sanitizer, activated by using the -fsanitize=realtime
353+
flag. This sanitizer detects unsafe system library calls, such as memory
354+
allocations and mutex locks. If any such function is called during invocation
355+
of a function marked with the ``[[clang::nonblocking]]`` attribute, an error
356+
is printed to the console and the process exits non-zero.
352357

353358
Python Binding Changes
354359
----------------------

0 commit comments

Comments
 (0)