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