|
1 | 1 | # rtcheck
|
2 | 2 | Dynamic library to catch run-time safety violations heavily inspired by [RADSan](https://github.com/realtime-sanitizer/radsan)
|
3 | 3 |
|
| 4 | +## Contents |
| 5 | +- [Adding rtcheck to a project](#adding-rtcheck-to-a-project) |
| 6 | +- [Using rtcheck](#using-rtcheck) |
| 7 | +- [Disabling checks](#disabling-checks) |
| 8 | +- [Catching your own violations](#catching-your-own-violations) |
| 9 | +- [Error modes](#error-modes) |
| 10 | + |
| 11 | +## Adding rtcheck to a project |
| 12 | + |
| 13 | +## Using rtcheck |
| 14 | +Simply add an instance of rtc::realtime_context at the start of the scope you want to check for real-time safety violations like this: |
| 15 | +```c++ |
| 16 | +int main() |
| 17 | +{ |
| 18 | + std::thread t ([] |
| 19 | + { |
| 20 | + rtc::realtime_context rc; |
| 21 | + malloc (1024); // Allocating memory is a real-time safety violation! |
| 22 | + }); |
| 23 | + |
| 24 | + t.join(); |
| 25 | + |
| 26 | + return 0; |
| 27 | +} |
| 28 | +``` |
| 29 | +The call to malloc in the above code will then be caught and logged as so: |
| 30 | +``` |
| 31 | +Real-time violation: intercepted call to real-time unsafe function malloc in real-time context! Stack trace: |
| 32 | +0 librt_check.dylib 0x000000010543f7d4 _ZN3rtc14get_stacktraceEv + 84 |
| 33 | + 1 librt_check.dylib 0x000000010543f450 _ZN3rtc32log_function_if_realtime_contextEPKc + 300 |
| 34 | + 2 librt_check.dylib 0x000000010543fe00 _Z44log_function_if_realtime_context_and_enabledN3rtc11check_flagsEPKc + 64 |
| 35 | + 3 librt_check.dylib 0x000000010543fe30 wrap_malloc + 32 |
| 36 | + 4 fail_malloc 0x0000000104fcff14 main + 32 |
| 37 | + 5 dyld 0x00000001901760e0 start + 2360 |
| 38 | +``` |
| 39 | + |
| 40 | +## Disabling checks |
| 41 | +There are two ways to disable checks. You can either disable all checks, which can be useful if you |
| 42 | +know you'll be calling a potentially unsafe function but in a safe way (e.g. an un-contented lock), or you want to log something: |
| 43 | +```c++ |
| 44 | +{ |
| 45 | + rtc::non_realtime_context nrc; |
| 46 | + std::cout << "need to get this message on the screen!"; |
| 47 | +} |
| 48 | +``` |
| 49 | +Or you can selectively disable checks: |
| 50 | +```c++ |
| 51 | +{ |
| 52 | + rtc::disable_checks_for_thread (check_flags::threads); |
| 53 | + mutex.lock(); // I know this is uncontended, don't for get to unlock! |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Catching your own violations |
| 58 | +If you have some code which you know is non-real-time safe e.g. an unbounded distribution function or some other async call, you can opt-in to let rtcheck catch it by calling the following function: |
| 59 | +```c++ |
| 60 | +void my_unsafe_function() |
| 61 | +{ |
| 62 | + log_function_if_realtime_context (__func__); |
| 63 | +} |
| 64 | +``` |
| 65 | +This will then get logged if called whilst a `rtc::realtime_context` is alive. |
| 66 | + |
| 67 | +## Error Modes |
| 68 | +There are two currently supported error modes |
| 69 | +- Exit with error code 1 (default) |
| 70 | +- Log and continue |
| 71 | + |
| 72 | +Exiting it useful for CI runs where you want the program to terminate in an obvious way (non-zero exit code) to fail the run. |
| 73 | +In normal use though, you may just want to log the violation and continue. |
| 74 | + |
| 75 | +You can change between these globally using the following function: |
| 76 | +```c++ |
| 77 | +/** Sets the global error more to determine the behaviour when a real-time |
| 78 | + violation is detected. |
| 79 | +*/ |
| 80 | +void set_error_mode (error_mode); |
| 81 | +``` |
| 82 | +--- |
| 83 | +# Notes: |
4 | 84 | ## Features
|
5 | 85 | - [x] Enable in scope
|
6 | 86 | - [x] Disable in scope
|
@@ -78,7 +158,7 @@ Dynamic library to catch run-time safety violations heavily inspired by [RADSan]
|
78 | 158 | - Passes
|
79 | 159 | - [x] Atomic double
|
80 | 160 | - [x] Small std::function
|
81 |
| - - [ ] thread_local? (pass on linux, fail on macOS) |
| 161 | + - [x] thread_local? (pass on linux, fail on macOS) |
82 | 162 | - [x] Running on CI Linux
|
83 | 163 | - [x] Running on CI macOS
|
84 | 164 | - [x] Tests via CTest (can catch failues)
|
|
0 commit comments