Skip to content

Commit 0f488a0

Browse files
[LLVM][rtsan] Add sanitize_realtime_unsafe attribute (#106754)
1 parent e2983e5 commit 0f488a0

File tree

10 files changed

+43
-3
lines changed

10 files changed

+43
-3
lines changed

llvm/docs/LangRef.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,6 +2325,11 @@ example:
23252325
This attribute indicates that RealtimeSanitizer checks
23262326
(realtime safety analysis - no allocations, syscalls or exceptions) are enabled
23272327
for this function.
2328+
``sanitize_realtime_unsafe``
2329+
This attribute indicates that RealtimeSanitizer should error immediately
2330+
if the attributed function is called during invocation of a function
2331+
attributed with ``sanitize_realtime``.
2332+
This attribute is incompatible with the ``sanitize_realtime`` attribute.
23282333
``speculative_load_hardening``
23292334
This attribute indicates that
23302335
`Speculative Load Hardening <https://llvm.org/docs/SpeculativeLoadHardening.html>`_

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ enum AttributeKindCodes {
761761
ATTR_KIND_INITIALIZES = 94,
762762
ATTR_KIND_HYBRID_PATCHABLE = 95,
763763
ATTR_KIND_SANITIZE_REALTIME = 96,
764-
ATTR_KIND_NO_SANITIZE_REALTIME = 97,
764+
ATTR_KIND_SANITIZE_REALTIME_UNSAFE = 97,
765765
ATTR_KIND_CORO_ELIDE_SAFE = 98,
766766
ATTR_KIND_NO_EXT = 99,
767767
};

llvm/include/llvm/IR/Attributes.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ def SanitizeNumericalStability : EnumAttr<"sanitize_numerical_stability", [FnAtt
303303
/// RealtimeSanitizer is on.
304304
def SanitizeRealtime : EnumAttr<"sanitize_realtime", [FnAttr]>;
305305

306+
/// RealtimeSanitizer should error if a real-time unsafe function is invoked
307+
/// during a real-time sanitized function (see `sanitize_realtime`).
308+
def SanitizeRealtimeUnsafe : EnumAttr<"sanitize_realtime_unsafe", [FnAttr]>;
309+
306310
/// Speculative Load Hardening is enabled.
307311
///
308312
/// Note that this uses the default compatibility (always compatible during
@@ -396,6 +400,7 @@ def : CompatRule<"isEqual<SanitizeHWAddressAttr>">;
396400
def : CompatRule<"isEqual<SanitizeMemTagAttr>">;
397401
def : CompatRule<"isEqual<SanitizeNumericalStabilityAttr>">;
398402
def : CompatRule<"isEqual<SanitizeRealtimeAttr>">;
403+
def : CompatRule<"isEqual<SanitizeRealtimeUnsafeAttr>">;
399404
def : CompatRule<"isEqual<SafeStackAttr>">;
400405
def : CompatRule<"isEqual<ShadowCallStackAttr>">;
401406
def : CompatRule<"isEqual<UseSampleProfileAttr>">;

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
21442144
return Attribute::SanitizeNumericalStability;
21452145
case bitc::ATTR_KIND_SANITIZE_REALTIME:
21462146
return Attribute::SanitizeRealtime;
2147+
case bitc::ATTR_KIND_SANITIZE_REALTIME_UNSAFE:
2148+
return Attribute::SanitizeRealtimeUnsafe;
21472149
case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:
21482150
return Attribute::SpeculativeLoadHardening;
21492151
case bitc::ATTR_KIND_SWIFT_ERROR:

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
849849
return bitc::ATTR_KIND_SANITIZE_NUMERICAL_STABILITY;
850850
case Attribute::SanitizeRealtime:
851851
return bitc::ATTR_KIND_SANITIZE_REALTIME;
852+
case Attribute::SanitizeRealtimeUnsafe:
853+
return bitc::ATTR_KIND_SANITIZE_REALTIME_UNSAFE;
852854
case Attribute::SpeculativeLoadHardening:
853855
return bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING;
854856
case Attribute::SwiftError:

llvm/lib/IR/Verifier.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,12 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
22242224
"Attributes 'optdebug and optnone' are incompatible!", V);
22252225
}
22262226

2227+
Check(!(Attrs.hasFnAttr(Attribute::SanitizeRealtime) &&
2228+
Attrs.hasFnAttr(Attribute::SanitizeRealtimeUnsafe)),
2229+
"Attributes "
2230+
"'sanitize_realtime and sanitize_realtime_unsafe' are incompatible!",
2231+
V);
2232+
22272233
if (Attrs.hasFnAttr(Attribute::OptimizeForDebugging)) {
22282234
Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
22292235
"Attributes 'optsize and optdebug' are incompatible!", V);

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
952952
case Attribute::SanitizeHWAddress:
953953
case Attribute::SanitizeMemTag:
954954
case Attribute::SanitizeRealtime:
955+
case Attribute::SanitizeRealtimeUnsafe:
955956
case Attribute::SpeculativeLoadHardening:
956957
case Attribute::StackProtect:
957958
case Attribute::StackProtectReq:

llvm/test/Bitcode/attributes.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ define void @f92() sanitize_realtime
511511
ret void;
512512
}
513513

514+
; CHECK: define void @f93() #54
515+
define void @f93() sanitize_realtime_unsafe {
516+
ret void;
517+
}
518+
514519
; CHECK: define void @f87() [[FNRETTHUNKEXTERN:#[0-9]+]]
515520
define void @f87() fn_ret_thunk_extern { ret void }
516521

@@ -606,6 +611,7 @@ define void @initializes(ptr initializes((-4, 0), (4, 8)) %a) {
606611
; CHECK: attributes #51 = { uwtable(sync) }
607612
; CHECK: attributes #52 = { nosanitize_bounds }
608613
; CHECK: attributes #53 = { sanitize_realtime }
614+
; CHECK: attributes #54 = { sanitize_realtime_unsafe }
609615
; CHECK: attributes [[FNRETTHUNKEXTERN]] = { fn_ret_thunk_extern }
610616
; CHECK: attributes [[SKIPPROFILE]] = { skipprofile }
611617
; CHECK: attributes [[OPTDEBUG]] = { optdebug }

llvm/test/Bitcode/compatibility.ll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ exit:
15901590
; CHECK: select <2 x i1> <i1 true, i1 false>, <2 x i8> <i8 2, i8 3>, <2 x i8> <i8 3, i8 2>
15911591

15921592
call void @f.nobuiltin() builtin
1593-
; CHECK: call void @f.nobuiltin() #53
1593+
; CHECK: call void @f.nobuiltin() #54
15941594

15951595
call fastcc noalias ptr @f.noalias() noinline
15961596
; CHECK: call fastcc noalias ptr @f.noalias() #12
@@ -2020,6 +2020,9 @@ declare void @f.sanitize_numerical_stability() sanitize_numerical_stability
20202020
declare void @f.sanitize_realtime() sanitize_realtime
20212021
; CHECK: declare void @f.sanitize_realtime() #52
20222022

2023+
declare void @f.sanitize_realtime_unsafe() sanitize_realtime_unsafe
2024+
; CHECK: declare void @f.sanitize_realtime_unsafe() #53
2025+
20232026
; CHECK: declare nofpclass(snan) float @nofpclass_snan(float nofpclass(snan))
20242027
declare nofpclass(snan) float @nofpclass_snan(float nofpclass(snan))
20252028

@@ -2143,7 +2146,8 @@ define float @nofpclass_callsites(float %arg) {
21432146
; CHECK: attributes #50 = { allockind("alloc,uninitialized") }
21442147
; CHECK: attributes #51 = { sanitize_numerical_stability }
21452148
; CHECK: attributes #52 = { sanitize_realtime }
2146-
; CHECK: attributes #53 = { builtin }
2149+
; CHECK: attributes #53 = { sanitize_realtime_unsafe }
2150+
; CHECK: attributes #54 = { builtin }
21472151

21482152
;; Metadata
21492153

llvm/test/Verifier/rtsan-attrs.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; RUN: not llvm-as -disable-output %s 2>&1 | FileCheck %s
2+
3+
; CHECK: Attributes 'sanitize_realtime and sanitize_realtime_unsafe' are incompatible!
4+
; CHECK-NEXT: ptr @sanitize_unsafe
5+
define void @sanitize_unsafe() #0 {
6+
ret void
7+
}
8+
9+
attributes #0 = { sanitize_realtime sanitize_realtime_unsafe }

0 commit comments

Comments
 (0)