#include "BPF.h"
#include "BPFCORE.h"
#include "BPFTargetMachine.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicsBPF.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <stack>
#define DEBUG_TYPE "bpf-abstract-member-access"
namespace llvm {
constexpr StringRef BPFCoreSharedInfo::AmaAttr;
uint32_t BPFCoreSharedInfo::SeqNum;
Instruction *BPFCoreSharedInfo::insertPassThrough(Module *M, BasicBlock *BB,
Instruction *Input,
Instruction *Before) {
Function *Fn = Intrinsic::getDeclaration(
M, Intrinsic::bpf_passthrough, {Input->getType(), Input->getType()});
Constant *SeqNumVal = ConstantInt::get(Type::getInt32Ty(BB->getContext()),
BPFCoreSharedInfo::SeqNum++);
auto *NewInst = CallInst::Create(Fn, {SeqNumVal, Input});
BB->getInstList().insert(Before->getIterator(), NewInst);
return NewInst;
}
}
using namespace llvm;
namespace {
class BPFAbstractMemberAccess final {
public:
BPFAbstractMemberAccess(BPFTargetMachine *TM) : TM(TM) {}
bool run(Function &F);
struct CallInfo {
uint32_t Kind;
uint32_t AccessIndex;
MaybeAlign RecordAlignment;
MDNode *Metadata;
Value *Base;
};
typedef std::stack<std::pair<CallInst *, CallInfo>> CallInfoStack;
private:
enum : uint32_t {
BPFPreserveArrayAI = 1,
BPFPreserveUnionAI = 2,
BPFPreserveStructAI = 3,
BPFPreserveFieldInfoAI = 4,
};
TargetMachine *TM;
const DataLayout *DL = nullptr;
Module *M = nullptr;
static std::map<std::string, GlobalVariable *> GEPGlobals;
std::map<CallInst *, std::pair<CallInst *, CallInfo>> AIChain;
std::map<CallInst *, CallInfo> BaseAICalls;
std::map<DICompositeType *, DIDerivedType *> AnonRecords;
void CheckAnonRecordType(DIDerivedType *ParentTy, DIType *Ty);
void CheckCompositeType(DIDerivedType *ParentTy, DICompositeType *CTy);
void CheckDerivedType(DIDerivedType *ParentTy, DIDerivedType *DTy);
void ResetMetadata(struct CallInfo &CInfo);
bool doTransformation(Function &F);
void traceAICall(CallInst *Call, CallInfo &ParentInfo);
void traceBitCast(BitCastInst *BitCast, CallInst *Parent,
CallInfo &ParentInfo);
void traceGEP(GetElementPtrInst *GEP, CallInst *Parent,
CallInfo &ParentInfo);
void collectAICallChains(Function &F);
bool IsPreserveDIAccessIndexCall(const CallInst *Call, CallInfo &Cinfo);
bool IsValidAIChain(const MDNode *ParentMeta, uint32_t ParentAI,
const MDNode *ChildMeta);
bool removePreserveAccessIndexIntrinsic(Function &F);
void replaceWithGEP(std::vector<CallInst *> &CallList,
uint32_t NumOfZerosIndex, uint32_t DIIndex);
bool HasPreserveFieldInfoCall(CallInfoStack &CallStack);
void GetStorageBitRange(DIDerivedType *MemberTy, Align RecordAlignment,
uint32_t &StartBitOffset, uint32_t &EndBitOffset);
uint32_t GetFieldInfo(uint32_t InfoKind, DICompositeType *CTy,
uint32_t AccessIndex, uint32_t PatchImm,
MaybeAlign RecordAlignment);
Value *computeBaseAndAccessKey(CallInst *Call, CallInfo &CInfo,
std::string &AccessKey, MDNode *&BaseMeta);
MDNode *computeAccessKey(CallInst *Call, CallInfo &CInfo,
std::string &AccessKey, bool &IsInt32Ret);
uint64_t getConstant(const Value *IndexValue);
bool transformGEPChain(CallInst *Call, CallInfo &CInfo);
};
std::map<std::string, GlobalVariable *> BPFAbstractMemberAccess::GEPGlobals;
class BPFAbstractMemberAccessLegacyPass final : public FunctionPass {
BPFTargetMachine *TM;
bool runOnFunction(Function &F) override {
return BPFAbstractMemberAccess(TM).run(F);
}
public:
static char ID;
BPFAbstractMemberAccessLegacyPass(BPFTargetMachine *TM = nullptr)
: FunctionPass(ID), TM(TM) {}
};
}
char BPFAbstractMemberAccessLegacyPass::ID = 0;
INITIALIZE_PASS(BPFAbstractMemberAccessLegacyPass, DEBUG_TYPE,
"BPF Abstract Member Access", false, false)
FunctionPass *llvm::createBPFAbstractMemberAccess(BPFTargetMachine *TM) {
return new BPFAbstractMemberAccessLegacyPass(TM);
}
bool BPFAbstractMemberAccess::run(Function &F) {
LLVM_DEBUG(dbgs() << "********** Abstract Member Accesses **********\n");
M = F.getParent();
if (!M)
return false;
if (M->debug_compile_units().empty())
return false;
DISubprogram *SP = F.getSubprogram();
if (SP && SP->isDefinition()) {
for (DIType *Ty: SP->getType()->getTypeArray())
CheckAnonRecordType(nullptr, Ty);
for (const DINode *DN : SP->getRetainedNodes()) {
if (const auto *DV = dyn_cast<DILocalVariable>(DN))
CheckAnonRecordType(nullptr, DV->getType());
}
}
DL = &M->getDataLayout();
return doTransformation(F);
}
void BPFAbstractMemberAccess::ResetMetadata(struct CallInfo &CInfo) {
if (auto Ty = dyn_cast<DICompositeType>(CInfo.Metadata)) {
if (AnonRecords.find(Ty) != AnonRecords.end()) {
if (AnonRecords[Ty] != nullptr)
CInfo.Metadata = AnonRecords[Ty];
}
}
}
void BPFAbstractMemberAccess::CheckCompositeType(DIDerivedType *ParentTy,
DICompositeType *CTy) {
if (!CTy->getName().empty() || !ParentTy ||
ParentTy->getTag() != dwarf::DW_TAG_typedef)
return;
if (AnonRecords.find(CTy) == AnonRecords.end()) {
AnonRecords[CTy] = ParentTy;
return;
}
DIDerivedType *CurrTy = AnonRecords[CTy];
if (CurrTy == ParentTy)
return;
AnonRecords[CTy] = nullptr;
}
void BPFAbstractMemberAccess::CheckDerivedType(DIDerivedType *ParentTy,
DIDerivedType *DTy) {
DIType *BaseType = DTy->getBaseType();
if (!BaseType)
return;
unsigned Tag = DTy->getTag();
if (Tag == dwarf::DW_TAG_pointer_type)
CheckAnonRecordType(nullptr, BaseType);
else if (Tag == dwarf::DW_TAG_typedef)
CheckAnonRecordType(DTy, BaseType);
else
CheckAnonRecordType(ParentTy, BaseType);
}
void BPFAbstractMemberAccess::CheckAnonRecordType(DIDerivedType *ParentTy,
DIType *Ty) {
if (!Ty)
return;
if (auto *CTy = dyn_cast<DICompositeType>(Ty))
return CheckCompositeType(ParentTy, CTy);
else if (auto *DTy = dyn_cast<DIDerivedType>(Ty))
return CheckDerivedType(ParentTy, DTy);
}
static bool SkipDIDerivedTag(unsigned Tag, bool skipTypedef) {
if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
Tag != dwarf::DW_TAG_volatile_type &&
Tag != dwarf::DW_TAG_restrict_type &&
Tag != dwarf::DW_TAG_member)
return false;
if (Tag == dwarf::DW_TAG_typedef && !skipTypedef)
return false;
return true;
}
static DIType * stripQualifiers(DIType *Ty, bool skipTypedef = true) {
while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
if (!SkipDIDerivedTag(DTy->getTag(), skipTypedef))
break;
Ty = DTy->getBaseType();
}
return Ty;
}
static const DIType * stripQualifiers(const DIType *Ty) {
while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
if (!SkipDIDerivedTag(DTy->getTag(), true))
break;
Ty = DTy->getBaseType();
}
return Ty;
}
static uint32_t calcArraySize(const DICompositeType *CTy, uint32_t StartDim) {
DINodeArray Elements = CTy->getElements();
uint32_t DimSize = 1;
for (uint32_t I = StartDim; I < Elements.size(); ++I) {
if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
const DISubrange *SR = cast<DISubrange>(Element);
auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
DimSize *= CI->getSExtValue();
}
}
return DimSize;
}
static Type *getBaseElementType(const CallInst *Call) {
return Call->getParamElementType(0);
}
bool BPFAbstractMemberAccess::IsPreserveDIAccessIndexCall(const CallInst *Call,
CallInfo &CInfo) {
if (!Call)
return false;
const auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand());
if (!GV)
return false;
if (GV->getName().startswith("llvm.preserve.array.access.index")) {
CInfo.Kind = BPFPreserveArrayAI;
CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
if (!CInfo.Metadata)
report_fatal_error("Missing metadata for llvm.preserve.array.access.index intrinsic");
CInfo.AccessIndex = getConstant(Call->getArgOperand(2));
CInfo.Base = Call->getArgOperand(0);
CInfo.RecordAlignment = DL->getABITypeAlign(getBaseElementType(Call));
return true;
}
if (GV->getName().startswith("llvm.preserve.union.access.index")) {
CInfo.Kind = BPFPreserveUnionAI;
CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
if (!CInfo.Metadata)
report_fatal_error("Missing metadata for llvm.preserve.union.access.index intrinsic");
ResetMetadata(CInfo);
CInfo.AccessIndex = getConstant(Call->getArgOperand(1));
CInfo.Base = Call->getArgOperand(0);
return true;
}
if (GV->getName().startswith("llvm.preserve.struct.access.index")) {
CInfo.Kind = BPFPreserveStructAI;
CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
if (!CInfo.Metadata)
report_fatal_error("Missing metadata for llvm.preserve.struct.access.index intrinsic");
ResetMetadata(CInfo);
CInfo.AccessIndex = getConstant(Call->getArgOperand(2));
CInfo.Base = Call->getArgOperand(0);
CInfo.RecordAlignment = DL->getABITypeAlign(getBaseElementType(Call));
return true;
}
if (GV->getName().startswith("llvm.bpf.preserve.field.info")) {
CInfo.Kind = BPFPreserveFieldInfoAI;
CInfo.Metadata = nullptr;
uint64_t InfoKind = getConstant(Call->getArgOperand(1));
if (InfoKind >= BPFCoreSharedInfo::MAX_FIELD_RELOC_KIND)
report_fatal_error("Incorrect info_kind for llvm.bpf.preserve.field.info intrinsic");
CInfo.AccessIndex = InfoKind;
return true;
}
if (GV->getName().startswith("llvm.bpf.preserve.type.info")) {
CInfo.Kind = BPFPreserveFieldInfoAI;
CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
if (!CInfo.Metadata)
report_fatal_error("Missing metadata for llvm.preserve.type.info intrinsic");
uint64_t Flag = getConstant(Call->getArgOperand(1));
if (Flag >= BPFCoreSharedInfo::MAX_PRESERVE_TYPE_INFO_FLAG)
report_fatal_error("Incorrect flag for llvm.bpf.preserve.type.info intrinsic");
if (Flag == BPFCoreSharedInfo::PRESERVE_TYPE_INFO_EXISTENCE)
CInfo.AccessIndex = BPFCoreSharedInfo::TYPE_EXISTENCE;
else if (Flag == BPFCoreSharedInfo::PRESERVE_TYPE_INFO_MATCH)
CInfo.AccessIndex = BPFCoreSharedInfo::TYPE_MATCH;
else
CInfo.AccessIndex = BPFCoreSharedInfo::TYPE_SIZE;
return true;
}
if (GV->getName().startswith("llvm.bpf.preserve.enum.value")) {
CInfo.Kind = BPFPreserveFieldInfoAI;
CInfo.Metadata = Call->getMetadata(LLVMContext::MD_preserve_access_index);
if (!CInfo.Metadata)
report_fatal_error("Missing metadata for llvm.preserve.enum.value intrinsic");
uint64_t Flag = getConstant(Call->getArgOperand(2));
if (Flag >= BPFCoreSharedInfo::MAX_PRESERVE_ENUM_VALUE_FLAG)
report_fatal_error("Incorrect flag for llvm.bpf.preserve.enum.value intrinsic");
if (Flag == BPFCoreSharedInfo::PRESERVE_ENUM_VALUE_EXISTENCE)
CInfo.AccessIndex = BPFCoreSharedInfo::ENUM_VALUE_EXISTENCE;
else
CInfo.AccessIndex = BPFCoreSharedInfo::ENUM_VALUE;
return true;
}
return false;
}
void BPFAbstractMemberAccess::replaceWithGEP(std::vector<CallInst *> &CallList,
uint32_t DimensionIndex,
uint32_t GEPIndex) {
for (auto Call : CallList) {
uint32_t Dimension = 1;
if (DimensionIndex > 0)
Dimension = getConstant(Call->getArgOperand(DimensionIndex));
Constant *Zero =
ConstantInt::get(Type::getInt32Ty(Call->getParent()->getContext()), 0);
SmallVector<Value *, 4> IdxList;
for (unsigned I = 0; I < Dimension; ++I)
IdxList.push_back(Zero);
IdxList.push_back(Call->getArgOperand(GEPIndex));
auto *GEP = GetElementPtrInst::CreateInBounds(
getBaseElementType(Call), Call->getArgOperand(0), IdxList, "", Call);
Call->replaceAllUsesWith(GEP);
Call->eraseFromParent();
}
}
bool BPFAbstractMemberAccess::removePreserveAccessIndexIntrinsic(Function &F) {
std::vector<CallInst *> PreserveArrayIndexCalls;
std::vector<CallInst *> PreserveUnionIndexCalls;
std::vector<CallInst *> PreserveStructIndexCalls;
bool Found = false;
for (auto &BB : F)
for (auto &I : BB) {
auto *Call = dyn_cast<CallInst>(&I);
CallInfo CInfo;
if (!IsPreserveDIAccessIndexCall(Call, CInfo))
continue;
Found = true;
if (CInfo.Kind == BPFPreserveArrayAI)
PreserveArrayIndexCalls.push_back(Call);
else if (CInfo.Kind == BPFPreserveUnionAI)
PreserveUnionIndexCalls.push_back(Call);
else
PreserveStructIndexCalls.push_back(Call);
}
replaceWithGEP(PreserveArrayIndexCalls, 1, 2);
replaceWithGEP(PreserveStructIndexCalls, 0, 1);
for (auto Call : PreserveUnionIndexCalls) {
Call->replaceAllUsesWith(Call->getArgOperand(0));
Call->eraseFromParent();
}
return Found;
}
bool BPFAbstractMemberAccess::IsValidAIChain(const MDNode *ParentType,
uint32_t ParentAI,
const MDNode *ChildType) {
if (!ChildType)
return true;
const DIType *PType = stripQualifiers(cast<DIType>(ParentType));
const DIType *CType = stripQualifiers(cast<DIType>(ChildType));
if (isa<DIDerivedType>(CType))
return false;
if (const auto *PtrTy = dyn_cast<DIDerivedType>(PType)) {
if (PtrTy->getTag() != dwarf::DW_TAG_pointer_type)
return false;
return stripQualifiers(PtrTy->getBaseType()) == CType;
}
const auto *PTy = dyn_cast<DICompositeType>(PType);
const auto *CTy = dyn_cast<DICompositeType>(CType);
assert(PTy && CTy && "ParentType or ChildType is null or not composite");
uint32_t PTyTag = PTy->getTag();
assert(PTyTag == dwarf::DW_TAG_array_type ||
PTyTag == dwarf::DW_TAG_structure_type ||
PTyTag == dwarf::DW_TAG_union_type);
uint32_t CTyTag = CTy->getTag();
assert(CTyTag == dwarf::DW_TAG_array_type ||
CTyTag == dwarf::DW_TAG_structure_type ||
CTyTag == dwarf::DW_TAG_union_type);
if (PTyTag == dwarf::DW_TAG_array_type && PTyTag == CTyTag)
return PTy->getBaseType() == CTy->getBaseType();
DIType *Ty;
if (PTyTag == dwarf::DW_TAG_array_type)
Ty = PTy->getBaseType();
else
Ty = dyn_cast<DIType>(PTy->getElements()[ParentAI]);
return dyn_cast<DICompositeType>(stripQualifiers(Ty)) == CTy;
}
void BPFAbstractMemberAccess::traceAICall(CallInst *Call,
CallInfo &ParentInfo) {
for (User *U : Call->users()) {
Instruction *Inst = dyn_cast<Instruction>(U);
if (!Inst)
continue;
if (auto *BI = dyn_cast<BitCastInst>(Inst)) {
traceBitCast(BI, Call, ParentInfo);
} else if (auto *CI = dyn_cast<CallInst>(Inst)) {
CallInfo ChildInfo;
if (IsPreserveDIAccessIndexCall(CI, ChildInfo) &&
IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex,
ChildInfo.Metadata)) {
AIChain[CI] = std::make_pair(Call, ParentInfo);
traceAICall(CI, ChildInfo);
} else {
BaseAICalls[Call] = ParentInfo;
}
} else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) {
if (GI->hasAllZeroIndices())
traceGEP(GI, Call, ParentInfo);
else
BaseAICalls[Call] = ParentInfo;
} else {
BaseAICalls[Call] = ParentInfo;
}
}
}
void BPFAbstractMemberAccess::traceBitCast(BitCastInst *BitCast,
CallInst *Parent,
CallInfo &ParentInfo) {
for (User *U : BitCast->users()) {
Instruction *Inst = dyn_cast<Instruction>(U);
if (!Inst)
continue;
if (auto *BI = dyn_cast<BitCastInst>(Inst)) {
traceBitCast(BI, Parent, ParentInfo);
} else if (auto *CI = dyn_cast<CallInst>(Inst)) {
CallInfo ChildInfo;
if (IsPreserveDIAccessIndexCall(CI, ChildInfo) &&
IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex,
ChildInfo.Metadata)) {
AIChain[CI] = std::make_pair(Parent, ParentInfo);
traceAICall(CI, ChildInfo);
} else {
BaseAICalls[Parent] = ParentInfo;
}
} else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) {
if (GI->hasAllZeroIndices())
traceGEP(GI, Parent, ParentInfo);
else
BaseAICalls[Parent] = ParentInfo;
} else {
BaseAICalls[Parent] = ParentInfo;
}
}
}
void BPFAbstractMemberAccess::traceGEP(GetElementPtrInst *GEP, CallInst *Parent,
CallInfo &ParentInfo) {
for (User *U : GEP->users()) {
Instruction *Inst = dyn_cast<Instruction>(U);
if (!Inst)
continue;
if (auto *BI = dyn_cast<BitCastInst>(Inst)) {
traceBitCast(BI, Parent, ParentInfo);
} else if (auto *CI = dyn_cast<CallInst>(Inst)) {
CallInfo ChildInfo;
if (IsPreserveDIAccessIndexCall(CI, ChildInfo) &&
IsValidAIChain(ParentInfo.Metadata, ParentInfo.AccessIndex,
ChildInfo.Metadata)) {
AIChain[CI] = std::make_pair(Parent, ParentInfo);
traceAICall(CI, ChildInfo);
} else {
BaseAICalls[Parent] = ParentInfo;
}
} else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) {
if (GI->hasAllZeroIndices())
traceGEP(GI, Parent, ParentInfo);
else
BaseAICalls[Parent] = ParentInfo;
} else {
BaseAICalls[Parent] = ParentInfo;
}
}
}
void BPFAbstractMemberAccess::collectAICallChains(Function &F) {
AIChain.clear();
BaseAICalls.clear();
for (auto &BB : F)
for (auto &I : BB) {
CallInfo CInfo;
auto *Call = dyn_cast<CallInst>(&I);
if (!IsPreserveDIAccessIndexCall(Call, CInfo) ||
AIChain.find(Call) != AIChain.end())
continue;
traceAICall(Call, CInfo);
}
}
uint64_t BPFAbstractMemberAccess::getConstant(const Value *IndexValue) {
const ConstantInt *CV = dyn_cast<ConstantInt>(IndexValue);
assert(CV);
return CV->getValue().getZExtValue();
}
void BPFAbstractMemberAccess::GetStorageBitRange(DIDerivedType *MemberTy,
Align RecordAlignment,
uint32_t &StartBitOffset,
uint32_t &EndBitOffset) {
uint32_t MemberBitSize = MemberTy->getSizeInBits();
uint32_t MemberBitOffset = MemberTy->getOffsetInBits();
if (RecordAlignment > 8) {
if (MemberBitOffset / 64 != (MemberBitOffset + MemberBitSize) / 64)
report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info, "
"requiring too big alignment");
RecordAlignment = Align(8);
}
uint32_t AlignBits = RecordAlignment.value() * 8;
if (MemberBitSize > AlignBits)
report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info, "
"bitfield size greater than record alignment");
StartBitOffset = MemberBitOffset & ~(AlignBits - 1);
if ((StartBitOffset + AlignBits) < (MemberBitOffset + MemberBitSize))
report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info, "
"cross alignment boundary");
EndBitOffset = StartBitOffset + AlignBits;
}
uint32_t BPFAbstractMemberAccess::GetFieldInfo(uint32_t InfoKind,
DICompositeType *CTy,
uint32_t AccessIndex,
uint32_t PatchImm,
MaybeAlign RecordAlignment) {
if (InfoKind == BPFCoreSharedInfo::FIELD_EXISTENCE)
return 1;
uint32_t Tag = CTy->getTag();
if (InfoKind == BPFCoreSharedInfo::FIELD_BYTE_OFFSET) {
if (Tag == dwarf::DW_TAG_array_type) {
auto *EltTy = stripQualifiers(CTy->getBaseType());
PatchImm += AccessIndex * calcArraySize(CTy, 1) *
(EltTy->getSizeInBits() >> 3);
} else if (Tag == dwarf::DW_TAG_structure_type) {
auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
if (!MemberTy->isBitField()) {
PatchImm += MemberTy->getOffsetInBits() >> 3;
} else {
unsigned SBitOffset, NextSBitOffset;
GetStorageBitRange(MemberTy, *RecordAlignment, SBitOffset,
NextSBitOffset);
PatchImm += SBitOffset >> 3;
}
}
return PatchImm;
}
if (InfoKind == BPFCoreSharedInfo::FIELD_BYTE_SIZE) {
if (Tag == dwarf::DW_TAG_array_type) {
auto *EltTy = stripQualifiers(CTy->getBaseType());
return calcArraySize(CTy, 1) * (EltTy->getSizeInBits() >> 3);
} else {
auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
uint32_t SizeInBits = MemberTy->getSizeInBits();
if (!MemberTy->isBitField())
return SizeInBits >> 3;
unsigned SBitOffset, NextSBitOffset;
GetStorageBitRange(MemberTy, *RecordAlignment, SBitOffset,
NextSBitOffset);
SizeInBits = NextSBitOffset - SBitOffset;
if (SizeInBits & (SizeInBits - 1))
report_fatal_error("Unsupported field expression for llvm.bpf.preserve.field.info");
return SizeInBits >> 3;
}
}
if (InfoKind == BPFCoreSharedInfo::FIELD_SIGNEDNESS) {
const DIType *BaseTy;
if (Tag == dwarf::DW_TAG_array_type) {
if (CTy->getElements().size() != 1)
report_fatal_error("Invalid array expression for llvm.bpf.preserve.field.info");
BaseTy = stripQualifiers(CTy->getBaseType());
} else {
auto *MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
BaseTy = stripQualifiers(MemberTy->getBaseType());
}
const auto *BTy = dyn_cast<DIBasicType>(BaseTy);
while (!BTy) {
const auto *CompTy = dyn_cast<DICompositeType>(BaseTy);
if (!CompTy || CompTy->getTag() != dwarf::DW_TAG_enumeration_type)
report_fatal_error("Invalid field expression for llvm.bpf.preserve.field.info");
BaseTy = stripQualifiers(CompTy->getBaseType());
BTy = dyn_cast<DIBasicType>(BaseTy);
}
uint32_t Encoding = BTy->getEncoding();
return (Encoding == dwarf::DW_ATE_signed || Encoding == dwarf::DW_ATE_signed_char);
}
if (InfoKind == BPFCoreSharedInfo::FIELD_LSHIFT_U64) {
const Triple &Triple = TM->getTargetTriple();
DIDerivedType *MemberTy = nullptr;
bool IsBitField = false;
uint32_t SizeInBits;
if (Tag == dwarf::DW_TAG_array_type) {
auto *EltTy = stripQualifiers(CTy->getBaseType());
SizeInBits = calcArraySize(CTy, 1) * EltTy->getSizeInBits();
} else {
MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
SizeInBits = MemberTy->getSizeInBits();
IsBitField = MemberTy->isBitField();
}
if (!IsBitField) {
if (SizeInBits > 64)
report_fatal_error("too big field size for llvm.bpf.preserve.field.info");
return 64 - SizeInBits;
}
unsigned SBitOffset, NextSBitOffset;
GetStorageBitRange(MemberTy, *RecordAlignment, SBitOffset, NextSBitOffset);
if (NextSBitOffset - SBitOffset > 64)
report_fatal_error("too big field size for llvm.bpf.preserve.field.info");
unsigned OffsetInBits = MemberTy->getOffsetInBits();
if (Triple.getArch() == Triple::bpfel)
return SBitOffset + 64 - OffsetInBits - SizeInBits;
else
return OffsetInBits + 64 - NextSBitOffset;
}
if (InfoKind == BPFCoreSharedInfo::FIELD_RSHIFT_U64) {
DIDerivedType *MemberTy = nullptr;
bool IsBitField = false;
uint32_t SizeInBits;
if (Tag == dwarf::DW_TAG_array_type) {
auto *EltTy = stripQualifiers(CTy->getBaseType());
SizeInBits = calcArraySize(CTy, 1) * EltTy->getSizeInBits();
} else {
MemberTy = cast<DIDerivedType>(CTy->getElements()[AccessIndex]);
SizeInBits = MemberTy->getSizeInBits();
IsBitField = MemberTy->isBitField();
}
if (!IsBitField) {
if (SizeInBits > 64)
report_fatal_error("too big field size for llvm.bpf.preserve.field.info");
return 64 - SizeInBits;
}
unsigned SBitOffset, NextSBitOffset;
GetStorageBitRange(MemberTy, *RecordAlignment, SBitOffset, NextSBitOffset);
if (NextSBitOffset - SBitOffset > 64)
report_fatal_error("too big field size for llvm.bpf.preserve.field.info");
return 64 - SizeInBits;
}
llvm_unreachable("Unknown llvm.bpf.preserve.field.info info kind");
}
bool BPFAbstractMemberAccess::HasPreserveFieldInfoCall(CallInfoStack &CallStack) {
while (CallStack.size()) {
auto StackElem = CallStack.top();
if (StackElem.second.Kind == BPFPreserveFieldInfoAI)
return true;
CallStack.pop();
}
return false;
}
Value *BPFAbstractMemberAccess::computeBaseAndAccessKey(CallInst *Call,
CallInfo &CInfo,
std::string &AccessKey,
MDNode *&TypeMeta) {
Value *Base = nullptr;
std::string TypeName;
CallInfoStack CallStack;
while (Call) {
CallStack.push(std::make_pair(Call, CInfo));
CInfo = AIChain[Call].second;
Call = AIChain[Call].first;
}
uint32_t FirstIndex = 0;
uint32_t PatchImm = 0; uint32_t InfoKind = BPFCoreSharedInfo::FIELD_BYTE_OFFSET;
while (CallStack.size()) {
auto StackElem = CallStack.top();
Call = StackElem.first;
CInfo = StackElem.second;
if (!Base)
Base = CInfo.Base;
DIType *PossibleTypeDef = stripQualifiers(cast<DIType>(CInfo.Metadata),
false);
DIType *Ty = stripQualifiers(PossibleTypeDef);
if (CInfo.Kind == BPFPreserveUnionAI ||
CInfo.Kind == BPFPreserveStructAI) {
TypeName = std::string(PossibleTypeDef->getName());
TypeMeta = PossibleTypeDef;
PatchImm += FirstIndex * (Ty->getSizeInBits() >> 3);
break;
}
assert(CInfo.Kind == BPFPreserveArrayAI);
CallStack.pop();
uint64_t AccessIndex = CInfo.AccessIndex;
DIType *BaseTy = nullptr;
bool CheckElemType = false;
if (const auto *CTy = dyn_cast<DICompositeType>(Ty)) {
assert(CTy->getTag() == dwarf::DW_TAG_array_type);
FirstIndex += AccessIndex * calcArraySize(CTy, 1);
BaseTy = stripQualifiers(CTy->getBaseType());
CheckElemType = CTy->getElements().size() == 1;
} else {
auto *DTy = cast<DIDerivedType>(Ty);
assert(DTy->getTag() == dwarf::DW_TAG_pointer_type);
BaseTy = stripQualifiers(DTy->getBaseType());
CTy = dyn_cast<DICompositeType>(BaseTy);
if (!CTy) {
CheckElemType = true;
} else if (CTy->getTag() != dwarf::DW_TAG_array_type) {
FirstIndex += AccessIndex;
CheckElemType = true;
} else {
FirstIndex += AccessIndex * calcArraySize(CTy, 0);
}
}
if (CheckElemType) {
auto *CTy = dyn_cast<DICompositeType>(BaseTy);
if (!CTy) {
if (HasPreserveFieldInfoCall(CallStack))
report_fatal_error("Invalid field access for llvm.preserve.field.info intrinsic");
return nullptr;
}
unsigned CTag = CTy->getTag();
if (CTag == dwarf::DW_TAG_structure_type || CTag == dwarf::DW_TAG_union_type) {
TypeName = std::string(CTy->getName());
} else {
if (HasPreserveFieldInfoCall(CallStack))
report_fatal_error("Invalid field access for llvm.preserve.field.info intrinsic");
return nullptr;
}
TypeMeta = CTy;
PatchImm += FirstIndex * (CTy->getSizeInBits() >> 3);
break;
}
}
assert(TypeName.size());
AccessKey += std::to_string(FirstIndex);
while (CallStack.size()) {
auto StackElem = CallStack.top();
CInfo = StackElem.second;
CallStack.pop();
if (CInfo.Kind == BPFPreserveFieldInfoAI) {
InfoKind = CInfo.AccessIndex;
if (InfoKind == BPFCoreSharedInfo::FIELD_EXISTENCE)
PatchImm = 1;
break;
}
if (CallStack.size()) {
auto StackElem2 = CallStack.top();
CallInfo CInfo2 = StackElem2.second;
if (CInfo2.Kind == BPFPreserveFieldInfoAI) {
InfoKind = CInfo2.AccessIndex;
assert(CallStack.size() == 1);
}
}
uint64_t AccessIndex = CInfo.AccessIndex;
AccessKey += ":" + std::to_string(AccessIndex);
MDNode *MDN = CInfo.Metadata;
auto *CTy = cast<DICompositeType>(stripQualifiers(cast<DIType>(MDN)));
PatchImm = GetFieldInfo(InfoKind, CTy, AccessIndex, PatchImm,
CInfo.RecordAlignment);
}
AccessKey = "llvm." + TypeName + ":" + std::to_string(InfoKind) + ":" +
std::to_string(PatchImm) + "$" + AccessKey;
return Base;
}
MDNode *BPFAbstractMemberAccess::computeAccessKey(CallInst *Call,
CallInfo &CInfo,
std::string &AccessKey,
bool &IsInt32Ret) {
DIType *Ty = stripQualifiers(cast<DIType>(CInfo.Metadata), false);
assert(!Ty->getName().empty());
int64_t PatchImm;
std::string AccessStr("0");
if (CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_EXISTENCE ||
CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_MATCH) {
PatchImm = 1;
} else if (CInfo.AccessIndex == BPFCoreSharedInfo::TYPE_SIZE) {
DIType *BaseTy = stripQualifiers(Ty, true);
PatchImm = BaseTy->getSizeInBits() / 8;
} else {
IsInt32Ret = false;
const GlobalVariable *GV =
cast<GlobalVariable>(Call->getArgOperand(1)->stripPointerCasts());
assert(GV->hasInitializer());
const ConstantDataArray *DA = cast<ConstantDataArray>(GV->getInitializer());
assert(DA->isString());
StringRef ValueStr = DA->getAsString();
size_t Separator = ValueStr.find_first_of(':');
StringRef EnumeratorStr = ValueStr.substr(0, Separator);
DIType *BaseTy = stripQualifiers(Ty, true);
const auto *CTy = cast<DICompositeType>(BaseTy);
assert(CTy->getTag() == dwarf::DW_TAG_enumeration_type);
int EnumIndex = 0;
for (const auto Element : CTy->getElements()) {
const auto *Enum = cast<DIEnumerator>(Element);
if (Enum->getName() == EnumeratorStr) {
AccessStr = std::to_string(EnumIndex);
break;
}
EnumIndex++;
}
if (CInfo.AccessIndex == BPFCoreSharedInfo::ENUM_VALUE) {
StringRef EValueStr = ValueStr.substr(Separator + 1);
PatchImm = std::stoll(std::string(EValueStr));
} else {
PatchImm = 1;
}
}
AccessKey = "llvm." + Ty->getName().str() + ":" +
std::to_string(CInfo.AccessIndex) + std::string(":") +
std::to_string(PatchImm) + std::string("$") + AccessStr;
return Ty;
}
bool BPFAbstractMemberAccess::transformGEPChain(CallInst *Call,
CallInfo &CInfo) {
std::string AccessKey;
MDNode *TypeMeta;
Value *Base = nullptr;
bool IsInt32Ret;
IsInt32Ret = CInfo.Kind == BPFPreserveFieldInfoAI;
if (CInfo.Kind == BPFPreserveFieldInfoAI && CInfo.Metadata) {
TypeMeta = computeAccessKey(Call, CInfo, AccessKey, IsInt32Ret);
} else {
Base = computeBaseAndAccessKey(Call, CInfo, AccessKey, TypeMeta);
if (!Base)
return false;
}
BasicBlock *BB = Call->getParent();
GlobalVariable *GV;
if (GEPGlobals.find(AccessKey) == GEPGlobals.end()) {
IntegerType *VarType;
if (IsInt32Ret)
VarType = Type::getInt32Ty(BB->getContext()); else
VarType = Type::getInt64Ty(BB->getContext());
GV = new GlobalVariable(*M, VarType, false, GlobalVariable::ExternalLinkage,
nullptr, AccessKey);
GV->addAttribute(BPFCoreSharedInfo::AmaAttr);
GV->setMetadata(LLVMContext::MD_preserve_access_index, TypeMeta);
GEPGlobals[AccessKey] = GV;
} else {
GV = GEPGlobals[AccessKey];
}
if (CInfo.Kind == BPFPreserveFieldInfoAI) {
LoadInst *LDInst;
if (IsInt32Ret)
LDInst = new LoadInst(Type::getInt32Ty(BB->getContext()), GV, "", Call);
else
LDInst = new LoadInst(Type::getInt64Ty(BB->getContext()), GV, "", Call);
Instruction *PassThroughInst =
BPFCoreSharedInfo::insertPassThrough(M, BB, LDInst, Call);
Call->replaceAllUsesWith(PassThroughInst);
Call->eraseFromParent();
return true;
}
auto *LDInst = new LoadInst(Type::getInt64Ty(BB->getContext()), GV, "", Call);
auto *BCInst = new BitCastInst(Base, Type::getInt8PtrTy(BB->getContext()));
BB->getInstList().insert(Call->getIterator(), BCInst);
auto *GEP = GetElementPtrInst::Create(Type::getInt8Ty(BB->getContext()),
BCInst, LDInst);
BB->getInstList().insert(Call->getIterator(), GEP);
auto *BCInst2 = new BitCastInst(GEP, Call->getType());
BB->getInstList().insert(Call->getIterator(), BCInst2);
Instruction *PassThroughInst =
BPFCoreSharedInfo::insertPassThrough(M, BB, BCInst2, Call);
Call->replaceAllUsesWith(PassThroughInst);
Call->eraseFromParent();
return true;
}
bool BPFAbstractMemberAccess::doTransformation(Function &F) {
bool Transformed = false;
collectAICallChains(F);
for (auto &C : BaseAICalls)
Transformed = transformGEPChain(C.first, C.second) || Transformed;
return removePreserveAccessIndexIntrinsic(F) || Transformed;
}
PreservedAnalyses
BPFAbstractMemberAccessPass::run(Function &F, FunctionAnalysisManager &AM) {
return BPFAbstractMemberAccess(TM).run(F) ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}