LLVM 24.0.0git
SimpleRemoteEPCUtils.h
Go to the documentation of this file.
1//===--- SimpleRemoteEPCUtils.h - Utils for Simple Remote EPC ---*- 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// Message definitions and other utilities for SimpleRemoteEPC and
10// SimpleRemoteEPCServer.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEREMOTEEPCUTILS_H
15#define LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEREMOTEEPCUTILS_H
16
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/StringMap.h"
24#include "llvm/Support/Error.h"
25
26#include <atomic>
27#include <mutex>
28#include <string>
29#include <thread>
30
31namespace llvm {
32namespace orc {
33
35LLVM_ABI extern const char *ExecutorSessionObjectName;
36LLVM_ABI extern const char *DispatchFnName;
37} // end namespace SimpleRemoteEPCDefaultBootstrapSymbolNames
38
46
53
54/// Encode an Error as the payload of a Hangup message.
55///
56/// A Hangup always carries a serialized Error saying why the session is ending:
57/// a success value for an orderly disconnect, otherwise the reason. Both ends
58/// of the protocol encode and decode hangups through these two functions, so
59/// that their idea of the payload format cannot drift apart.
61
62/// Decode a Hangup payload produced by encodeHangupPayload.
63///
64/// Returns the encoded Error, or an Error describing the payload if it cannot
65/// be decoded -- including an empty payload, which is never valid. Both
66/// outcomes end the session with an error; they are distinguished only by the
67/// message.
69
71public:
73
75
76 /// Handle receipt of a message.
77 ///
78 /// Returns an Error if the message cannot be handled, 'EndSession' if the
79 /// client will not accept any further messages, and 'ContinueSession'
80 /// otherwise.
84
85 /// Handle a disconnection from the underlying transport. No further messages
86 /// should be sent to handleMessage after this is called.
87 /// Err may contain an Error value indicating unexpected disconnection. This
88 /// allows clients to log such errors, but no attempt should be made at
89 /// recovery (which should be handled inside the transport class, if it is
90 /// supported at all).
91 virtual void handleDisconnect(Error Err) = 0;
92};
93
95public:
97
98 /// Called during setup of the client to indicate that the client is ready
99 /// to receive messages.
100 ///
101 /// Transport objects should not access the client until this method is
102 /// called.
103 virtual Error start() = 0;
104
105 /// Send a SimpleRemoteEPC message.
106 ///
107 /// This function may be called concurrently. Subclasses should implement
108 /// locking if required for the underlying transport.
110 ExecutorAddr TagAddr, ArrayRef<char> ArgBytes) = 0;
111
112 /// Trigger disconnection from the transport. The implementation should
113 /// respond by calling handleDisconnect on the client once disconnection
114 /// is complete. May be called more than once and from different threads.
115 virtual void disconnect() = 0;
116};
117
118/// Uses read/write on FileDescriptors for transport.
119class LLVM_ABI FDSimpleRemoteEPCTransport : public SimpleRemoteEPCTransport {
120public:
121 /// Create a FDSimpleRemoteEPCTransport using the given FDs for
122 /// reading (InFD) and writing (OutFD).
124 Create(SimpleRemoteEPCTransportClient &C, int InFD, int OutFD);
125
126 /// Create a FDSimpleRemoteEPCTransport using the given FD for both
127 /// reading and writing.
130 return Create(C, FD, FD);
131 }
132
134
135 Error start() override;
136
137 Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
138 ExecutorAddr TagAddr, ArrayRef<char> ArgBytes) override;
139
140 void disconnect() override;
141
142private:
144 int OutFD)
145 : C(C), InFD(InFD), OutFD(OutFD) {}
146
147 Error readBytes(char *Dst, size_t Size, bool *IsEOF = nullptr);
148 int writeBytes(const char *Src, size_t Size);
149 void listenLoop();
150
151 std::mutex M;
152 SimpleRemoteEPCTransportClient &C;
153 std::thread ListenerThread;
154 int InFD, OutFD;
155 std::atomic<bool> Disconnected{false};
156};
157
159 std::string Name;
161};
162
163using RemoteSymbolLookupSet = std::vector<RemoteSymbolLookupSetElement>;
164
169
170namespace shared {
171
173
175
177
178/// Tuple containing target triple, page size, and bootstrap symbols.
183
184template <>
187public:
188 static size_t size(const RemoteSymbolLookupSetElement &V) {
189 return SPSArgList<SPSString, bool>::size(V.Name, V.Required);
190 }
191
192 static size_t serialize(SPSOutputBuffer &OB,
194 return SPSArgList<SPSString, bool>::serialize(OB, V.Name, V.Required);
195 }
196
197 static size_t deserialize(SPSInputBuffer &IB,
199 return SPSArgList<SPSString, bool>::deserialize(IB, V.Name, V.Required);
200 }
201};
202
203template <>
205public:
206 static size_t size(const RemoteSymbolLookup &V) {
208 }
209
210 static size_t serialize(SPSOutputBuffer &OB, const RemoteSymbolLookup &V) {
212 V.Symbols);
213 }
214
217 IB, V.H, V.Symbols);
218 }
219};
220
221template <>
224public:
225 static size_t size(const SimpleRemoteEPCExecutorInfo &SI) {
226 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::size(
227 SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
228 }
229
230 static bool serialize(SPSOutputBuffer &OB,
232 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::serialize(
233 OB, SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
234 }
235
237 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::deserialize(
238 IB, SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
239 }
240};
241
244
248
249} // end namespace shared
250} // end namespace orc
251} // end namespace llvm
252
253#endif // LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEREMOTEEPCUTILS_H
This file defines the StringMap class.
unsigned uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:215
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:128
Represents an address in the executor process.
Uses read/write on FileDescriptors for transport.
static Expected< std::unique_ptr< FDSimpleRemoteEPCTransport > > Create(SimpleRemoteEPCTransportClient &C, int InFD, int OutFD)
Create a FDSimpleRemoteEPCTransport using the given FDs for reading (InFD) and writing (OutFD).
static Expected< std::unique_ptr< FDSimpleRemoteEPCTransport > > Create(SimpleRemoteEPCTransportClient &C, int FD)
Create a FDSimpleRemoteEPCTransport using the given FD for both reading and writing.
virtual void handleDisconnect(Error Err)=0
Handle a disconnection from the underlying transport.
virtual Expected< HandleMessageAction > handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, shared::WrapperFunctionBuffer ArgBytes)=0
Handle receipt of a message.
virtual void disconnect()=0
Trigger disconnection from the transport.
virtual Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, ArrayRef< char > ArgBytes)=0
Send a SimpleRemoteEPC message.
virtual Error start()=0
Called during setup of the client to indicate that the client is ready to receive messages.
A utility class for serializing to a blob from a variadic list.
SPS tag type for expecteds, which are either a T or a string representing an error.
Input char buffer with underflow check.
Output char buffer with overflow check.
static size_t serialize(SPSOutputBuffer &OB, const RemoteSymbolLookup &V)
Specialize to describe how to serialize/deserialize to/from the given concrete type.
C++ wrapper function buffer: Same as CWrapperFunctionBuffer but auto-releases memory.
SPSExpected< SPSSequence< SPSSequence< SPSExecutorAddr > > >( SPSExecutorAddr, SPSSequence< SPSRemoteSymbolLookup >) SPSLookupSymbolsSignature
SPSSequence< char > SPSString
SPS tag type for strings, which are equivalent to sequences of chars.
SPSTuple< SPSString, uint64_t, SPSSequence< SPSTuple< SPSString, SPSSequence< char > > >, SPSSequence< SPSTuple< SPSString, SPSExecutorAddr > > > SPSSimpleRemoteEPCExecutorInfo
Tuple containing target triple, page size, and bootstrap symbols.
SPSTuple< SPSString, bool > SPSRemoteSymbolLookupSetElement
SPSTuple< uint64_t, SPSRemoteSymbolLookupSet > SPSRemoteSymbolLookup
SPSSequence< SPSRemoteSymbolLookupSetElement > SPSRemoteSymbolLookupSet
SPSExpected< SPSExecutorAddr >(SPSExecutorAddr, SPSString, uint64_t) SPSLoadDylibSignature
LLVM_ABI shared::WrapperFunctionBuffer encodeHangupPayload(Error Err)
Encode an Error as the payload of a Hangup message.
std::vector< RemoteSymbolLookupSetElement > RemoteSymbolLookupSet
LLVM_ABI Error decodeHangupPayload(shared::WrapperFunctionBuffer Payload)
Decode a Hangup payload produced by encodeHangupPayload.
This is an optimization pass for GlobalISel generic memory operations.
StringMap< std::vector< char > > BootstrapMap