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#include <utility>
31
32namespace llvm {
33namespace orc {
34
36LLVM_ABI extern const char *ExecutorSessionObjectName;
37LLVM_ABI extern const char *DispatchFnName;
38} // end namespace SimpleRemoteEPCDefaultBootstrapSymbolNames
39
47
48/// Result message kind: either a value, or an out-of-band error.
54
61
62/// Encode an Error as the payload of a Hangup message.
63///
64/// A Hangup always carries a serialized Error saying why the session is ending:
65/// a success value for an orderly disconnect, otherwise the reason. Both ends
66/// of the protocol encode and decode hangups through these two functions, so
67/// that their idea of the payload format cannot drift apart.
69
70/// Decode a Hangup payload produced by encodeHangupPayload.
71///
72/// Returns the encoded Error, or an Error describing the payload if it cannot
73/// be decoded -- including an empty payload, which is never valid. Both
74/// outcomes end the session with an error; they are distinguished only by the
75/// message.
77
78/// Encode a wrapper function result as the TagAddr and payload of a Result
79/// message.
80///
81/// An ordinary result travels as its own bytes. An out-of-band error -- which
82/// reports a failure of the wrapper machinery itself, rather than one the
83/// wrapper chose to report -- has no bytes of its own, so its message is
84/// serialized into the payload and the result is tagged OutOfBandError instead.
85LLVM_ABI std::pair<ExecutorAddr, shared::WrapperFunctionBuffer>
87
88/// Decode a Result message produced by encodeResultMessage, returning the
89/// result to complete the pending call with.
90///
91/// Returns an Error if TagAddr does not name a known result kind: like an
92/// unrecognized opcode, that means the peer is speaking a dialect this build
93/// does not know, so the session cannot safely continue. A payload that will
94/// not decode is not fatal by contrast -- it comes back as an out-of-band error
95/// describing itself, so the call waiting on it is still unblocked.
99
101public:
103
105
106 /// Handle receipt of a message.
107 ///
108 /// Returns an Error if the message cannot be handled, 'EndSession' if the
109 /// client will not accept any further messages, and 'ContinueSession'
110 /// otherwise.
113 shared::WrapperFunctionBuffer ArgBytes) = 0;
114
115 /// Handle a disconnection from the underlying transport. No further messages
116 /// should be sent to handleMessage after this is called.
117 /// Err may contain an Error value indicating unexpected disconnection. This
118 /// allows clients to log such errors, but no attempt should be made at
119 /// recovery (which should be handled inside the transport class, if it is
120 /// supported at all).
121 virtual void handleDisconnect(Error Err) = 0;
122};
123
125public:
127
128 /// Called during setup of the client to indicate that the client is ready
129 /// to receive messages.
130 ///
131 /// Transport objects should not access the client until this method is
132 /// called.
133 virtual Error start() = 0;
134
135 /// Send a SimpleRemoteEPC message.
136 ///
137 /// This function may be called concurrently. Subclasses should implement
138 /// locking if required for the underlying transport.
140 ExecutorAddr TagAddr, ArrayRef<char> ArgBytes) = 0;
141
142 /// Trigger disconnection from the transport. The implementation should
143 /// respond by calling handleDisconnect on the client once disconnection
144 /// is complete. May be called more than once and from different threads.
145 virtual void disconnect() = 0;
146};
147
148/// Uses read/write on FileDescriptors for transport.
149class LLVM_ABI FDSimpleRemoteEPCTransport : public SimpleRemoteEPCTransport {
150public:
151 /// Create a FDSimpleRemoteEPCTransport using the given FDs for
152 /// reading (InFD) and writing (OutFD).
154 Create(SimpleRemoteEPCTransportClient &C, int InFD, int OutFD);
155
156 /// Create a FDSimpleRemoteEPCTransport using the given FD for both
157 /// reading and writing.
160 return Create(C, FD, FD);
161 }
162
164
165 Error start() override;
166
167 Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
168 ExecutorAddr TagAddr, ArrayRef<char> ArgBytes) override;
169
170 void disconnect() override;
171
172private:
174 int OutFD)
175 : C(C), InFD(InFD), OutFD(OutFD) {}
176
177 Error readBytes(char *Dst, size_t Size, bool *IsEOF = nullptr);
178 int writeBytes(const char *Src, size_t Size);
179 void listenLoop();
180
181 std::mutex M;
182 SimpleRemoteEPCTransportClient &C;
183 std::thread ListenerThread;
184 int InFD, OutFD;
185 std::atomic<bool> Disconnected{false};
186};
187
188namespace shared {
189
190/// Tuple containing target triple, page size, and bootstrap symbols.
195
196template <>
199public:
200 static size_t size(const SimpleRemoteEPCExecutorInfo &SI) {
201 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::size(
202 SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
203 }
204
205 static bool serialize(SPSOutputBuffer &OB,
207 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::serialize(
208 OB, SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
209 }
210
212 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::deserialize(
213 IB, SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
214 }
215};
216
217} // end namespace shared
218} // end namespace orc
219} // end namespace llvm
220
221#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:129
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.
Input char buffer with underflow check.
Output char buffer with overflow check.
Specialize to describe how to serialize/deserialize to/from the given concrete type.
C++ wrapper function buffer: Same as CWrapperFunctionBuffer but auto-releases memory.
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.
LLVM_ABI shared::WrapperFunctionBuffer encodeHangupPayload(Error Err)
Encode an Error as the payload of a Hangup message.
LLVM_ABI std::pair< ExecutorAddr, shared::WrapperFunctionBuffer > encodeResultMessage(shared::WrapperFunctionBuffer ResultBytes)
Encode a wrapper function result as the TagAddr and payload of a Result message.
SimpleRemoteEPCResultKind
Result message kind: either a value, or an out-of-band error.
LLVM_ABI Error decodeHangupPayload(shared::WrapperFunctionBuffer Payload)
Decode a Hangup payload produced by encodeHangupPayload.
LLVM_ABI Expected< shared::WrapperFunctionBuffer > decodeResultMessage(ExecutorAddr TagAddr, shared::WrapperFunctionBuffer Payload)
Decode a Result message produced by encodeResultMessage, returning the result to complete the pending...
This is an optimization pass for GlobalISel generic memory operations.
StringMap< std::vector< char > > BootstrapMap