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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "InterpState.h" 10 #include "InterpFrame.h" 11 #include "InterpStack.h" 12 #include "Program.h" 13 #include "State.h" 14 15 using namespace clang; 16 using namespace clang::interp; 17 18 InterpState::InterpState(State &Parent, Program &P, InterpStack &Stk, 19 Context &Ctx, SourceMapper *M) 20 : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), Current(nullptr) {} 21 22 bool InterpState::inConstantContext() const { 23 if (ConstantContextOverride) 24 return *ConstantContextOverride; 25 26 return Parent.InConstantContext; 27 } 28 29 InterpState::~InterpState() { 30 while (Current) { 31 InterpFrame *Next = Current->Caller; 32 delete Current; 33 Current = Next; 34 } 35 36 while (DeadBlocks) { 37 DeadBlock *Next = DeadBlocks->Next; 38 std::free(DeadBlocks); 39 DeadBlocks = Next; 40 } 41 } 42 43 void InterpState::cleanup() { 44 // As a last resort, make sure all pointers still pointing to a dead block 45 // don't point to it anymore. 46 for (DeadBlock *DB = DeadBlocks; DB; DB = DB->Next) { 47 for (Pointer *P = DB->B.Pointers; P; P = P->Next) { 48 P->PointeeStorage.BS.Pointee = nullptr; 49 } 50 } 51 52 Alloc.cleanup(); 53 } 54 55 Frame *InterpState::getCurrentFrame() { 56 if (Current && Current->Caller) 57 return Current; 58 return Parent.getCurrentFrame(); 59 } 60 61 bool InterpState::reportOverflow(const Expr *E, const llvm::APSInt &Value) { 62 QualType Type = E->getType(); 63 CCEDiag(E, diag::note_constexpr_overflow) << Value << Type; 64 return noteUndefinedBehavior(); 65 } 66 67 void InterpState::deallocate(Block *B) { 68 assert(B); 69 const Descriptor *Desc = B->getDescriptor(); 70 assert(Desc); 71 72 if (B->hasPointers()) { 73 size_t Size = B->getSize(); 74 75 // Allocate a new block, transferring over pointers. 76 char *Memory = 77 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size)); 78 auto *D = new (Memory) DeadBlock(DeadBlocks, B); 79 std::memset(D->B.rawData(), 0, D->B.getSize()); 80 81 // Move data and metadata from the old block to the new (dead)block. 82 if (B->IsInitialized && Desc->MoveFn) { 83 Desc->MoveFn(B, B->data(), D->data(), Desc); 84 if (Desc->getMetadataSize() > 0) 85 std::memcpy(D->rawData(), B->rawData(), Desc->getMetadataSize()); 86 } 87 D->B.IsInitialized = B->IsInitialized; 88 89 // We moved the contents over to the DeadBlock. 90 B->IsInitialized = false; 91 } else if (B->IsInitialized) { 92 B->invokeDtor(); 93 } 94 } 95 96 bool InterpState::maybeDiagnoseDanglingAllocations() { 97 bool NoAllocationsLeft = (Alloc.getNumAllocations() == 0); 98 99 if (!checkingPotentialConstantExpression()) { 100 for (const auto &It : Alloc.allocation_sites()) { 101 assert(It.second.size() > 0); 102 103 const Expr *Source = It.first; 104 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak) 105 << (It.second.size() - 1) << Source->getSourceRange(); 106 } 107 } 108 return NoAllocationsLeft; 109 } 110