Skip to content

Commit ba1b2d4

Browse files
[proto] Replace builtin casts with proto-specific casts for protobuf types. (#45731)
There is a protobuf experiment to "devirtualize" messages, allowing protobuf to manage the dynamic dispatch of messages. When enabled, it breaks builtin dynamic/down casts, so the protobuf-specific alternatives should be preferred. Risk Level: Low Testing: unit test Docs Changes: n/a Release Notes: n/a Platform Specific Features: n/a --------- Signed-off-by: Clayton Knittel <cknit1999@gmail.com>
1 parent 27c2427 commit ba1b2d4

68 files changed

Lines changed: 207 additions & 182 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

source/common/grpc/typed_async_client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ template <typename Response> class AsyncRequestCallbacks : public RawAsyncReques
7777

7878
private:
7979
void onSuccessRaw(Buffer::InstancePtr&& response, Tracing::Span& span) override {
80-
auto message = ResponsePtr<Response>(dynamic_cast<Response*>(
80+
auto message = ResponsePtr<Response>(Envoy::Protobuf::DynamicCastMessage<Response>(
8181
Internal::parseMessageUntyped(std::make_unique<Response>(), std::move(response))
8282
.release()));
8383
if (!message) {
@@ -98,7 +98,7 @@ template <typename Response> class AsyncStreamCallbacks : public RawAsyncStreamC
9898

9999
private:
100100
bool onReceiveMessageRaw(Buffer::InstancePtr&& response) override {
101-
auto message = ResponsePtr<Response>(dynamic_cast<Response*>(
101+
auto message = ResponsePtr<Response>(Envoy::Protobuf::DynamicCastMessage<Response>(
102102
Internal::parseMessageUntyped(std::make_unique<Response>(), std::move(response))
103103
.release()));
104104
if (!message) {

source/common/listener_manager/filter_chain_manager_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class FilterChainNameActionFactory : public Matcher::ActionFactory<FilterChainAc
5353
FilterChainActionFactoryContext&,
5454
ProtobufMessage::ValidationVisitor&) override {
5555
return std::make_shared<FilterChainNameAction>(
56-
dynamic_cast<const Protobuf::StringValue&>(config).value());
56+
Envoy::Protobuf::DynamicCastMessage<Protobuf::StringValue>(config).value());
5757
}
5858
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
5959
return std::make_unique<Protobuf::StringValue>();

source/common/listener_manager/lds_api.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ LdsApiImpl::onConfigUpdate(const std::vector<Config::DecodedResourceRef>& added_
9090

9191
TRY_ASSERT_MAIN_THREAD {
9292
const envoy::config::listener::v3::Listener& listener =
93-
dynamic_cast<const envoy::config::listener::v3::Listener&>(resource.get().resource());
93+
Envoy::Protobuf::DynamicCastMessage<envoy::config::listener::v3::Listener>(
94+
resource.get().resource());
9495
listener_name = listener.name();
9596
if (!listener_names.insert(listener.name()).second) {
9697
// NOTE: at this point, the first of these duplicates has already been successfully

source/common/matcher/actions/string_returning_action.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class StringReturningDirectActionFactory
6767
StringReturningActionFactoryContext&,
6868
ProtobufMessage::ValidationVisitor&) override {
6969
// validate function doesn't exist for StringValue, so just cast.
70-
const auto& config = dynamic_cast<const Protobuf::StringValue&>(proto_config);
70+
const auto& config = Envoy::Protobuf::DynamicCastMessage<Protobuf::StringValue>(proto_config);
7171
return std::make_shared<StringReturningDirectActionImpl>(config);
7272
}
7373
ProtobufTypes::MessagePtr createEmptyConfigProto() override {

source/common/rds/common/config_traits_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ConfigTraitsImpl : public ConfigTraits {
2222
ConfigConstSharedPtr createConfig(const Protobuf::Message& rc,
2323
Server::Configuration::ServerFactoryContext& context,
2424
bool validate_clusters_default) const override {
25-
ASSERT(dynamic_cast<const RouteConfiguration*>(&rc));
25+
ASSERT(Envoy::Protobuf::DynamicCastMessage<RouteConfiguration>(&rc));
2626
return std::make_shared<const ConfigImpl>(static_cast<const RouteConfiguration&>(rc), context,
2727
validate_clusters_default);
2828
}

source/common/router/route_config_update_receiver_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Rds::ConfigConstSharedPtr
4343
ConfigTraitsImpl::createConfig(const Protobuf::Message& rc,
4444
Server::Configuration::ServerFactoryContext& factory_context,
4545
bool validate_clusters_default) const {
46-
ASSERT(dynamic_cast<const envoy::config::route::v3::RouteConfiguration*>(&rc));
46+
ASSERT(Envoy::Protobuf::DynamicCastMessage<envoy::config::route::v3::RouteConfiguration>(&rc));
4747
return THROW_OR_RETURN_VALUE(
4848
ConfigImpl::create(static_cast<const envoy::config::route::v3::RouteConfiguration&>(rc),
4949
factory_context, validator_, validate_clusters_default),

source/common/router/route_config_update_receiver_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class RouteConfigUpdateReceiverImpl : public RouteConfigUpdateReceiver {
6666
return resource_ids_in_last_update_;
6767
}
6868
const envoy::config::route::v3::RouteConfiguration& protobufConfigurationCast() const override {
69-
ASSERT(dynamic_cast<const envoy::config::route::v3::RouteConfiguration*>(
69+
ASSERT(Envoy::Protobuf::DynamicCastMessage<envoy::config::route::v3::RouteConfiguration>(
7070
&RouteConfigUpdateReceiverImpl::protobufConfiguration()));
7171
return static_cast<const envoy::config::route::v3::RouteConfiguration&>(
7272
RouteConfigUpdateReceiverImpl::protobufConfiguration());

source/common/router/scoped_rds.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ absl::StatusOr<bool> ScopedRdsConfigSubscription::addOrUpdateScopes(
276276
for (const auto& resource : resources) {
277277
// Explicit copy so that we can std::move later.
278278
envoy::config::route::v3::ScopedRouteConfiguration scoped_route_config =
279-
dynamic_cast<const envoy::config::route::v3::ScopedRouteConfiguration&>(
279+
Envoy::Protobuf::DynamicCastMessage<envoy::config::route::v3::ScopedRouteConfiguration>(
280280
resource.get().resource());
281281
const std::string scope_name = scoped_route_config.name();
282282
if (const auto& scope_info_iter = scoped_route_map_.find(scope_name);
@@ -510,7 +510,7 @@ ScopedRdsConfigSubscription::detectUpdateConflictAndCleanupRemoved(
510510
}
511511
for (const auto& resource : resources) {
512512
const auto& scoped_route =
513-
dynamic_cast<const envoy::config::route::v3::ScopedRouteConfiguration&>(
513+
Envoy::Protobuf::DynamicCastMessage<envoy::config::route::v3::ScopedRouteConfiguration>(
514514
resource.get().resource());
515515
updated_or_removed_scopes.insert(scoped_route.name());
516516
}
@@ -526,7 +526,7 @@ ScopedRdsConfigSubscription::detectUpdateConflictAndCleanupRemoved(
526526
for (const auto& resource : resources) {
527527
// Throws (thus rejects all) on any error.
528528
const auto& scoped_route =
529-
dynamic_cast<const envoy::config::route::v3::ScopedRouteConfiguration&>(
529+
Envoy::Protobuf::DynamicCastMessage<envoy::config::route::v3::ScopedRouteConfiguration>(
530530
resource.get().resource());
531531
const std::string& scope_name = scoped_route.name();
532532
auto scope_config_inserted = scoped_routes.try_emplace(scope_name, std::move(scoped_route));
@@ -657,8 +657,8 @@ ConfigProviderPtr ScopedRoutesConfigProviderManager::createXdsConfigProvider(
657657
&typed_optarg](const uint64_t manager_identifier,
658658
ConfigProviderManagerImplBase& config_provider_manager)
659659
-> Envoy::Config::ConfigSubscriptionCommonBaseSharedPtr {
660-
const auto& scoped_rds_config_source = dynamic_cast<
661-
const envoy::extensions::filters::network::http_connection_manager::v3::ScopedRds&>(
660+
const auto& scoped_rds_config_source = Envoy::Protobuf::DynamicCastMessage<
661+
envoy::extensions::filters::network::http_connection_manager::v3::ScopedRds>(
662662
config_source_proto);
663663
return std::make_shared<ScopedRdsConfigSubscription>(
664664
scoped_rds_config_source, manager_identifier, typed_optarg.scoped_routes_name_,

source/common/router/vhds.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ absl::Status VhdsSubscription::onConfigUpdate(
119119
continue;
120120
}
121121
added_vhosts.emplace_back(
122-
dynamic_cast<const envoy::config::route::v3::VirtualHost&>(resource.get().resource()));
122+
Envoy::Protobuf::DynamicCastMessage<envoy::config::route::v3::VirtualHost>(
123+
resource.get().resource()));
123124
}
124125
if (config_update_info_->onVhdsUpdate(added_vhosts, std::move(added_resource_ids),
125126
removed_resources, version_info)) {

source/common/upstream/cds_api_helper.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ CdsApiHelper::onConfigUpdate(const std::vector<Config::DecodedResourceRef>& adde
4141
absl::string_view cluster_name = EMPTY_STRING;
4242
TRY_ASSERT_MAIN_THREAD {
4343
const envoy::config::cluster::v3::Cluster& cluster =
44-
dynamic_cast<const envoy::config::cluster::v3::Cluster&>(resource.get().resource());
44+
Envoy::Protobuf::DynamicCastMessage<envoy::config::cluster::v3::Cluster>(
45+
resource.get().resource());
4546
cluster_name = cluster.name();
4647
if (!cluster_names.insert(cluster.name()).second) {
4748
// NOTE: at this point, the first of these duplicates has already been successfully applied.

0 commit comments

Comments
 (0)