Skip to content

[LLVM][rtsan] Add module pass to initialize rtsan #118989

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 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,14 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
FPM.addPass(BoundsCheckingPass());
});

if (LangOpts.Sanitize.has(SanitizerKind::Realtime))
if (LangOpts.Sanitize.has(SanitizerKind::Realtime)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjappl
@why do we use this location and not addSanitizers? In particular registerOptimizerLastEPCallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll investigate if moving it is possible! Off the top of my head I don't know if there is a reason.

I'll report back with PRs on either/both of these soon.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to consider performance vs quality.

For other sanitizers it's essentially:

  1. If it's early - better precision - in sense we detect issues in C/C++ program which can be optimized out. (can Realtime related issue be optimized out, and do you care about them at all)
  2. If it's late - better performance, but we may miss bugs
    I believe performance is ~2x difference, so we can't afford that e.g. in Asan.

PB.registerScalarOptimizerLateEPCallback(
[](FunctionPassManager &FPM, OptimizationLevel Level) {
RealtimeSanitizerOptions Opts;
FPM.addPass(RealtimeSanitizerPass(Opts));
});
MPM.addPass(ModuleRealtimeSanitizerPass());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we converted all but tsan from function+module to just module pass. Easier to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll work on swapping rtsan over.

}

// Don't add sanitizers if we are here from ThinLTO PostLink. That already
// done on PreLink stage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class RealtimeSanitizerPass : public PassInfoMixin<RealtimeSanitizerPass> {
static bool isRequired() { return true; }
};

/// Create ctor and init functions.
struct ModuleRealtimeSanitizerPass
: public PassInfoMixin<ModuleRealtimeSanitizerPass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
static bool isRequired() { return true; }
};

} // namespace llvm

#endif // LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZER_H
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ MODULE_PASS("rel-lookup-table-converter", RelLookupTableConverterPass())
MODULE_PASS("rewrite-statepoints-for-gc", RewriteStatepointsForGC())
MODULE_PASS("rewrite-symbols", RewriteSymbolPass())
MODULE_PASS("rpo-function-attrs", ReversePostOrderFunctionAttrsPass())
MODULE_PASS("rtsan-module", ModuleRealtimeSanitizerPass())
MODULE_PASS("sample-profile", SampleProfileLoaderPass())
MODULE_PASS("sancov-module", SanitizerCoveragePass())
MODULE_PASS("sanmd-module", SanitizerBinaryMetadataPass())
Expand Down
15 changes: 15 additions & 0 deletions llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"

#include "llvm/Demangle/Demangle.h"
#include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"

using namespace llvm;

const char kRtsanModuleCtorName[] = "rtsan.module_ctor";
const char kRtsanInitName[] = "__rtsan_ensure_initialized";

static SmallVector<Type *> getArgTypes(ArrayRef<Value *> FunctionArgs) {
SmallVector<Type *> Types;
for (Value *Arg : FunctionArgs)
Expand Down Expand Up @@ -89,3 +93,14 @@ PreservedAnalyses RealtimeSanitizerPass::run(Function &Fn,

return PreservedAnalyses::all();
}

PreservedAnalyses ModuleRealtimeSanitizerPass::run(Module &M,
ModuleAnalysisManager &MAM) {
getOrCreateSanitizerCtorAndInitFunctions(
M, kRtsanModuleCtorName, kRtsanInitName, /*InitArgTypes=*/{},
/*InitArgs=*/{},
// This callback is invoked when the functions are created the first
// time. Hook them into the global ctors list in that case:
[&](Function *Ctor, FunctionCallee) { appendToGlobalCtors(M, Ctor, 0); });
return PreservedAnalyses::none();
}
9 changes: 8 additions & 1 deletion llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: opt < %s -passes=rtsan -S | FileCheck %s
; RUN: opt < %s -passes='function(rtsan),module(rtsan-module)' -S | FileCheck %s

define void @violation() #0 {
%1 = alloca ptr, align 8
Expand All @@ -18,10 +18,17 @@ define noundef i32 @main() #2 {

attributes #0 = { mustprogress noinline sanitize_realtime optnone ssp uwtable(sync) }

; RealtimeSanitizer pass should insert call to initialize the runtime
; CHECK: @llvm.used = appending global [1 x ptr] [ptr @rtsan.module_ctor]
; CHECK: @llvm.global_ctors = {{.*}}@rtsan.module_ctor

; RealtimeSanitizer pass should insert __rtsan_realtime_enter right after function definition
; CHECK-LABEL: @violation()
; CHECK-NEXT: call{{.*}}@__rtsan_realtime_enter

; RealtimeSanitizer pass should insert __rtsan_realtime_exit right before function return
; CHECK: call{{.*}}@__rtsan_realtime_exit
; CHECK-NEXT: ret{{.*}}void

; CHECK-LABEL: define internal void @rtsan.module_ctor()
; CHECK-NEXT: call void @__rtsan_ensure_initialized()
Loading