clang 21.0.0git
InterpState.cpp
Go to the documentation of this file.
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
15using namespace clang;
16using namespace clang::interp;
17
19 Context &Ctx, SourceMapper *M)
20 : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),
21 Current(&BottomFrame) {}
22
24 Context &Ctx, const Function *Func)
25 : Parent(Parent), M(nullptr), P(P), Stk(Stk), Ctx(Ctx),
26 BottomFrame(*this, Func, nullptr, CodePtr(), Func->getArgSize()),
27 Current(&BottomFrame) {}
28
30 if (ConstantContextOverride)
31 return *ConstantContextOverride;
32
33 return Parent.InConstantContext;
34}
35
37 while (Current && !Current->isBottomFrame()) {
38 InterpFrame *Next = Current->Caller;
39 delete Current;
40 Current = Next;
41 }
43
44 while (DeadBlocks) {
45 DeadBlock *Next = DeadBlocks->Next;
46 std::free(DeadBlocks);
47 DeadBlocks = Next;
48 }
49}
50
52 // As a last resort, make sure all pointers still pointing to a dead block
53 // don't point to it anymore.
54 for (DeadBlock *DB = DeadBlocks; DB; DB = DB->Next) {
55 for (Pointer *P = DB->B.Pointers; P; P = P->Next) {
56 P->PointeeStorage.BS.Pointee = nullptr;
57 }
58 }
59
60 Alloc.cleanup();
61}
62
64 if (Current && Current->Caller)
65 return Current;
66 return Parent.getCurrentFrame();
67}
68
69bool InterpState::reportOverflow(const Expr *E, const llvm::APSInt &Value) {
71 CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
72 return noteUndefinedBehavior();
73}
74
76 assert(B);
77 const Descriptor *Desc = B->getDescriptor();
78 assert(Desc);
79
80 if (B->hasPointers()) {
81 size_t Size = B->getSize();
82
83 // Allocate a new block, transferring over pointers.
84 char *Memory =
85 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));
86 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
87 std::memset(D->B.rawData(), 0, D->B.getSize());
88
89 // Move data and metadata from the old block to the new (dead)block.
90 if (B->IsInitialized && Desc->MoveFn) {
91 Desc->MoveFn(B, B->data(), D->data(), Desc);
92 if (Desc->getMetadataSize() > 0)
93 std::memcpy(D->rawData(), B->rawData(), Desc->getMetadataSize());
94 }
95 D->B.IsInitialized = B->IsInitialized;
96
97 // We moved the contents over to the DeadBlock.
98 B->IsInitialized = false;
99 } else if (B->IsInitialized) {
100 B->invokeDtor();
101 }
102}
103
105 bool NoAllocationsLeft = (Alloc.getNumAllocations() == 0);
106
108 for (const auto &It : Alloc.allocation_sites()) {
109 assert(It.second.size() > 0);
110
111 const Expr *Source = It.first;
112 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
113 << (It.second.size() - 1) << Source->getSourceRange();
114 }
115 }
116 return NoAllocationsLeft;
117}
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
const Decl * D
Expr * E
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:276
QualType getType() const
Definition: Expr.h:142
A (possibly-)qualified type.
Definition: Type.h:929
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:334
The base class of the type hierarchy.
Definition: Type.h:1828
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
unsigned getSize() const
Returns the size of the block.
Definition: InterpBlock.h:80
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:123
std::byte * data()
Returns a pointer to the stored data.
Definition: InterpBlock.h:91
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition: InterpBlock.h:68
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:104
bool hasPointers() const
Checks if the block has any live pointers.
Definition: InterpBlock.h:70
Pointer into the code segment.
Definition: Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
Descriptor for a dead block.
Definition: InterpBlock.h:184
llvm::iterator_range< const_virtual_iter > allocation_sites() const
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Bytecode function.
Definition: Function.h:81
Frame storing local variables.
Definition: InterpFrame.h:26
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:29
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:28
InterpFrame BottomFrame
Bottom function frame.
Definition: InterpState.h:140
bool reportOverflow(const Expr *E, const llvm::APSInt &Value)
Reports overflow and return true if evaluation should continue.
Definition: InterpState.cpp:69
bool noteUndefinedBehavior() override
Definition: InterpState.h:79
Frame * getCurrentFrame() override
Definition: InterpState.cpp:63
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
InterpFrame * Current
The current frame.
Definition: InterpState.h:142
InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
Definition: InterpState.cpp:18
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:75
bool checkingPotentialConstantExpression() const override
Definition: InterpState.h:76
bool inConstantContext() const
Definition: InterpState.cpp:29
Program & P
Reference to the module containing all bytecode.
Definition: InterpState.h:134
A pointer to a memory block, live or dead.
Definition: Pointer.h:88
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Interface for classes which map locations to sources.
Definition: Source.h:103
Interface for the VM to interact with the AST walker's context.
Definition: State.h:57
virtual Frame * getCurrentFrame()=0
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition: State.cpp:42
bool InConstantContext
Whether or not we're in a context where the front end requires a constant value.
Definition: State.h:128
The JSON file list parser is used to communicate input to InstallAPI.
Describes a memory block created by an allocation site.
Definition: Descriptor.h:116
const BlockMoveFn MoveFn
Definition: Descriptor.h:167
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:240