-
Couldn't load subscription status.
- Fork 3.9k
ARROW-4558: [C++][Flight] Implement gRPC customizations without UB #3633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
23fe416
ac405b3
b3609d4
ed6eb80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <limits> | ||
| #include <memory> | ||
|
|
||
| #include "grpcpp/impl/codegen/config_protobuf.h" | ||
|
|
||
| // It is necessary to undefined this macro so that the protobuf | ||
| // SerializationTraits specialization is not declared in proto_utils.h. We've | ||
| // copied that specialization below and modified it to exclude | ||
| // protocol::FlightData from the default implementation so we can specialize | ||
| // for our faster serialization-deserialization path | ||
| #undef GRPC_OPEN_SOURCE_PROTO | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment on this? I think it's easy to miss it or misunderstand why it's here. (as in "what happens if I remove this") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| #include "grpcpp/impl/codegen/proto_utils.h" | ||
|
|
||
| namespace arrow { | ||
| namespace ipc { | ||
| namespace internal { | ||
|
|
||
| struct IpcPayload; | ||
|
|
||
| } // namespace internal | ||
| } // namespace ipc | ||
|
|
||
| namespace flight { | ||
|
|
||
| struct FlightData; | ||
|
|
||
| namespace protocol { | ||
|
|
||
| class FlightData; | ||
|
|
||
| } // namespace protocol | ||
| } // namespace flight | ||
| } // namespace arrow | ||
|
|
||
| namespace grpc { | ||
|
|
||
| using arrow::flight::FlightData; | ||
| using arrow::ipc::internal::IpcPayload; | ||
|
|
||
| class ByteBuffer; | ||
| class Status; | ||
|
|
||
| Status FlightDataSerialize(const IpcPayload& msg, ByteBuffer* out, bool* own_buffer); | ||
| Status FlightDataDeserialize(ByteBuffer* buffer, FlightData* out); | ||
|
|
||
| // This class provides a protobuf serializer. It translates between protobuf | ||
| // objects and grpc_byte_buffers. More information about SerializationTraits can | ||
| // be found in include/grpcpp/impl/codegen/serialization_traits.h. | ||
| template <class T> | ||
| class SerializationTraits< | ||
| T, typename std::enable_if< | ||
| std::is_base_of<grpc::protobuf::Message, T>::value && | ||
| !std::is_same<arrow::flight::protocol::FlightData, T>::value>::type> { | ||
| public: | ||
| static Status Serialize(const grpc::protobuf::Message& msg, ByteBuffer* bb, | ||
| bool* own_buffer) { | ||
| return GenericSerialize<ProtoBufferWriter, T>(msg, bb, own_buffer); | ||
| } | ||
|
|
||
| static Status Deserialize(ByteBuffer* buffer, grpc::protobuf::Message* msg) { | ||
| return GenericDeserialize<ProtoBufferReader, T>(buffer, msg); | ||
| } | ||
| }; | ||
|
|
||
| template <class T> | ||
| class SerializationTraits<T, typename std::enable_if<std::is_same< | ||
| arrow::flight::protocol::FlightData, T>::value>::type> { | ||
| public: | ||
| static Status Serialize(const grpc::protobuf::Message& msg, ByteBuffer* bb, | ||
| bool* own_buffer) { | ||
| return FlightDataSerialize(*reinterpret_cast<const IpcPayload*>(&msg), bb, | ||
| own_buffer); | ||
| } | ||
|
|
||
| static Status Deserialize(ByteBuffer* buffer, grpc::protobuf::Message* msg) { | ||
| return FlightDataDeserialize(buffer, reinterpret_cast<FlightData*>(msg)); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace grpc | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
|
|
||
| #include "arrow/flight/protocol-internal.h" | ||
|
|
||
| // NOTE(wesm): Including .cc files in another .cc file would ordinarily be a | ||
| // no-no. We have customized the serialization path for FlightData, which is | ||
| // currently only possible through some pre-processor commands that need to be | ||
| // included before either of these files is compiled. Because we don't want to | ||
| // edit the generated C++ files, we include them here and do our gRPC | ||
| // customizations in protocol-internal.h | ||
| #include "arrow/flight/Flight.grpc.pb.cc" // NOLINT | ||
| #include "arrow/flight/Flight.pb.cc" // NOLINT |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
|
|
||
| #pragma once | ||
|
|
||
| // Need to include this first to get our gRPC customizations | ||
| #include "arrow/flight/customize_protobuf.h" | ||
|
|
||
| #include "arrow/flight/Flight.grpc.pb.h" | ||
| #include "arrow/flight/Flight.pb.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically we are praying that
Writedoesn't do anything with the FlightData pointer except pass it toSerializationTraits<FlightData>, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. But the reader/writer layers have no knowledge of the message types; SerializationTraits is the only point of contact
https://github.com/grpc/grpc/blob/f41affc9c7e0358612b9747e914e8b01af53f75d/include/grpcpp/impl/codegen/call_op_set.h#L395
https://github.com/grpc/grpc/blob/46bd2f7adb926053345665d5c487fa20acd2b5b0/include/grpcpp/impl/codegen/server_interface.h#L255