Skip to content

Commit 14194bb

Browse files
committed
uda: add realTimeUnsafe LDC-specific attribute ...
... this attribute can be used to mark a function as "blocking" (same mechanism as Clang's `[[clang::blocking]]` C/C++ attribute.
1 parent 6bb0ff2 commit 14194bb

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

dmd/id.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ immutable Msgtable[] msgtable =
584584
{ "udaNoSanitize", "noSanitize" },
585585
{ "udaNoSplitStack", "_noSplitStack" },
586586
{ "udaSwiftStub", "swift"},
587+
{ "udaRealTimeUnsafe", "_realTimeUnsafe"},
587588

588589
// IN_LLVM: DCompute specific types and functionss
589590
{ "dcompute" },

dmd/id.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct Id
9898
static Identifier *udaHidden;
9999
static Identifier *udaNoSanitize;
100100
static Identifier *udaNoSplitStack;
101+
static Identifier *udaRealTimeUnsafe;
101102
static Identifier *io;
102103
#endif
103104
};

gen/functions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,10 @@ void DtoDefineFunction(FuncDeclaration *fd, bool linkageAvailableExternally) {
11781178

11791179
#if LDC_LLVM_VER >= 2000
11801180
if (opts::isSanitizerEnabled(opts::RealTimeSanitizer & noSanitizeMask)) {
1181-
func->addFnAttr(LLAttribute::SanitizeRealtime);
1181+
// mimick the `[[clang::blocking]]` attribute behavior
1182+
func->addFnAttr(hasRealTimeUnsafeUDA(fd)
1183+
? LLAttribute::SanitizeRealtimeBlocking
1184+
: LLAttribute::SanitizeRealtime);
11821185
}
11831186
#endif
11841187
}

gen/uda.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,12 @@ bool hasNoSplitStackUDA(FuncDeclaration *fd) {
677677
return sle != nullptr;
678678
}
679679

680+
/// Check whether `fd` has the `@ldc.attributes.realTimeUnsafe` UDA applied.
681+
bool hasRealTimeUnsafeUDA(FuncDeclaration *fd) {
682+
auto sle = getMagicAttribute(fd, Id::udaRealTimeUnsafe, Id::attributes);
683+
return sle != nullptr;
684+
}
685+
680686
/// Creates a mask (for &) of @ldc.attributes.noSanitize UDA applied to the
681687
/// function.
682688
/// If a bit is set in the mask, then the sanitizer is enabled.

gen/uda.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ enum class DComputeCompileFor : int
3939
};
4040
extern "C" DComputeCompileFor hasComputeAttr(Dsymbol *sym);
4141
bool hasNoSplitStackUDA(FuncDeclaration *fd);
42+
bool hasRealTimeUnsafeUDA(FuncDeclaration *fd);
4243

4344
unsigned getMaskFromNoSanitizeUDA(FuncDeclaration &fd);

runtime/druntime/src/ldc/attributes.d

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,22 @@ private struct _noSplitStack
308308
{
309309
}
310310

311+
/**
312+
* Marks this function as real-time unsafe.
313+
* This attribute is akin to Clang's `[[clang::blocking]]` attribute
314+
* and is only effective when realtime sanitizer is enabled
315+
* (e.g. by specifying `-fsanitize=realtime` on the commandline).
316+
*
317+
* If you have a function that is real-time unsafe, rather than using
318+
* `@noSanitize("realtime")` to disable the check, please use this attribute
319+
* to mark the function as real-time unsafe. The sanitizer will also skip
320+
* checking this function for real-time violations.
321+
*/
322+
immutable realTimeUnsafe = _realTimeUnsafe();
323+
private struct _realTimeUnsafe
324+
{
325+
}
326+
311327
/**
312328
* Sets the optimization strategy for a function.
313329
* Valid strategies are "none", "optsize", "minsize". The strategies are mutually exclusive.

0 commit comments

Comments
 (0)