LLVM 20.0.0git
RealtimeSanitizer.cpp
Go to the documentation of this file.
1//===- RealtimeSanitizer.cpp - RealtimeSanitizer instrumentation *- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of the RealtimeSanitizer, an LLVM transformation for
10// detecting and reporting realtime safety violations.
11//
12// See also: llvm-project/compiler-rt/lib/rtsan/
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/IR/Analysis.h"
17#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/Module.h"
21
24
25using namespace llvm;
26
27const char kRtsanModuleCtorName[] = "rtsan.module_ctor";
28const char kRtsanInitName[] = "__rtsan_ensure_initialized";
29
32 for (Value *Arg : FunctionArgs)
33 Types.push_back(Arg->getType());
34 return Types;
35}
36
38 const char *FunctionName,
39 ArrayRef<Value *> FunctionArgs) {
40 LLVMContext &Context = Fn.getContext();
41 FunctionType *FuncType = FunctionType::get(Type::getVoidTy(Context),
42 getArgTypes(FunctionArgs), false);
43 FunctionCallee Func =
44 Fn.getParent()->getOrInsertFunction(FunctionName, FuncType);
45 IRBuilder<> Builder{&Instruction};
46 Builder.CreateCall(Func, FunctionArgs);
47}
48
50 const char *InsertFnName,
51 ArrayRef<Value *> FunctionArgs) {
52 insertCallBeforeInstruction(Fn, Fn.front().front(), InsertFnName,
53 FunctionArgs);
54}
55
57 const char *InsertFnName,
58 ArrayRef<Value *> FunctionArgs) {
59 for (auto &I : instructions(Fn))
60 if (isa<ReturnInst>(&I))
61 insertCallBeforeInstruction(Fn, I, InsertFnName, FunctionArgs);
62}
63
67 return PA;
68}
69
71 insertCallAtFunctionEntryPoint(Fn, "__rtsan_realtime_enter", {});
72 insertCallAtAllFunctionExitPoints(Fn, "__rtsan_realtime_exit", {});
74}
75
77 IRBuilder<> Builder(&Fn.front().front());
79 insertCallAtFunctionEntryPoint(Fn, "__rtsan_notify_blocking_call", {Name});
81}
82
86 M, kRtsanModuleCtorName, kRtsanInitName, /*InitArgTypes=*/{},
87 /*InitArgs=*/{},
88 // This callback is invoked when the functions are created the first
89 // time. Hook them into the global ctors list in that case:
90 [&](Function *Ctor, FunctionCallee) { appendToGlobalCtors(M, Ctor, 0); });
91
92 for (Function &F : M) {
93 if (F.hasFnAttribute(Attribute::SanitizeRealtime))
95
96 if (F.hasFnAttribute(Attribute::SanitizeRealtimeBlocking))
98 }
99
101}
Expand Atomic instructions
std::string Name
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
ModuleAnalysisManager MAM
static SmallVector< Type * > getArgTypes(ArrayRef< Value * > FunctionArgs)
static PreservedAnalyses runSanitizeRealtime(Function &Fn)
static PreservedAnalyses rtsanPreservedCFGAnalyses()
static PreservedAnalyses runSanitizeRealtimeBlocking(Function &Fn)
static void insertCallAtAllFunctionExitPoints(Function &Fn, const char *InsertFnName, ArrayRef< Value * > FunctionArgs)
static void insertCallBeforeInstruction(Function &Fn, Instruction &Instruction, const char *FunctionName, ArrayRef< Value * > FunctionArgs)
const char kRtsanInitName[]
static void insertCallAtFunctionEntryPoint(Function &Fn, const char *InsertFnName, ArrayRef< Value * > FunctionArgs)
const char kRtsanModuleCtorName[]
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const Instruction & front() const
Definition: BasicBlock.h:471
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
const BasicBlock & front() const
Definition: Function.h:860
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:369
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
GlobalVariable * CreateGlobalString(StringRef Str, const Twine &Name="", unsigned AddressSpace=0, Module *M=nullptr, bool AddNull=true)
Make a new global variable with initializer type i8*.
Definition: IRBuilder.cpp:44
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:204
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
static Type * getVoidTy(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::pair< Function *, FunctionCallee > getOrCreateSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, function_ref< void(Function *, FunctionCallee)> FunctionsCreatedCallback, StringRef VersionCheckName=StringRef(), bool Weak=false)
Creates sanitizer constructor function lazily.
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:74
std::string demangle(std::string_view MangledName)
Attempt to demangle a string using different demangling schemes.
Definition: Demangle.cpp:20
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)