================
@@ -1747,6 +1747,99 @@ void CodeGenFunction::EmitContinueStmt(const
ContinueStmt &S) {
EmitBranchThroughCleanup(GetDestForLoopControlStmt(S)->ContinueBlock);
}
+/// Return whether S has an enumerator of a BPF preserve_access_index enum,
+/// which Sema keeps as a pseudo-object for __builtin_preserve_enum_value().
+static bool hasBPFEnumeratorRef(const Stmt *S) {
+ if (!S)
+ return false;
+ if (const auto *POE = dyn_cast<PseudoObjectExpr>(S))
+ if (const auto *DRE = dyn_cast<DeclRefExpr>(POE->getSyntacticForm());
+ DRE && isa<EnumConstantDecl>(DRE->getDecl()))
+ return true;
+ return llvm::any_of(S->children(), hasBPFEnumeratorRef);
+}
+
+/// If the case value E has a relocated enumerator, see
+/// SemaBPF::verifyCaseExpr(), return E without its ConstantExpr and casts.
+/// Sema does not check it for duplicates, so it is not added to the switch
even
+/// without debug info, where its local value is used.
+static const Expr *getBPFRelocatedCaseValue(CodeGenFunction &CGF,
+ const Expr *E) {
+ if (!E || !CGF.getTarget().getTriple().isBPF())
+ return nullptr;
+ while (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
+ E = ICE->getSubExpr();
+ const auto *CE = dyn_cast<ConstantExpr>(E);
+ if (!CE || !hasBPFEnumeratorRef(CE->getSubExpr()))
+ return nullptr;
+ return CE->getSubExpr();
+}
+
+static bool isBPFRelocatedCase(CodeGenFunction &CGF, const CaseStmt &S) {
+ return getBPFRelocatedCaseValue(CGF, S.getLHS()) ||
+ getBPFRelocatedCaseValue(CGF, S.getRHS());
+}
+
+/// Emit a case statement whose value is relocated: move the switch to a new
+/// block, and in its old block, branch to the case if the condition matches
the
+/// relocated value, otherwise to the switch. So the relocated cases are
checked
+/// in source order, before the other cases. E.g., with relocated A, B and C,
+///
+/// switch (x) { case 1: ... case A: ... case B ... C: ... }
+///
+/// is emitted like:
+///
+/// if (x == A) goto case_A;
+/// if (x >= B && x <= C) goto case_BC;
+/// switch (x) { case 1: ... }
+void CodeGenFunction::EmitBPFRelocatedCaseStmt(const CaseStmt &S,
+ ArrayRef<const Attr *> Attrs) {
+ llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
+ EmitBlockWithFallThrough(CaseDest, &S);
+
+ llvm::BasicBlock *CheckBB = SwitchInsn->getParent();
+ llvm::BasicBlock *SwitchBB = createBasicBlock("sw.dispatch");
+ CurFn->insert(std::next(CheckBB->getIterator()), SwitchBB);
+ SwitchInsn->removeFromParent();
+ SwitchInsn->insertInto(SwitchBB, SwitchBB->end());
+
+ // Emit the check at the end of CheckBB, then come back to the case body.
+ {
+ llvm::IRBuilderBase::InsertPointGuard IPG(Builder);
+ Builder.SetInsertPoint(CheckBB);
+ ApplyDebugLocation DL(*this, S.getLHS());
+ llvm::Value *CondV = SwitchInsn->getCondition();
+ auto EmitCaseValue = [&](const Expr *E) -> llvm::Value * {
+ if (const Expr *Reloc = getBPFRelocatedCaseValue(*this, E))
+ return Builder.CreateIntCast(
+ EmitScalarExpr(Reloc), CondV->getType(),
+ Reloc->getType()->isSignedIntegerOrEnumerationType());
+ return Builder.getInt(E->EvaluateKnownConstInt(getContext()));
+ };
+
+ llvm::Value *Cond;
+ if (!S.getRHS()) {
+ Cond = Builder.CreateICmpEQ(CondV, EmitCaseValue(S.getLHS()));
----------------
4ast wrote:
Something to note as a tradeoff....
Since this fails the load on a kernel that doesn't have one of the enumerators.
libbpf poisons the `ld_imm64` of an unresolved `ENUM_VALUE` relocation. The
chain runs before the switch, so the verifier reaches that insn.
Take `B` that a later kernel adds to the enum:
```
source kernel without B
----------------------------- -----------------------
case B: without the attribute loads
if (exists(B) && x == B) loads, dead code
case B: with this patch poisoned insn, rejected
```
A case label can't have such a guard, since the explicit builtin is an error
there. The only opt-out is `-fbpf-no-enum-reloc=` for the whole enum.
Add to a doc somewhere?
https://github.com/llvm/llvm-project/pull/243338
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits