1 //===------ BPFAbstractMemberAccess.cpp - Abstracting Member Accesses -----===//
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
7 //===----------------------------------------------------------------------===//
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.
28 // For the member access e.c.b, the compiler will generate code
31 // The compile-once run-everywhere instead generates the following code
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
43 // By repeating the member access on the host, the bpf loader can
44 // adjust "r = 4" as "r = 8".
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>
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:
59 // addr = preserve_struct_access_index(base, 1, 1) !struct s
60 // uint32_t result = bpf_preserve_field_info(addr, info_kind)
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
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
75 //===----------------------------------------------------------------------===//
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"
84 #include "llvm/IR/Module.h"
85 #include "llvm/IR/Type.h"
86 #include "llvm/IR/User.h"
87 #include "llvm/IR/Value.h"
88 #include "llvm/Pass.h"
89 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
92 #define DEBUG_TYPE "bpf-abstract-member-access"
95 constexpr StringRef BPFCoreSharedInfo::AmaAttr;
102 class BPFAbstractMemberAccess final : public ModulePass {
103 StringRef getPassName() const override {
104 return "BPF Abstract Member Access";
107 bool runOnModule(Module &M) override;
112 // Add optional BPFTargetMachine parameter so that BPF backend can add the phase
113 // with target machine to find out the endianness. The default constructor (without
114 // parameters) is used by the pass manager for managing purposes.
115 BPFAbstractMemberAccess(BPFTargetMachine *TM = nullptr) : ModulePass(ID), TM(TM) {}
119 uint32_t AccessIndex;
120 Align RecordAlignment;
124 typedef std::stack<std::pair<CallInst *, CallInfo>> CallInfoStack;
128 BPFPreserveArrayAI = 1,
129 BPFPreserveUnionAI = 2,
130 BPFPreserveStructAI = 3,
131 BPFPreserveFieldInfoAI = 4,
134 const DataLayout *DL = nullptr;
136 std::map<std::string, GlobalVariable *> GEPGlobals;
137 // A map to link preserve_*_access_index instrinsic calls.
138 std::map<CallInst *, std::pair<CallInst *, CallInfo>> AIChain;
139 // A map to hold all the base preserve_*_access_index instrinsic calls.
140 // The base call is not an input of any other preserve_*
142 std::map<CallInst *, CallInfo> BaseAICalls;
144 bool doTransformation(Module &M);
146 void traceAICall(CallInst *Call, CallInfo &ParentInfo);
147 void traceBitCast(BitCastInst *BitCast, CallInst *Parent,
148 CallInfo &ParentInfo);
149 void traceGEP(GetElementPtrInst *GEP, CallInst *Parent,
150 CallInfo &ParentInfo);
151 void collectAICallChains(Module &M, Function &F);
153 bool IsPreserveDIAccessIndexCall(const CallInst *Call, CallInfo &Cinfo);
154 bool IsValidAIChain(const MDNode *ParentMeta, uint32_t ParentAI,
155 const MDNode *ChildMeta);
156 bool removePreserveAccessIndexIntrinsic(Module &M);
157 void replaceWithGEP(std::vector<CallInst *> &CallList,
158 uint32_t NumOfZerosIndex, uint32_t DIIndex);
159 bool HasPreserveFieldInfoCall(CallInfoStack &CallStack);
160 void GetStorageBitRange(DIDerivedType *MemberTy, Align RecordAlignment,
161 uint32_t &StartBitOffset, uint32_t &EndBitOffset);
162 uint32_t GetFieldInfo(uint32_t InfoKind, DICompositeType *CTy,
163 uint32_t AccessIndex, uint32_t PatchImm,
164 Align RecordAlignment);
166 Value *computeBaseAndAccessKey(CallInst *Call, CallInfo &CInfo,
167 std::string &AccessKey, MDNode *&BaseMeta);
168 uint64_t getConstant(const Value *IndexValue);
169 bool transformGEPChain(Module &M, CallInst *Call, CallInfo &CInfo);
171 } // End anonymous namespace
173 char BPFAbstractMemberAccess::ID = 0;
174 INITIALIZE_PASS(BPFAbstractMemberAccess, DEBUG_TYPE,
175 "abstracting struct/union member accessees", false, false)
177 ModulePass *llvm::createBPFAbstractMemberAccess(BPFTargetMachine *TM) {
178 return new BPFAbstractMemberAccess(TM);
181 bool BPFAbstractMemberAccess::runOnModule(Module &M) {
182 LLVM_DEBUG(dbgs() << "********** Abstract Member Accesses **********\n");
184 // Bail out if no debug info.
185 if (M.debug_compile_units().empty())
188 DL = &M.getDataLayout();
189 return doTransformation(M);
192 static bool SkipDIDerivedTag(unsigned Tag, bool skipTypedef) {
193 if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
194 Tag != dwarf::DW_TAG_volatile_type &&
195 Tag != dwarf::DW_TAG_restrict_type &&
196 Tag != dwarf::DW_TAG_member)
198 if (Tag == dwarf::DW_TAG_typedef && !skipTypedef)
203 static DIType * stripQualifiers(DIType *Ty, bool skipTypedef = true) {
204 while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
205 if (!SkipDIDerivedTag(DTy->getTag(), skipTypedef))
207 Ty = DTy->getBaseType();
212 static const DIType * stripQualifiers(const DIType *Ty) {
213 while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
214 if (!SkipDIDerivedTag(DTy->getTag(), true))
216 Ty = DTy->getBaseType();
221 static uint32_t calcArraySize(const DICompositeType *CTy, uint32_t StartDim) {
222 DINodeArray Elements = CTy->getElements();
223 uint32_t DimSize = 1;
224 for (uint32_t I = StartDim; I < Elements.size(); ++I) {
225 if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
226 if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
227 const DISubrange *SR = cast<DISubrange>(Element);
228 auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
229 DimSize *= CI->getSExtValue();
236 /// Check whether a call is a preserve_*_access_index intrinsic call or not.
237 bool BPFAbstractMemberAccess::IsPreserveDIAccessIndexCall(const CallInst *Call,
242 const auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand());
245 if (GV->getName().startswith("llvm.preserve.array.access.index")) {
246 CInfo.Kind = BPFPreserveArrayAI;
247 CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
249 report_fatal_error("Missing metadata for llvm.preserve.array.access.index intrinsic");
250 CInfo.AccessIndex = getConstant(Call->getArgOperand(2));
251 CInfo.Base = Call->getArgOperand(0);
252 CInfo.RecordAlignment =
253 DL->getABITypeAlign(CInfo.Base->getType()->getPointerElementType());
256 if (GV->getName().startswith("llvm.preserve.union.access.index")) {
257 CInfo.Kind = BPFPreserveUnionAI;
258 CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
260 report_fatal_error("Missing metadata for llvm.preserve.union.access.index intrinsic");
261 CInfo.AccessIndex = getConstant(Call->getArgOperand(1));
262 CInfo.Base = Call->getArgOperand(0);
263 CInfo.RecordAlignment =
264 DL->getABITypeAlign(CInfo.Base->getType()->getPointerElementType());
267 if (GV->getName().startswith("llvm.preserve.struct.access.index")) {
268 CInfo.Kind = BPFPreserveStructAI;
269 CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
271 report_fatal_error("Missing metadata for llvm.preserve.struct.access.index intrinsic");
272 CInfo.AccessIndex = getConstant(Call->getArgOperand(2));
273 CInfo.Base = Call->getArgOperand(0);
274 CInfo.RecordAlignment =
275 DL->getABITypeAlign(CInfo.Base->getType()->getPointerElementType());
278 if (GV->getName().startswith("llvm.bpf.preserve.field.info")) {
279 CInfo.Kind = BPFPreserveFieldInfoAI;
280 CInfo.Metadata = nullptr;
281 // Check validity of info_kind as clang did not check this.
282 uint64_t InfoKind = getConstant(Call->getArgOperand(1));
283 if (InfoKind >= BPFCoreSharedInfo::MAX_FIELD_RELOC_KIND)
284 report_fatal_error("Incorrect info_kind for llvm.bpf.preserve.field.info intrinsic");
285 CInfo.AccessIndex = InfoKind;
292 void BPFAbstractMemberAccess::replaceWithGEP(std::vector<CallInst *> &CallList,
293 uint32_t DimensionIndex,
295 for (auto Call : CallList) {
296 uint32_t Dimension = 1;
297 if (DimensionIndex > 0)
298 Dimension = getConstant(Call->getArgOperand(DimensionIndex));
301 ConstantInt::get(Type::getInt32Ty(Call->getParent()->getContext()), 0);
302 SmallVector<Value *, 4> IdxList;
303 for (unsigned I = 0; I < Dimension; ++I)
304 IdxList.push_back(Zero);
305 IdxList.push_back(Call->getArgOperand(GEPIndex));
307 auto *GEP = GetElementPtrInst::CreateInBounds(Call->getArgOperand(0),
309 Call->replaceAllUsesWith(GEP);
310 Call->eraseFromParent();
314 bool BPFAbstractMemberAccess::removePreserveAccessIndexIntrinsic(Module &M) {
315 std::vector<CallInst *> PreserveArrayIndexCalls;
316 std::vector<CallInst *> PreserveUnionIndexCalls;
317 std::vector<CallInst *> PreserveStructIndexCalls;
320 for (Function &F : M)
323 auto *Call = dyn_cast<CallInst>(&I);
325 if (!IsPreserveDIAccessIndexCall(Call, CInfo))
329 if (CInfo.Kind == BPFPreserveArrayAI)
330 PreserveArrayIndexCalls.push_back(Call);
331 else if (CInfo.Kind == BPFPreserveUnionAI)
332 PreserveUnionIndexCalls.push_back(Call);
334 PreserveStructIndexCalls.push_back(Call);
337 // do the following transformation:
338 // . addr = preserve_array_access_index(base, dimension, index)
340 // addr = GEP(base, dimenion's zero's, index)
341 // . addr = preserve_union_access_index(base, di_index)
343 // addr = base, i.e., all usages of "addr" are replaced by "base".
344 // . addr = preserve_struct_access_index(base, gep_index, di_index)
346 // addr = GEP(base, 0, gep_index)
347 replaceWithGEP(PreserveArrayIndexCalls, 1, 2);
348 replaceWithGEP(PreserveStructIndexCalls, 0, 1);
349 for (auto Call : PreserveUnionIndexCalls) {
350 Call->replaceAllUsesWith(Call->getArgOperand(0));
351 Call->eraseFromParent();
357 /// Check whether the access index chain is valid. We check
358 /// here because there may be type casts between two
359 /// access indexes. We want to ensure memory access still valid.
360 bool BPFAbstractMemberAccess::IsValidAIChain(const MDNode *ParentType,
362 const MDNode *ChildType) {
364 return true; // preserve_field_info, no type comparison needed.
366 const DIType *PType = stripQualifiers(cast<DIType>(ParentType));
367 const DIType *CType = stripQualifiers(cast<DIType>(ChildType));
369 // Child is a derived/pointer type, which is due to type casting.
370 // Pointer type cannot be in the middle of chain.
371 if (isa<DIDerivedType>(CType))
374 // Parent is a pointer type.
375 if (const auto *PtrTy = dyn_cast<DIDerivedType>(PType)) {
376 if (PtrTy->getTag() != dwarf::DW_TAG_pointer_type)
378 return stripQualifiers(PtrTy->getBaseType()) == CType;
381 // Otherwise, struct/union/array types
382 const auto *PTy = dyn_cast<DICompositeType>(PType);
383 const auto *CTy = dyn_cast<DICompositeType>(CType);
384 assert(PTy && CTy && "ParentType or ChildType is null or not composite");
386 uint32_t PTyTag = PTy->getTag();
387 assert(PTyTag == dwarf::DW_TAG_array_type ||
388 PTyTag == dwarf::DW_TAG_structure_type ||
389 PTyTag == dwarf::DW_TAG_union_type);
391 uint32_t CTyTag = CTy->getTag();
392 assert(CTyTag == dwarf::DW_TAG_array_type ||
393 CTyTag == dwarf::DW_TAG_structure_type ||
394 CTyTag == dwarf::DW_TAG_union_type);
396 // Multi dimensional arrays, base element should be the same
397 if (PTyTag == dwarf::DW_TAG_array_type && PTyTag == CTyTag)
398 return PTy->getBaseType() == CTy->getBaseType();
401 if (PTyTag == dwarf::DW_TAG_array_type)
402 Ty = PTy->getBaseType();
404 Ty = dyn_cast<DIType>(PTy->getElements()[ParentAI]);
406 return dyn_cast<DICompositeType>(stripQualifiers(Ty)) == CTy;
409 void BPFAbstractMemberAccess::traceAICall(CallInst *Call,
410 CallInfo &ParentInfo) {
411 for (User *U : Call->users()) {
412 Instruction *Inst = dyn_cast<Instruction>(U);
416 if (auto *BI = dyn_cast<BitCastInst>(Inst)) {
417 traceBitCast(BI, Call, ParentInfo);
418 } else if (auto *CI = dyn_cast<CallInst>(Inst)) {
421 if (IsPreserveDIAccessIndexCall(CI, ChildInfo) &&
422 IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex,
423 ChildInfo.Metadata)) {
424 AIChain[CI] = std::make_pair(Call, ParentInfo);
425 traceAICall(CI, ChildInfo);
427 BaseAICalls[Call] = ParentInfo;
429 } else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) {
430 if (GI->hasAllZeroIndices())
431 traceGEP(GI, Call, ParentInfo);
433 BaseAICalls[Call] = ParentInfo;
435 BaseAICalls[Call] = ParentInfo;
440 void BPFAbstractMemberAccess::traceBitCast(BitCastInst *BitCast,
442 CallInfo &ParentInfo) {
443 for (User *U : BitCast->users()) {
444 Instruction *Inst = dyn_cast<Instruction>(U);
448 if (auto *BI = dyn_cast<BitCastInst>(Inst)) {
449 traceBitCast(BI, Parent, ParentInfo);
450 } else if (auto *CI = dyn_cast<CallInst>(Inst)) {
452 if (IsPreserveDIAccessIndexCall(CI, ChildInfo) &&
453 IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex,
454 ChildInfo.Metadata)) {
455 AIChain[CI] = std::make_pair(Parent, ParentInfo);
456 traceAICall(CI, ChildInfo);
458 BaseAICalls[Parent] = ParentInfo;
460 } else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) {
461 if (GI->hasAllZeroIndices())
462 traceGEP(GI, Parent, ParentInfo);
464 BaseAICalls[Parent] = ParentInfo;
466 BaseAICalls[Parent] = ParentInfo;
471 void BPFAbstractMemberAccess::traceGEP(GetElementPtrInst *GEP, CallInst *Parent,
472 CallInfo &ParentInfo) {
473 for (User *U : GEP->users()) {
474 Instruction *Inst = dyn_cast<Instruction>(U);
478 if (auto *BI = dyn_cast<BitCastInst>(Inst)) {
479 traceBitCast(BI, Parent, ParentInfo);
480 } else if (auto *CI = dyn_cast<CallInst>(Inst)) {
482 if (IsPreserveDIAccessIndexCall(CI, ChildInfo) &&
483 IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex,
484 ChildInfo.Metadata)) {
485 AIChain[CI] = std::make_pair(Parent, ParentInfo);
486 traceAICall(CI, ChildInfo);
488 BaseAICalls[Parent] = ParentInfo;
490 } else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) {
491 if (GI->hasAllZeroIndices())
492 traceGEP(GI, Parent, ParentInfo);
494 BaseAICalls[Parent] = ParentInfo;
496 BaseAICalls[Parent] = ParentInfo;
501 void BPFAbstractMemberAccess::collectAICallChains(Module &M, Function &F) {
508 auto *Call = dyn_cast<CallInst>(&I);
509 if (!IsPreserveDIAccessIndexCall(Call, CInfo) ||
510 AIChain.find(Call) != AIChain.end())
513 traceAICall(Call, CInfo);
517 uint64_t BPFAbstractMemberAccess::getConstant(const Value *IndexValue) {
518 const ConstantInt *CV = dyn_cast<ConstantInt>(IndexValue);
520 return CV->getValue().getZExtValue();
523 /// Get the start and the end of storage offset for \p MemberTy.
524 void BPFAbstractMemberAccess::GetStorageBitRange(DIDerivedType *MemberTy,
525 Align RecordAlignment,
526 uint32_t &StartBitOffset,
527 uint32_t &EndBitOffset) {
528 uint32_t MemberBitSize = MemberTy->getSizeInBits();
529 uint32_t MemberBitOffset = MemberTy->getOffsetInBits();
530 uint32_t AlignBits = RecordAlignment.value() * 8;
531 if (RecordAlignment > 8 || MemberBitSize > AlignBits)
532 report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info, "
533 "requiring too big alignment");
535 StartBitOffset = MemberBitOffset & ~(AlignBits - 1);
536 if ((StartBitOffset + AlignBits) < (MemberBitOffset + MemberBitSize))
537 report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info, "
538 "cross alignment boundary");
539 EndBitOffset = StartBitOffset + AlignBits;
542 uint32_t BPFAbstractMemberAccess::GetFieldInfo(uint32_t InfoKind,
543 DICompositeType *CTy,
544 uint32_t AccessIndex,
546 Align RecordAlignment) {
547 if (InfoKind == BPFCoreSharedInfo::FIELD_EXISTENCE)
550 uint32_t Tag = CTy->getTag();
551 if (InfoKind == BPFCoreSharedInfo::FIELD_BYTE_OFFSET) {
552 if (Tag == dwarf::DW_TAG_array_type) {
553 auto *EltTy = stripQualifiers(CTy->getBaseType());
554 PatchImm += AccessIndex * calcArraySize(CTy, 1) *
555 (EltTy->getSizeInBits() >> 3);
556 } else if (Tag == dwarf::DW_TAG_structure_type) {
557 auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
558 if (!MemberTy->isBitField()) {
559 PatchImm += MemberTy->getOffsetInBits() >> 3;
561 unsigned SBitOffset, NextSBitOffset;
562 GetStorageBitRange(MemberTy, RecordAlignment, SBitOffset,
564 PatchImm += SBitOffset >> 3;
570 if (InfoKind == BPFCoreSharedInfo::FIELD_BYTE_SIZE) {
571 if (Tag == dwarf::DW_TAG_array_type) {
572 auto *EltTy = stripQualifiers(CTy->getBaseType());
573 return calcArraySize(CTy, 1) * (EltTy->getSizeInBits() >> 3);
575 auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
576 uint32_t SizeInBits = MemberTy->getSizeInBits();
577 if (!MemberTy->isBitField())
578 return SizeInBits >> 3;
580 unsigned SBitOffset, NextSBitOffset;
581 GetStorageBitRange(MemberTy, RecordAlignment, SBitOffset, NextSBitOffset);
582 SizeInBits = NextSBitOffset - SBitOffset;
583 if (SizeInBits & (SizeInBits - 1))
584 report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info");
585 return SizeInBits >> 3;
589 if (InfoKind == BPFCoreSharedInfo::FIELD_SIGNEDNESS) {
590 const DIType *BaseTy;
591 if (Tag == dwarf::DW_TAG_array_type) {
592 // Signedness only checked when final array elements are accessed.
593 if (CTy->getElements().size() != 1)
594 report_fatal_error("Invalid array expression for llvm.bpf.preserve.field.info");
595 BaseTy = stripQualifiers(CTy->getBaseType());
597 auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
598 BaseTy = stripQualifiers(MemberTy->getBaseType());
601 // Only basic types and enum types have signedness.
602 const auto *BTy = dyn_cast<DIBasicType>(BaseTy);
604 const auto *CompTy = dyn_cast<DICompositeType>(BaseTy);
605 // Report an error if the field expression does not have signedness.
606 if (!CompTy || CompTy->getTag() != dwarf::DW_TAG_enumeration_type)
607 report_fatal_error("Invalid field expression for llvm.bpf.preserve.field.info");
608 BaseTy = stripQualifiers(CompTy->getBaseType());
609 BTy = dyn_cast<DIBasicType>(BaseTy);
611 uint32_t Encoding = BTy->getEncoding();
612 return (Encoding == dwarf::DW_ATE_signed || Encoding == dwarf::DW_ATE_signed_char);
615 if (InfoKind == BPFCoreSharedInfo::FIELD_LSHIFT_U64) {
616 // The value is loaded into a value with FIELD_BYTE_SIZE size,
617 // and then zero or sign extended to U64.
618 // FIELD_LSHIFT_U64 and FIELD_RSHIFT_U64 are operations
619 // to extract the original value.
620 const Triple &Triple = TM->getTargetTriple();
621 DIDerivedType *MemberTy = nullptr;
622 bool IsBitField = false;
625 if (Tag == dwarf::DW_TAG_array_type) {
626 auto *EltTy = stripQualifiers(CTy->getBaseType());
627 SizeInBits = calcArraySize(CTy, 1) * EltTy->getSizeInBits();
629 MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
630 SizeInBits = MemberTy->getSizeInBits();
631 IsBitField = MemberTy->isBitField();
636 report_fatal_error("too big field size for llvm.bpf.preserve.field.info");
637 return 64 - SizeInBits;
640 unsigned SBitOffset, NextSBitOffset;
641 GetStorageBitRange(MemberTy, RecordAlignment, SBitOffset, NextSBitOffset);
642 if (NextSBitOffset - SBitOffset > 64)
643 report_fatal_error("too big field size for llvm.bpf.preserve.field.info");
645 unsigned OffsetInBits = MemberTy->getOffsetInBits();
646 if (Triple.getArch() == Triple::bpfel)
647 return SBitOffset + 64 - OffsetInBits - SizeInBits;
649 return OffsetInBits + 64 - NextSBitOffset;
652 if (InfoKind == BPFCoreSharedInfo::FIELD_RSHIFT_U64) {
653 DIDerivedType *MemberTy = nullptr;
654 bool IsBitField = false;
656 if (Tag == dwarf::DW_TAG_array_type) {
657 auto *EltTy = stripQualifiers(CTy->getBaseType());
658 SizeInBits = calcArraySize(CTy, 1) * EltTy->getSizeInBits();
660 MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
661 SizeInBits = MemberTy->getSizeInBits();
662 IsBitField = MemberTy->isBitField();
667 report_fatal_error("too big field size for llvm.bpf.preserve.field.info");
668 return 64 - SizeInBits;
671 unsigned SBitOffset, NextSBitOffset;
672 GetStorageBitRange(MemberTy, RecordAlignment, SBitOffset, NextSBitOffset);
673 if (NextSBitOffset - SBitOffset > 64)
674 report_fatal_error("too big field size for llvm.bpf.preserve.field.info");
676 return 64 - SizeInBits;
679 llvm_unreachable("Unknown llvm.bpf.preserve.field.info info kind");
682 bool BPFAbstractMemberAccess::HasPreserveFieldInfoCall(CallInfoStack &CallStack) {
683 // This is called in error return path, no need to maintain CallStack.
684 while (CallStack.size()) {
685 auto StackElem = CallStack.top();
686 if (StackElem.second.Kind == BPFPreserveFieldInfoAI)
693 /// Compute the base of the whole preserve_* intrinsics chains, i.e., the base
694 /// pointer of the first preserve_*_access_index call, and construct the access
695 /// string, which will be the name of a global variable.
696 Value *BPFAbstractMemberAccess::computeBaseAndAccessKey(CallInst *Call,
698 std::string &AccessKey,
700 Value *Base = nullptr;
701 std::string TypeName;
702 CallInfoStack CallStack;
704 // Put the access chain into a stack with the top as the head of the chain.
706 CallStack.push(std::make_pair(Call, CInfo));
707 CInfo = AIChain[Call].second;
708 Call = AIChain[Call].first;
711 // The access offset from the base of the head of chain is also
712 // calculated here as all debuginfo types are available.
714 // Get type name and calculate the first index.
715 // We only want to get type name from typedef, structure or union.
716 // If user wants a relocation like
717 // int *p; ... __builtin_preserve_access_index(&p[4]) ...
719 // int a[10][20]; ... __builtin_preserve_access_index(&a[2][3]) ...
720 // we will skip them.
721 uint32_t FirstIndex = 0;
722 uint32_t PatchImm = 0; // AccessOffset or the requested field info
723 uint32_t InfoKind = BPFCoreSharedInfo::FIELD_BYTE_OFFSET;
724 while (CallStack.size()) {
725 auto StackElem = CallStack.top();
726 Call = StackElem.first;
727 CInfo = StackElem.second;
732 DIType *PossibleTypeDef = stripQualifiers(cast<DIType>(CInfo.Metadata),
734 DIType *Ty = stripQualifiers(PossibleTypeDef);
735 if (CInfo.Kind == BPFPreserveUnionAI ||
736 CInfo.Kind == BPFPreserveStructAI) {
737 // struct or union type. If the typedef is in the metadata, always
739 TypeName = std::string(PossibleTypeDef->getName());
740 TypeMeta = PossibleTypeDef;
741 PatchImm += FirstIndex * (Ty->getSizeInBits() >> 3);
745 assert(CInfo.Kind == BPFPreserveArrayAI);
747 // Array entries will always be consumed for accumulative initial index.
750 // BPFPreserveArrayAI
751 uint64_t AccessIndex = CInfo.AccessIndex;
753 DIType *BaseTy = nullptr;
754 bool CheckElemType = false;
755 if (const auto *CTy = dyn_cast<DICompositeType>(Ty)) {
757 assert(CTy->getTag() == dwarf::DW_TAG_array_type);
760 FirstIndex += AccessIndex * calcArraySize(CTy, 1);
761 BaseTy = stripQualifiers(CTy->getBaseType());
762 CheckElemType = CTy->getElements().size() == 1;
765 auto *DTy = cast<DIDerivedType>(Ty);
766 assert(DTy->getTag() == dwarf::DW_TAG_pointer_type);
768 BaseTy = stripQualifiers(DTy->getBaseType());
769 CTy = dyn_cast<DICompositeType>(BaseTy);
771 CheckElemType = true;
772 } else if (CTy->getTag() != dwarf::DW_TAG_array_type) {
773 FirstIndex += AccessIndex;
774 CheckElemType = true;
776 FirstIndex += AccessIndex * calcArraySize(CTy, 0);
781 auto *CTy = dyn_cast<DICompositeType>(BaseTy);
783 if (HasPreserveFieldInfoCall(CallStack))
784 report_fatal_error("Invalid field access for llvm.preserve.field.info intrinsic");
788 unsigned CTag = CTy->getTag();
789 if (CTag == dwarf::DW_TAG_structure_type || CTag == dwarf::DW_TAG_union_type) {
790 TypeName = std::string(CTy->getName());
792 if (HasPreserveFieldInfoCall(CallStack))
793 report_fatal_error("Invalid field access for llvm.preserve.field.info intrinsic");
797 PatchImm += FirstIndex * (CTy->getSizeInBits() >> 3);
801 assert(TypeName.size());
802 AccessKey += std::to_string(FirstIndex);
804 // Traverse the rest of access chain to complete offset calculation
805 // and access key construction.
806 while (CallStack.size()) {
807 auto StackElem = CallStack.top();
808 CInfo = StackElem.second;
811 if (CInfo.Kind == BPFPreserveFieldInfoAI) {
812 InfoKind = CInfo.AccessIndex;
816 // If the next Call (the top of the stack) is a BPFPreserveFieldInfoAI,
817 // the action will be extracting field info.
818 if (CallStack.size()) {
819 auto StackElem2 = CallStack.top();
820 CallInfo CInfo2 = StackElem2.second;
821 if (CInfo2.Kind == BPFPreserveFieldInfoAI) {
822 InfoKind = CInfo2.AccessIndex;
823 assert(CallStack.size() == 1);
828 uint64_t AccessIndex = CInfo.AccessIndex;
829 AccessKey += ":" + std::to_string(AccessIndex);
831 MDNode *MDN = CInfo.Metadata;
832 // At this stage, it cannot be pointer type.
833 auto *CTy = cast<DICompositeType>(stripQualifiers(cast<DIType>(MDN)));
834 PatchImm = GetFieldInfo(InfoKind, CTy, AccessIndex, PatchImm,
835 CInfo.RecordAlignment);
839 // "llvm." + type name + ":" + reloc type + ":" + patched imm + "$" +
841 // uniquely identifying one relocation.
842 // The prefix "llvm." indicates this is a temporary global, which should
843 // not be emitted to ELF file.
844 AccessKey = "llvm." + TypeName + ":" + std::to_string(InfoKind) + ":" +
845 std::to_string(PatchImm) + "$" + AccessKey;
850 /// Call/Kind is the base preserve_*_access_index() call. Attempts to do
851 /// transformation to a chain of relocable GEPs.
852 bool BPFAbstractMemberAccess::transformGEPChain(Module &M, CallInst *Call,
854 std::string AccessKey;
857 computeBaseAndAccessKey(Call, CInfo, AccessKey, TypeMeta);
861 BasicBlock *BB = Call->getParent();
864 if (GEPGlobals.find(AccessKey) == GEPGlobals.end()) {
865 IntegerType *VarType;
866 if (CInfo.Kind == BPFPreserveFieldInfoAI)
867 VarType = Type::getInt32Ty(BB->getContext()); // 32bit return value
869 VarType = Type::getInt64Ty(BB->getContext()); // 64bit ptr arith
871 GV = new GlobalVariable(M, VarType, false, GlobalVariable::ExternalLinkage,
873 GV->addAttribute(BPFCoreSharedInfo::AmaAttr);
874 GV->setMetadata(LLVMContext::MD_preserve_access_index, TypeMeta);
875 GEPGlobals[AccessKey] = GV;
877 GV = GEPGlobals[AccessKey];
880 if (CInfo.Kind == BPFPreserveFieldInfoAI) {
881 // Load the global variable which represents the returned field info.
882 auto *LDInst = new LoadInst(Type::getInt32Ty(BB->getContext()), GV, "",
884 Call->replaceAllUsesWith(LDInst);
885 Call->eraseFromParent();
889 // For any original GEP Call and Base %2 like
890 // %4 = bitcast %struct.net_device** %dev1 to i64*
891 // it is transformed to:
892 // %6 = load sk_buff:50:$0:0:0:2:0
893 // %7 = bitcast %struct.sk_buff* %2 to i8*
894 // %8 = getelementptr i8, i8* %7, %6
895 // %9 = bitcast i8* %8 to i64*
896 // using %9 instead of %4
897 // The original Call inst is removed.
899 // Load the global variable.
900 auto *LDInst = new LoadInst(Type::getInt64Ty(BB->getContext()), GV, "", Call);
902 // Generate a BitCast
903 auto *BCInst = new BitCastInst(Base, Type::getInt8PtrTy(BB->getContext()));
904 BB->getInstList().insert(Call->getIterator(), BCInst);
906 // Generate a GetElementPtr
907 auto *GEP = GetElementPtrInst::Create(Type::getInt8Ty(BB->getContext()),
909 BB->getInstList().insert(Call->getIterator(), GEP);
911 // Generate a BitCast
912 auto *BCInst2 = new BitCastInst(GEP, Call->getType());
913 BB->getInstList().insert(Call->getIterator(), BCInst2);
915 Call->replaceAllUsesWith(BCInst2);
916 Call->eraseFromParent();
921 bool BPFAbstractMemberAccess::doTransformation(Module &M) {
922 bool Transformed = false;
924 for (Function &F : M) {
925 // Collect PreserveDIAccessIndex Intrinsic call chains.
926 // The call chains will be used to generate the access
927 // patterns similar to GEP.
928 collectAICallChains(M, F);
930 for (auto &C : BaseAICalls)
931 Transformed = transformGEPChain(M, C.first, C.second) || Transformed;
934 return removePreserveAccessIndexIntrinsic(M) || Transformed;