29 #define DEBUG_TYPE "cfguard" 31 STATISTIC(CFGuardCounter,
"Number of Control Flow Guard checks added");
43 enum Mechanism { CF_Check, CF_Dispatch };
49 GuardMechanism = CF_Check;
101 void insertCFGuardCheck(
CallBase *CB);
142 void insertCFGuardDispatch(
CallBase *CB);
144 bool doInitialization(
Module &M)
override;
149 int cfguard_module_flag = 0;
150 Mechanism GuardMechanism = CF_Check;
158 void CFGuard::insertCFGuardCheck(
CallBase *CB) {
161 "Only applicable for Windows targets");
163 "Control Flow Guard checks can only be added to indirect calls");
169 LoadInst *GuardCheckLoad =
B.CreateLoad(GuardFnPtrType, GuardFnGlobal);
174 B.CreateCall(GuardFnType, GuardCheckLoad,
175 {
B.CreateBitCast(CalledOperand,
B.getInt8PtrTy())});
182 void CFGuard::insertCFGuardDispatch(
CallBase *CB) {
185 "Only applicable for Windows targets");
187 "Control Flow Guard checks can only be added to indirect calls");
191 Type *CalledOperandType = CalledOperand->
getType();
195 if (GuardFnGlobal->getType() != PTy)
199 LoadInst *GuardDispatchLoad =
B.CreateLoad(CalledOperandType, GuardFnGlobal);
208 if (
CallInst *CI = dyn_cast<CallInst>(CB)) {
211 assert(isa<InvokeInst>(CB) &&
"Unknown indirect call type");
226 bool CFGuard::doInitialization(
Module &M) {
230 mdconst::extract_or_null<ConstantInt>(M.
getModuleFlag(
"cfguard")))
231 cfguard_module_flag = MD->getZExtValue();
234 if (cfguard_module_flag != 2)
243 if (GuardMechanism == CF_Check) {
247 assert(GuardMechanism == CF_Dispatch &&
"Invalid CFGuard mechanism");
258 if (cfguard_module_flag != 2)
278 if (IndirectCalls.
empty()) {
283 if (GuardMechanism == CF_Dispatch) {
284 for (
CallBase *CB : IndirectCalls) {
285 insertCFGuardDispatch(CB);
288 for (
CallBase *CB : IndirectCalls) {
289 insertCFGuardCheck(CB);
300 return new CFGuard(CFGuard::CF_Check);
304 return new CFGuard(CFGuard::CF_Dispatch);
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
reference emplace_back(ArgTypes &&... Args)
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
FunctionPass * createCFGuardCheckPass()
Insert Control FLow Guard checks on indirect function calls.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
void push_back(const T &Elt)
This class represents a function call, abstracting a target machine's calling convention.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
FunctionPass * createCFGuardDispatchPass()
Insert Control FLow Guard dispatches on indirect function calls.
STATISTIC(NumFunctions, "Total number of functions")
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
An instruction for reading from memory.
bool isIndirectCall() const
Return true if the callsite is an indirect call.
LLVMContext & getContext() const
Get the global data context.
Class to represent function types.
Type * getType() const
All values are typed, get the type of this value.
Value * getCalledOperand() const
void setCalledOperand(Value *V)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Class to represent pointers.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static bool runOnFunction(Function &F, bool PostInlining)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
This is an important base class in LLVM.
static Type * getVoidTy(LLVMContext &C)
void setCallingConv(CallingConv::ID CC)
FunctionPass class - This class is used to implement most global optimizations.
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
void initializeCFGuardPass(PassRegistry &)
Triple - Helper class for working with autoconf configuration names.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Constant * getOrInsertGlobal(StringRef Name, Type *Ty, function_ref< GlobalVariable *()> CreateGlobalCallback)
Look up the specified global in the module symbol table.
LLVM_NODISCARD bool empty() const
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value *> Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const BasicBlockListType & getBasicBlockList() const
Get the underlying elements of the Function...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Special calling convention on Windows for calling the Control Guard Check ICall funtion.
LLVM Value Representation.
A container for an operand bundle being viewed as a set of values rather than a set of uses...