29#define DEBUG_TYPE "cross-dso-cfi"
31STATISTIC(NumTypeIds,
"Number of unique type identifiers");
39 void buildCFICheck(
Module &M);
40 bool runOnModule(
Module &M);
52 if (!
C)
return nullptr;
54 if (
C->getBitWidth() != 64)
return nullptr;
60void CrossDSOCFI::buildCFICheck(
Module &M) {
63 SetVector<uint64_t> TypeIds;
65 for (GlobalObject &GO :
M.global_objects()) {
67 GO.getMetadata(LLVMContext::MD_type, Types);
68 for (MDNode *
Type : Types)
69 if (ConstantInt *TypeId = extractNumericTypeId(
Type))
70 TypeIds.
insert(TypeId->getZExtValue());
73 NamedMDNode *CfiFunctionsMD =
M.getNamedMetadata(
"cfi.functions");
75 for (
auto *Func : CfiFunctionsMD->
operands()) {
77 for (
unsigned I = 2;
I <
Func->getNumOperands(); ++
I)
78 if (ConstantInt *TypeId =
80 TypeIds.
insert(TypeId->getZExtValue());
84 LLVMContext &Ctx =
M.getContext();
85 FunctionCallee
C =
M.getOrInsertFunction(
86 "__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx),
87 PointerType::getUnqual(Ctx), PointerType::getUnqual(Ctx));
92 F->setAlignment(
Align(4096));
94 Triple
T(
M.getTargetTriple());
95 if (
T.isARM() ||
T.isThumb())
96 F->addFnAttr(
"target-features",
"+thumb-mode");
98 auto args =
F->arg_begin();
100 CallSiteTypeId.
setName(
"CallSiteTypeId");
104 CFICheckFailData.
setName(
"CFICheckFailData");
112 FunctionCallee CFICheckFailFn =
M.getOrInsertFunction(
113 "__cfi_check_fail", Type::getVoidTy(Ctx), PointerType::getUnqual(Ctx),
114 PointerType::getUnqual(Ctx));
115 IRBFail.CreateCall(CFICheckFailFn, {&CFICheckFailData, &Addr});
116 IRBFail.CreateBr(ExitBB);
119 IRBExit.CreateRetVoid();
122 SwitchInst *
SI = IRB.CreateSwitch(&CallSiteTypeId, TrapBB, TypeIds.
size());
123 for (uint64_t TypeId : TypeIds) {
124 ConstantInt *CaseTypeId = ConstantInt::get(Type::getInt64Ty(Ctx), TypeId);
129 Intrinsic::type_test,
132 BranchInst *BI = IRBTest.CreateCondBr(
Test, ExitBB, TrapBB);
133 BI->
setMetadata(LLVMContext::MD_prof, VeryLikelyWeights);
135 SI->addCase(CaseTypeId, TestBB);
140bool CrossDSOCFI::runOnModule(
Module &M) {
141 VeryLikelyWeights = MDBuilder(
M.getContext()).createLikelyBranchWeights();
142 if (
M.getModuleFlag(
"Cross-DSO CFI") ==
nullptr)
150 bool Changed = Impl.runOnModule(M);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
This file implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
This is the shared class of boolean and integer constants.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
const MDOperand & getOperand(unsigned I) const
A Module instance is used to store all the information related to an LLVM module.
iterator_range< op_iterator > operands()
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.
size_type size() const
Determine the number of elements in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ C
The default llvm calling convention, compatible with C.
@ BasicBlock
Various leaf nodes.
NodeAddr< FuncNode * > Func
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.
auto dyn_cast_or_null(const Y &Val)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.