30#define DEBUG_TYPE "cfguard"
32STATISTIC(CFGuardCounter,
"Number of Control Flow Guard checks added");
47 CFGuardImpl(Mechanism M) : GuardMechanism(M) {
49 switch (GuardMechanism) {
50 case Mechanism::Check:
53 case Mechanism::Dispatch:
102 void insertCFGuardCheck(
CallBase *CB);
143 void insertCFGuardDispatch(
CallBase *CB);
145 bool doInitialization(
Module &M);
150 int cfguard_module_flag = 0;
152 Mechanism GuardMechanism = Mechanism::Check;
169 bool doInitialization(
Module &M)
override {
return Impl.doInitialization(M); }
175void CFGuardImpl::insertCFGuardCheck(
CallBase *CB) {
178 "Only applicable for Windows targets");
180 "Control Flow Guard checks can only be added to indirect calls");
192 LoadInst *GuardCheckLoad =
B.CreateLoad(GuardFnPtrType, GuardFnGlobal);
196 CallInst *GuardCheck =
197 B.CreateCall(GuardFnType, GuardCheckLoad, {CalledOperand}, Bundles);
204void CFGuardImpl::insertCFGuardDispatch(CallBase *CB) {
207 "Only applicable for Windows targets");
209 "Control Flow Guard checks can only be added to indirect calls");
213 Type *CalledOperandType = CalledOperand->
getType();
216 LoadInst *GuardDispatchLoad =
B.CreateLoad(CalledOperandType, GuardFnGlobal);
225 "Unknown indirect call type");
238bool CFGuardImpl::doInitialization(
Module &M) {
243 cfguard_module_flag = MD->getZExtValue();
246 if (cfguard_module_flag != 2)
251 FunctionType::get(Type::getVoidTy(
M.getContext()),
252 {PointerType::getUnqual(M.getContext())},
false);
253 GuardFnPtrType = PointerType::get(
M.getContext(), 0);
255 GuardFnGlobal =
M.getOrInsertGlobal(GuardFnName, GuardFnPtrType, [&] {
256 auto *Var =
new GlobalVariable(M, GuardFnPtrType,
false,
257 GlobalVariable::ExternalLinkage,
nullptr,
259 Var->setDSOLocal(
true);
266bool CFGuardImpl::runOnFunction(Function &
F) {
269 if (cfguard_module_flag != 2)
278 for (BasicBlock &BB :
F) {
279 for (Instruction &
I : BB) {
289 if (IndirectCalls.
empty()) {
294 if (GuardMechanism == Mechanism::Dispatch) {
295 for (CallBase *CB : IndirectCalls) {
296 insertCFGuardDispatch(CB);
299 for (CallBase *CB : IndirectCalls) {
300 insertCFGuardCheck(CB);
308 CFGuardImpl Impl(GuardMechanism);
309 bool Changed = Impl.doInitialization(*
F.getParent());
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
constexpr StringRef GuardCheckFunctionName
constexpr StringRef GuardDispatchFunctionName
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
FunctionAnalysisManager FAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void setCallingConv(CallingConv::ID CC)
LLVM_ABI void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
Value * getCalledOperand() const
static LLVM_ABI CallBase * Create(CallBase *CB, ArrayRef< OperandBundleDef > Bundles, InsertPosition InsertPt=nullptr)
Create a clone of CB with a different set of operand bundles and insert it before InsertPt.
void setCalledOperand(Value *V)
This is an important base class in LLVM.
FunctionPass class - This class is used to implement most global optimizations.
LinkageTypes getLinkage() const
@ ExternalLinkage
Externally visible function.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A Module instance is used to store all the information related to an LLVM module.
const Triple & getTargetTriple() const
Get the target triple which is a string describing the target host.
A container for an operand bundle being viewed as a set of values rather than a set of uses.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
bool isOSWindows() const
Tests whether the OS is Windows.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
bool isCFGuardFunction(const GlobalValue *GV)
FunctionPass * createCFGuardDispatchPass()
Insert Control FLow Guard dispatches on indirect function calls.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI void initializeCFGuardPass(PassRegistry &)
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
OperandBundleDefT< Value * > OperandBundleDef
FunctionPass * createCFGuardCheckPass()
Insert Control FLow Guard checks on indirect function calls.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.