-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang][rtsan] Introduce realtime sanitizer codegen and driver #102622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d3d49f
[clang][rtsan] Introduce realtime sanitizer codegen and driver
cjappl 57de561
[PR] Test hasAnyFunctionEffects before iterating
cjappl 88c1efc
Documentation draft 1
cjappl f03d8b6
[PR] Documentation feedback from David and MaskRay
cjappl 4a978ed
[PR] Maskray - fix braces, change codegen tests to use clang_cc1
cjappl e85d100
[PR] Maskray - fix grammar, merge entry and exit test, change to aarc…
cjappl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
================= | ||
RealtimeSanitizer | ||
================= | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Introduction | ||
============ | ||
RealtimeSanitizer (a.k.a. RTSan) is a real-time safety testing tool for C and C++ | ||
projects. RTSan can be used to detect real-time violations, i.e. calls to methods | ||
that are not safe for use in functions with deterministic runtime requirements. | ||
RTSan considers any function marked with the ``[[clang::nonblocking]]`` attribute | ||
to be a real-time function. If RTSan detects a call to ``malloc``, ``free``, | ||
``pthread_mutex_lock``, or anything else that could have a non-deterministic | ||
execution time in a function marked ``[[clang::nonblocking]]`` | ||
RTSan raises an error. | ||
|
||
The runtime slowdown introduced by RealtimeSanitizer is negligible. | ||
|
||
How to build | ||
============ | ||
|
||
Build LLVM/Clang with `CMake <https://llvm.org/docs/CMake.html>` and enable the | ||
``compiler-rt`` runtime. An example CMake configuration that will allow for the | ||
use/testing of RealtimeSanitizer: | ||
|
||
.. code-block:: console | ||
|
||
$ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="compiler-rt" <path to source>/llvm | ||
|
||
Usage | ||
===== | ||
|
||
There are two requirements: | ||
|
||
1. The code must be compiled with the ``-fsanitize=realtime`` flag. | ||
2. Functions that are subject to real-time constraints must be marked | ||
with the ``[[clang::nonblocking]]`` attribute. | ||
|
||
Typically, these attributes should be added onto the functions that are entry | ||
points for threads with real-time priority. These threads are subject to a fixed | ||
callback time, such as audio callback threads or rendering loops in video game | ||
code. | ||
|
||
.. code-block:: console | ||
|
||
% cat example_realtime_violation.cpp | ||
#include <vector> | ||
|
||
void violation() [[clang::nonblocking]]{ | ||
std::vector<float> v; | ||
v.resize(100); | ||
} | ||
|
||
int main() { | ||
violation(); | ||
return 0; | ||
} | ||
# Compile and link | ||
% clang++ -fsanitize=realtime -g example_realtime_violation.cpp | ||
|
||
If a real-time safety violation is detected in a ``[[clang::nonblocking]]`` | ||
context, or any function invoked by that function, the program will exit with a | ||
non-zero exit code. | ||
|
||
.. code-block:: console | ||
|
||
% clang++ -fsanitize=realtime -g example_realtime_violation.cpp | ||
% ./a.out | ||
Real-time violation: intercepted call to real-time unsafe function `malloc` in real-time context! Stack trace: | ||
#0 0x000102893034 in __rtsan::PrintStackTrace() rtsan_stack.cpp:45 | ||
#1 0x000102892e64 in __rtsan::Context::ExpectNotRealtime(char const*) rtsan_context.cpp:78 | ||
#2 0x00010289397c in malloc rtsan_interceptors.cpp:286 | ||
#3 0x000195bd7bd0 in operator new(unsigned long)+0x1c (libc++abi.dylib:arm64+0x16bd0) | ||
#4 0x5c7f00010230f07c (<unknown module>) | ||
#5 0x00010230f058 in std::__1::__libcpp_allocate[abi:ue170006](unsigned long, unsigned long) new:324 | ||
#6 0x00010230effc in std::__1::allocator<float>::allocate[abi:ue170006](unsigned long) allocator.h:114 | ||
... snip ... | ||
#10 0x00010230e4bc in std::__1::vector<float, std::__1::allocator<float>>::__append(unsigned long) vector:1162 | ||
#11 0x00010230dcdc in std::__1::vector<float, std::__1::allocator<float>>::resize(unsigned long) vector:1981 | ||
#12 0x00010230dc28 in violation() main.cpp:5 | ||
#13 0x00010230dd64 in main main.cpp:9 | ||
#14 0x0001958960dc (<unknown module>) | ||
#15 0x2f557ffffffffffc (<unknown module>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -845,6 +845,15 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, | |
if (SanOpts.has(SanitizerKind::ShadowCallStack)) | ||
Fn->addFnAttr(llvm::Attribute::ShadowCallStack); | ||
|
||
if (SanOpts.has(SanitizerKind::Realtime)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (FD && FD->getASTContext().hasAnyFunctionEffects()) { | ||
for (const FunctionEffectWithCondition &Fe : FD->getFunctionEffects()) { | ||
if (Fe.Effect.kind() == FunctionEffect::Kind::NonBlocking) | ||
Fn->addFnAttr(llvm::Attribute::SanitizeRealtime); | ||
} | ||
} | ||
} | ||
cjappl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Apply fuzzing attribute to the function. | ||
if (SanOpts.hasOneOf(SanitizerKind::Fuzzer | SanitizerKind::FuzzerNoLink)) | ||
Fn->addFnAttr(llvm::Attribute::OptForFuzzing); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -552,11 +552,15 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC, | |
SanitizerKind::Leak | SanitizerKind::Thread | | ||
SanitizerKind::Memory | SanitizerKind::KernelAddress | | ||
SanitizerKind::Scudo | SanitizerKind::SafeStack), | ||
std::make_pair(SanitizerKind::MemTag, | ||
SanitizerKind::Address | SanitizerKind::KernelAddress | | ||
SanitizerKind::HWAddress | | ||
SanitizerKind::KernelHWAddress), | ||
std::make_pair(SanitizerKind::KCFI, SanitizerKind::Function)}; | ||
std::make_pair(SanitizerKind::MemTag, SanitizerKind::Address | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, a little clang-tidy cruft here, the only thing changed functionally was the addition of the realtime sanitizer pairs on line 560 |
||
SanitizerKind::KernelAddress | | ||
SanitizerKind::HWAddress | | ||
SanitizerKind::KernelHWAddress), | ||
std::make_pair(SanitizerKind::KCFI, SanitizerKind::Function), | ||
std::make_pair(SanitizerKind::Realtime, | ||
SanitizerKind::Address | SanitizerKind::Thread | | ||
SanitizerKind::Undefined | SanitizerKind::Memory)}; | ||
|
||
// Enable toolchain specific default sanitizers if not explicitly disabled. | ||
SanitizerMask Default = TC.getDefaultSanitizers() & ~AllRemove; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// RUN: %clang -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -o - | FileCheck %s | ||
|
||
float process(float *a) [[clang::nonblocking]] { return *a; } | ||
|
||
// CHECK-LABEL: @process{{.*}}#0 { | ||
// CHECK: attributes #0 = { | ||
// CHECK-SAME: {{.*sanitize_realtime.*}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -fsanitize=realtime -emit-llvm -o - %s | FileCheck %s | ||
cjappl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// The first instruction after the function is entred should be a call to | ||
// enable the realtime sanitizer stack | ||
|
||
int foo(int *a) [[clang::nonblocking]] { return *a; } | ||
// CHECK-LABEL: define{{.*}}@foo | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: call{{.*}}__rtsan_realtime_enter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -fsanitize=realtime -emit-llvm -o - %s | FileCheck %s | ||
cjappl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// __rtsan_realtime_exit should be inserted at all function returns | ||
cjappl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
int bar(int* x) [[clang::nonblocking]] { | ||
return *x; | ||
} | ||
// CHECK-LABEL: call{{.*}}__rtsan_realtime_exit | ||
// CHECK-NEXT: ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// RUN: %clang -target x86_64-unknown-linux %s -S -emit-llvm -o - | FileCheck %s | ||
|
||
|
||
float process(float *a) [[clang::nonblocking]] { return *a; } | ||
|
||
// Without the -fsanitize=realtime flag, we shouldn't attach | ||
// the attribute | ||
// CHECK-NOT: {{.*sanitize_realtime.*}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// RUN: %clang -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -o - | FileCheck %s | ||
cjappl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// RUN: %clang -O1 -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -o - | FileCheck %s | ||
// RUN: %clang -O2 -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -o - | FileCheck %s | ||
// RUN: %clang -O3 -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -o - | FileCheck %s | ||
// RUN: %clang -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -flto=thin -o - | FileCheck %s | ||
// RUN: %clang -O2 -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -flto=thin -o - | FileCheck %s | ||
// RUN: %clang -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -flto -o - | FileCheck %s | ||
// RUN: %clang -O2 -target x86_64-unknown-linux -fsanitize=realtime %s -S -emit-llvm -flto -o - | FileCheck %s | ||
|
||
// Ensure the rtsan_realtime calls are never optimized away | ||
|
||
int foo(int *a) [[clang::nonblocking]] { return *a; } | ||
// CHECK: __rtsan_realtime_enter | ||
// CHECK: __rtsan_realtime_exit |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.