Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1 | //===------ BPFAbstractMemberAccess.cpp - Abstracting Member Accesses -----===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass abstracted struct/union member accesses in order to support |
| 10 | // compile-once run-everywhere (CO-RE). The CO-RE intends to compile the program |
| 11 | // which can run on different kernels. In particular, if bpf program tries to |
| 12 | // access a particular kernel data structure member, the details of the |
| 13 | // intermediate member access will be remembered so bpf loader can do |
| 14 | // necessary adjustment right before program loading. |
| 15 | // |
| 16 | // For example, |
| 17 | // |
| 18 | // struct s { |
| 19 | // int a; |
| 20 | // int b; |
| 21 | // }; |
| 22 | // struct t { |
| 23 | // struct s c; |
| 24 | // int d; |
| 25 | // }; |
| 26 | // struct t e; |
| 27 | // |
| 28 | // For the member access e.c.b, the compiler will generate code |
| 29 | // &e + 4 |
| 30 | // |
| 31 | // The compile-once run-everywhere instead generates the following code |
| 32 | // r = 4 |
| 33 | // &e + r |
| 34 | // The "4" in "r = 4" can be changed based on a particular kernel version. |
| 35 | // For example, on a particular kernel version, if struct s is changed to |
| 36 | // |
| 37 | // struct s { |
| 38 | // int new_field; |
| 39 | // int a; |
| 40 | // int b; |
| 41 | // } |
| 42 | // |
| 43 | // By repeating the member access on the host, the bpf loader can |
| 44 | // adjust "r = 4" as "r = 8". |
| 45 | // |
| 46 | // This feature relies on the following three intrinsic calls: |
| 47 | // addr = preserve_array_access_index(base, dimension, index) |
| 48 | // addr = preserve_union_access_index(base, di_index) |
| 49 | // !llvm.preserve.access.index <union_ditype> |
| 50 | // addr = preserve_struct_access_index(base, gep_index, di_index) |
| 51 | // !llvm.preserve.access.index <struct_ditype> |
| 52 | // |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 53 | // Bitfield member access needs special attention. User cannot take the |
| 54 | // address of a bitfield acceess. To facilitate kernel verifier |
| 55 | // for easy bitfield code optimization, a new clang intrinsic is introduced: |
| 56 | // uint32_t __builtin_preserve_field_info(member_access, info_kind) |
| 57 | // In IR, a chain with two (or more) intrinsic calls will be generated: |
| 58 | // ... |
| 59 | // addr = preserve_struct_access_index(base, 1, 1) !struct s |
| 60 | // uint32_t result = bpf_preserve_field_info(addr, info_kind) |
| 61 | // |
| 62 | // Suppose the info_kind is FIELD_SIGNEDNESS, |
| 63 | // The above two IR intrinsics will be replaced with |
| 64 | // a relocatable insn: |
| 65 | // signness = /* signness of member_access */ |
| 66 | // and signness can be changed by bpf loader based on the |
| 67 | // types on the host. |
| 68 | // |
| 69 | // User can also test whether a field exists or not with |
| 70 | // uint32_t result = bpf_preserve_field_info(member_access, FIELD_EXISTENCE) |
| 71 | // The field will be always available (result = 1) during initial |
| 72 | // compilation, but bpf loader can patch with the correct value |
| 73 | // on the target host where the member_access may or may not be available |
| 74 | // |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
| 76 | |
| 77 | #include "BPF.h" |
| 78 | #include "BPFCORE.h" |
| 79 | #include "BPFTargetMachine.h" |
| 80 | #include "llvm/IR/DebugInfoMetadata.h" |
| 81 | #include "llvm/IR/GlobalVariable.h" |
| 82 | #include "llvm/IR/Instruction.h" |
| 83 | #include "llvm/IR/Instructions.h" |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 84 | #include "llvm/IR/IntrinsicsBPF.h" |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 85 | #include "llvm/IR/Module.h" |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 86 | #include "llvm/IR/PassManager.h" |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 87 | #include "llvm/IR/Type.h" |
| 88 | #include "llvm/IR/User.h" |
| 89 | #include "llvm/IR/Value.h" |
| 90 | #include "llvm/Pass.h" |
| 91 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 92 | #include <stack> |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 93 | |
| 94 | #define DEBUG_TYPE "bpf-abstract-member-access" |
| 95 | |
| 96 | namespace llvm { |
Benjamin Kramer | df9a51d | 2020-06-17 14:27:36 +0200 | [diff] [blame] | 97 | constexpr StringRef BPFCoreSharedInfo::AmaAttr; |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 98 | uint32_t BPFCoreSharedInfo::SeqNum; |
| 99 | |
| 100 | Instruction *BPFCoreSharedInfo::insertPassThrough(Module *M, BasicBlock *BB, |
| 101 | Instruction *Input, |
| 102 | Instruction *Before) { |
| 103 | Function *Fn = Intrinsic::getDeclaration( |
| 104 | M, Intrinsic::bpf_passthrough, {Input->getType(), Input->getType()}); |
| 105 | Constant *SeqNumVal = ConstantInt::get(Type::getInt32Ty(BB->getContext()), |
| 106 | BPFCoreSharedInfo::SeqNum++); |
| 107 | |
| 108 | auto *NewInst = CallInst::Create(Fn, {SeqNumVal, Input}); |
| 109 | BB->getInstList().insert(Before->getIterator(), NewInst); |
| 110 | return NewInst; |
| 111 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 112 | } // namespace llvm |
| 113 | |
| 114 | using namespace llvm; |
| 115 | |
| 116 | namespace { |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 117 | class BPFAbstractMemberAccess final { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 118 | public: |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 119 | BPFAbstractMemberAccess(BPFTargetMachine *TM) : TM(TM) {} |
| 120 | |
| 121 | bool run(Function &F); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 122 | |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 123 | struct CallInfo { |
| 124 | uint32_t Kind; |
| 125 | uint32_t AccessIndex; |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 126 | Align RecordAlignment; |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 127 | MDNode *Metadata; |
| 128 | Value *Base; |
| 129 | }; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 130 | typedef std::stack<std::pair<CallInst *, CallInfo>> CallInfoStack; |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 131 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 132 | private: |
| 133 | enum : uint32_t { |
| 134 | BPFPreserveArrayAI = 1, |
| 135 | BPFPreserveUnionAI = 2, |
| 136 | BPFPreserveStructAI = 3, |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 137 | BPFPreserveFieldInfoAI = 4, |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 140 | TargetMachine *TM; |
Simon Pilgrim | f784ad8 | 2019-11-14 13:53:35 +0000 | [diff] [blame] | 141 | const DataLayout *DL = nullptr; |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 142 | Module *M = nullptr; |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 143 | |
Yonghong Song | edd71db | 2020-10-06 20:29:09 -0700 | [diff] [blame] | 144 | static std::map<std::string, GlobalVariable *> GEPGlobals; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 145 | // A map to link preserve_*_access_index instrinsic calls. |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 146 | std::map<CallInst *, std::pair<CallInst *, CallInfo>> AIChain; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 147 | // A map to hold all the base preserve_*_access_index instrinsic calls. |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 148 | // The base call is not an input of any other preserve_* |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 149 | // intrinsics. |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 150 | std::map<CallInst *, CallInfo> BaseAICalls; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 151 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 152 | bool doTransformation(Function &F); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 153 | |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 154 | void traceAICall(CallInst *Call, CallInfo &ParentInfo); |
| 155 | void traceBitCast(BitCastInst *BitCast, CallInst *Parent, |
| 156 | CallInfo &ParentInfo); |
| 157 | void traceGEP(GetElementPtrInst *GEP, CallInst *Parent, |
| 158 | CallInfo &ParentInfo); |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 159 | void collectAICallChains(Function &F); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 160 | |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 161 | bool IsPreserveDIAccessIndexCall(const CallInst *Call, CallInfo &Cinfo); |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 162 | bool IsValidAIChain(const MDNode *ParentMeta, uint32_t ParentAI, |
| 163 | const MDNode *ChildMeta); |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 164 | bool removePreserveAccessIndexIntrinsic(Function &F); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 165 | void replaceWithGEP(std::vector<CallInst *> &CallList, |
| 166 | uint32_t NumOfZerosIndex, uint32_t DIIndex); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 167 | bool HasPreserveFieldInfoCall(CallInfoStack &CallStack); |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 168 | void GetStorageBitRange(DIDerivedType *MemberTy, Align RecordAlignment, |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 169 | uint32_t &StartBitOffset, uint32_t &EndBitOffset); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 170 | uint32_t GetFieldInfo(uint32_t InfoKind, DICompositeType *CTy, |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 171 | uint32_t AccessIndex, uint32_t PatchImm, |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 172 | Align RecordAlignment); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 173 | |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 174 | Value *computeBaseAndAccessKey(CallInst *Call, CallInfo &CInfo, |
| 175 | std::string &AccessKey, MDNode *&BaseMeta); |
Yonghong Song | 6d218b4 | 2020-07-29 22:46:07 -0700 | [diff] [blame] | 176 | MDNode *computeAccessKey(CallInst *Call, CallInfo &CInfo, |
| 177 | std::string &AccessKey, bool &IsInt32Ret); |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 178 | uint64_t getConstant(const Value *IndexValue); |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 179 | bool transformGEPChain(CallInst *Call, CallInfo &CInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 180 | }; |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 181 | |
Yonghong Song | edd71db | 2020-10-06 20:29:09 -0700 | [diff] [blame] | 182 | std::map<std::string, GlobalVariable *> BPFAbstractMemberAccess::GEPGlobals; |
| 183 | |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 184 | class BPFAbstractMemberAccessLegacyPass final : public FunctionPass { |
| 185 | BPFTargetMachine *TM; |
| 186 | |
| 187 | bool runOnFunction(Function &F) override { |
| 188 | return BPFAbstractMemberAccess(TM).run(F); |
| 189 | } |
| 190 | |
| 191 | public: |
| 192 | static char ID; |
| 193 | |
| 194 | // Add optional BPFTargetMachine parameter so that BPF backend can add the |
| 195 | // phase with target machine to find out the endianness. The default |
| 196 | // constructor (without parameters) is used by the pass manager for managing |
| 197 | // purposes. |
| 198 | BPFAbstractMemberAccessLegacyPass(BPFTargetMachine *TM = nullptr) |
| 199 | : FunctionPass(ID), TM(TM) {} |
| 200 | }; |
| 201 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 202 | } // End anonymous namespace |
| 203 | |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 204 | char BPFAbstractMemberAccessLegacyPass::ID = 0; |
| 205 | INITIALIZE_PASS(BPFAbstractMemberAccessLegacyPass, DEBUG_TYPE, |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 206 | "BPF Abstract Member Access", false, false) |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 207 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 208 | FunctionPass *llvm::createBPFAbstractMemberAccess(BPFTargetMachine *TM) { |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 209 | return new BPFAbstractMemberAccessLegacyPass(TM); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 212 | bool BPFAbstractMemberAccess::run(Function &F) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 213 | LLVM_DEBUG(dbgs() << "********** Abstract Member Accesses **********\n"); |
| 214 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 215 | M = F.getParent(); |
| 216 | if (!M) |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 217 | return false; |
| 218 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 219 | // Bail out if no debug info. |
| 220 | if (M->debug_compile_units().empty()) |
| 221 | return false; |
| 222 | |
| 223 | DL = &M->getDataLayout(); |
| 224 | return doTransformation(F); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Yonghong Song | 6d07802 | 2020-02-01 21:00:00 -0800 | [diff] [blame] | 227 | static bool SkipDIDerivedTag(unsigned Tag, bool skipTypedef) { |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 228 | if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type && |
| 229 | Tag != dwarf::DW_TAG_volatile_type && |
| 230 | Tag != dwarf::DW_TAG_restrict_type && |
| 231 | Tag != dwarf::DW_TAG_member) |
Yonghong Song | 6d07802 | 2020-02-01 21:00:00 -0800 | [diff] [blame] | 232 | return false; |
| 233 | if (Tag == dwarf::DW_TAG_typedef && !skipTypedef) |
| 234 | return false; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
Yonghong Song | 6d07802 | 2020-02-01 21:00:00 -0800 | [diff] [blame] | 238 | static DIType * stripQualifiers(DIType *Ty, bool skipTypedef = true) { |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 239 | while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) { |
Yonghong Song | 6d07802 | 2020-02-01 21:00:00 -0800 | [diff] [blame] | 240 | if (!SkipDIDerivedTag(DTy->getTag(), skipTypedef)) |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 241 | break; |
| 242 | Ty = DTy->getBaseType(); |
| 243 | } |
| 244 | return Ty; |
| 245 | } |
| 246 | |
| 247 | static const DIType * stripQualifiers(const DIType *Ty) { |
| 248 | while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) { |
Yonghong Song | 6d07802 | 2020-02-01 21:00:00 -0800 | [diff] [blame] | 249 | if (!SkipDIDerivedTag(DTy->getTag(), true)) |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 250 | break; |
| 251 | Ty = DTy->getBaseType(); |
| 252 | } |
| 253 | return Ty; |
| 254 | } |
| 255 | |
| 256 | static uint32_t calcArraySize(const DICompositeType *CTy, uint32_t StartDim) { |
| 257 | DINodeArray Elements = CTy->getElements(); |
| 258 | uint32_t DimSize = 1; |
| 259 | for (uint32_t I = StartDim; I < Elements.size(); ++I) { |
| 260 | if (auto *Element = dyn_cast_or_null<DINode>(Elements[I])) |
| 261 | if (Element->getTag() == dwarf::DW_TAG_subrange_type) { |
| 262 | const DISubrange *SR = cast<DISubrange>(Element); |
| 263 | auto *CI = SR->getCount().dyn_cast<ConstantInt *>(); |
| 264 | DimSize *= CI->getSExtValue(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return DimSize; |
| 269 | } |
| 270 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 271 | /// Check whether a call is a preserve_*_access_index intrinsic call or not. |
| 272 | bool BPFAbstractMemberAccess::IsPreserveDIAccessIndexCall(const CallInst *Call, |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 273 | CallInfo &CInfo) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 274 | if (!Call) |
| 275 | return false; |
| 276 | |
Craig Topper | a58b62b | 2020-04-27 20:15:59 -0700 | [diff] [blame] | 277 | const auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand()); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 278 | if (!GV) |
| 279 | return false; |
| 280 | if (GV->getName().startswith("llvm.preserve.array.access.index")) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 281 | CInfo.Kind = BPFPreserveArrayAI; |
| 282 | CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index); |
| 283 | if (!CInfo.Metadata) |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 284 | report_fatal_error("Missing metadata for llvm.preserve.array.access.index intrinsic"); |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 285 | CInfo.AccessIndex = getConstant(Call->getArgOperand(2)); |
| 286 | CInfo.Base = Call->getArgOperand(0); |
Yonghong Song | 9f34447 | 2019-11-04 22:12:52 -0800 | [diff] [blame] | 287 | CInfo.RecordAlignment = |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 288 | DL->getABITypeAlign(CInfo.Base->getType()->getPointerElementType()); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 289 | return true; |
| 290 | } |
| 291 | if (GV->getName().startswith("llvm.preserve.union.access.index")) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 292 | CInfo.Kind = BPFPreserveUnionAI; |
| 293 | CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index); |
| 294 | if (!CInfo.Metadata) |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 295 | report_fatal_error("Missing metadata for llvm.preserve.union.access.index intrinsic"); |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 296 | CInfo.AccessIndex = getConstant(Call->getArgOperand(1)); |
| 297 | CInfo.Base = Call->getArgOperand(0); |
Yonghong Song | 9f34447 | 2019-11-04 22:12:52 -0800 | [diff] [blame] | 298 | CInfo.RecordAlignment = |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 299 | DL->getABITypeAlign(CInfo.Base->getType()->getPointerElementType()); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 300 | return true; |
| 301 | } |
| 302 | if (GV->getName().startswith("llvm.preserve.struct.access.index")) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 303 | CInfo.Kind = BPFPreserveStructAI; |
| 304 | CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index); |
| 305 | if (!CInfo.Metadata) |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 306 | report_fatal_error("Missing metadata for llvm.preserve.struct.access.index intrinsic"); |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 307 | CInfo.AccessIndex = getConstant(Call->getArgOperand(2)); |
| 308 | CInfo.Base = Call->getArgOperand(0); |
Yonghong Song | 9f34447 | 2019-11-04 22:12:52 -0800 | [diff] [blame] | 309 | CInfo.RecordAlignment = |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 310 | DL->getABITypeAlign(CInfo.Base->getType()->getPointerElementType()); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 311 | return true; |
| 312 | } |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 313 | if (GV->getName().startswith("llvm.bpf.preserve.field.info")) { |
| 314 | CInfo.Kind = BPFPreserveFieldInfoAI; |
| 315 | CInfo.Metadata = nullptr; |
| 316 | // Check validity of info_kind as clang did not check this. |
| 317 | uint64_t InfoKind = getConstant(Call->getArgOperand(1)); |
| 318 | if (InfoKind >= BPFCoreSharedInfo::MAX_FIELD_RELOC_KIND) |
| 319 | report_fatal_error("Incorrect info_kind for llvm.bpf.preserve.field.info intrinsic"); |
| 320 | CInfo.AccessIndex = InfoKind; |
| 321 | return true; |
| 322 | } |
Yonghong Song | 6d218b4 | 2020-07-29 22:46:07 -0700 | [diff] [blame] | 323 | if (GV->getName().startswith("llvm.bpf.preserve.type.info")) { |
| 324 | CInfo.Kind = BPFPreserveFieldInfoAI; |
| 325 | CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index); |
| 326 | if (!CInfo.Metadata) |
| 327 | report_fatal_error("Missing metadata for llvm.preserve.type.info intrinsic"); |
| 328 | uint64_t Flag = getConstant(Call->getArgOperand(1)); |
| 329 | if (Flag >= BPFCoreSharedInfo::MAX_PRESERVE_TYPE_INFO_FLAG) |
| 330 | report_fatal_error("Incorrect flag for llvm.bpf.preserve.type.info intrinsic"); |
| 331 | if (Flag == BPFCoreSharedInfo::PRESERVE_TYPE_INFO_EXISTENCE) |
| 332 | CInfo.AccessIndex = BPFCoreSharedInfo::TYPE_EXISTENCE; |
| 333 | else |
| 334 | CInfo.AccessIndex = BPFCoreSharedInfo::TYPE_SIZE; |
| 335 | return true; |
| 336 | } |
| 337 | if (GV->getName().startswith("llvm.bpf.preserve.enum.value")) { |
| 338 | CInfo.Kind = BPFPreserveFieldInfoAI; |
| 339 | CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index); |
| 340 | if (!CInfo.Metadata) |
| 341 | report_fatal_error("Missing metadata for llvm.preserve.enum.value intrinsic"); |
| 342 | uint64_t Flag = getConstant(Call->getArgOperand(2)); |
| 343 | if (Flag >= BPFCoreSharedInfo::MAX_PRESERVE_ENUM_VALUE_FLAG) |
| 344 | report_fatal_error("Incorrect flag for llvm.bpf.preserve.enum.value intrinsic"); |
| 345 | if (Flag == BPFCoreSharedInfo::PRESERVE_ENUM_VALUE_EXISTENCE) |
| 346 | CInfo.AccessIndex = BPFCoreSharedInfo::ENUM_VALUE_EXISTENCE; |
| 347 | else |
| 348 | CInfo.AccessIndex = BPFCoreSharedInfo::ENUM_VALUE; |
| 349 | return true; |
| 350 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 351 | |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | void BPFAbstractMemberAccess::replaceWithGEP(std::vector<CallInst *> &CallList, |
| 356 | uint32_t DimensionIndex, |
| 357 | uint32_t GEPIndex) { |
| 358 | for (auto Call : CallList) { |
| 359 | uint32_t Dimension = 1; |
| 360 | if (DimensionIndex > 0) |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 361 | Dimension = getConstant(Call->getArgOperand(DimensionIndex)); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 362 | |
| 363 | Constant *Zero = |
| 364 | ConstantInt::get(Type::getInt32Ty(Call->getParent()->getContext()), 0); |
| 365 | SmallVector<Value *, 4> IdxList; |
| 366 | for (unsigned I = 0; I < Dimension; ++I) |
| 367 | IdxList.push_back(Zero); |
| 368 | IdxList.push_back(Call->getArgOperand(GEPIndex)); |
| 369 | |
| 370 | auto *GEP = GetElementPtrInst::CreateInBounds(Call->getArgOperand(0), |
| 371 | IdxList, "", Call); |
| 372 | Call->replaceAllUsesWith(GEP); |
| 373 | Call->eraseFromParent(); |
| 374 | } |
| 375 | } |
| 376 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 377 | bool BPFAbstractMemberAccess::removePreserveAccessIndexIntrinsic(Function &F) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 378 | std::vector<CallInst *> PreserveArrayIndexCalls; |
| 379 | std::vector<CallInst *> PreserveUnionIndexCalls; |
| 380 | std::vector<CallInst *> PreserveStructIndexCalls; |
| 381 | bool Found = false; |
| 382 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 383 | for (auto &BB : F) |
| 384 | for (auto &I : BB) { |
| 385 | auto *Call = dyn_cast<CallInst>(&I); |
| 386 | CallInfo CInfo; |
| 387 | if (!IsPreserveDIAccessIndexCall(Call, CInfo)) |
| 388 | continue; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 389 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 390 | Found = true; |
| 391 | if (CInfo.Kind == BPFPreserveArrayAI) |
| 392 | PreserveArrayIndexCalls.push_back(Call); |
| 393 | else if (CInfo.Kind == BPFPreserveUnionAI) |
| 394 | PreserveUnionIndexCalls.push_back(Call); |
| 395 | else |
| 396 | PreserveStructIndexCalls.push_back(Call); |
| 397 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 398 | |
| 399 | // do the following transformation: |
| 400 | // . addr = preserve_array_access_index(base, dimension, index) |
| 401 | // is transformed to |
| 402 | // addr = GEP(base, dimenion's zero's, index) |
| 403 | // . addr = preserve_union_access_index(base, di_index) |
| 404 | // is transformed to |
| 405 | // addr = base, i.e., all usages of "addr" are replaced by "base". |
| 406 | // . addr = preserve_struct_access_index(base, gep_index, di_index) |
| 407 | // is transformed to |
| 408 | // addr = GEP(base, 0, gep_index) |
| 409 | replaceWithGEP(PreserveArrayIndexCalls, 1, 2); |
| 410 | replaceWithGEP(PreserveStructIndexCalls, 0, 1); |
| 411 | for (auto Call : PreserveUnionIndexCalls) { |
| 412 | Call->replaceAllUsesWith(Call->getArgOperand(0)); |
| 413 | Call->eraseFromParent(); |
| 414 | } |
| 415 | |
| 416 | return Found; |
| 417 | } |
| 418 | |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 419 | /// Check whether the access index chain is valid. We check |
| 420 | /// here because there may be type casts between two |
| 421 | /// access indexes. We want to ensure memory access still valid. |
| 422 | bool BPFAbstractMemberAccess::IsValidAIChain(const MDNode *ParentType, |
| 423 | uint32_t ParentAI, |
| 424 | const MDNode *ChildType) { |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 425 | if (!ChildType) |
| 426 | return true; // preserve_field_info, no type comparison needed. |
| 427 | |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 428 | const DIType *PType = stripQualifiers(cast<DIType>(ParentType)); |
| 429 | const DIType *CType = stripQualifiers(cast<DIType>(ChildType)); |
| 430 | |
| 431 | // Child is a derived/pointer type, which is due to type casting. |
| 432 | // Pointer type cannot be in the middle of chain. |
Bill Wendling | ebc2cf9 | 2019-08-06 07:27:26 +0000 | [diff] [blame] | 433 | if (isa<DIDerivedType>(CType)) |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 434 | return false; |
| 435 | |
| 436 | // Parent is a pointer type. |
| 437 | if (const auto *PtrTy = dyn_cast<DIDerivedType>(PType)) { |
| 438 | if (PtrTy->getTag() != dwarf::DW_TAG_pointer_type) |
| 439 | return false; |
| 440 | return stripQualifiers(PtrTy->getBaseType()) == CType; |
| 441 | } |
| 442 | |
| 443 | // Otherwise, struct/union/array types |
| 444 | const auto *PTy = dyn_cast<DICompositeType>(PType); |
| 445 | const auto *CTy = dyn_cast<DICompositeType>(CType); |
| 446 | assert(PTy && CTy && "ParentType or ChildType is null or not composite"); |
| 447 | |
| 448 | uint32_t PTyTag = PTy->getTag(); |
| 449 | assert(PTyTag == dwarf::DW_TAG_array_type || |
| 450 | PTyTag == dwarf::DW_TAG_structure_type || |
| 451 | PTyTag == dwarf::DW_TAG_union_type); |
| 452 | |
| 453 | uint32_t CTyTag = CTy->getTag(); |
| 454 | assert(CTyTag == dwarf::DW_TAG_array_type || |
| 455 | CTyTag == dwarf::DW_TAG_structure_type || |
| 456 | CTyTag == dwarf::DW_TAG_union_type); |
| 457 | |
| 458 | // Multi dimensional arrays, base element should be the same |
| 459 | if (PTyTag == dwarf::DW_TAG_array_type && PTyTag == CTyTag) |
| 460 | return PTy->getBaseType() == CTy->getBaseType(); |
| 461 | |
| 462 | DIType *Ty; |
| 463 | if (PTyTag == dwarf::DW_TAG_array_type) |
| 464 | Ty = PTy->getBaseType(); |
| 465 | else |
| 466 | Ty = dyn_cast<DIType>(PTy->getElements()[ParentAI]); |
| 467 | |
| 468 | return dyn_cast<DICompositeType>(stripQualifiers(Ty)) == CTy; |
| 469 | } |
| 470 | |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 471 | void BPFAbstractMemberAccess::traceAICall(CallInst *Call, |
| 472 | CallInfo &ParentInfo) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 473 | for (User *U : Call->users()) { |
| 474 | Instruction *Inst = dyn_cast<Instruction>(U); |
| 475 | if (!Inst) |
| 476 | continue; |
| 477 | |
| 478 | if (auto *BI = dyn_cast<BitCastInst>(Inst)) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 479 | traceBitCast(BI, Call, ParentInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 480 | } else if (auto *CI = dyn_cast<CallInst>(Inst)) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 481 | CallInfo ChildInfo; |
| 482 | |
| 483 | if (IsPreserveDIAccessIndexCall(CI, ChildInfo) && |
| 484 | IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex, |
| 485 | ChildInfo.Metadata)) { |
| 486 | AIChain[CI] = std::make_pair(Call, ParentInfo); |
| 487 | traceAICall(CI, ChildInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 488 | } else { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 489 | BaseAICalls[Call] = ParentInfo; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 490 | } |
| 491 | } else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) { |
| 492 | if (GI->hasAllZeroIndices()) |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 493 | traceGEP(GI, Call, ParentInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 494 | else |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 495 | BaseAICalls[Call] = ParentInfo; |
Yonghong Song | c68ee0c | 2019-09-18 03:49:07 +0000 | [diff] [blame] | 496 | } else { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 497 | BaseAICalls[Call] = ParentInfo; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void BPFAbstractMemberAccess::traceBitCast(BitCastInst *BitCast, |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 503 | CallInst *Parent, |
| 504 | CallInfo &ParentInfo) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 505 | for (User *U : BitCast->users()) { |
| 506 | Instruction *Inst = dyn_cast<Instruction>(U); |
| 507 | if (!Inst) |
| 508 | continue; |
| 509 | |
| 510 | if (auto *BI = dyn_cast<BitCastInst>(Inst)) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 511 | traceBitCast(BI, Parent, ParentInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 512 | } else if (auto *CI = dyn_cast<CallInst>(Inst)) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 513 | CallInfo ChildInfo; |
| 514 | if (IsPreserveDIAccessIndexCall(CI, ChildInfo) && |
| 515 | IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex, |
| 516 | ChildInfo.Metadata)) { |
| 517 | AIChain[CI] = std::make_pair(Parent, ParentInfo); |
| 518 | traceAICall(CI, ChildInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 519 | } else { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 520 | BaseAICalls[Parent] = ParentInfo; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 521 | } |
| 522 | } else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) { |
| 523 | if (GI->hasAllZeroIndices()) |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 524 | traceGEP(GI, Parent, ParentInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 525 | else |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 526 | BaseAICalls[Parent] = ParentInfo; |
Yonghong Song | c68ee0c | 2019-09-18 03:49:07 +0000 | [diff] [blame] | 527 | } else { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 528 | BaseAICalls[Parent] = ParentInfo; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void BPFAbstractMemberAccess::traceGEP(GetElementPtrInst *GEP, CallInst *Parent, |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 534 | CallInfo &ParentInfo) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 535 | for (User *U : GEP->users()) { |
| 536 | Instruction *Inst = dyn_cast<Instruction>(U); |
| 537 | if (!Inst) |
| 538 | continue; |
| 539 | |
| 540 | if (auto *BI = dyn_cast<BitCastInst>(Inst)) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 541 | traceBitCast(BI, Parent, ParentInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 542 | } else if (auto *CI = dyn_cast<CallInst>(Inst)) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 543 | CallInfo ChildInfo; |
| 544 | if (IsPreserveDIAccessIndexCall(CI, ChildInfo) && |
| 545 | IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex, |
| 546 | ChildInfo.Metadata)) { |
| 547 | AIChain[CI] = std::make_pair(Parent, ParentInfo); |
| 548 | traceAICall(CI, ChildInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 549 | } else { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 550 | BaseAICalls[Parent] = ParentInfo; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 551 | } |
| 552 | } else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) { |
| 553 | if (GI->hasAllZeroIndices()) |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 554 | traceGEP(GI, Parent, ParentInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 555 | else |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 556 | BaseAICalls[Parent] = ParentInfo; |
Yonghong Song | c68ee0c | 2019-09-18 03:49:07 +0000 | [diff] [blame] | 557 | } else { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 558 | BaseAICalls[Parent] = ParentInfo; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 563 | void BPFAbstractMemberAccess::collectAICallChains(Function &F) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 564 | AIChain.clear(); |
| 565 | BaseAICalls.clear(); |
| 566 | |
| 567 | for (auto &BB : F) |
| 568 | for (auto &I : BB) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 569 | CallInfo CInfo; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 570 | auto *Call = dyn_cast<CallInst>(&I); |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 571 | if (!IsPreserveDIAccessIndexCall(Call, CInfo) || |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 572 | AIChain.find(Call) != AIChain.end()) |
| 573 | continue; |
| 574 | |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 575 | traceAICall(Call, CInfo); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 579 | uint64_t BPFAbstractMemberAccess::getConstant(const Value *IndexValue) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 580 | const ConstantInt *CV = dyn_cast<ConstantInt>(IndexValue); |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 581 | assert(CV); |
| 582 | return CV->getValue().getZExtValue(); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 585 | /// Get the start and the end of storage offset for \p MemberTy. |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 586 | void BPFAbstractMemberAccess::GetStorageBitRange(DIDerivedType *MemberTy, |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 587 | Align RecordAlignment, |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 588 | uint32_t &StartBitOffset, |
| 589 | uint32_t &EndBitOffset) { |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 590 | uint32_t MemberBitSize = MemberTy->getSizeInBits(); |
| 591 | uint32_t MemberBitOffset = MemberTy->getOffsetInBits(); |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 592 | uint32_t AlignBits = RecordAlignment.value() * 8; |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 593 | if (RecordAlignment > 8 || MemberBitSize > AlignBits) |
| 594 | report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info, " |
| 595 | "requiring too big alignment"); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 596 | |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 597 | StartBitOffset = MemberBitOffset & ~(AlignBits - 1); |
| 598 | if ((StartBitOffset + AlignBits) < (MemberBitOffset + MemberBitSize)) |
| 599 | report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info, " |
| 600 | "cross alignment boundary"); |
| 601 | EndBitOffset = StartBitOffset + AlignBits; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | uint32_t BPFAbstractMemberAccess::GetFieldInfo(uint32_t InfoKind, |
| 605 | DICompositeType *CTy, |
| 606 | uint32_t AccessIndex, |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 607 | uint32_t PatchImm, |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 608 | Align RecordAlignment) { |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 609 | if (InfoKind == BPFCoreSharedInfo::FIELD_EXISTENCE) |
| 610 | return 1; |
| 611 | |
| 612 | uint32_t Tag = CTy->getTag(); |
| 613 | if (InfoKind == BPFCoreSharedInfo::FIELD_BYTE_OFFSET) { |
| 614 | if (Tag == dwarf::DW_TAG_array_type) { |
| 615 | auto *EltTy = stripQualifiers(CTy->getBaseType()); |
| 616 | PatchImm += AccessIndex * calcArraySize(CTy, 1) * |
| 617 | (EltTy->getSizeInBits() >> 3); |
| 618 | } else if (Tag == dwarf::DW_TAG_structure_type) { |
| 619 | auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]); |
| 620 | if (!MemberTy->isBitField()) { |
| 621 | PatchImm += MemberTy->getOffsetInBits() >> 3; |
| 622 | } else { |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 623 | unsigned SBitOffset, NextSBitOffset; |
| 624 | GetStorageBitRange(MemberTy, RecordAlignment, SBitOffset, |
| 625 | NextSBitOffset); |
| 626 | PatchImm += SBitOffset >> 3; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | return PatchImm; |
| 630 | } |
| 631 | |
| 632 | if (InfoKind == BPFCoreSharedInfo::FIELD_BYTE_SIZE) { |
| 633 | if (Tag == dwarf::DW_TAG_array_type) { |
| 634 | auto *EltTy = stripQualifiers(CTy->getBaseType()); |
| 635 | return calcArraySize(CTy, 1) * (EltTy->getSizeInBits() >> 3); |
| 636 | } else { |
| 637 | auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]); |
| 638 | uint32_t SizeInBits = MemberTy->getSizeInBits(); |
| 639 | if (!MemberTy->isBitField()) |
| 640 | return SizeInBits >> 3; |
| 641 | |
| 642 | unsigned SBitOffset, NextSBitOffset; |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 643 | GetStorageBitRange(MemberTy, RecordAlignment, SBitOffset, NextSBitOffset); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 644 | SizeInBits = NextSBitOffset - SBitOffset; |
| 645 | if (SizeInBits & (SizeInBits - 1)) |
| 646 | report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info"); |
| 647 | return SizeInBits >> 3; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | if (InfoKind == BPFCoreSharedInfo::FIELD_SIGNEDNESS) { |
| 652 | const DIType *BaseTy; |
| 653 | if (Tag == dwarf::DW_TAG_array_type) { |
| 654 | // Signedness only checked when final array elements are accessed. |
| 655 | if (CTy->getElements().size() != 1) |
| 656 | report_fatal_error("Invalid array expression for llvm.bpf.preserve.field.info"); |
| 657 | BaseTy = stripQualifiers(CTy->getBaseType()); |
| 658 | } else { |
| 659 | auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]); |
| 660 | BaseTy = stripQualifiers(MemberTy->getBaseType()); |
| 661 | } |
| 662 | |
| 663 | // Only basic types and enum types have signedness. |
| 664 | const auto *BTy = dyn_cast<DIBasicType>(BaseTy); |
| 665 | while (!BTy) { |
| 666 | const auto *CompTy = dyn_cast<DICompositeType>(BaseTy); |
| 667 | // Report an error if the field expression does not have signedness. |
| 668 | if (!CompTy || CompTy->getTag() != dwarf::DW_TAG_enumeration_type) |
| 669 | report_fatal_error("Invalid field expression for llvm.bpf.preserve.field.info"); |
| 670 | BaseTy = stripQualifiers(CompTy->getBaseType()); |
| 671 | BTy = dyn_cast<DIBasicType>(BaseTy); |
| 672 | } |
| 673 | uint32_t Encoding = BTy->getEncoding(); |
| 674 | return (Encoding == dwarf::DW_ATE_signed || Encoding == dwarf::DW_ATE_signed_char); |
| 675 | } |
| 676 | |
| 677 | if (InfoKind == BPFCoreSharedInfo::FIELD_LSHIFT_U64) { |
| 678 | // The value is loaded into a value with FIELD_BYTE_SIZE size, |
| 679 | // and then zero or sign extended to U64. |
| 680 | // FIELD_LSHIFT_U64 and FIELD_RSHIFT_U64 are operations |
| 681 | // to extract the original value. |
| 682 | const Triple &Triple = TM->getTargetTriple(); |
| 683 | DIDerivedType *MemberTy = nullptr; |
| 684 | bool IsBitField = false; |
| 685 | uint32_t SizeInBits; |
| 686 | |
| 687 | if (Tag == dwarf::DW_TAG_array_type) { |
| 688 | auto *EltTy = stripQualifiers(CTy->getBaseType()); |
| 689 | SizeInBits = calcArraySize(CTy, 1) * EltTy->getSizeInBits(); |
| 690 | } else { |
| 691 | MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]); |
| 692 | SizeInBits = MemberTy->getSizeInBits(); |
| 693 | IsBitField = MemberTy->isBitField(); |
| 694 | } |
| 695 | |
| 696 | if (!IsBitField) { |
| 697 | if (SizeInBits > 64) |
| 698 | report_fatal_error("too big field size for llvm.bpf.preserve.field.info"); |
| 699 | return 64 - SizeInBits; |
| 700 | } |
| 701 | |
| 702 | unsigned SBitOffset, NextSBitOffset; |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 703 | GetStorageBitRange(MemberTy, RecordAlignment, SBitOffset, NextSBitOffset); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 704 | if (NextSBitOffset - SBitOffset > 64) |
| 705 | report_fatal_error("too big field size for llvm.bpf.preserve.field.info"); |
| 706 | |
| 707 | unsigned OffsetInBits = MemberTy->getOffsetInBits(); |
| 708 | if (Triple.getArch() == Triple::bpfel) |
| 709 | return SBitOffset + 64 - OffsetInBits - SizeInBits; |
| 710 | else |
| 711 | return OffsetInBits + 64 - NextSBitOffset; |
| 712 | } |
| 713 | |
| 714 | if (InfoKind == BPFCoreSharedInfo::FIELD_RSHIFT_U64) { |
| 715 | DIDerivedType *MemberTy = nullptr; |
| 716 | bool IsBitField = false; |
| 717 | uint32_t SizeInBits; |
| 718 | if (Tag == dwarf::DW_TAG_array_type) { |
| 719 | auto *EltTy = stripQualifiers(CTy->getBaseType()); |
| 720 | SizeInBits = calcArraySize(CTy, 1) * EltTy->getSizeInBits(); |
| 721 | } else { |
| 722 | MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]); |
| 723 | SizeInBits = MemberTy->getSizeInBits(); |
| 724 | IsBitField = MemberTy->isBitField(); |
| 725 | } |
| 726 | |
| 727 | if (!IsBitField) { |
| 728 | if (SizeInBits > 64) |
| 729 | report_fatal_error("too big field size for llvm.bpf.preserve.field.info"); |
| 730 | return 64 - SizeInBits; |
| 731 | } |
| 732 | |
| 733 | unsigned SBitOffset, NextSBitOffset; |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 734 | GetStorageBitRange(MemberTy, RecordAlignment, SBitOffset, NextSBitOffset); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 735 | if (NextSBitOffset - SBitOffset > 64) |
| 736 | report_fatal_error("too big field size for llvm.bpf.preserve.field.info"); |
| 737 | |
| 738 | return 64 - SizeInBits; |
| 739 | } |
| 740 | |
| 741 | llvm_unreachable("Unknown llvm.bpf.preserve.field.info info kind"); |
| 742 | } |
| 743 | |
| 744 | bool BPFAbstractMemberAccess::HasPreserveFieldInfoCall(CallInfoStack &CallStack) { |
| 745 | // This is called in error return path, no need to maintain CallStack. |
| 746 | while (CallStack.size()) { |
| 747 | auto StackElem = CallStack.top(); |
| 748 | if (StackElem.second.Kind == BPFPreserveFieldInfoAI) |
| 749 | return true; |
| 750 | CallStack.pop(); |
| 751 | } |
| 752 | return false; |
| 753 | } |
| 754 | |
| 755 | /// Compute the base of the whole preserve_* intrinsics chains, i.e., the base |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 756 | /// pointer of the first preserve_*_access_index call, and construct the access |
| 757 | /// string, which will be the name of a global variable. |
Yonghong Song | d8efec9 | 2019-07-25 16:01:26 +0000 | [diff] [blame] | 758 | Value *BPFAbstractMemberAccess::computeBaseAndAccessKey(CallInst *Call, |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 759 | CallInfo &CInfo, |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 760 | std::string &AccessKey, |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 761 | MDNode *&TypeMeta) { |
| 762 | Value *Base = nullptr; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 763 | std::string TypeName; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 764 | CallInfoStack CallStack; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 765 | |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 766 | // Put the access chain into a stack with the top as the head of the chain. |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 767 | while (Call) { |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 768 | CallStack.push(std::make_pair(Call, CInfo)); |
| 769 | CInfo = AIChain[Call].second; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 770 | Call = AIChain[Call].first; |
| 771 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 772 | |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 773 | // The access offset from the base of the head of chain is also |
| 774 | // calculated here as all debuginfo types are available. |
| 775 | |
| 776 | // Get type name and calculate the first index. |
Yonghong Song | 6d07802 | 2020-02-01 21:00:00 -0800 | [diff] [blame] | 777 | // We only want to get type name from typedef, structure or union. |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 778 | // If user wants a relocation like |
| 779 | // int *p; ... __builtin_preserve_access_index(&p[4]) ... |
| 780 | // or |
| 781 | // int a[10][20]; ... __builtin_preserve_access_index(&a[2][3]) ... |
| 782 | // we will skip them. |
| 783 | uint32_t FirstIndex = 0; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 784 | uint32_t PatchImm = 0; // AccessOffset or the requested field info |
| 785 | uint32_t InfoKind = BPFCoreSharedInfo::FIELD_BYTE_OFFSET; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 786 | while (CallStack.size()) { |
| 787 | auto StackElem = CallStack.top(); |
| 788 | Call = StackElem.first; |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 789 | CInfo = StackElem.second; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 790 | |
| 791 | if (!Base) |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 792 | Base = CInfo.Base; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 793 | |
Yonghong Song | 6d07802 | 2020-02-01 21:00:00 -0800 | [diff] [blame] | 794 | DIType *PossibleTypeDef = stripQualifiers(cast<DIType>(CInfo.Metadata), |
| 795 | false); |
| 796 | DIType *Ty = stripQualifiers(PossibleTypeDef); |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 797 | if (CInfo.Kind == BPFPreserveUnionAI || |
| 798 | CInfo.Kind == BPFPreserveStructAI) { |
Yonghong Song | 6d07802 | 2020-02-01 21:00:00 -0800 | [diff] [blame] | 799 | // struct or union type. If the typedef is in the metadata, always |
| 800 | // use the typedef. |
| 801 | TypeName = std::string(PossibleTypeDef->getName()); |
| 802 | TypeMeta = PossibleTypeDef; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 803 | PatchImm += FirstIndex * (Ty->getSizeInBits() >> 3); |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 804 | break; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 807 | assert(CInfo.Kind == BPFPreserveArrayAI); |
| 808 | |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 809 | // Array entries will always be consumed for accumulative initial index. |
| 810 | CallStack.pop(); |
| 811 | |
| 812 | // BPFPreserveArrayAI |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 813 | uint64_t AccessIndex = CInfo.AccessIndex; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 814 | |
| 815 | DIType *BaseTy = nullptr; |
| 816 | bool CheckElemType = false; |
| 817 | if (const auto *CTy = dyn_cast<DICompositeType>(Ty)) { |
| 818 | // array type |
| 819 | assert(CTy->getTag() == dwarf::DW_TAG_array_type); |
| 820 | |
| 821 | |
| 822 | FirstIndex += AccessIndex * calcArraySize(CTy, 1); |
| 823 | BaseTy = stripQualifiers(CTy->getBaseType()); |
| 824 | CheckElemType = CTy->getElements().size() == 1; |
| 825 | } else { |
| 826 | // pointer type |
| 827 | auto *DTy = cast<DIDerivedType>(Ty); |
| 828 | assert(DTy->getTag() == dwarf::DW_TAG_pointer_type); |
| 829 | |
| 830 | BaseTy = stripQualifiers(DTy->getBaseType()); |
| 831 | CTy = dyn_cast<DICompositeType>(BaseTy); |
| 832 | if (!CTy) { |
| 833 | CheckElemType = true; |
| 834 | } else if (CTy->getTag() != dwarf::DW_TAG_array_type) { |
| 835 | FirstIndex += AccessIndex; |
| 836 | CheckElemType = true; |
| 837 | } else { |
| 838 | FirstIndex += AccessIndex * calcArraySize(CTy, 0); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | if (CheckElemType) { |
| 843 | auto *CTy = dyn_cast<DICompositeType>(BaseTy); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 844 | if (!CTy) { |
| 845 | if (HasPreserveFieldInfoCall(CallStack)) |
| 846 | report_fatal_error("Invalid field access for llvm.preserve.field.info intrinsic"); |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 847 | return nullptr; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 848 | } |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 849 | |
| 850 | unsigned CTag = CTy->getTag(); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 851 | if (CTag == dwarf::DW_TAG_structure_type || CTag == dwarf::DW_TAG_union_type) { |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 852 | TypeName = std::string(CTy->getName()); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 853 | } else { |
| 854 | if (HasPreserveFieldInfoCall(CallStack)) |
| 855 | report_fatal_error("Invalid field access for llvm.preserve.field.info intrinsic"); |
| 856 | return nullptr; |
| 857 | } |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 858 | TypeMeta = CTy; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 859 | PatchImm += FirstIndex * (CTy->getSizeInBits() >> 3); |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 860 | break; |
| 861 | } |
| 862 | } |
| 863 | assert(TypeName.size()); |
| 864 | AccessKey += std::to_string(FirstIndex); |
| 865 | |
| 866 | // Traverse the rest of access chain to complete offset calculation |
| 867 | // and access key construction. |
| 868 | while (CallStack.size()) { |
| 869 | auto StackElem = CallStack.top(); |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 870 | CInfo = StackElem.second; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 871 | CallStack.pop(); |
| 872 | |
Yonghong Song | 7f6bc84 | 2020-06-30 15:27:24 -0700 | [diff] [blame] | 873 | if (CInfo.Kind == BPFPreserveFieldInfoAI) { |
| 874 | InfoKind = CInfo.AccessIndex; |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 875 | break; |
Yonghong Song | 7f6bc84 | 2020-06-30 15:27:24 -0700 | [diff] [blame] | 876 | } |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 877 | |
| 878 | // If the next Call (the top of the stack) is a BPFPreserveFieldInfoAI, |
| 879 | // the action will be extracting field info. |
| 880 | if (CallStack.size()) { |
| 881 | auto StackElem2 = CallStack.top(); |
| 882 | CallInfo CInfo2 = StackElem2.second; |
| 883 | if (CInfo2.Kind == BPFPreserveFieldInfoAI) { |
| 884 | InfoKind = CInfo2.AccessIndex; |
| 885 | assert(CallStack.size() == 1); |
| 886 | } |
| 887 | } |
| 888 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 889 | // Access Index |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 890 | uint64_t AccessIndex = CInfo.AccessIndex; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 891 | AccessKey += ":" + std::to_string(AccessIndex); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 892 | |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 893 | MDNode *MDN = CInfo.Metadata; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 894 | // At this stage, it cannot be pointer type. |
| 895 | auto *CTy = cast<DICompositeType>(stripQualifiers(cast<DIType>(MDN))); |
Yonghong Song | fff2721 | 2019-10-30 12:44:49 -0400 | [diff] [blame] | 896 | PatchImm = GetFieldInfo(InfoKind, CTy, AccessIndex, PatchImm, |
Guillaume Chatelet | 0f9d623 | 2020-07-01 16:23:52 +0000 | [diff] [blame] | 897 | CInfo.RecordAlignment); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Yonghong Song | 6db023b | 2019-11-25 16:31:33 -0800 | [diff] [blame] | 900 | // Access key is the |
| 901 | // "llvm." + type name + ":" + reloc type + ":" + patched imm + "$" + |
| 902 | // access string, |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 903 | // uniquely identifying one relocation. |
Yonghong Song | 6db023b | 2019-11-25 16:31:33 -0800 | [diff] [blame] | 904 | // The prefix "llvm." indicates this is a temporary global, which should |
| 905 | // not be emitted to ELF file. |
| 906 | AccessKey = "llvm." + TypeName + ":" + std::to_string(InfoKind) + ":" + |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 907 | std::to_string(PatchImm) + "$" + AccessKey; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 908 | |
| 909 | return Base; |
| 910 | } |
| 911 | |
Yonghong Song | 6d218b4 | 2020-07-29 22:46:07 -0700 | [diff] [blame] | 912 | MDNode *BPFAbstractMemberAccess::computeAccessKey(CallInst *Call, |
| 913 | CallInfo &CInfo, |
| 914 | std::string &AccessKey, |
| 915 | bool &IsInt32Ret) { |
| 916 | DIType *Ty = stripQualifiers(cast<DIType>(CInfo.Metadata), false); |
| 917 | assert(!Ty->getName().empty()); |
| 918 | |
| 919 | int64_t PatchImm; |
| 920 | std::string AccessStr("0"); |
| 921 | if (CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_EXISTENCE) { |
| 922 | PatchImm = 1; |
| 923 | } else if (CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_SIZE) { |
| 924 | // typedef debuginfo type has size 0, get the eventual base type. |
| 925 | DIType *BaseTy = stripQualifiers(Ty, true); |
| 926 | PatchImm = BaseTy->getSizeInBits() / 8; |
| 927 | } else { |
| 928 | // ENUM_VALUE_EXISTENCE and ENUM_VALUE |
| 929 | IsInt32Ret = false; |
| 930 | |
| 931 | const auto *CE = cast<ConstantExpr>(Call->getArgOperand(1)); |
| 932 | const GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
| 933 | assert(GV->hasInitializer()); |
| 934 | const ConstantDataArray *DA = cast<ConstantDataArray>(GV->getInitializer()); |
| 935 | assert(DA->isString()); |
| 936 | StringRef ValueStr = DA->getAsString(); |
| 937 | |
| 938 | // ValueStr format: <EnumeratorStr>:<Value> |
| 939 | size_t Separator = ValueStr.find_first_of(':'); |
| 940 | StringRef EnumeratorStr = ValueStr.substr(0, Separator); |
| 941 | |
| 942 | // Find enumerator index in the debuginfo |
| 943 | DIType *BaseTy = stripQualifiers(Ty, true); |
| 944 | const auto *CTy = cast<DICompositeType>(BaseTy); |
| 945 | assert(CTy->getTag() == dwarf::DW_TAG_enumeration_type); |
| 946 | int EnumIndex = 0; |
| 947 | for (const auto Element : CTy->getElements()) { |
| 948 | const auto *Enum = cast<DIEnumerator>(Element); |
| 949 | if (Enum->getName() == EnumeratorStr) { |
| 950 | AccessStr = std::to_string(EnumIndex); |
| 951 | break; |
| 952 | } |
| 953 | EnumIndex++; |
| 954 | } |
| 955 | |
| 956 | if (CInfo.AccessIndex == BPFCoreSharedInfo::ENUM_VALUE) { |
| 957 | StringRef EValueStr = ValueStr.substr(Separator + 1); |
| 958 | PatchImm = std::stoll(std::string(EValueStr)); |
| 959 | } else { |
| 960 | PatchImm = 1; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | AccessKey = "llvm." + Ty->getName().str() + ":" + |
| 965 | std::to_string(CInfo.AccessIndex) + std::string(":") + |
| 966 | std::to_string(PatchImm) + std::string("$") + AccessStr; |
| 967 | |
| 968 | return Ty; |
| 969 | } |
| 970 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 971 | /// Call/Kind is the base preserve_*_access_index() call. Attempts to do |
| 972 | /// transformation to a chain of relocable GEPs. |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 973 | bool BPFAbstractMemberAccess::transformGEPChain(CallInst *Call, |
Yonghong Song | 02ac750 | 2019-10-03 16:30:29 +0000 | [diff] [blame] | 974 | CallInfo &CInfo) { |
Yonghong Song | d8efec9 | 2019-07-25 16:01:26 +0000 | [diff] [blame] | 975 | std::string AccessKey; |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 976 | MDNode *TypeMeta; |
Yonghong Song | 6d218b4 | 2020-07-29 22:46:07 -0700 | [diff] [blame] | 977 | Value *Base = nullptr; |
| 978 | bool IsInt32Ret; |
| 979 | |
| 980 | IsInt32Ret = CInfo.Kind == BPFPreserveFieldInfoAI; |
| 981 | if (CInfo.Kind == BPFPreserveFieldInfoAI && CInfo.Metadata) { |
| 982 | TypeMeta = computeAccessKey(Call, CInfo, AccessKey, IsInt32Ret); |
| 983 | } else { |
| 984 | Base = computeBaseAndAccessKey(Call, CInfo, AccessKey, TypeMeta); |
| 985 | if (!Base) |
| 986 | return false; |
| 987 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 988 | |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 989 | BasicBlock *BB = Call->getParent(); |
| 990 | GlobalVariable *GV; |
| 991 | |
| 992 | if (GEPGlobals.find(AccessKey) == GEPGlobals.end()) { |
| 993 | IntegerType *VarType; |
Yonghong Song | 6d218b4 | 2020-07-29 22:46:07 -0700 | [diff] [blame] | 994 | if (IsInt32Ret) |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 995 | VarType = Type::getInt32Ty(BB->getContext()); // 32bit return value |
| 996 | else |
Yonghong Song | 6d218b4 | 2020-07-29 22:46:07 -0700 | [diff] [blame] | 997 | VarType = Type::getInt64Ty(BB->getContext()); // 64bit ptr or enum value |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 998 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 999 | GV = new GlobalVariable(*M, VarType, false, GlobalVariable::ExternalLinkage, |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 1000 | NULL, AccessKey); |
| 1001 | GV->addAttribute(BPFCoreSharedInfo::AmaAttr); |
| 1002 | GV->setMetadata(LLVMContext::MD_preserve_access_index, TypeMeta); |
| 1003 | GEPGlobals[AccessKey] = GV; |
| 1004 | } else { |
| 1005 | GV = GEPGlobals[AccessKey]; |
| 1006 | } |
| 1007 | |
| 1008 | if (CInfo.Kind == BPFPreserveFieldInfoAI) { |
| 1009 | // Load the global variable which represents the returned field info. |
Yonghong Song | 6d218b4 | 2020-07-29 22:46:07 -0700 | [diff] [blame] | 1010 | LoadInst *LDInst; |
| 1011 | if (IsInt32Ret) |
| 1012 | LDInst = new LoadInst(Type::getInt32Ty(BB->getContext()), GV, "", Call); |
| 1013 | else |
| 1014 | LDInst = new LoadInst(Type::getInt64Ty(BB->getContext()), GV, "", Call); |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 1015 | |
| 1016 | Instruction *PassThroughInst = |
| 1017 | BPFCoreSharedInfo::insertPassThrough(M, BB, LDInst, Call); |
| 1018 | Call->replaceAllUsesWith(PassThroughInst); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 1019 | Call->eraseFromParent(); |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1023 | // For any original GEP Call and Base %2 like |
| 1024 | // %4 = bitcast %struct.net_device** %dev1 to i64* |
| 1025 | // it is transformed to: |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 1026 | // %6 = load llvm.sk_buff:0:50$0:0:0:2:0 |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1027 | // %7 = bitcast %struct.sk_buff* %2 to i8* |
| 1028 | // %8 = getelementptr i8, i8* %7, %6 |
| 1029 | // %9 = bitcast i8* %8 to i64* |
| 1030 | // using %9 instead of %4 |
| 1031 | // The original Call inst is removed. |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Load the global variable. |
Eli Friedman | 565b56a | 2020-04-07 16:25:52 -0700 | [diff] [blame] | 1034 | auto *LDInst = new LoadInst(Type::getInt64Ty(BB->getContext()), GV, "", Call); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1035 | |
| 1036 | // Generate a BitCast |
| 1037 | auto *BCInst = new BitCastInst(Base, Type::getInt8PtrTy(BB->getContext())); |
| 1038 | BB->getInstList().insert(Call->getIterator(), BCInst); |
| 1039 | |
| 1040 | // Generate a GetElementPtr |
| 1041 | auto *GEP = GetElementPtrInst::Create(Type::getInt8Ty(BB->getContext()), |
| 1042 | BCInst, LDInst); |
| 1043 | BB->getInstList().insert(Call->getIterator(), GEP); |
| 1044 | |
| 1045 | // Generate a BitCast |
| 1046 | auto *BCInst2 = new BitCastInst(GEP, Call->getType()); |
| 1047 | BB->getInstList().insert(Call->getIterator(), BCInst2); |
| 1048 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 1049 | // For the following code, |
| 1050 | // Block0: |
| 1051 | // ... |
| 1052 | // if (...) goto Block1 else ... |
| 1053 | // Block1: |
| 1054 | // %6 = load llvm.sk_buff:0:50$0:0:0:2:0 |
| 1055 | // %7 = bitcast %struct.sk_buff* %2 to i8* |
| 1056 | // %8 = getelementptr i8, i8* %7, %6 |
| 1057 | // ... |
| 1058 | // goto CommonExit |
| 1059 | // Block2: |
| 1060 | // ... |
| 1061 | // if (...) goto Block3 else ... |
| 1062 | // Block3: |
| 1063 | // %6 = load llvm.bpf_map:0:40$0:0:0:2:0 |
| 1064 | // %7 = bitcast %struct.sk_buff* %2 to i8* |
| 1065 | // %8 = getelementptr i8, i8* %7, %6 |
| 1066 | // ... |
| 1067 | // goto CommonExit |
| 1068 | // CommonExit |
| 1069 | // SimplifyCFG may generate: |
| 1070 | // Block0: |
| 1071 | // ... |
| 1072 | // if (...) goto Block_Common else ... |
| 1073 | // Block2: |
| 1074 | // ... |
| 1075 | // if (...) goto Block_Common else ... |
| 1076 | // Block_Common: |
| 1077 | // PHI = [llvm.sk_buff:0:50$0:0:0:2:0, llvm.bpf_map:0:40$0:0:0:2:0] |
| 1078 | // %6 = load PHI |
| 1079 | // %7 = bitcast %struct.sk_buff* %2 to i8* |
| 1080 | // %8 = getelementptr i8, i8* %7, %6 |
| 1081 | // ... |
| 1082 | // goto CommonExit |
| 1083 | // For the above code, we cannot perform proper relocation since |
| 1084 | // "load PHI" has two possible relocations. |
| 1085 | // |
| 1086 | // To prevent above tail merging, we use __builtin_bpf_passthrough() |
| 1087 | // where one of its parameters is a seq_num. Since two |
| 1088 | // __builtin_bpf_passthrough() funcs will always have different seq_num, |
| 1089 | // tail merging cannot happen. The __builtin_bpf_passthrough() will be |
| 1090 | // removed in the beginning of Target IR passes. |
| 1091 | // |
| 1092 | // This approach is also used in other places when global var |
| 1093 | // representing a relocation is used. |
| 1094 | Instruction *PassThroughInst = |
| 1095 | BPFCoreSharedInfo::insertPassThrough(M, BB, BCInst2, Call); |
| 1096 | Call->replaceAllUsesWith(PassThroughInst); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1097 | Call->eraseFromParent(); |
| 1098 | |
| 1099 | return true; |
| 1100 | } |
| 1101 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 1102 | bool BPFAbstractMemberAccess::doTransformation(Function &F) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1103 | bool Transformed = false; |
| 1104 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 1105 | // Collect PreserveDIAccessIndex Intrinsic call chains. |
| 1106 | // The call chains will be used to generate the access |
| 1107 | // patterns similar to GEP. |
| 1108 | collectAICallChains(F); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1109 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 1110 | for (auto &C : BaseAICalls) |
| 1111 | Transformed = transformGEPChain(C.first, C.second) || Transformed; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1112 | |
Yonghong Song | 54d9f74 | 2020-09-02 22:56:41 -0700 | [diff] [blame] | 1113 | return removePreserveAccessIndexIntrinsic(F) || Transformed; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1114 | } |
Arthur Eubanks | 40251fe | 2020-10-05 15:17:12 -0700 | [diff] [blame] | 1115 | |
| 1116 | PreservedAnalyses |
| 1117 | BPFAbstractMemberAccessPass::run(Function &F, FunctionAnalysisManager &AM) { |
| 1118 | return BPFAbstractMemberAccess(TM).run(F) ? PreservedAnalyses::none() |
| 1119 | : PreservedAnalyses::all(); |
| 1120 | } |