From ece75daa3a5b0e2e3137a78f3389eaf18e5d5aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Tue, 10 Nov 2026 17:14:13 +0100 Subject: [PATCH 1/3] GH-55479: [C++][FlightRPC] Give gRPC real pb::FlightData messages instead of casting Arrow structs WritePayload() and ReadPayload() passed a FlightPayload or internal::FlightData to gRPC as a pb::FlightData through reinterpret_cast, and SerializationTraits cast it back. The object is not a pb::FlightData, which is undefined behavior and which Clang CFI (-fsanitize=cfi-unrelated-cast) rejects. gRPC only passes the message to SerializationTraits, so give it an empty, real pb::FlightData that sits next to a pointer to the Arrow struct, and read the struct back from it. Co-Authored-By: Claude Opus 5.5 --- .../flight/transport/grpc/customize_grpc.h | 15 +++- .../transport/grpc/serialization_internal.cc | 88 +++++++++++++------ 2 files changed, 71 insertions(+), 32 deletions(-) diff --git a/cpp/src/arrow/flight/transport/grpc/customize_grpc.h b/cpp/src/arrow/flight/transport/grpc/customize_grpc.h index 4a64e4e07214..3698f2f135c8 100644 --- a/cpp/src/arrow/flight/transport/grpc/customize_grpc.h +++ b/cpp/src/arrow/flight/transport/grpc/customize_grpc.h @@ -112,6 +112,13 @@ ::grpc::Status FlightDataSerialize(const arrow::flight::FlightPayload& msg, // protobuf without copying ARROW_FLIGHT_EXPORT ::grpc::Status FlightDataDeserialize( ::grpc::ByteBuffer* buffer, arrow::flight::internal::FlightData* out); + +// The Arrow structs carried by the `pb::FlightData` messages that WritePayload() +// and ReadPayload() give to gRPC (see serialization_internal.cc). +ARROW_FLIGHT_EXPORT const arrow::flight::FlightPayload& FlightPayloadOfMessage( + const void* message); +ARROW_FLIGHT_EXPORT arrow::flight::internal::FlightData* FlightDataOfMessage( + void* message); } // namespace grpc } // namespace transport } // namespace flight @@ -128,16 +135,16 @@ class SerializationTraits { #endif public: - // In the functions below, we cast back the Message argument to its real - // type (see ReadPayload() and WritePayload() for the initial cast). + // The message carries the Arrow struct to serialize or to deserialize into + // (see ReadPayload() and WritePayload()). static Status Serialize(const MessageType& msg, ByteBuffer* bb, bool* own_buffer) { return arrow::flight::transport::grpc::FlightDataSerialize( - *reinterpret_cast(&msg), bb, own_buffer); + arrow::flight::transport::grpc::FlightPayloadOfMessage(&msg), bb, own_buffer); } static Status Deserialize(ByteBuffer* buffer, MessageType* msg) { return arrow::flight::transport::grpc::FlightDataDeserialize( - buffer, reinterpret_cast(msg)); + buffer, arrow::flight::transport::grpc::FlightDataOfMessage(msg)); } }; diff --git a/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc b/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc index ee13a8e2022f..ec5c9462263c 100644 --- a/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc +++ b/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc @@ -17,6 +17,7 @@ #include "arrow/flight/transport/grpc/serialization_internal.h" +#include #include #include #include @@ -203,72 +204,103 @@ ::grpc::Status FlightDataDeserialize(ByteBuffer* buffer, # pragma GCC diagnostic ignored "-Wstrict-aliasing" #endif -// Pointer bitcast explanation: grpc::*Writer::Write() and grpc::*Reader::Read() -// both take a T* argument (here pb::FlightData*). But they don't do anything -// with that argument except pass it to SerializationTraits::Serialize() and -// SerializationTraits::Deserialize(). -// -// Since we control SerializationTraits, we can interpret the -// pointer argument whichever way we want, including cast it back to the original type. -// (see customize_grpc.h). +// grpc::*Writer::Write() and grpc::*Reader::Read() take a T (here +// pb::FlightData) and only pass it to SerializationTraits::Serialize() and +// SerializationTraits::Deserialize(), which we control (see customize_grpc.h). +// So instead of building the protobuf message, we give gRPC an empty, real +// pb::FlightData that carries the Arrow struct, and the traits read the struct +// from it. The message must be a real pb::FlightData: an Arrow struct cast to +// pb::FlightData is not one, which is undefined behavior and which Clang CFI +// (`-fsanitize=cfi-unrelated-cast`) rejects. + +namespace { + +struct FlightPayloadMessage { + pb::FlightData message; + const FlightPayload* payload; +}; + +struct FlightDataMessage { + pb::FlightData message; + flight::internal::FlightData* data; +}; + +} // namespace + +// `message` points to the `message` member of one of the structs above. +const FlightPayload& FlightPayloadOfMessage(const void* message) { + return *reinterpret_cast( + static_cast(message) - offsetof(FlightPayloadMessage, message)) + ->payload; +} + +flight::internal::FlightData* FlightDataOfMessage(void* message) { + return reinterpret_cast(static_cast(message) - + offsetof(FlightDataMessage, message)) + ->data; +} arrow::Result WritePayload( const FlightPayload& payload, ::grpc::ClientReaderWriter* writer) { RETURN_NOT_OK(payload.Validate()); - // Pretend to be pb::FlightData and intercept in SerializationTraits - return writer->Write(*reinterpret_cast(&payload), - ::grpc::WriteOptions()); + FlightPayloadMessage message; + message.payload = &payload; + return writer->Write(message.message, ::grpc::WriteOptions()); } arrow::Result WritePayload( const FlightPayload& payload, ::grpc::ClientReaderWriter* writer) { RETURN_NOT_OK(payload.Validate()); - // Pretend to be pb::FlightData and intercept in SerializationTraits - return writer->Write(*reinterpret_cast(&payload), - ::grpc::WriteOptions()); + FlightPayloadMessage message; + message.payload = &payload; + return writer->Write(message.message, ::grpc::WriteOptions()); } arrow::Result WritePayload( const FlightPayload& payload, ::grpc::ServerReaderWriter* writer) { RETURN_NOT_OK(payload.Validate()); - // Pretend to be pb::FlightData and intercept in SerializationTraits - return writer->Write(*reinterpret_cast(&payload), - ::grpc::WriteOptions()); + FlightPayloadMessage message; + message.payload = &payload; + return writer->Write(message.message, ::grpc::WriteOptions()); } arrow::Result WritePayload(const FlightPayload& payload, ::grpc::ServerWriter* writer) { RETURN_NOT_OK(payload.Validate()); - // Pretend to be pb::FlightData and intercept in SerializationTraits - return writer->Write(*reinterpret_cast(&payload), - ::grpc::WriteOptions()); + FlightPayloadMessage message; + message.payload = &payload; + return writer->Write(message.message, ::grpc::WriteOptions()); } bool ReadPayload(::grpc::ClientReader* reader, flight::internal::FlightData* data) { - // Pretend to be pb::FlightData and intercept in SerializationTraits - return reader->Read(reinterpret_cast(data)); + FlightDataMessage message; + message.data = data; + return reader->Read(&message.message); } bool ReadPayload(::grpc::ClientReaderWriter* reader, flight::internal::FlightData* data) { - // Pretend to be pb::FlightData and intercept in SerializationTraits - return reader->Read(reinterpret_cast(data)); + FlightDataMessage message; + message.data = data; + return reader->Read(&message.message); } bool ReadPayload(::grpc::ServerReaderWriter* reader, flight::internal::FlightData* data) { - // Pretend to be pb::FlightData and intercept in SerializationTraits - return reader->Read(reinterpret_cast(data)); + FlightDataMessage message; + message.data = data; + return reader->Read(&message.message); } bool ReadPayload(::grpc::ServerReaderWriter* reader, flight::internal::FlightData* data) { - // Pretend to be pb::FlightData and intercept in SerializationTraits - return reader->Read(reinterpret_cast(data)); + FlightDataMessage message; + message.data = data; + return reader->Read(&message.message); } bool ReadPayload(::grpc::ClientReaderWriter* reader, From f4229ccf43c27ee20c27d186c65158b6af9df420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Tue, 10 Nov 2026 18:00:49 +0100 Subject: [PATCH 2/3] Suppress -Winvalid-offsetof instead of -Wstrict-aliasing offsetof() on the message wrappers, which are not standard-layout, is conditionally-supported and warns, which fails Debug builds with -Werror. GCC, Clang and MSVC support it for types without virtual base classes. The code no longer casts between unrelated types, so -Wstrict-aliasing does not need to be silenced anymore. Co-Authored-By: Claude Opus 5.5 --- .../arrow/flight/transport/grpc/serialization_internal.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc b/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc index ec5c9462263c..bbe90709ed50 100644 --- a/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc +++ b/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc @@ -198,10 +198,12 @@ ::grpc::Status FlightDataDeserialize(ByteBuffer* buffer, return ::grpc::Status::OK; } -// The pointer bitcast hack below causes legitimate warnings, silence them. +// offsetof() below is used on types that are not standard-layout, which is +// conditionally-supported. GCC, Clang and MSVC support it for types without +// virtual base classes, as here. #ifndef _WIN32 # pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wstrict-aliasing" +# pragma GCC diagnostic ignored "-Winvalid-offsetof" #endif // grpc::*Writer::Write() and grpc::*Reader::Read() take a T (here From 8b526a426b10d6e7e41a80abe4111c8382776ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Wed, 11 Nov 2026 13:35:43 +0100 Subject: [PATCH 3/3] Guard the pragmas by compiler and share the message setup Guard the -Winvalid-offsetof suppression by compiler instead of by OS, so that GCC and Clang targeting Windows (MinGW, clang-cl) get it too. Give the message wrappers a constructor that takes the Arrow struct, and move the setup shared by the WritePayload() and ReadPayload() overloads into two templates. Say what Clang CFI rejects instead of calling the old cast undefined behavior. Co-Authored-By: Claude Opus 5.5 --- .../transport/grpc/serialization_internal.cc | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc b/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc index bbe90709ed50..c5428ec68155 100644 --- a/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc +++ b/cpp/src/arrow/flight/transport/grpc/serialization_internal.cc @@ -201,7 +201,7 @@ ::grpc::Status FlightDataDeserialize(ByteBuffer* buffer, // offsetof() below is used on types that are not standard-layout, which is // conditionally-supported. GCC, Clang and MSVC support it for types without // virtual base classes, as here. -#ifndef _WIN32 +#if defined(__GNUC__) || defined(__clang__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Winvalid-offsetof" #endif @@ -211,22 +211,36 @@ ::grpc::Status FlightDataDeserialize(ByteBuffer* buffer, // SerializationTraits::Deserialize(), which we control (see customize_grpc.h). // So instead of building the protobuf message, we give gRPC an empty, real // pb::FlightData that carries the Arrow struct, and the traits read the struct -// from it. The message must be a real pb::FlightData: an Arrow struct cast to -// pb::FlightData is not one, which is undefined behavior and which Clang CFI -// (`-fsanitize=cfi-unrelated-cast`) rejects. +// from it. The message must be a real pb::FlightData: Clang CFI +// (`-fsanitize=cfi-unrelated-cast`) rejects casting an Arrow struct to one. namespace { struct FlightPayloadMessage { + explicit FlightPayloadMessage(const FlightPayload* payload) : payload(payload) {} pb::FlightData message; const FlightPayload* payload; }; struct FlightDataMessage { + explicit FlightDataMessage(flight::internal::FlightData* data) : data(data) {} pb::FlightData message; flight::internal::FlightData* data; }; +template +arrow::Result WriteFlightPayload(const FlightPayload& payload, Writer* writer) { + RETURN_NOT_OK(payload.Validate()); + FlightPayloadMessage message(&payload); + return writer->Write(message.message, ::grpc::WriteOptions()); +} + +template +bool ReadFlightData(Reader* reader, flight::internal::FlightData* data) { + FlightDataMessage message(data); + return reader->Read(&message.message); +} + } // namespace // `message` points to the `message` member of one of the structs above. @@ -245,64 +259,44 @@ flight::internal::FlightData* FlightDataOfMessage(void* message) { arrow::Result WritePayload( const FlightPayload& payload, ::grpc::ClientReaderWriter* writer) { - RETURN_NOT_OK(payload.Validate()); - FlightPayloadMessage message; - message.payload = &payload; - return writer->Write(message.message, ::grpc::WriteOptions()); + return WriteFlightPayload(payload, writer); } arrow::Result WritePayload( const FlightPayload& payload, ::grpc::ClientReaderWriter* writer) { - RETURN_NOT_OK(payload.Validate()); - FlightPayloadMessage message; - message.payload = &payload; - return writer->Write(message.message, ::grpc::WriteOptions()); + return WriteFlightPayload(payload, writer); } arrow::Result WritePayload( const FlightPayload& payload, ::grpc::ServerReaderWriter* writer) { - RETURN_NOT_OK(payload.Validate()); - FlightPayloadMessage message; - message.payload = &payload; - return writer->Write(message.message, ::grpc::WriteOptions()); + return WriteFlightPayload(payload, writer); } arrow::Result WritePayload(const FlightPayload& payload, ::grpc::ServerWriter* writer) { - RETURN_NOT_OK(payload.Validate()); - FlightPayloadMessage message; - message.payload = &payload; - return writer->Write(message.message, ::grpc::WriteOptions()); + return WriteFlightPayload(payload, writer); } bool ReadPayload(::grpc::ClientReader* reader, flight::internal::FlightData* data) { - FlightDataMessage message; - message.data = data; - return reader->Read(&message.message); + return ReadFlightData(reader, data); } bool ReadPayload(::grpc::ClientReaderWriter* reader, flight::internal::FlightData* data) { - FlightDataMessage message; - message.data = data; - return reader->Read(&message.message); + return ReadFlightData(reader, data); } bool ReadPayload(::grpc::ServerReaderWriter* reader, flight::internal::FlightData* data) { - FlightDataMessage message; - message.data = data; - return reader->Read(&message.message); + return ReadFlightData(reader, data); } bool ReadPayload(::grpc::ServerReaderWriter* reader, flight::internal::FlightData* data) { - FlightDataMessage message; - message.data = data; - return reader->Read(&message.message); + return ReadFlightData(reader, data); } bool ReadPayload(::grpc::ClientReaderWriter* reader, @@ -310,7 +304,7 @@ bool ReadPayload(::grpc::ClientReaderWriter* read return reader->Read(data); } -#ifndef _WIN32 +#if defined(__GNUC__) || defined(__clang__) # pragma GCC diagnostic pop #endif