Skip to content

Commit

Permalink
[rtsan][llvm][NFC] Rename sanitize_realtime_unsafe attr to sanitize_r…
Browse files Browse the repository at this point in the history
…ealtime_blocking (llvm#113155)

# What

This PR renames the newly-introduced llvm attribute
`sanitize_realtime_unsafe` to `sanitize_realtime_blocking`. Likewise,
sibling variables such as `SanitizeRealtimeUnsafe` are renamed to
`SanitizeRealtimeBlocking` respectively. There are no other functional
changes.


# Why?

- There are a number of problems that can cause a function to be
real-time "unsafe",
- we wish to communicate what problems rtsan detects and *why* they're
unsafe, and
- a generic "unsafe" attribute is, in our opinion, too broad a net -
which may lead to future implementations that need extra contextual
information passed through them in order to communicate meaningful
reasons to users.
- We want to avoid this situation and make the runtime library boundary
API/ABI as simple as possible, and
- we believe that restricting the scope of attributes to names like
`sanitize_realtime_blocking` is an effective means of doing so.

We also feel that the symmetry between `[[clang::blocking]]` and
`sanitize_realtime_blocking` is easier to follow as a developer.

# Concerns

- I'm aware that the LLVM attribute `sanitize_realtime_unsafe` has been
part of the tree for a few weeks now (introduced here:
llvm#106754). Given that it hasn't
been released in version 20 yet, am I correct in considering this to not
be a breaking change?
  • Loading branch information
davidtrevelyan authored Oct 26, 2024
1 parent 69ead94 commit 4102625
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
if (Fe.Effect.kind() == FunctionEffect::Kind::NonBlocking)
Fn->addFnAttr(llvm::Attribute::SanitizeRealtime);
else if (Fe.Effect.kind() == FunctionEffect::Kind::Blocking)
Fn->addFnAttr(llvm::Attribute::SanitizeRealtimeUnsafe);
Fn->addFnAttr(llvm::Attribute::SanitizeRealtimeBlocking);
}

// Apply fuzzing attribute to the function.
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/rtsan_attribute_inserted.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ float process(float *a) [[clang::nonblocking]] { return *a; }
int spinlock(int *a) [[clang::blocking]] { return *a; }
// CHECK: @spinlock{{.*}} #1 {
// CHECK: attributes #1 = {
// CHECK-SAME: {{.*sanitize_realtime_unsafe .*}}
// CHECK-SAME: {{.*sanitize_realtime_blocking .*}}
2 changes: 1 addition & 1 deletion clang/test/CodeGen/rtsan_no_attribute_sanitizer_disabled.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ int spinlock(int *a) [[clang::blocking]] { return *a; }

// Without the -fsanitize=realtime flag, we shouldn't attach the attributes.
// CHECK-NOT: {{.*sanitize_realtime .*}}
// CHECK-NOT: {{.*sanitize_realtime_unsafe .*}}
// CHECK-NOT: {{.*sanitize_realtime_blocking .*}}
4 changes: 2 additions & 2 deletions compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ TEST(TestRtsan, ThrowingAnExceptionDiesWhenRealtime) {

TEST(TestRtsan, DoesNotDieIfTurnedOff) {
std::mutex mutex;
auto RealtimeUnsafeFunc = [&]() {
auto RealtimeBlockingFunc = [&]() {
__rtsan_disable();
mutex.lock();
mutex.unlock();
__rtsan_enable();
};
RealtimeInvoke(RealtimeUnsafeFunc);
RealtimeInvoke(RealtimeBlockingFunc);
}
2 changes: 1 addition & 1 deletion llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2334,7 +2334,7 @@ example:
This attribute indicates that RealtimeSanitizer checks
(realtime safety analysis - no allocations, syscalls or exceptions) are enabled
for this function.
``sanitize_realtime_unsafe``
``sanitize_realtime_blocking``
This attribute indicates that RealtimeSanitizer should error immediately
if the attributed function is called during invocation of a function
attributed with ``sanitize_realtime``.
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Bitcode/LLVMBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ enum AttributeKindCodes {
ATTR_KIND_INITIALIZES = 94,
ATTR_KIND_HYBRID_PATCHABLE = 95,
ATTR_KIND_SANITIZE_REALTIME = 96,
ATTR_KIND_SANITIZE_REALTIME_UNSAFE = 97,
ATTR_KIND_SANITIZE_REALTIME_BLOCKING = 97,
ATTR_KIND_CORO_ELIDE_SAFE = 98,
ATTR_KIND_NO_EXT = 99,
ATTR_KIND_NO_DIVERGENCE_SOURCE = 100,
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/IR/Attributes.td
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def SanitizeRealtime : EnumAttr<"sanitize_realtime", IntersectPreserve, [FnAttr]

/// RealtimeSanitizer should error if a real-time unsafe function is invoked
/// during a real-time sanitized function (see `sanitize_realtime`).
def SanitizeRealtimeUnsafe : EnumAttr<"sanitize_realtime_unsafe", IntersectPreserve, [FnAttr]>;
def SanitizeRealtimeBlocking : EnumAttr<"sanitize_realtime_blocking", IntersectPreserve, [FnAttr]>;

/// Speculative Load Hardening is enabled.
///
Expand Down Expand Up @@ -430,7 +430,7 @@ def : CompatRule<"isEqual<SanitizeHWAddressAttr>">;
def : CompatRule<"isEqual<SanitizeMemTagAttr>">;
def : CompatRule<"isEqual<SanitizeNumericalStabilityAttr>">;
def : CompatRule<"isEqual<SanitizeRealtimeAttr>">;
def : CompatRule<"isEqual<SanitizeRealtimeUnsafeAttr>">;
def : CompatRule<"isEqual<SanitizeRealtimeBlockingAttr>">;
def : CompatRule<"isEqual<SafeStackAttr>">;
def : CompatRule<"isEqual<ShadowCallStackAttr>">;
def : CompatRule<"isEqual<UseSampleProfileAttr>">;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2165,8 +2165,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
return Attribute::SanitizeNumericalStability;
case bitc::ATTR_KIND_SANITIZE_REALTIME:
return Attribute::SanitizeRealtime;
case bitc::ATTR_KIND_SANITIZE_REALTIME_UNSAFE:
return Attribute::SanitizeRealtimeUnsafe;
case bitc::ATTR_KIND_SANITIZE_REALTIME_BLOCKING:
return Attribute::SanitizeRealtimeBlocking;
case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:
return Attribute::SpeculativeLoadHardening;
case bitc::ATTR_KIND_SWIFT_ERROR:
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
return bitc::ATTR_KIND_SANITIZE_NUMERICAL_STABILITY;
case Attribute::SanitizeRealtime:
return bitc::ATTR_KIND_SANITIZE_REALTIME;
case Attribute::SanitizeRealtimeUnsafe:
return bitc::ATTR_KIND_SANITIZE_REALTIME_UNSAFE;
case Attribute::SanitizeRealtimeBlocking:
return bitc::ATTR_KIND_SANITIZE_REALTIME_BLOCKING;
case Attribute::SpeculativeLoadHardening:
return bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING;
case Attribute::SwiftError:
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2235,9 +2235,9 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
}

Check(!(Attrs.hasFnAttr(Attribute::SanitizeRealtime) &&
Attrs.hasFnAttr(Attribute::SanitizeRealtimeUnsafe)),
Attrs.hasFnAttr(Attribute::SanitizeRealtimeBlocking)),
"Attributes "
"'sanitize_realtime and sanitize_realtime_unsafe' are incompatible!",
"'sanitize_realtime and sanitize_realtime_blocking' are incompatible!",
V);

if (Attrs.hasFnAttr(Attribute::OptimizeForDebugging)) {
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static PreservedAnalyses runSanitizeRealtime(Function &Fn) {
return rtsanPreservedCFGAnalyses();
}

static PreservedAnalyses runSanitizeRealtimeUnsafe(Function &Fn) {
static PreservedAnalyses runSanitizeRealtimeBlocking(Function &Fn) {
IRBuilder<> Builder(&Fn.front().front());
Value *Name = Builder.CreateGlobalString(demangle(Fn.getName()));
insertCallAtFunctionEntryPoint(Fn, "__rtsan_notify_blocking_call", {Name});
Expand All @@ -84,8 +84,8 @@ PreservedAnalyses RealtimeSanitizerPass::run(Function &Fn,
if (Fn.hasFnAttribute(Attribute::SanitizeRealtime))
return runSanitizeRealtime(Fn);

if (Fn.hasFnAttribute(Attribute::SanitizeRealtimeUnsafe))
return runSanitizeRealtimeUnsafe(Fn);
if (Fn.hasFnAttribute(Attribute::SanitizeRealtimeBlocking))
return runSanitizeRealtimeBlocking(Fn);

return PreservedAnalyses::all();
}
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/CodeExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
case Attribute::SanitizeHWAddress:
case Attribute::SanitizeMemTag:
case Attribute::SanitizeRealtime:
case Attribute::SanitizeRealtimeUnsafe:
case Attribute::SanitizeRealtimeBlocking:
case Attribute::SpeculativeLoadHardening:
case Attribute::StackProtect:
case Attribute::StackProtectReq:
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Bitcode/attributes.ll
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ define void @f92() sanitize_realtime
}

; CHECK: define void @f93() #54
define void @f93() sanitize_realtime_unsafe {
define void @f93() sanitize_realtime_blocking {
ret void;
}

Expand Down Expand Up @@ -616,7 +616,7 @@ define void @initializes(ptr initializes((-4, 0), (4, 8)) %a) {
; CHECK: attributes #51 = { uwtable(sync) }
; CHECK: attributes #52 = { nosanitize_bounds }
; CHECK: attributes #53 = { sanitize_realtime }
; CHECK: attributes #54 = { sanitize_realtime_unsafe }
; CHECK: attributes #54 = { sanitize_realtime_blocking }
; CHECK: attributes [[FNRETTHUNKEXTERN]] = { fn_ret_thunk_extern }
; CHECK: attributes [[SKIPPROFILE]] = { skipprofile }
; CHECK: attributes [[OPTDEBUG]] = { optdebug }
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Bitcode/compatibility.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2048,8 +2048,8 @@ declare void @f.sanitize_numerical_stability() sanitize_numerical_stability
declare void @f.sanitize_realtime() sanitize_realtime
; CHECK: declare void @f.sanitize_realtime() #52

declare void @f.sanitize_realtime_unsafe() sanitize_realtime_unsafe
; CHECK: declare void @f.sanitize_realtime_unsafe() #53
declare void @f.sanitize_realtime_blocking() sanitize_realtime_blocking
; CHECK: declare void @f.sanitize_realtime_blocking() #53

; CHECK: declare nofpclass(snan) float @nofpclass_snan(float nofpclass(snan))
declare nofpclass(snan) float @nofpclass_snan(float nofpclass(snan))
Expand Down Expand Up @@ -2183,7 +2183,7 @@ define float @nofpclass_callsites(float %arg, { float } %arg1) {
; CHECK: attributes #50 = { allockind("alloc,uninitialized") }
; CHECK: attributes #51 = { sanitize_numerical_stability }
; CHECK: attributes #52 = { sanitize_realtime }
; CHECK: attributes #53 = { sanitize_realtime_unsafe }
; CHECK: attributes #53 = { sanitize_realtime_blocking }
; CHECK: attributes #54 = { builtin }

;; Metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ define noundef i32 @main() #2 {
ret i32 0
}

attributes #0 = { mustprogress noinline sanitize_realtime_unsafe optnone ssp uwtable(sync) }
attributes #0 = { mustprogress noinline sanitize_realtime_blocking optnone ssp uwtable(sync) }
;.
; CHECK: attributes #[[ATTR0]] = { mustprogress noinline optnone sanitize_realtime_unsafe ssp uwtable(sync) }
; CHECK: attributes #[[ATTR0]] = { mustprogress noinline optnone sanitize_realtime_blocking ssp uwtable(sync) }
;.
4 changes: 2 additions & 2 deletions llvm/test/Verifier/rtsan-attrs.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
; RUN: not llvm-as -disable-output %s 2>&1 | FileCheck %s

; CHECK: Attributes 'sanitize_realtime and sanitize_realtime_unsafe' are incompatible!
; CHECK: Attributes 'sanitize_realtime and sanitize_realtime_blocking' are incompatible!
; CHECK-NEXT: ptr @sanitize_unsafe
define void @sanitize_unsafe() #0 {
ret void
}

attributes #0 = { sanitize_realtime sanitize_realtime_unsafe }
attributes #0 = { sanitize_realtime sanitize_realtime_blocking }

0 comments on commit 4102625

Please sign in to comment.