https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/241345
Block pointers are the only writable pointer type, which is why we're checking for them very early in `CheckStore`. Make the distinction more explicit and use the block and the `PtrView` of the pointer explicitly. >From 23de51167617e7f7d024e270311f5c6fa2892c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Wed, 11 Nov 2026 15:34:07 +0100 Subject: [PATCH] checkstore --- clang/lib/AST/ByteCode/Interp.cpp | 45 +++++++++++++++++++------------ clang/lib/AST/ByteCode/Pointer.h | 8 +++--- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp index 2f4e8ee0fe59b4..0377dbe6c130fc 100644 --- a/clang/lib/AST/ByteCode/Interp.cpp +++ b/clang/lib/AST/ByteCode/Interp.cpp @@ -199,9 +199,9 @@ static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC, S.Note(VD->getLocation(), diag::note_declared_at); } -static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { - if (auto ID = Ptr.getDeclID()) { - if (!Ptr.isStatic()) +static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Block *B) { + if (auto ID = B->getDeclID()) { + if (!B->isGlobal()) return true; if (S.P.getCurrentDecl() == ID) @@ -625,7 +625,7 @@ bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr, return false; } -bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { +static bool CheckConst(InterpState &S, CodePtr OpPC, PtrView Ptr) { assert(Ptr.isLive() && "Pointer is not live"); if (!Ptr.isConst()) return true; @@ -633,9 +633,6 @@ bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { if (Ptr.isMutable() && !Ptr.isConstInMutable()) return true; - if (!Ptr.isBlockPointer()) - return false; - // The This pointer is writable in constructors and destructors, // even if isConst() returns true. for (PtrView V : llvm::reverse(S.InitializingPtrs)) { @@ -657,7 +654,7 @@ bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { // that is marked const but NOT in InitializingPtrs. If that is the case, // Ptr is currently not writable. bool FoundProblem = false; - for (PtrView P = Ptr.view(); P != V; P = P.getBase()) { + for (PtrView P = Ptr; P != V; P = P.getBase()) { if (P.getFieldDesc()->IsConst) { FoundProblem = true; break; @@ -682,6 +679,17 @@ bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { return false; } +bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { + assert(Ptr.isLive() && "Pointer is not live"); + + if (!Ptr.isConst()) + return true; + + if (!Ptr.isBlockPointer()) + return false; + return CheckConst(S, OpPC, Ptr.view()); +} + bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr, AccessKinds AK) { assert(Ptr.isLive() && "Pointer is not live"); if (!Ptr.isMutable()) @@ -1082,26 +1090,29 @@ bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr, if (!Ptr.isBlockPointer()) return false; - if (!Ptr.block()->isAccessible()) { + PtrView P = Ptr.view(); + const Block *B = Ptr.block(); + + if (!B->isAccessible()) { if (!CheckLive(S, OpPC, Ptr, AK)) return false; if (!CheckExtern(S, OpPC, Ptr)) return false; return false; } - if (!WillBeActivated && !CheckLifetime(S, OpPC, Ptr, AK)) + if (!WillBeActivated && !CheckLifetime(S, OpPC, P.getLifetime(), B, AK)) return false; if (!CheckRange(S, OpPC, Ptr, AK)) return false; - if (!CheckActive(S, OpPC, Ptr, AK, WillBeActivated)) + if (!CheckActive(S, OpPC, P, AK, WillBeActivated)) return false; - if (!CheckGlobal(S, OpPC, Ptr)) + if (!CheckGlobal(S, OpPC, B)) return false; - if (!CheckConst(S, OpPC, Ptr)) + if (!CheckConst(S, OpPC, P)) return false; - if (!CheckVolatile(S, OpPC, Ptr.view(), AK)) + if (!CheckVolatile(S, OpPC, P, AK)) return false; - if (!CheckMutable(S, OpPC, Ptr.view(), AK)) + if (!CheckMutable(S, OpPC, P, AK)) return false; return true; } @@ -2870,9 +2881,9 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E, if (!CheckRange(S, OpPC, Ptr, AK_Construct)) return false; - if (!CheckGlobal(S, OpPC, Ptr)) + if (!CheckGlobal(S, OpPC, Ptr.block())) return false; - if (!CheckConst(S, OpPC, Ptr)) + if (!CheckConst(S, OpPC, Ptr.view())) return false; const auto *NewExpr = cast<CXXNewExpr>(E); diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h index d9123a7658d105..8e747c6effb5fc 100644 --- a/clang/lib/AST/ByteCode/Pointer.h +++ b/clang/lib/AST/ByteCode/Pointer.h @@ -49,6 +49,9 @@ struct PtrView { bool isMutable() const { return !isRoot() && getInlineDesc()->IsFieldMutable; } + bool isConstInMutable() const { + return !isRoot() && getInlineDesc()->IsConstInMutable; + } bool isExtern() const { return Pointee && Pointee->isExtern(); } bool isVolatile() const { return isRoot() ? getDeclDesc()->IsVolatile : getInlineDesc()->IsVolatile; @@ -971,11 +974,6 @@ class Pointer { return false; return view().isConst(); } - bool isConstInMutable() const { - if (!isBlockPointer()) - return false; - return isRoot() ? false : getInlineDesc()->IsConstInMutable; - } /// Checks if an object or a subfield is volatile. bool isVolatile() const { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
