[llvm] [ORC] Carry out-of-band errors in SimpleRemoteEPC results (PR #223378)
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 04:57:58 PDT 2026
https://github.com/lhames created https://github.com/llvm/llvm-project/pull/223378
ORC Wrapper functions may produce out-of-band error results (e.g. due to serialization errors), but SimpleRemoteEPC / SimpleRemoteEPCServer never handled these cases (they incorrectly asserted that they never appeared).
Update the SimpleRemote protocol to use the TagAddr field (previously unused for result messages) to encode Result message kinds: either a Value, or an OutOfBandError. This allows out-of-band errors to flow across the wire and be passed as results to their callers.
encodeResultMessage and decodeResultMessage implement the format / handling: out-of-band errors (or failures to deserialize out-of-band errors) are now passed to handlers as out-of-band error results. An invalid Result message kind in the TagAddr field results in an llvm::Error, ending the session.
SimpleRemoteEPCUtilsTest covers both kinds' round-trips, an unrecognized kind, and a malformed out-of-band payload.
>From 0ab573d5522819fe3bc86c2c83473581cf212c36 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Mon, 14 Sep 2026 21:38:53 +1000
Subject: [PATCH] [ORC] Carry out-of-band errors in SimpleRemoteEPC results
ORC Wrapper functions may produce out-of-band error results (e.g. due to
serialization errors), but SimpleRemoteEPC / SimpleRemoteEPCServer
never handled these cases (they incorrectly asserted that they never
appeared).
Update the SimpleRemote protocol to use the TagAddr field (previously
unused for result messages) to encode Result message kinds: either a
Value, or an OutOfBandError. This allows out-of-band errors to flow
across the wire and be passed as results to their callers.
encodeResultMessage and decodeResultMessage implement the format /
handling: out-of-band errors (or failures to deserialize out-of-band
errors) are now passed to handlers as out-of-band error results. An
invalid Result message kind in the TagAddr field results in an
llvm::Error, ending the session.
SimpleRemoteEPCUtilsTest covers both kinds' round-trips, an unrecognized
kind, and a malformed out-of-band payload.
---
.../Orc/Shared/SimpleRemoteEPCUtils.h | 30 ++++++++
.../Orc/Shared/SimpleRemoteEPCUtils.cpp | 48 ++++++++++++
.../ExecutionEngine/Orc/SimpleRemoteEPC.cpp | 15 ++--
.../TargetProcess/SimpleRemoteEPCServer.cpp | 15 ++--
.../ExecutionEngine/Orc/CMakeLists.txt | 1 +
.../Orc/SimpleRemoteEPCUtilsTest.cpp | 77 +++++++++++++++++++
6 files changed, 170 insertions(+), 16 deletions(-)
create mode 100644 llvm/unittests/ExecutionEngine/Orc/SimpleRemoteEPCUtilsTest.cpp
diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h
index 83137eadf4461..174839b72aa35 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h
@@ -27,6 +27,7 @@
#include <mutex>
#include <string>
#include <thread>
+#include <utility>
namespace llvm {
namespace orc {
@@ -44,6 +45,13 @@ enum class SimpleRemoteEPCOpcode : uint8_t {
LastOpC = CallWrapper
};
+/// Result message kind: either a value, or an out-of-band error.
+enum class SimpleRemoteEPCResultKind : uint64_t {
+ Value,
+ OutOfBandError,
+ LastResultKind = OutOfBandError
+};
+
struct SimpleRemoteEPCExecutorInfo {
std::string TargetTriple;
uint64_t PageSize;
@@ -67,6 +75,28 @@ LLVM_ABI shared::WrapperFunctionBuffer encodeHangupPayload(Error Err);
/// message.
LLVM_ABI Error decodeHangupPayload(shared::WrapperFunctionBuffer Payload);
+/// Encode a wrapper function result as the TagAddr and payload of a Result
+/// message.
+///
+/// An ordinary result travels as its own bytes. An out-of-band error -- which
+/// reports a failure of the wrapper machinery itself, rather than one the
+/// wrapper chose to report -- has no bytes of its own, so its message is
+/// serialized into the payload and the result is tagged OutOfBandError instead.
+LLVM_ABI std::pair<ExecutorAddr, shared::WrapperFunctionBuffer>
+encodeResultMessage(shared::WrapperFunctionBuffer ResultBytes);
+
+/// Decode a Result message produced by encodeResultMessage, returning the
+/// result to complete the pending call with.
+///
+/// Returns an Error if TagAddr does not name a known result kind: like an
+/// unrecognized opcode, that means the peer is speaking a dialect this build
+/// does not know, so the session cannot safely continue. A payload that will
+/// not decode is not fatal by contrast -- it comes back as an out-of-band error
+/// describing itself, so the call waiting on it is still unblocked.
+LLVM_ABI Expected<shared::WrapperFunctionBuffer>
+decodeResultMessage(ExecutorAddr TagAddr,
+ shared::WrapperFunctionBuffer Payload);
+
class LLVM_ABI SimpleRemoteEPCTransportClient {
public:
enum HandleMessageAction { ContinueSession, EndSession };
diff --git a/llvm/lib/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.cpp b/llvm/lib/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.cpp
index 781e2fc8e23e8..0e80dd7cb3f98 100644
--- a/llvm/lib/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.cpp
@@ -70,6 +70,54 @@ Error decodeHangupPayload(shared::WrapperFunctionBuffer Payload) {
return shared::detail::fromSPSSerializable(std::move(Info));
}
+std::pair<ExecutorAddr, shared::WrapperFunctionBuffer>
+encodeResultMessage(shared::WrapperFunctionBuffer ResultBytes) {
+ auto Tag = [](SimpleRemoteEPCResultKind K) {
+ return ExecutorAddr(static_cast<uint64_t>(K));
+ };
+
+ const char *ErrMsg = ResultBytes.getOutOfBandError();
+ if (!ErrMsg)
+ return {Tag(SimpleRemoteEPCResultKind::Value), std::move(ResultBytes)};
+
+ using SPSSerialize = shared::SPSArgList<shared::SPSString>;
+ StringRef M(ErrMsg);
+ auto Payload = shared::WrapperFunctionBuffer::allocate(SPSSerialize::size(M));
+ shared::SPSOutputBuffer OB(Payload.data(), Payload.size());
+ bool Success = SPSSerialize::serialize(OB, M);
+ (void)Success;
+ assert(Success && "Out-of-band error serialization should not fail");
+ return {Tag(SimpleRemoteEPCResultKind::OutOfBandError), std::move(Payload)};
+}
+
+Expected<shared::WrapperFunctionBuffer>
+decodeResultMessage(ExecutorAddr TagAddr,
+ shared::WrapperFunctionBuffer Payload) {
+ using UT = std::underlying_type_t<SimpleRemoteEPCResultKind>;
+ UT KindVal = TagAddr.getValue();
+ if (KindVal > static_cast<UT>(SimpleRemoteEPCResultKind::LastResultKind))
+ return make_error<StringError>("Unexpected result kind " + Twine(KindVal) +
+ " in result message",
+ inconvertibleErrorCode());
+
+ switch (static_cast<SimpleRemoteEPCResultKind>(KindVal)) {
+ case SimpleRemoteEPCResultKind::Value:
+ return std::move(Payload);
+ case SimpleRemoteEPCResultKind::OutOfBandError: {
+ // A malformed payload is reported as the out-of-band error itself: the
+ // call waiting on this result must be unblocked either way, and an error
+ // about the error is more use to the caller than a dead session.
+ std::string Msg;
+ shared::SPSInputBuffer IB(Payload.data(), Payload.size());
+ if (!shared::SPSArgList<shared::SPSString>::deserialize(IB, Msg))
+ return shared::WrapperFunctionBuffer::createOutOfBandError(
+ "Could not deserialize out-of-band error message");
+ return shared::WrapperFunctionBuffer::createOutOfBandError(Msg);
+ }
+ }
+ llvm_unreachable("Invalid result kind");
+}
+
SimpleRemoteEPCTransportClient::~SimpleRemoteEPCTransportClient() = default;
SimpleRemoteEPCTransport::~SimpleRemoteEPCTransport() = default;
diff --git a/llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp b/llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp
index 80f9a4c5d8beb..c47fef1b9cd40 100644
--- a/llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp
@@ -143,7 +143,6 @@ SimpleRemoteEPC::handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
break;
case SimpleRemoteEPCOpcode::Result:
dbgs() << "Result";
- assert(!TagAddr && "Non-zero TagAddr for Result?");
break;
case SimpleRemoteEPCOpcode::CallWrapper:
dbgs() << "CallWrapper";
@@ -242,7 +241,6 @@ Error SimpleRemoteEPC::sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
break;
case SimpleRemoteEPCOpcode::Result:
dbgs() << "Result";
- assert(!TagAddr && "Non-zero TagAddr for Result?");
break;
case SimpleRemoteEPCOpcode::CallWrapper:
dbgs() << "CallWrapper";
@@ -353,9 +351,9 @@ Error SimpleRemoteEPC::handleResult(uint64_t SeqNo, ExecutorAddr TagAddr,
shared::WrapperFunctionBuffer ArgBytes) {
IncomingWFRHandler SendResult;
- if (TagAddr)
- return make_error<StringError>("Unexpected TagAddr in result message",
- inconvertibleErrorCode());
+ auto WFR = decodeResultMessage(TagAddr, std::move(ArgBytes));
+ if (!WFR)
+ return WFR.takeError();
{
std::lock_guard<std::mutex> Lock(SimpleRemoteEPCMutex);
@@ -369,9 +367,7 @@ Error SimpleRemoteEPC::handleResult(uint64_t SeqNo, ExecutorAddr TagAddr,
releaseSeqNo(SeqNo);
}
- auto WFR =
- shared::WrapperFunctionBuffer::copyFrom(ArgBytes.data(), ArgBytes.size());
- SendResult(std::move(WFR));
+ SendResult(std::move(*WFR));
return Error::success();
}
@@ -383,9 +379,10 @@ void SimpleRemoteEPC::handleCallWrapper(
[this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() mutable {
ES->runJITDispatchHandler(
[this, RemoteSeqNo](shared::WrapperFunctionBuffer WFR) {
+ auto [ResultTag, Payload] = encodeResultMessage(std::move(WFR));
if (auto Err =
sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
- ExecutorAddr(), {WFR.data(), WFR.size()}))
+ ResultTag, {Payload.data(), Payload.size()}))
getExecutionSession().reportError(std::move(Err));
},
TagAddr, std::move(ArgBytes));
diff --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp
index d750110fffd27..42e971db4773d 100644
--- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.cpp
@@ -76,7 +76,6 @@ SimpleRemoteEPCServer::handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
break;
case SimpleRemoteEPCOpcode::Result:
dbgs() << "Result";
- assert(!TagAddr && "Non-zero TagAddr for Result?");
break;
case SimpleRemoteEPCOpcode::CallWrapper:
dbgs() << "CallWrapper";
@@ -191,7 +190,6 @@ Error SimpleRemoteEPCServer::sendMessage(SimpleRemoteEPCOpcode OpC,
break;
case SimpleRemoteEPCOpcode::Result:
dbgs() << "Result";
- assert(!TagAddr && "Non-zero TagAddr for Result?");
break;
case SimpleRemoteEPCOpcode::CallWrapper:
dbgs() << "CallWrapper";
@@ -249,6 +247,11 @@ Error SimpleRemoteEPCServer::handleResult(
uint64_t SeqNo, ExecutorAddr TagAddr,
shared::WrapperFunctionBuffer ArgBytes) {
std::promise<shared::WrapperFunctionBuffer> *P = nullptr;
+
+ auto R = decodeResultMessage(TagAddr, std::move(ArgBytes));
+ if (!R)
+ return R.takeError();
+
{
std::lock_guard<std::mutex> Lock(ServerStateMutex);
auto I = PendingJITDispatchResults.find(SeqNo);
@@ -260,9 +263,7 @@ Error SimpleRemoteEPCServer::handleResult(
PendingJITDispatchResults.erase(I);
releaseSeqNo(SeqNo);
}
- auto R = shared::WrapperFunctionBuffer::allocate(ArgBytes.size());
- memcpy(R.data(), ArgBytes.data(), ArgBytes.size());
- P->set_value(std::move(R));
+ P->set_value(std::move(*R));
return Error::success();
}
@@ -275,9 +276,9 @@ void SimpleRemoteEPCServer::handleCallWrapper(
auto *Fn = TagAddr.toPtr<WrapperFnTy>();
shared::WrapperFunctionBuffer ResultBytes(
Fn(ArgBytes.data(), ArgBytes.size()));
+ auto [ResultTag, Payload] = encodeResultMessage(std::move(ResultBytes));
if (auto Err = sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
- ExecutorAddr(),
- {ResultBytes.data(), ResultBytes.size()}))
+ ResultTag, {Payload.data(), Payload.size()}))
ReportError(std::move(Err));
});
}
diff --git a/llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt b/llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt
index 7e5f3f2192011..db2b50e05d07b 100644
--- a/llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt
+++ b/llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt
@@ -48,6 +48,7 @@ add_llvm_unittest(OrcJITTests
SharedMemoryMapperTest.cpp
SimpleExecutorMemoryManagerTest.cpp
SimplePackedSerializationTest.cpp
+ SimpleRemoteEPCUtilsTest.cpp
SPSProxySpecTest.cpp
SymbolLookupSetTest.cpp
SymbolStringPoolTest.cpp
diff --git a/llvm/unittests/ExecutionEngine/Orc/SimpleRemoteEPCUtilsTest.cpp b/llvm/unittests/ExecutionEngine/Orc/SimpleRemoteEPCUtilsTest.cpp
new file mode 100644
index 0000000000000..7501e8de9c46b
--- /dev/null
+++ b/llvm/unittests/ExecutionEngine/Orc/SimpleRemoteEPCUtilsTest.cpp
@@ -0,0 +1,77 @@
+//===--- SimpleRemoteEPCUtilsTest.cpp - Test SimpleRemoteEPC utilities ----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h"
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace llvm::orc;
+
+static ExecutorAddr tagFor(SimpleRemoteEPCResultKind K) {
+ return ExecutorAddr(static_cast<uint64_t>(K));
+}
+
+TEST(SimpleRemoteEPCUtilsTest, ResultMessageValueRoundTrip) {
+ constexpr StringRef Content = "result bytes";
+ auto [TagAddr, Payload] = encodeResultMessage(
+ shared::WrapperFunctionBuffer::copyFrom(Content.data(), Content.size()));
+
+ EXPECT_EQ(TagAddr, tagFor(SimpleRemoteEPCResultKind::Value));
+
+ auto Decoded = decodeResultMessage(TagAddr, std::move(Payload));
+ ASSERT_THAT_EXPECTED(Decoded, Succeeded());
+ EXPECT_EQ(Decoded->getOutOfBandError(), nullptr);
+ EXPECT_EQ(StringRef(Decoded->data(), Decoded->size()), Content);
+}
+
+TEST(SimpleRemoteEPCUtilsTest, ResultMessageEmptyValueRoundTrip) {
+ // An empty buffer is what a void-returning wrapper produces. It shares
+ // 'Size == 0' with an out-of-band error, so check it is not mistaken for one.
+ auto [TagAddr, Payload] =
+ encodeResultMessage(shared::WrapperFunctionBuffer());
+
+ EXPECT_EQ(TagAddr, tagFor(SimpleRemoteEPCResultKind::Value));
+
+ auto Decoded = decodeResultMessage(TagAddr, std::move(Payload));
+ ASSERT_THAT_EXPECTED(Decoded, Succeeded());
+ EXPECT_TRUE(Decoded->empty());
+}
+
+TEST(SimpleRemoteEPCUtilsTest, ResultMessageOutOfBandErrorRoundTrip) {
+ constexpr const char *Msg = "Could not deserialize wrapper function arg data";
+ auto [TagAddr, Payload] = encodeResultMessage(
+ shared::WrapperFunctionBuffer::createOutOfBandError(Msg));
+
+ EXPECT_EQ(TagAddr, tagFor(SimpleRemoteEPCResultKind::OutOfBandError));
+
+ auto Decoded = decodeResultMessage(TagAddr, std::move(Payload));
+ ASSERT_THAT_EXPECTED(Decoded, Succeeded());
+ ASSERT_NE(Decoded->getOutOfBandError(), nullptr);
+ EXPECT_STREQ(Decoded->getOutOfBandError(), Msg);
+}
+
+TEST(SimpleRemoteEPCUtilsTest, ResultMessageUnknownKindIsProtocolError) {
+ // An unrecognized kind means the peer is speaking a dialect this build does
+ // not know, so it ends the session rather than guessing at the payload.
+ auto Decoded = decodeResultMessage(
+ ExecutorAddr(
+ static_cast<uint64_t>(SimpleRemoteEPCResultKind::LastResultKind) + 1),
+ shared::WrapperFunctionBuffer());
+ EXPECT_THAT_EXPECTED(Decoded, Failed());
+}
+
+TEST(SimpleRemoteEPCUtilsTest, ResultMessageMalformedOOBPayloadUnblocksCaller) {
+ // A payload that will not decode is not fatal: the call waiting on it still
+ // has to be completed, so it comes back as an out-of-band error about itself.
+ auto Decoded =
+ decodeResultMessage(tagFor(SimpleRemoteEPCResultKind::OutOfBandError),
+ shared::WrapperFunctionBuffer::allocate(4));
+ ASSERT_THAT_EXPECTED(Decoded, Succeeded());
+ EXPECT_NE(Decoded->getOutOfBandError(), nullptr);
+}
More information about the llvm-commits
mailing list