LLVM 24.0.0git
ExecutorBase.h
Go to the documentation of this file.
1//===--- ExecutorBase.h - Non-visitor methods of InstExecutor -------------===//
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// This file declares non-visitor methods of InstExecutor for code reuse.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TOOLS_LLUBI_EXECUTORBASE_H
14#define LLVM_TOOLS_LLUBI_EXECUTORBASE_H
15
16#include "llvm/IR/Intrinsics.h"
20#include <optional>
21#include <string>
22#include <utility>
23
24namespace llvm::ubi {
25
26enum class FrameState {
27 // It is about to enter the function.
28 // Valid transition:
29 // -> Running
31 // It is executing instructions inside the function.
32 // Valid transitions:
33 // -> Pending (on call)
34 // -> Exit (on return)
36 // It is about to enter a callee or handle return value from the callee.
37 // Valid transitions:
38 // -> Running (after returning from callee)
40 // It is about to return the control to the caller.
42};
43
44/// Context for a function call.
45/// This struct maintains the state during the execution of a function,
46/// including the control flow, values of executed instructions, and stack
47/// objects.
48struct Frame {
54
59 // Stack objects allocated in this frame. They will be automatically freed
60 // when the function returns.
62 // Values of arguments and executed instructions in this function.
64
65 // Reserved for in-flight subroutines.
68 // Temporary memory objects created via pointer arguments with byval.
69 // They belong to the caller.
73
76 const TargetLibraryInfoImpl &TLIImpl);
77};
78
83
84class DiagnosticReporter;
85
87 friend class DiagnosticReporter;
88
89protected:
92 Frame *CurrentFrame = nullptr;
93 std::optional<ProgramExitInfo> ExitInfo;
94
97 ~ExecutorBase() = default;
98
99private:
100 void reportImmediateUBString(StringRef Msg);
101 void reportErrorString(StringRef Msg);
102
103public:
106
107 /// Check if the upcoming memory access is valid. Returns the resolved memory
108 /// object and offset if it is valid.
109 std::pair<MemoryObject *, uint64_t> verifyMemAccess(const Pointer &Ptr,
110 uint64_t AccessSize,
111 Align Alignment,
112 bool IsStore);
113
114 AnyValue load(const AnyValue &Ptr, Align Alignment, Type *ValTy,
115 bool NoUndef);
116 void store(const AnyValue &Ptr, Align Alignment, const AnyValue &Val,
117 Type *ValTy);
118
119 /// Copy `Length` bytes from `Src` to `Dest` according to the intrinsic `IID`.
120 /// If `IsLibCall` is true, the C library semantics are used instead.
121 void executeMemTransfer(const AnyValue &Dest, const AnyValue &Src,
122 const AnyValue &Length, Intrinsic::ID IID,
123 bool IsLibCall);
124
125 /// Fill `Length` bytes at `Dest` with `Val`.
126 void executeMemSet(const AnyValue &Dest, const AnyValue &Val,
127 const AnyValue &Length);
128
130 uint64_t ExitCode = 0);
131 void setFailed();
132
133 bool hasProgramExited() const;
134 std::optional<ProgramExitInfo> getExitInfo() const;
135
136 unsigned getIntSize() const;
137
138 void dumpStackTrace() const;
139};
140
142 ExecutorBase &Executor;
143 std::string Buf;
145 AnyValuePrinter Printer;
146 DiagnosticKind Kind;
147
148public:
150 : Executor(E), OS(Buf), Printer(E.Ctx, OS), Kind(K) {}
151
154
156 DiagnosticReporter &operator=(DiagnosticReporter &&) noexcept = delete;
157
159 switch (Kind) {
161 Executor.reportImmediateUBString(Buf);
162 break;
164 Executor.reportErrorString(Buf);
165 break;
166 }
167 }
168
169 template <typename T> DiagnosticReporter &operator<<(const T &Val) {
170 Printer << Val;
171 return *this;
172 }
173};
174
175} // namespace llvm::ubi
176
177#endif // LLVM_TOOLS_LLUBI_EXECUTORBASE_H
aarch64 promote const
uint64_t uint64_t
AMDGPU Mark last scratch load
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define F(x, y, z)
Definition MD5.cpp:54
#define H(x, y, z)
Definition MD5.cpp:56
#define T
const char * Msg
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Implementation of the target library information.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
A raw_ostream that writes to an std::string.
The global context for the interpreter.
Definition Context.h:227
DiagnosticReporter(const DiagnosticReporter &)=delete
DiagnosticReporter(ExecutorBase &E, DiagnosticKind K)
DiagnosticReporter & operator<<(const T &Val)
DiagnosticReporter(DiagnosticReporter &&) noexcept=delete
An interface for handling events and managing outputs during interpretation.
Definition Context.h:166
EventHandler & Handler
unsigned getIntSize() const
DiagnosticReporter reportError()
std::pair< MemoryObject *, uint64_t > verifyMemAccess(const Pointer &Ptr, uint64_t AccessSize, Align Alignment, bool IsStore)
Check if the upcoming memory access is valid.
friend class DiagnosticReporter
void executeMemSet(const AnyValue &Dest, const AnyValue &Val, const AnyValue &Length)
Fill Length bytes at Dest with Val.
ExecutorBase(Context &C, EventHandler &H)
std::optional< ProgramExitInfo > ExitInfo
std::optional< ProgramExitInfo > getExitInfo() const
void requestProgramExit(ProgramExitInfo::ProgramExitKind Kind, uint64_t ExitCode=0)
DiagnosticReporter reportImmediateUB()
void store(const AnyValue &Ptr, Align Alignment, const AnyValue &Val, Type *ValTy)
void executeMemTransfer(const AnyValue &Dest, const AnyValue &Src, const AnyValue &Length, Intrinsic::ID IID, bool IsLibCall)
Copy Length bytes from Src to Dest according to the intrinsic IID.
@ Length
Definition DWP.cpp:577
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Context for a function call.
Frame(Function &F, CallBase *CallSite, Frame *LastFrame, ArrayRef< AnyValue > Args, AnyValue &RetVal, const TargetLibraryInfoImpl &TLIImpl)
BasicBlock::iterator PC
AnyValue CalleeRetVal
ArrayRef< AnyValue > Args
BasicBlock * BB
CallBase * CallSite
Function * ResolvedCallee
TargetLibraryInfo TLI
bool HasInaccessibleStackObjects
SmallVector< IntrusiveRefCntPtr< MemoryObject > > CalleeByValArgs
SmallVector< IntrusiveRefCntPtr< MemoryObject > > Allocas
FrameState State
SmallVector< AnyValue > CalleeArgs
Function & Func
AnyValue & RetVal
DenseMap< Value *, AnyValue > ValueMap