Skip to content

[clang][bytecode] Handle more invalid member pointer casts #152546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1646,8 +1646,17 @@ bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,

const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl());
const auto *InitialFunction = cast<CXXMethodDecl>(Callee);
const CXXMethodDecl *Overrider = S.getContext().getOverridingFunction(
DynamicDecl, StaticDecl, InitialFunction);
const CXXMethodDecl *Overrider;

if (StaticDecl != DynamicDecl) {
if (!DynamicDecl->isDerivedFrom(StaticDecl))
return false;
Overrider = S.getContext().getOverridingFunction(DynamicDecl, StaticDecl,
InitialFunction);

} else {
Overrider = InitialFunction;
}

if (Overrider != InitialFunction) {
// DR1872: An instantiated virtual constexpr function can't be called in a
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3201,6 +3201,9 @@ inline bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D) {
inline bool GetMemberPtrBase(InterpState &S, CodePtr OpPC) {
const auto &MP = S.Stk.pop<MemberPointer>();

if (!MP.isBaseCastPossible())
return false;

S.Stk.push<Pointer>(MP.getBase());
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/AST/ByteCode/MemberPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class MemberPointer final {

FunctionPointer toFunctionPointer(const Context &Ctx) const;

bool isBaseCastPossible() const {
if (PtrOffset < 0)
return true;
return static_cast<uint64_t>(PtrOffset) <= Base.getByteOffset();
}

Pointer getBase() const {
if (PtrOffset < 0)
return Base.atField(-PtrOffset);
Expand Down
30 changes: 30 additions & 0 deletions clang/test/AST/ByteCode/cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,33 @@ namespace ReadMutableInCopyCtor {
// both-note {{read of mutable member 'u'}} \
// both-note {{in call to 'G(g1)'}}
}

namespace GH150709 {
struct C { };
struct D : C {
constexpr int f() const { return 1; };
};
struct E : C { };
struct F : D { };
struct G : E { };

constexpr C c1, c2[2];
constexpr D d1, d2[2];
constexpr E e1, e2[2];
constexpr F f;
constexpr G g;

constexpr auto mp = static_cast<int (C::*)() const>(&D::f);

// sanity checks for fix of GH150709 (unchanged behavior)
static_assert((c1.*mp)() == 1, ""); // both-error {{constant expression}}
static_assert((d1.*mp)() == 1, "");
static_assert((f.*mp)() == 1, "");
static_assert((c2[0].*mp)() == 1, ""); // ref-error {{constant expression}}
static_assert((d2[0].*mp)() == 1, "");

// incorrectly undiagnosed before fix of GH150709
static_assert((e1.*mp)() == 1, ""); // ref-error {{constant expression}}
static_assert((e2[0].*mp)() == 1, ""); // ref-error {{constant expression}}
static_assert((g.*mp)() == 1, ""); // ref-error {{constant expression}}
}
14 changes: 14 additions & 0 deletions clang/test/AST/ByteCode/cxx2a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,17 @@ namespace Dtor {
static_assert(pseudo(true, false)); // both-error {{constant expression}} both-note {{in call}}
static_assert(pseudo(false, true));
}

namespace GH150705 {
struct A { };
struct B : A { };
struct C : A {
constexpr virtual int foo() const { return 0; }
};

constexpr auto p = &C::foo;
constexpr auto q = static_cast<int (A::*)() const>(p);
constexpr B b;
constexpr const A& a = b;
constexpr auto x = (a.*q)(); // both-error {{constant expression}}
}