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 "EvalSettings.h"
11#include "InterpFrame.h"
12#include "InterpStack.h"
13#include "Program.h"
14#include "Reflect.h"
15#include "State.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclTemplate.h"
18
19using namespace clang;
20using namespace clang::interp;
21
22InterpState::InterpState(const EvalSettings &Settings, Program &P,
23 InterpStack &Stk, FrameAllocator &FrameAlloc,
24 Context &Ctx, SourceMapper *M)
25 : State(Ctx.getASTContext(), Settings.EvalStatus), M(M),
26 FrameAlloc(FrameAlloc), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),
27 Current(&BottomFrame), StepsLeft(Ctx.getLangOpts().ConstexprStepLimit),
28 InfiniteSteps(StepsLeft == 0), EvalID(Ctx.getEvalID()) {
29 InConstantContext = Settings.InConstantContext;
30 CheckingPotentialConstantExpression =
31 Settings.CheckingPotentialConstantExpression;
32 CheckingForUndefinedBehavior = Settings.CheckingForUndefinedBehavior;
33 EvalMode = Settings.EvalMode;
34}
35
36InterpState::InterpState(const EvalSettings &Settings, Program &P,
37 InterpStack &Stk, FrameAllocator &FrameAlloc,
38 Context &Ctx, const Function *Func)
39 : State(Ctx.getASTContext(), Settings.EvalStatus), M(nullptr),
40 FrameAlloc(FrameAlloc), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),
41 Current(&BottomFrame), StepsLeft(Ctx.getLangOpts().ConstexprStepLimit),
42 InfiniteSteps(StepsLeft == 0), EvalID(Ctx.getEvalID()) {
43 InConstantContext = Settings.InConstantContext;
44 CheckingPotentialConstantExpression =
45 Settings.CheckingPotentialConstantExpression;
46 CheckingForUndefinedBehavior = Settings.CheckingForUndefinedBehavior;
47 EvalMode = Settings.EvalMode;
48}
49
50InterpState::InterpState(Expr::EvalStatus &Status, Program &P, InterpStack &Stk,
51 FrameAllocator &FrameAlloc, Context &Ctx,
52 SourceMapper *M)
53 : State(Ctx.getASTContext(), Status), M(M), FrameAlloc(FrameAlloc), P(P),
54 Stk(Stk), Ctx(Ctx), BottomFrame(*this), Current(&BottomFrame),
55 StepsLeft(Ctx.getLangOpts().ConstexprStepLimit),
56 InfiniteSteps(StepsLeft == 0), EvalID(Ctx.getEvalID()) {
57 InConstantContext = true;
58 CheckingPotentialConstantExpression = false;
59 CheckingForUndefinedBehavior = true;
60 EvalMode = EvaluationMode::ConstantExpression;
61}
62
63bool InterpState::inConstantContext() const {
64 if (ConstantContextOverride)
65 return *ConstantContextOverride;
66
67 return InConstantContext;
68}
69
70InterpState::~InterpState() {
71 assert(Current->isBottomFrame());
72
73 while (DeadBlocks) {
74 DeadBlock *Next = DeadBlocks->Next;
75
76 // There might be a pointer in a global structure pointing to the dead
77 // block.
78 for (Pointer *P = DeadBlocks->B.Pointers; P; P = P->asBlockPointer().Next)
79 DeadBlocks->B.removePointer(P);
80
81 std::free(ptr: DeadBlocks);
82 DeadBlocks = Next;
83 }
84}
85
86void InterpState::cleanup() {
87 // As a last resort, make sure all pointers still pointing to a dead block
88 // don't point to it anymore.
89 if (Alloc)
90 Alloc->cleanup();
91}
92
93const Frame *InterpState::getCurrentFrame() { return Current; }
94
95void InterpState::deallocate(Block *B) {
96 assert(B);
97 assert(!B->isDynamic());
98 assert(!B->isStatic());
99 assert(!B->isDead());
100
101 // The block might have a pointer saved in a field in its data
102 // that points to the block itself. We call the dtor first,
103 // which will destroy all the data but leave InlineDescriptors
104 // intact. If the block THEN still has pointers, we create a
105 // DeadBlock for it.
106 if (B->IsInitialized)
107 B->invokeDtor();
108
109 assert(!B->isInitialized());
110 if (B->hasPointers()) {
111 size_t Size = B->getSize();
112 // Allocate a new block, transferring over pointers.
113 char *Memory =
114 reinterpret_cast<char *>(std::malloc(size: sizeof(DeadBlock) + Size));
115 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
116 // Since the block doesn't hold any actual data anymore, we can just
117 // memcpy() everything over.
118 std::memcpy(dest: D->rawData(), src: B->rawData(), n: Size);
119 D->B.IsInitialized = false;
120 }
121}
122
123bool InterpState::maybeDiagnoseDanglingAllocations() {
124 if (!Alloc)
125 return true;
126
127 bool NoAllocationsLeft = !Alloc->hasAllocations();
128
129 if (!checkingPotentialConstantExpression()) {
130 for (const auto &[Source, Site] : Alloc->allocation_sites()) {
131 assert(!Site.empty());
132
133 CCEDiag(Loc: Source->getExprLoc(), DiagId: diag::note_constexpr_memory_leak)
134 << (Site.size() - 1) << Source->getSourceRange();
135 }
136 }
137 // Keep evaluating before C++20, since the CXXNewExpr wasn't valid there
138 // in the first place.
139 return NoAllocationsLeft || !getLangOpts().CPlusPlus20;
140}
141
142StdAllocatorCaller InterpState::getStdAllocatorCaller(StringRef Name) const {
143 for (const InterpFrame *F = Current; F; F = F->Caller) {
144 const Function *Func = F->getFunction();
145 if (!Func)
146 continue;
147 const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Val: Func->getDecl());
148 if (!MD)
149 continue;
150 const IdentifierInfo *FnII = MD->getIdentifier();
151 if (!FnII || !FnII->isStr(Str: Name))
152 continue;
153
154 const auto *CTSD =
155 dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent());
156 if (!CTSD)
157 continue;
158
159 const IdentifierInfo *ClassII = CTSD->getIdentifier();
160 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
161 if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "allocator") &&
162 TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {
163 QualType ElemType = TAL[0].getAsType();
164 const auto *NewCall = cast<CallExpr>(Val: F->Caller->getExpr(PC: F->getRetOpPC()));
165 return {.Call: NewCall, .AllocType: ElemType};
166 }
167 }
168
169 return {};
170}
171
172bool InterpState::diagnoseStepLimitExceeded(CodePtr OpPC) {
173 FFDiag(SI: Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_step_limit_exceeded, ExtraNotes: 1)
174 << getLangOpts().ConstexprStepLimit;
175 Note(Loc: Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_steps);
176 return false;
177}
178