Author: Timm Baeder Date: 2026-11-18T09:27:59+01:00 New Revision: a2d33dd8bee6f5501c3b961a210961b6f32faa28
URL: https://github.com/llvm/llvm-project/commit/a2d33dd8bee6f5501c3b961a210961b6f32faa28 DIFF: https://github.com/llvm/llvm-project/commit/a2d33dd8bee6f5501c3b961a210961b6f32faa28.diff LOG: [clang][bytecode] Reject enumerators with BPFPreserveAccessIndexAttr (#243449) See https://github.com/llvm/llvm-project/commit/c080a0b9b673ecce94e06c19213487cb233d24e1 Added: Modified: clang/lib/AST/ByteCode/Compiler.cpp clang/lib/AST/ByteCode/Interp.h clang/lib/AST/ByteCode/Opcodes.td clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c Removed: ################################################################################ diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 5044656e3afff..ad786154db8cb 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -4725,6 +4725,35 @@ bool Compiler<Emitter>::VisitCXXRewrittenBinaryOperator( template <class Emitter> bool Compiler<Emitter>::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { + // Diagnose a relocated BPF enumerator, i.e., of an enum with the + // preserve_access_index attribute, which is not a constant expression. Return + // true if diagnosed. + auto getAccessIndexConstantDecl = [&]() -> const EnumConstantDecl * { + const auto *DRE = dyn_cast<DeclRefExpr>(E->getSyntacticForm()); + if (!DRE) + return nullptr; + const auto *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl()); + if (!ECD) + return nullptr; + const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()); + if (ED && ED->hasAttr<BPFPreserveAccessIndexAttr>()) + return ECD; + return nullptr; + }; + + if (const EnumConstantDecl *ECD = getAccessIndexConstantDecl()) { + const auto *ED = cast<EnumDecl>(ECD->getDeclContext()); + // An anonymous enum is only relocated through its typedef name. + QualType EnumTy; + if (ED->getDeclName()) + EnumTy = Ctx.getASTContext().getCanonicalTagType(ED); + else if (const TypedefNameDecl *TD = ED->getTypedefNameForAnonDecl()) + EnumTy = Ctx.getASTContext().getTypeDeclType(TD); + else + return false; + + return this->emitInvalidAccessIndexAttr(ECD, EnumTy.getTypePtr(), E); + } for (const Expr *SemE : E->semantics()) { if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) { diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index 7509ced04de41..f43c6c9227200 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -3794,6 +3794,14 @@ inline bool InvalidStore(InterpState &S, CodePtr OpPC, const Type *T) { return false; } +inline bool InvalidAccessIndexAttr(InterpState &S, CodePtr OpPC, + const ValueDecl *ECD, const Type *EnumTy) { + const Expr *E = S.Current->getExpr(OpPC); + S.FFDiag(E, diag::note_constexpr_bpf_preserve_ai_enumerator) + << ECD << QualType(EnumTy, 0) << E->getSourceRange(); + return false; +} + inline bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC) { if (S.inConstantContext()) { const SourceRange &ArgRange = S.Current->getRange(OpPC); diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td index 23aa5abeb0a88..18c265a5dee5c 100644 --- a/clang/lib/AST/ByteCode/Opcodes.td +++ b/clang/lib/AST/ByteCode/Opcodes.td @@ -1078,3 +1078,7 @@ def PopMSVCCE : SuccessOpcode; def GetStringPtr : SuccessOpcode { let Args = [ArgExpr]; } + +def InvalidAccessIndexAttr : Opcode { + let Args = [ArgValueDecl, ArgTypePtr]; +} diff --git a/clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c b/clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c index 26cdc0fc93902..a474160b7ffc3 100644 --- a/clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c +++ b/clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -triple bpf -debug-info-kind=limited -fsyntax-only -verify=err %s +// RUN: %clang_cc1 -triple bpf -debug-info-kind=limited -fsyntax-only -verify=err %s -fexperimental-new-constant-interpreter // RUN: %clang_cc1 -triple bpf -debug-info-kind=limited -fsyntax-only -verify=warn \ // RUN: -Wno-error=bpf-enum-not-relocated %s // RUN: %clang_cc1 -triple bpf -debug-info-kind=limited -fsyntax-only -verify=none \ _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
