blob: 99d583339548c967b5c015385a03ea1bf36cae97 [file] [log] [blame]
Evgeniy Stepanov67849d52015-12-15 23:00:08 +00001//===-- CrossDSOCFI.cpp - Externalize this module's CFI checks ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass exports all llvm.bitset's found in the module in the form of a
11// __cfi_check function, which can be used to verify cross-DSO call targets.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/IPO.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/EquivalenceClasses.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/IR/Constant.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/GlobalObject.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/MDBuilder.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/Operator.h"
30#include "llvm/Pass.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Transforms/Utils/BasicBlockUtils.h"
34
35using namespace llvm;
36
37#define DEBUG_TYPE "cross-dso-cfi"
38
Peter Collingbourne7efd7502016-06-24 21:21:32 +000039STATISTIC(NumTypeIds, "Number of unique type identifiers");
Evgeniy Stepanov67849d52015-12-15 23:00:08 +000040
41namespace {
42
43struct CrossDSOCFI : public ModulePass {
44 static char ID;
45 CrossDSOCFI() : ModulePass(ID) {
46 initializeCrossDSOCFIPass(*PassRegistry::getPassRegistry());
47 }
48
49 Module *M;
50 MDNode *VeryLikelyWeights;
51
Peter Collingbourne7efd7502016-06-24 21:21:32 +000052 ConstantInt *extractNumericTypeId(MDNode *MD);
Evgeniy Stepanov67849d52015-12-15 23:00:08 +000053 void buildCFICheck();
54
55 bool doInitialization(Module &M) override;
56 bool runOnModule(Module &M) override;
57};
58
59} // anonymous namespace
60
61INITIALIZE_PASS_BEGIN(CrossDSOCFI, "cross-dso-cfi", "Cross-DSO CFI", false,
62 false)
63INITIALIZE_PASS_END(CrossDSOCFI, "cross-dso-cfi", "Cross-DSO CFI", false, false)
64char CrossDSOCFI::ID = 0;
65
66ModulePass *llvm::createCrossDSOCFIPass() { return new CrossDSOCFI; }
67
68bool CrossDSOCFI::doInitialization(Module &Mod) {
69 M = &Mod;
70 VeryLikelyWeights =
71 MDBuilder(M->getContext()).createBranchWeights((1U << 20) - 1, 1);
72
73 return false;
74}
75
Peter Collingbourne7efd7502016-06-24 21:21:32 +000076/// Extracts a numeric type identifier from an MDNode containing type metadata.
77ConstantInt *CrossDSOCFI::extractNumericTypeId(MDNode *MD) {
Evgeniy Stepanov67849d52015-12-15 23:00:08 +000078 // This check excludes vtables for classes inside anonymous namespaces.
Peter Collingbourne7efd7502016-06-24 21:21:32 +000079 auto TM = dyn_cast<ValueAsMetadata>(MD->getOperand(1));
Evgeniy Stepanov67849d52015-12-15 23:00:08 +000080 if (!TM)
81 return nullptr;
82 auto C = dyn_cast_or_null<ConstantInt>(TM->getValue());
83 if (!C) return nullptr;
84 // We are looking for i64 constants.
85 if (C->getBitWidth() != 64) return nullptr;
86
Evgeniy Stepanov67849d52015-12-15 23:00:08 +000087 return C;
88}
89
90/// buildCFICheck - emits __cfi_check for the current module.
91void CrossDSOCFI::buildCFICheck() {
92 // FIXME: verify that __cfi_check ends up near the end of the code section,
Peter Collingbourne7efd7502016-06-24 21:21:32 +000093 // but before the jump slots created in LowerTypeTests.
94 llvm::DenseSet<uint64_t> TypeIds;
95 SmallVector<MDNode *, 2> Types;
96 for (GlobalObject &GO : M->global_objects()) {
97 Types.clear();
98 GO.getMetadata(LLVMContext::MD_type, Types);
99 for (MDNode *Type : Types) {
100 // Sanity check. GO must not be a function declaration.
101 auto F = dyn_cast<Function>(&GO);
102 assert(!F || !F->isDeclaration());
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000103
Peter Collingbourne7efd7502016-06-24 21:21:32 +0000104 if (ConstantInt *TypeId = extractNumericTypeId(Type))
105 TypeIds.insert(TypeId->getZExtValue());
106 }
107 }
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000108
109 LLVMContext &Ctx = M->getContext();
110 Constant *C = M->getOrInsertFunction(
Evgeniy Stepanovfbc3da52016-01-25 23:35:03 +0000111 "__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx),
112 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx), nullptr);
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000113 Function *F = dyn_cast<Function>(C);
114 F->setAlignment(4096);
115 auto args = F->arg_begin();
Evgeniy Stepanovfbc3da52016-01-25 23:35:03 +0000116 Value &CallSiteTypeId = *(args++);
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000117 CallSiteTypeId.setName("CallSiteTypeId");
Evgeniy Stepanovfbc3da52016-01-25 23:35:03 +0000118 Value &Addr = *(args++);
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000119 Addr.setName("Addr");
Evgeniy Stepanovfbc3da52016-01-25 23:35:03 +0000120 Value &CFICheckFailData = *(args++);
121 CFICheckFailData.setName("CFICheckFailData");
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000122 assert(args == F->arg_end());
123
124 BasicBlock *BB = BasicBlock::Create(Ctx, "entry", F);
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000125 BasicBlock *ExitBB = BasicBlock::Create(Ctx, "exit", F);
Evgeniy Stepanovfbc3da52016-01-25 23:35:03 +0000126
127 BasicBlock *TrapBB = BasicBlock::Create(Ctx, "fail", F);
128 IRBuilder<> IRBFail(TrapBB);
129 Constant *CFICheckFailFn = M->getOrInsertFunction(
130 "__cfi_check_fail", Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx),
131 Type::getInt8PtrTy(Ctx), nullptr);
132 IRBFail.CreateCall(CFICheckFailFn, {&CFICheckFailData, &Addr});
133 IRBFail.CreateBr(ExitBB);
134
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000135 IRBuilder<> IRBExit(ExitBB);
136 IRBExit.CreateRetVoid();
137
138 IRBuilder<> IRB(BB);
Peter Collingbourne7efd7502016-06-24 21:21:32 +0000139 SwitchInst *SI = IRB.CreateSwitch(&CallSiteTypeId, TrapBB, TypeIds.size());
140 for (uint64_t TypeId : TypeIds) {
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000141 ConstantInt *CaseTypeId = ConstantInt::get(Type::getInt64Ty(Ctx), TypeId);
142 BasicBlock *TestBB = BasicBlock::Create(Ctx, "test", F);
143 IRBuilder<> IRBTest(TestBB);
Peter Collingbourne7efd7502016-06-24 21:21:32 +0000144 Function *BitsetTestFn = Intrinsic::getDeclaration(M, Intrinsic::type_test);
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000145
146 Value *Test = IRBTest.CreateCall(
147 BitsetTestFn, {&Addr, MetadataAsValue::get(
148 Ctx, ConstantAsMetadata::get(CaseTypeId))});
149 BranchInst *BI = IRBTest.CreateCondBr(Test, ExitBB, TrapBB);
150 BI->setMetadata(LLVMContext::MD_prof, VeryLikelyWeights);
151
152 SI->addCase(CaseTypeId, TestBB);
Peter Collingbourne7efd7502016-06-24 21:21:32 +0000153 ++NumTypeIds;
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000154 }
155}
156
157bool CrossDSOCFI::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000158 if (skipModule(M))
159 return false;
160
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000161 if (M.getModuleFlag("Cross-DSO CFI") == nullptr)
162 return false;
163 buildCFICheck();
164 return true;
165}