-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23fe416
Implement gRPC customizations another way without calling reinterpret…
wesm ac405b3
Ensure .proto file is compiled before anything else
wesm b3609d4
Add comments about the purpose of protocol.cc
wesm ed6eb80
Further refinements, make protocol.h an internal header. Comments per…
wesm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
Member
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")
Member
Author
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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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