Skip to content

[clang][bytecode] Call CheckLocalLoad in GetLocal #152090

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 5, 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
2 changes: 2 additions & 0 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {

// Similarly, for local loads.
bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (!CheckLifetime(S, OpPC, Ptr, AK_Read))
return false;
if (!CheckInitialized(S, OpPC, Ptr, AK_Read))
return false;
if (!CheckVolatile(S, OpPC, Ptr, AK_Read))
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ inline bool ConstFloat(InterpState &S, CodePtr OpPC, const Floating &F) {
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
const Pointer &Ptr = S.Current->getLocalPointer(I);
if (!CheckLoad(S, OpPC, Ptr))
if (!CheckLocalLoad(S, OpPC, Ptr))
return false;
S.Stk.push<T>(Ptr.deref<T>());
return true;
Expand Down
38 changes: 38 additions & 0 deletions clang/test/AST/ByteCode/cxx2a.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
// RUN: %clang_cc1 -std=c++2a -fsyntax-only -fcxx-exceptions -verify=ref,both %s
// RUN: %clang_cc1 -std=c++2a -fsyntax-only -fcxx-exceptions -verify=expected,both %s -fexperimental-new-constant-interpreter


namespace std {
struct type_info;
struct destroying_delete_t {
explicit destroying_delete_t() = default;
} inline constexpr destroying_delete{};
struct nothrow_t {
explicit nothrow_t() = default;
} inline constexpr nothrow{};
using size_t = decltype(sizeof(0));
enum class align_val_t : size_t {};
};

constexpr void *operator new(std::size_t, void *p) { return p; }
namespace std {
template<typename T> constexpr T *construct(T *p) { return new (p) T; }
template<typename T> constexpr void destroy(T *p) { p->~T(); }
}

template <unsigned N>
struct S {
S() requires (N==1) = default;
Expand Down Expand Up @@ -187,3 +206,22 @@ namespace PureVirtual {
struct PureVirtualCall : Abstract { void f(); }; // both-note {{in call to 'Abstract}}
constexpr PureVirtualCall pure_virtual_call; // both-error {{constant expression}} both-note {{in call to 'PureVirtualCall}}
}

namespace Dtor {
constexpr bool pseudo(bool read, bool recreate) {
using T = bool;
bool b = false; // both-note {{lifetime has already ended}}
// This evaluates the store to 'b'...
(b = true).~T();
// ... and ends the lifetime of the object.
return (read
? b // both-note {{read of object outside its lifetime}}
: true) +
(recreate
? (std::construct(&b), true)
: true);
}
static_assert(pseudo(false, false)); // both-error {{constant expression}} both-note {{in call}}
static_assert(pseudo(true, false)); // both-error {{constant expression}} both-note {{in call}}
static_assert(pseudo(false, true));
}