Skip to content

[clang][bytecode] Check if operator delete calls are in the right frame #136141

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
Apr 17, 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
35 changes: 35 additions & 0 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,41 @@ static bool interp__builtin_operator_delete(InterpState &S, CodePtr OpPC,
const Expr *Source = nullptr;
const Block *BlockToDelete = nullptr;

if (S.checkingPotentialConstantExpression())
return false;

// This is permitted only within a call to std::allocator<T>::deallocate.
bool DeallocateFrameFound = false;
for (const InterpFrame *F = Frame; F; F = F->Caller) {
const Function *Func = F->getFunction();
if (!Func)
continue;
const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Func->getDecl());
if (!MD)
continue;
const IdentifierInfo *FnII = MD->getIdentifier();
if (!FnII || !FnII->isStr("deallocate"))
continue;

const auto *CTSD =
dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());
if (!CTSD)
continue;

const IdentifierInfo *ClassII = CTSD->getIdentifier();
const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr("allocator") &&
TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {
DeallocateFrameFound = true;
break;
}
}

if (!DeallocateFrameFound) {
S.FFDiag(Call);
return true;
}

{
const Pointer &Ptr = S.Stk.peek<Pointer>();

Expand Down
12 changes: 12 additions & 0 deletions clang/test/AST/ByteCode/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,18 @@ namespace ZeroSizeSub {
// both-note {{in call to}}
}

namespace WrongFrame {
constexpr int foo() {
int *p = nullptr;
__builtin_operator_delete(p); // both-note {{subexpression not valid in a constant expression}}

return 1;
}
static_assert(foo()); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}

}

#else
/// Make sure we reject this prior to C++20
constexpr int a() { // both-error {{never produces a constant expression}}
Expand Down
Loading