156 lines · cpp
1//===--- InterpState.cpp - Interpreter for the constexpr VM -----*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "InterpState.h"10#include "InterpFrame.h"11#include "InterpStack.h"12#include "Program.h"13#include "State.h"14#include "clang/AST/DeclCXX.h"15#include "clang/AST/DeclTemplate.h"16 17using namespace clang;18using namespace clang::interp;19 20InterpState::InterpState(State &Parent, Program &P, InterpStack &Stk,21 Context &Ctx, SourceMapper *M)22 : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),23 Current(&BottomFrame) {24 InConstantContext = Parent.InConstantContext;25 CheckingPotentialConstantExpression =26 Parent.CheckingPotentialConstantExpression;27 CheckingForUndefinedBehavior = Parent.CheckingForUndefinedBehavior;28 EvalMode = Parent.EvalMode;29}30 31InterpState::InterpState(State &Parent, Program &P, InterpStack &Stk,32 Context &Ctx, const Function *Func)33 : Parent(Parent), M(nullptr), P(P), Stk(Stk), Ctx(Ctx),34 BottomFrame(*this, Func, nullptr, CodePtr(), Func->getArgSize()),35 Current(&BottomFrame) {36 InConstantContext = Parent.InConstantContext;37 CheckingPotentialConstantExpression =38 Parent.CheckingPotentialConstantExpression;39 CheckingForUndefinedBehavior = Parent.CheckingForUndefinedBehavior;40 EvalMode = Parent.EvalMode;41}42 43bool InterpState::inConstantContext() const {44 if (ConstantContextOverride)45 return *ConstantContextOverride;46 47 return InConstantContext;48}49 50InterpState::~InterpState() {51 while (Current && !Current->isBottomFrame()) {52 InterpFrame *Next = Current->Caller;53 delete Current;54 Current = Next;55 }56 BottomFrame.destroyScopes();57 58 while (DeadBlocks) {59 DeadBlock *Next = DeadBlocks->Next;60 61 // There might be a pointer in a global structure pointing to the dead62 // block.63 for (Pointer *P = DeadBlocks->B.Pointers; P; P = P->asBlockPointer().Next)64 DeadBlocks->B.removePointer(P);65 66 std::free(DeadBlocks);67 DeadBlocks = Next;68 }69}70 71void InterpState::cleanup() {72 // As a last resort, make sure all pointers still pointing to a dead block73 // don't point to it anymore.74 if (Alloc)75 Alloc->cleanup();76}77 78Frame *InterpState::getCurrentFrame() { return Current; }79 80void InterpState::deallocate(Block *B) {81 assert(B);82 assert(!B->isDynamic());83 assert(!B->isStatic());84 assert(!B->isDead());85 86 // The block might have a pointer saved in a field in its data87 // that points to the block itself. We call the dtor first,88 // which will destroy all the data but leave InlineDescriptors89 // intact. If the block THEN still has pointers, we create a90 // DeadBlock for it.91 if (B->IsInitialized)92 B->invokeDtor();93 94 assert(!B->isInitialized());95 if (B->hasPointers()) {96 size_t Size = B->getSize();97 // Allocate a new block, transferring over pointers.98 char *Memory =99 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));100 auto *D = new (Memory) DeadBlock(DeadBlocks, B);101 // Since the block doesn't hold any actual data anymore, we can just102 // memcpy() everything over.103 std::memcpy(D->rawData(), B->rawData(), Size);104 D->B.IsInitialized = false;105 }106}107 108bool InterpState::maybeDiagnoseDanglingAllocations() {109 if (!Alloc)110 return true;111 112 bool NoAllocationsLeft = !Alloc->hasAllocations();113 114 if (!checkingPotentialConstantExpression()) {115 for (const auto &[Source, Site] : Alloc->allocation_sites()) {116 assert(!Site.empty());117 118 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)119 << (Site.size() - 1) << Source->getSourceRange();120 }121 }122 // Keep evaluating before C++20, since the CXXNewExpr wasn't valid there123 // in the first place.124 return NoAllocationsLeft || !getLangOpts().CPlusPlus20;125}126 127StdAllocatorCaller InterpState::getStdAllocatorCaller(StringRef Name) const {128 for (const InterpFrame *F = Current; F; F = F->Caller) {129 const Function *Func = F->getFunction();130 if (!Func)131 continue;132 const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Func->getDecl());133 if (!MD)134 continue;135 const IdentifierInfo *FnII = MD->getIdentifier();136 if (!FnII || !FnII->isStr(Name))137 continue;138 139 const auto *CTSD =140 dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());141 if (!CTSD)142 continue;143 144 const IdentifierInfo *ClassII = CTSD->getIdentifier();145 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();146 if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr("allocator") &&147 TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {148 QualType ElemType = TAL[0].getAsType();149 const auto *NewCall = cast<CallExpr>(F->Caller->getExpr(F->getRetPC()));150 return {NewCall, ElemType};151 }152 }153 154 return {};155}156