Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 1 | //===-- CrossDSOCFI.cpp - Externalize this module's CFI checks ------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass exports all llvm.bitset's found in the module in the form of a |
| 10 | // __cfi_check function, which can be used to verify cross-DSO call targets. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Davide Italiano | 92b933a | 2016-07-09 03:25:35 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/IPO/CrossDSOCFI.h" |
Mandeep Singh Grang | e3bbb68 | 2017-08-30 04:47:21 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SetVector.h" |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Evgeniy Stepanov | 4731ad81 | 2017-08-29 22:29:15 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constant.h" |
| 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/GlobalObject.h" |
| 22 | #include "llvm/IR/GlobalVariable.h" |
| 23 | #include "llvm/IR/IRBuilder.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/Intrinsics.h" |
| 26 | #include "llvm/IR/MDBuilder.h" |
| 27 | #include "llvm/IR/Module.h" |
| 28 | #include "llvm/IR/Operator.h" |
| 29 | #include "llvm/Pass.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Davide Italiano | 92b933a | 2016-07-09 03:25:35 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/IPO.h" |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | #define DEBUG_TYPE "cross-dso-cfi" |
| 37 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 38 | STATISTIC(NumTypeIds, "Number of unique type identifiers"); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 39 | |
| 40 | namespace { |
| 41 | |
| 42 | struct CrossDSOCFI : public ModulePass { |
| 43 | static char ID; |
| 44 | CrossDSOCFI() : ModulePass(ID) { |
| 45 | initializeCrossDSOCFIPass(*PassRegistry::getPassRegistry()); |
| 46 | } |
| 47 | |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 48 | MDNode *VeryLikelyWeights; |
| 49 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 50 | ConstantInt *extractNumericTypeId(MDNode *MD); |
Davide Italiano | b4b9db8 | 2016-07-08 19:30:06 +0000 | [diff] [blame] | 51 | void buildCFICheck(Module &M); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 52 | bool runOnModule(Module &M) override; |
| 53 | }; |
| 54 | |
| 55 | } // anonymous namespace |
| 56 | |
| 57 | INITIALIZE_PASS_BEGIN(CrossDSOCFI, "cross-dso-cfi", "Cross-DSO CFI", false, |
| 58 | false) |
| 59 | INITIALIZE_PASS_END(CrossDSOCFI, "cross-dso-cfi", "Cross-DSO CFI", false, false) |
| 60 | char CrossDSOCFI::ID = 0; |
| 61 | |
| 62 | ModulePass *llvm::createCrossDSOCFIPass() { return new CrossDSOCFI; } |
| 63 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 64 | /// Extracts a numeric type identifier from an MDNode containing type metadata. |
| 65 | ConstantInt *CrossDSOCFI::extractNumericTypeId(MDNode *MD) { |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 66 | // This check excludes vtables for classes inside anonymous namespaces. |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 67 | auto TM = dyn_cast<ValueAsMetadata>(MD->getOperand(1)); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 68 | if (!TM) |
| 69 | return nullptr; |
| 70 | auto C = dyn_cast_or_null<ConstantInt>(TM->getValue()); |
| 71 | if (!C) return nullptr; |
| 72 | // We are looking for i64 constants. |
| 73 | if (C->getBitWidth() != 64) return nullptr; |
| 74 | |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 75 | return C; |
| 76 | } |
| 77 | |
| 78 | /// buildCFICheck - emits __cfi_check for the current module. |
Davide Italiano | b4b9db8 | 2016-07-08 19:30:06 +0000 | [diff] [blame] | 79 | void CrossDSOCFI::buildCFICheck(Module &M) { |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 80 | // FIXME: verify that __cfi_check ends up near the end of the code section, |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 81 | // but before the jump slots created in LowerTypeTests. |
Mandeep Singh Grang | e3bbb68 | 2017-08-30 04:47:21 +0000 | [diff] [blame] | 82 | SetVector<uint64_t> TypeIds; |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 83 | SmallVector<MDNode *, 2> Types; |
Davide Italiano | b4b9db8 | 2016-07-08 19:30:06 +0000 | [diff] [blame] | 84 | for (GlobalObject &GO : M.global_objects()) { |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 85 | Types.clear(); |
| 86 | GO.getMetadata(LLVMContext::MD_type, Types); |
| 87 | for (MDNode *Type : Types) { |
| 88 | // Sanity check. GO must not be a function declaration. |
Peter Collingbourne | 995d6cc | 2016-06-24 21:37:11 +0000 | [diff] [blame] | 89 | assert(!isa<Function>(&GO) || !cast<Function>(&GO)->isDeclaration()); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 90 | |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 91 | if (ConstantInt *TypeId = extractNumericTypeId(Type)) |
| 92 | TypeIds.insert(TypeId->getZExtValue()); |
| 93 | } |
| 94 | } |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 95 | |
Evgeniy Stepanov | 4d4ee93 | 2017-06-16 00:18:29 +0000 | [diff] [blame] | 96 | NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions"); |
| 97 | if (CfiFunctionsMD) { |
| 98 | for (auto Func : CfiFunctionsMD->operands()) { |
| 99 | assert(Func->getNumOperands() >= 2); |
| 100 | for (unsigned I = 2; I < Func->getNumOperands(); ++I) |
| 101 | if (ConstantInt *TypeId = |
| 102 | extractNumericTypeId(cast<MDNode>(Func->getOperand(I).get()))) |
| 103 | TypeIds.insert(TypeId->getZExtValue()); |
| 104 | } |
| 105 | } |
| 106 | |
Davide Italiano | b4b9db8 | 2016-07-08 19:30:06 +0000 | [diff] [blame] | 107 | LLVMContext &Ctx = M.getContext(); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 108 | FunctionCallee C = M.getOrInsertFunction( |
Evgeniy Stepanov | fbc3da5 | 2016-01-25 23:35:03 +0000 | [diff] [blame] | 109 | "__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 110 | Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx)); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 111 | Function *F = dyn_cast<Function>(C.getCallee()); |
Evgeniy Stepanov | 349adba | 2017-04-07 23:00:20 +0000 | [diff] [blame] | 112 | // Take over the existing function. The frontend emits a weak stub so that the |
| 113 | // linker knows about the symbol; this pass replaces the function body. |
| 114 | F->deleteBody(); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 115 | F->setAlignment(4096); |
Evgeniy Stepanov | 4731ad81 | 2017-08-29 22:29:15 +0000 | [diff] [blame] | 116 | |
| 117 | Triple T(M.getTargetTriple()); |
| 118 | if (T.isARM() || T.isThumb()) |
| 119 | F->addFnAttr("target-features", "+thumb-mode"); |
| 120 | |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 121 | auto args = F->arg_begin(); |
Evgeniy Stepanov | fbc3da5 | 2016-01-25 23:35:03 +0000 | [diff] [blame] | 122 | Value &CallSiteTypeId = *(args++); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 123 | CallSiteTypeId.setName("CallSiteTypeId"); |
Evgeniy Stepanov | fbc3da5 | 2016-01-25 23:35:03 +0000 | [diff] [blame] | 124 | Value &Addr = *(args++); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 125 | Addr.setName("Addr"); |
Evgeniy Stepanov | fbc3da5 | 2016-01-25 23:35:03 +0000 | [diff] [blame] | 126 | Value &CFICheckFailData = *(args++); |
| 127 | CFICheckFailData.setName("CFICheckFailData"); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 128 | assert(args == F->arg_end()); |
| 129 | |
| 130 | BasicBlock *BB = BasicBlock::Create(Ctx, "entry", F); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 131 | BasicBlock *ExitBB = BasicBlock::Create(Ctx, "exit", F); |
Evgeniy Stepanov | fbc3da5 | 2016-01-25 23:35:03 +0000 | [diff] [blame] | 132 | |
| 133 | BasicBlock *TrapBB = BasicBlock::Create(Ctx, "fail", F); |
| 134 | IRBuilder<> IRBFail(TrapBB); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 135 | FunctionCallee CFICheckFailFn = |
| 136 | M.getOrInsertFunction("__cfi_check_fail", Type::getVoidTy(Ctx), |
| 137 | Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx)); |
Evgeniy Stepanov | fbc3da5 | 2016-01-25 23:35:03 +0000 | [diff] [blame] | 138 | IRBFail.CreateCall(CFICheckFailFn, {&CFICheckFailData, &Addr}); |
| 139 | IRBFail.CreateBr(ExitBB); |
| 140 | |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 141 | IRBuilder<> IRBExit(ExitBB); |
| 142 | IRBExit.CreateRetVoid(); |
| 143 | |
| 144 | IRBuilder<> IRB(BB); |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 145 | SwitchInst *SI = IRB.CreateSwitch(&CallSiteTypeId, TrapBB, TypeIds.size()); |
| 146 | for (uint64_t TypeId : TypeIds) { |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 147 | ConstantInt *CaseTypeId = ConstantInt::get(Type::getInt64Ty(Ctx), TypeId); |
| 148 | BasicBlock *TestBB = BasicBlock::Create(Ctx, "test", F); |
| 149 | IRBuilder<> IRBTest(TestBB); |
Davide Italiano | b4b9db8 | 2016-07-08 19:30:06 +0000 | [diff] [blame] | 150 | Function *BitsetTestFn = Intrinsic::getDeclaration(&M, Intrinsic::type_test); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 151 | |
| 152 | Value *Test = IRBTest.CreateCall( |
| 153 | BitsetTestFn, {&Addr, MetadataAsValue::get( |
| 154 | Ctx, ConstantAsMetadata::get(CaseTypeId))}); |
| 155 | BranchInst *BI = IRBTest.CreateCondBr(Test, ExitBB, TrapBB); |
| 156 | BI->setMetadata(LLVMContext::MD_prof, VeryLikelyWeights); |
| 157 | |
| 158 | SI->addCase(CaseTypeId, TestBB); |
Peter Collingbourne | 7efd750 | 2016-06-24 21:21:32 +0000 | [diff] [blame] | 159 | ++NumTypeIds; |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | bool CrossDSOCFI::runOnModule(Module &M) { |
Davide Italiano | b4b9db8 | 2016-07-08 19:30:06 +0000 | [diff] [blame] | 164 | VeryLikelyWeights = |
| 165 | MDBuilder(M.getContext()).createBranchWeights((1U << 20) - 1, 1); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 166 | if (M.getModuleFlag("Cross-DSO CFI") == nullptr) |
| 167 | return false; |
Davide Italiano | b4b9db8 | 2016-07-08 19:30:06 +0000 | [diff] [blame] | 168 | buildCFICheck(M); |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 169 | return true; |
| 170 | } |
Davide Italiano | 92b933a | 2016-07-09 03:25:35 +0000 | [diff] [blame] | 171 | |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 172 | PreservedAnalyses CrossDSOCFIPass::run(Module &M, ModuleAnalysisManager &AM) { |
Davide Italiano | 92b933a | 2016-07-09 03:25:35 +0000 | [diff] [blame] | 173 | CrossDSOCFI Impl; |
| 174 | bool Changed = Impl.runOnModule(M); |
| 175 | if (!Changed) |
| 176 | return PreservedAnalyses::all(); |
| 177 | return PreservedAnalyses::none(); |
| 178 | } |