diff --git a/src/google/protobuf/compiler/cpp/field.cc b/src/google/protobuf/compiler/cpp/field.cc index 8c357cfbaf..265f379049 100644 --- a/src/google/protobuf/compiler/cpp/field.cc +++ b/src/google/protobuf/compiler/cpp/field.cc @@ -78,6 +78,12 @@ std::vector FieldVars(const FieldDescriptor* field, const Options& opts) { {"ns", Namespace(field, opts)}, {"tag_size", WireFormat::TagSize(field->number(), field->type())}, {"deprecated_attr", DeprecatedAttribute(opts, field)}, + Sub("WeakDescriptorSelfPin", + UsingImplicitWeakDescriptor(field->file(), opts) + ? absl::StrCat("::", ProtobufNamespace(opts), + "::internal::StrongReference(default_instance());") + : "") + .WithSuffix(";"), }; if (const auto* oneof = field->containing_oneof()) { diff --git a/src/google/protobuf/compiler/cpp/field_generators/cord_field.cc b/src/google/protobuf/compiler/cpp/field_generators/cord_field.cc index 8924bef438..dfb7418c05 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/cord_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/cord_field.cc @@ -181,6 +181,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions( printer->Emit(R"cc( inline const ::absl::Cord& $classname$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$full_name$) return _internal_$name_internal$(); @@ -195,6 +196,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions( )cc"); printer->Emit(R"cc( inline void $classname$::set_$name$(const ::absl::Cord& value) { + $WeakDescriptorSelfPin$; $PrepareSplitMessageForWrite$; _internal_set_$name_internal$(value); $annotate_set$; @@ -203,6 +205,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions( )cc"); printer->Emit(R"cc( inline void $classname$::set_$name$(::absl::string_view value) { + $WeakDescriptorSelfPin$; $PrepareSplitMessageForWrite$; $set_hasbit$; $field$ = value; @@ -345,6 +348,7 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions( printer->Emit(R"cc( inline const ::absl::Cord& $classname$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$full_name$) return _internal_$name_internal$(); @@ -352,6 +356,7 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions( )cc"); printer->Emit(R"cc( inline void $classname$::set_$name$(const ::absl::Cord& value) { + $WeakDescriptorSelfPin$; if ($not_has_field$) { clear_$oneof_name$(); set_has_$name_internal$(); @@ -368,6 +373,7 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions( )cc"); printer->Emit(R"cc( inline void $classname$::set_$name$(::absl::string_view value) { + $WeakDescriptorSelfPin$; if ($not_has_field$) { clear_$oneof_name$(); set_has_$name_internal$(); diff --git a/src/google/protobuf/compiler/cpp/field_generators/enum_field.cc b/src/google/protobuf/compiler/cpp/field_generators/enum_field.cc index 8f3a7f1d56..94419909fd 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/enum_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/enum_field.cc @@ -165,6 +165,7 @@ void SingularEnum::GenerateAccessorDeclarations(io::Printer* p) const { void SingularEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { p->Emit(R"cc( inline $Enum$ $Msg$::$name$() const { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) return _internal_$name_internal$(); @@ -174,6 +175,7 @@ void SingularEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { if (is_oneof()) { p->Emit(R"cc( inline void $Msg$::set_$name$($Enum$ value) { + $WeakDescriptorSelfPin$; $PrepareSplitMessageForWrite$; $assert_valid$; if ($not_has_field$) { @@ -194,6 +196,7 @@ void SingularEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { } else { p->Emit(R"cc( inline void $Msg$::set_$name$($Enum$ value) { + $WeakDescriptorSelfPin$; $PrepareSplitMessageForWrite$; _internal_set_$name_internal$(value); $set_hasbit$; @@ -398,6 +401,7 @@ void RepeatedEnum::GenerateAccessorDeclarations(io::Printer* p) const { void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { p->Emit(R"cc( inline $Enum$ $Msg$::$name$(int index) const { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) return static_cast<$Enum$>(_internal_$name_internal$().Get(index)); @@ -405,6 +409,7 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline void $Msg$::set_$name$(int index, $Enum$ value) { + $WeakDescriptorSelfPin$; $assert_valid$; _internal_mutable_$name_internal$()->Set(index, value); $annotate_set$ @@ -413,6 +418,7 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline void $Msg$::add_$name$($Enum$ value) { + $WeakDescriptorSelfPin$; $assert_valid$; $TsanDetectConcurrentMutation$; _internal_mutable_$name_internal$()->Add(value); @@ -423,6 +429,7 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { p->Emit(R"cc( inline const $pb$::RepeatedField& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_list$; // @@protoc_insertion_point(field_list:$pkg.Msg.field$) return _internal_$name_internal$(); @@ -431,6 +438,7 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { p->Emit(R"cc( inline $pb$::RepeatedField* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_mutable_list$; // @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$) $TsanDetectConcurrentMutation$; diff --git a/src/google/protobuf/compiler/cpp/field_generators/map_field.cc b/src/google/protobuf/compiler/cpp/field_generators/map_field.cc index 79690963a0..70f74386b1 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/map_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/map_field.cc @@ -222,6 +222,7 @@ void Map::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline const $Map$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_map:$pkg.Msg.field$) return _internal_$name_internal$(); @@ -236,6 +237,7 @@ void Map::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline $Map$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_mutable$; // @@protoc_insertion_point(field_mutable_map:$pkg.Msg.field$) return _internal_mutable_$name_internal$(); diff --git a/src/google/protobuf/compiler/cpp/field_generators/message_field.cc b/src/google/protobuf/compiler/cpp/field_generators/message_field.cc index 318cf4c598..43517cf2c8 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/message_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/message_field.cc @@ -205,11 +205,13 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { return p != nullptr ? *p : reinterpret_cast($kDefault$); } inline const $Submsg$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) return _internal_$name_internal$(); } inline void $Msg$::unsafe_arena_set_allocated_$name$($Submsg$* value) { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; $PrepareSplitMessageForWrite$; //~ If we're not on an arena, free whatever we were holding before. @@ -223,6 +225,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { // @@protoc_insertion_point(field_unsafe_arena_set_allocated:$pkg.Msg.field$) } inline $Submsg$* $Msg$::$release_name$() { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; $StrongRef$; $annotate_release$; @@ -245,6 +248,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { return released; } inline $Submsg$* $Msg$::unsafe_arena_release_$name$() { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; $annotate_release$; // @@protoc_insertion_point(field_release:$pkg.Msg.field$) @@ -268,6 +272,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { inline $Submsg$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { //~ TODO: add tests to make sure all write accessors are //~ able to prepare split message allocation. + $WeakDescriptorSelfPin$; $PrepareSplitMessageForWrite$; $set_hasbit$; $Submsg$* _msg = _internal_mutable_$name_internal$(); @@ -278,6 +283,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { //~ We handle the most common case inline, and delegate less common //~ cases to the slow fallback function. inline void $Msg$::set_allocated_$name$($Submsg$* value) { + $WeakDescriptorSelfPin$; $pb$::Arena* message_arena = GetArena(); $TsanDetectConcurrentMutation$; $PrepareSplitMessageForWrite$; @@ -522,6 +528,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { p->Emit(R"cc( inline $Submsg$* $Msg$::$release_name$() { + $WeakDescriptorSelfPin$; $annotate_release$; // @@protoc_insertion_point(field_release:$pkg.Msg.field$) $StrongRef$; @@ -546,6 +553,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline const $Submsg$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) return _internal_$name_internal$(); @@ -553,6 +561,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline $Submsg$* $Msg$::unsafe_arena_release_$name$() { + $WeakDescriptorSelfPin$; $annotate_release$; // @@protoc_insertion_point(field_unsafe_arena_release:$pkg.Msg.field$) $StrongRef$; @@ -568,6 +577,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline void $Msg$::unsafe_arena_set_allocated_$name$($Submsg$* value) { + $WeakDescriptorSelfPin$; // We rely on the oneof clear method to free the earlier contents // of this oneof. We can directly use the pointer we're given to // set the new value. @@ -594,6 +604,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline $Submsg$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $Submsg$* _msg = _internal_mutable_$name_internal$(); $annotate_mutable$; // @@protoc_insertion_point(field_mutable:$pkg.Msg.field$) @@ -742,6 +753,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { p->Emit(R"cc( inline $Submsg$* $Msg$::mutable_$name$(int index) ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_mutable$; // @@protoc_insertion_point(field_mutable:$pkg.Msg.field$) $StrongRef$; @@ -751,6 +763,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { p->Emit(R"cc( inline $pb$::RepeatedPtrField<$Submsg$>* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_mutable_list$; // @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$) $StrongRef$; @@ -771,6 +784,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { R"cc( inline const $Submsg$& $Msg$::$name$(int index) const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) $StrongRef$; @@ -779,6 +793,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { )cc"); p->Emit(R"cc( inline $Submsg$* $Msg$::add_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; $Submsg$* _add = _internal_mutable_$name_internal$()->Add(); $annotate_add_mutable$; @@ -789,6 +804,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { p->Emit(R"cc( inline const $pb$::RepeatedPtrField<$Submsg$>& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_list$; // @@protoc_insertion_point(field_list:$pkg.Msg.field$) $StrongRef$; diff --git a/src/google/protobuf/compiler/cpp/field_generators/primitive_field.cc b/src/google/protobuf/compiler/cpp/field_generators/primitive_field.cc index e0e1ff3ca4..44dff20400 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/primitive_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/primitive_field.cc @@ -188,6 +188,7 @@ void SingularPrimitive::GenerateInlineAccessorDefinitions( io::Printer* p) const { p->Emit(R"cc( inline $Type$ $Msg$::$name$() const { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) return _internal_$name_internal$(); @@ -197,6 +198,7 @@ void SingularPrimitive::GenerateInlineAccessorDefinitions( if (is_oneof()) { p->Emit(R"cc( inline void $Msg$::set_$name$($Type$ value) { + $WeakDescriptorSelfPin$; $PrepareSplitMessageForWrite$; if ($not_has_field$) { clear_$oneof_name$(); @@ -216,6 +218,7 @@ void SingularPrimitive::GenerateInlineAccessorDefinitions( } else { p->Emit(R"cc( inline void $Msg$::set_$name$($Type$ value) { + $WeakDescriptorSelfPin$; $PrepareSplitMessageForWrite$; _internal_set_$name_internal$(value); $set_hasbit$; @@ -467,6 +470,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions( io::Printer* p) const { p->Emit(R"cc( inline $Type$ $Msg$::$name$(int index) const { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) return _internal_$name_internal$().Get(index); @@ -474,6 +478,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions( )cc"); p->Emit(R"cc( inline void $Msg$::set_$name$(int index, $Type$ value) { + $WeakDescriptorSelfPin$; $annotate_set$; _internal_mutable_$name_internal$()->Set(index, value); // @@protoc_insertion_point(field_set:$pkg.Msg.field$) @@ -481,6 +486,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions( )cc"); p->Emit(R"cc( inline void $Msg$::add_$name$($Type$ value) { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; _internal_mutable_$name_internal$()->Add(value); $annotate_add$; @@ -490,6 +496,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions( p->Emit(R"cc( inline const $pb$::RepeatedField<$Type$>& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_list$; // @@protoc_insertion_point(field_list:$pkg.Msg.field$) return _internal_$name_internal$(); @@ -498,6 +505,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions( p->Emit(R"cc( inline $pb$::RepeatedField<$Type$>* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_mutable_list$; // @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$) $TsanDetectConcurrentMutation$; diff --git a/src/google/protobuf/compiler/cpp/field_generators/string_field.cc b/src/google/protobuf/compiler/cpp/field_generators/string_field.cc index cd1049eea2..dfd84252bf 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/string_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/string_field.cc @@ -448,6 +448,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const { R"cc( inline const std::string& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) $if_IsDefault$; @@ -456,6 +457,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const { template inline PROTOBUF_ALWAYS_INLINE void $Msg$::set_$name$(Arg_&& arg, Args_... args) { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; $PrepareSplitMessageForWrite$; $update_hasbit$; @@ -464,6 +466,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const { // @@protoc_insertion_point(field_set:$pkg.Msg.field$) } inline std::string* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $PrepareSplitMessageForWrite$; std::string* _s = _internal_mutable_$name_internal$(); $annotate_mutable$; @@ -488,6 +491,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const { return $field_$.Mutable($lazy_args$, $set_args$); } inline std::string* $Msg$::$release_name$() { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; $annotate_release$; $PrepareSplitMessageForWrite$; @@ -495,6 +499,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const { $release_impl$; } inline void $Msg$::set_allocated_$name$(std::string* value) { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; $PrepareSplitMessageForWrite$; $set_allocated_impl$; @@ -870,6 +875,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const { R"cc( inline std::string* $Msg$::add_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; std::string* _s = _internal_mutable_$name_internal$()->Add(); $annotate_add_mutable$; @@ -878,27 +884,32 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const { } inline const std::string& $Msg$::$name$(int index) const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) return _internal_$name_internal$().$Get$(index$GetExtraArg$); } inline std::string* $Msg$::mutable_$name$(int index) ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_mutable$; // @@protoc_insertion_point(field_mutable:$pkg.Msg.field$) return _internal_mutable_$name_internal$()->Mutable(index); } inline void $Msg$::set_$name$(int index, const std::string& value) { + $WeakDescriptorSelfPin$; _internal_mutable_$name_internal$()->Mutable(index)->assign(value); $annotate_set$; // @@protoc_insertion_point(field_set:$pkg.Msg.field$) } inline void $Msg$::set_$name$(int index, std::string&& value) { + $WeakDescriptorSelfPin$; _internal_mutable_$name_internal$()->Mutable(index)->assign(std::move(value)); $annotate_set$; // @@protoc_insertion_point(field_set:$pkg.Msg.field$) } inline void $Msg$::set_$name$(int index, const char* value) { + $WeakDescriptorSelfPin$; $DCHK$(value != nullptr); _internal_mutable_$name_internal$()->Mutable(index)->assign(value); $annotate_set$; @@ -906,30 +917,35 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const { } inline void $Msg$::set_$name$(int index, const $byte$* value, std::size_t size) { + $WeakDescriptorSelfPin$; _internal_mutable_$name_internal$()->Mutable(index)->assign( reinterpret_cast(value), size); $annotate_set$; // @@protoc_insertion_point(field_set_pointer:$pkg.Msg.field$) } inline void $Msg$::set_$name$(int index, absl::string_view value) { + $WeakDescriptorSelfPin$; _internal_mutable_$name_internal$()->Mutable(index)->assign( value.data(), value.size()); $annotate_set$; // @@protoc_insertion_point(field_set_string_piece:$pkg.Msg.field$) } inline void $Msg$::add_$name$(const std::string& value) { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; _internal_mutable_$name_internal$()->Add()->assign(value); $annotate_add$; // @@protoc_insertion_point(field_add:$pkg.Msg.field$) } inline void $Msg$::add_$name$(std::string&& value) { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; _internal_mutable_$name_internal$()->Add(std::move(value)); $annotate_add$; // @@protoc_insertion_point(field_add:$pkg.Msg.field$) } inline void $Msg$::add_$name$(const char* value) { + $WeakDescriptorSelfPin$; $DCHK$(value != nullptr); $TsanDetectConcurrentMutation$; _internal_mutable_$name_internal$()->Add()->assign(value); @@ -937,6 +953,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const { // @@protoc_insertion_point(field_add_char:$pkg.Msg.field$) } inline void $Msg$::add_$name$(const $byte$* value, std::size_t size) { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; _internal_mutable_$name_internal$()->Add()->assign( reinterpret_cast(value), size); @@ -944,6 +961,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const { // @@protoc_insertion_point(field_add_pointer:$pkg.Msg.field$) } inline void $Msg$::add_$name$(absl::string_view value) { + $WeakDescriptorSelfPin$; $TsanDetectConcurrentMutation$; _internal_mutable_$name_internal$()->Add()->assign(value.data(), value.size()); @@ -952,12 +970,14 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const { } inline const ::$proto_ns$::RepeatedPtrField& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_list$; // @@protoc_insertion_point(field_list:$pkg.Msg.field$) return _internal_$name_internal$(); } inline ::$proto_ns$::RepeatedPtrField* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_mutable_list$; // @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$) $TsanDetectConcurrentMutation$; diff --git a/src/google/protobuf/compiler/cpp/helpers.h b/src/google/protobuf/compiler/cpp/helpers.h index 64e2a82aa4..86aefb3d20 100644 --- a/src/google/protobuf/compiler/cpp/helpers.h +++ b/src/google/protobuf/compiler/cpp/helpers.h @@ -802,7 +802,10 @@ bool IsImplicitWeakField(const FieldDescriptor* field, const Options& options, inline std::string SimpleBaseClass(const Descriptor* desc, const Options& options) { + // The only base class we have derived from `Message`. if (!HasDescriptorMethods(desc->file(), options)) return ""; + // We don't use the base class to be able to inject the weak descriptor pins. + if (UsingImplicitWeakDescriptor(desc->file(), options)) return ""; if (desc->extension_range_count() != 0) return ""; // Don't use a simple base class if the field tracking is enabled. This // ensures generating all methods to track. diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index cbd5aa9d5e..dd173b7bf7 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -472,24 +472,35 @@ bool MaybeEmitHaswordsCheck(ChunkIterator it, ChunkIterator end, return true; } -absl::flat_hash_map ClassVars( - const Descriptor* desc, Options opts) { - absl::flat_hash_map vars = MessageVars(desc); - - vars.emplace("pkg", Namespace(desc, opts)); - vars.emplace("Msg", ClassName(desc, false)); - vars.emplace("pkg::Msg", QualifiedClassName(desc, opts)); - vars.emplace("pkg.Msg", desc->full_name()); - - // Old-style names, to be removed once all usages are gone in this and other - // files. - vars.emplace("classname", ClassName(desc, false)); - vars.emplace("classtype", QualifiedClassName(desc, opts)); - vars.emplace("full_name", desc->full_name()); - vars.emplace("superclass", SuperClassName(desc, opts)); +using Sub = ::google::protobuf::io::Printer::Sub; +std::vector ClassVars(const Descriptor* desc, Options opts) { + std::vector vars = { + {"pkg", Namespace(desc, opts)}, + {"Msg", ClassName(desc, false)}, + {"pkg::Msg", QualifiedClassName(desc, opts)}, + {"pkg.Msg", desc->full_name()}, + + // Old-style names, to be removed once all usages are gone in this and + // other files. + {"classname", ClassName(desc, false)}, + {"classtype", QualifiedClassName(desc, opts)}, + {"full_name", desc->full_name()}, + {"superclass", SuperClassName(desc, opts)}, + + Sub("WeakDescriptorSelfPin", + UsingImplicitWeakDescriptor(desc->file(), opts) + ? absl::StrCat("::", ProtobufNamespace(opts), + "::internal::StrongReference(default_instance());") + : "") + .WithSuffix(";"), + }; + + for (auto& pair : MessageVars(desc)) { + vars.push_back({std::string(pair.first), pair.second}); + } for (auto& pair : UnknownFieldsVars(desc, opts)) { - vars.emplace(pair); + vars.push_back({std::string(pair.first), pair.second}); } return vars; @@ -747,6 +758,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { inline bool HasExtension( const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) const { + $WeakDescriptorSelfPin$; $annotate_extension_has$; return $extensions$.Has(id.number()); } @@ -756,6 +768,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { inline void ClearExtension( const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) { + $WeakDescriptorSelfPin$; $extensions$.ClearExtension(id.number()); $annotate_extension_clear$; } @@ -765,6 +778,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { inline int ExtensionSize( const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) const { + $WeakDescriptorSelfPin$; $annotate_extension_repeated_size$; return $extensions$.ExtensionSize(id.number()); } @@ -775,6 +789,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { inline typename _proto_TypeTraits::Singular::ConstType GetExtension( const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) const { + $WeakDescriptorSelfPin$; $annotate_extension_get$; return _proto_TypeTraits::Get(id.number(), $extensions$, id.default_value()); } @@ -786,6 +801,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_extension_get$; return _proto_TypeTraits::Get(id.number(), $extensions$, id.default_value()); } @@ -796,6 +812,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_extension_mutable$; return _proto_TypeTraits::Mutable(id.number(), _field_type, &$extensions$); } @@ -806,6 +823,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id, typename _proto_TypeTraits::Singular::ConstType value) { + $WeakDescriptorSelfPin$; _proto_TypeTraits::Set(id.number(), _field_type, value, &$extensions$); $annotate_extension_set$; } @@ -816,6 +834,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id, typename _proto_TypeTraits::Singular::MutableType value) { + $WeakDescriptorSelfPin$; _proto_TypeTraits::SetAllocated(id.number(), _field_type, value, &$extensions$); $annotate_extension_set$; @@ -826,6 +845,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id, typename _proto_TypeTraits::Singular::MutableType value) { + $WeakDescriptorSelfPin$; _proto_TypeTraits::UnsafeArenaSetAllocated(id.number(), _field_type, value, &$extensions$); $annotate_extension_set$; @@ -837,6 +857,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { ReleaseExtension( const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) { + $WeakDescriptorSelfPin$; $annotate_extension_release$; return _proto_TypeTraits::Release(id.number(), _field_type, &$extensions$); } @@ -846,6 +867,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { UnsafeArenaReleaseExtension( const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) { + $WeakDescriptorSelfPin$; $annotate_extension_release$; return _proto_TypeTraits::UnsafeArenaRelease(id.number(), _field_type, &$extensions$); @@ -858,6 +880,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id, int index) const { + $WeakDescriptorSelfPin$; $annotate_repeated_extension_get$; return _proto_TypeTraits::Get(id.number(), $extensions$, index); } @@ -869,6 +892,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id, int index) const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_repeated_extension_get$; return _proto_TypeTraits::Get(id.number(), $extensions$, index); } @@ -879,6 +903,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id, int index) ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_repeated_extension_mutable$; return _proto_TypeTraits::Mutable(id.number(), index, &$extensions$); } @@ -889,6 +914,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id, int index, typename _proto_TypeTraits::Repeated::ConstType value) { + $WeakDescriptorSelfPin$; _proto_TypeTraits::Set(id.number(), index, value, &$extensions$); $annotate_repeated_extension_set$; } @@ -899,6 +925,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; typename _proto_TypeTraits::Repeated::MutableType to_add = _proto_TypeTraits::Add(id.number(), _field_type, &$extensions$); $annotate_repeated_extension_add_mutable$; @@ -911,6 +938,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id, typename _proto_TypeTraits::Repeated::ConstType value) { + $WeakDescriptorSelfPin$; _proto_TypeTraits::Add(id.number(), _field_type, _is_packed, value, &$extensions$); $annotate_repeated_extension_add$; @@ -923,6 +951,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) const ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_repeated_extension_list$; return _proto_TypeTraits::GetRepeated(id.number(), $extensions$); } @@ -934,6 +963,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, _field_type, _is_packed>& id) ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; $annotate_repeated_extension_list_mutable$; return _proto_TypeTraits::MutableRepeated(id.number(), _field_type, _is_packed, &$extensions$); @@ -971,6 +1001,7 @@ void MessageGenerator::GenerateSingularFieldHasBits( p->Emit( R"cc( inline bool $classname$::has_$name$() const { + $WeakDescriptorSelfPin$; $annotate_has$; return $weak_field_map$.Has($number$); } @@ -995,6 +1026,7 @@ void MessageGenerator::GenerateSingularFieldHasBits( .WithSuffix(";")}, R"cc( inline bool $classname$::has_$name$() const { + $WeakDescriptorSelfPin$; $annotate_has$; bool value = ($has_bits$[$has_array_index$] & $has_mask$) != 0; $ASSUME$; @@ -1054,6 +1086,7 @@ void MessageGenerator::GenerateOneofMemberHasBits(const FieldDescriptor* field, auto t = p->WithVars(MakeTrackerCalls(field, options_)); p->Emit(R"cc( inline bool $classname$::has_$name$() const { + $WeakDescriptorSelfPin$; $annotate_has$; return $has_field$; } @@ -1115,6 +1148,7 @@ void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field, R"cc( $inline $void $classname$::clear_$name$() { PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + $WeakDescriptorSelfPin$; $body$; $annotate_clear$; } @@ -1127,9 +1161,10 @@ class AccessorVerifier { public: using SourceLocation = io::Printer::SourceLocation; - AccessorVerifier(const FieldDescriptor* field) : field_(field) {} + explicit AccessorVerifier(const FieldDescriptor* field) : field_(field) {} ~AccessorVerifier() { ABSL_CHECK(!needs_annotate_) << Error(SourceLocation::current()); + ABSL_CHECK(!needs_weak_descriptor_pin_) << Error(SourceLocation::current()); } void operator()(absl::string_view label, io::Printer::SourceLocation loc) { @@ -1137,14 +1172,17 @@ class AccessorVerifier { // All accessors use $name$ or $release_name$ when constructing the // function name. We hook into those to determine that an accessor is // starting. - ABSL_CHECK(!needs_annotate_) << Error(loc); + SetTracker(needs_annotate_, true, loc); + SetTracker(needs_weak_descriptor_pin_, true, loc); loc_ = loc; - needs_annotate_ = true; } else if (absl::StartsWith(label, "annotate")) { // All annotation labels start with `annotate`. Eg `annotate_get`. - ABSL_CHECK(needs_annotate_) << Error(loc); + SetTracker(needs_annotate_, false, loc); + loc_ = loc; + } else if (label == "WeakDescriptorSelfPin") { + // The self pin for weak descriptor types must be on every accessor. + SetTracker(needs_weak_descriptor_pin_, false, loc); loc_ = loc; - needs_annotate_ = false; } } @@ -1155,7 +1193,13 @@ class AccessorVerifier { loc_.file_name(), loc_.line()); } + void SetTracker(bool& v, bool new_value, SourceLocation loc) { + ABSL_CHECK_NE(v, new_value) << Error(loc); + v = new_value; + } + bool needs_annotate_ = false; + bool needs_weak_descriptor_pin_ = false; // We keep these fields for error reporting. const FieldDescriptor* field_; // On error, we report two locations: the current one and the last one. This @@ -1194,6 +1238,7 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) { return _internal_$name_internal$().size(); } inline int $classname$::$name$_size() const { + $WeakDescriptorSelfPin$; $annotate_size$; return _internal_$name_internal$_size(); } @@ -2712,6 +2757,7 @@ void MessageGenerator::GenerateSharedDestructorCode(io::Printer* p) { R"cc( inline void $classname$::SharedDtor() { $DCHK$(GetArena() == nullptr); + $WeakDescriptorSelfPin$; $field_dtors$; $split_field_dtors$; $oneof_field_dtors$; @@ -3453,6 +3499,7 @@ void MessageGenerator::GenerateSwap(io::Printer* p) { "{\n"); format.Indent(); format("using std::swap;\n"); + format("$WeakDescriptorSelfPin$"); if (HasGeneratedMethods(descriptor_->file(), options_)) { if (descriptor_->extension_range_count() > 0) { @@ -3657,6 +3704,7 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* p) { format( "void $classname$::MergeImpl(::$proto_ns$::MessageLite& to_msg, const " "::$proto_ns$::MessageLite& from_msg) {\n" + "$WeakDescriptorSelfPin$" " auto* const _this = static_cast<$classname$*>(&to_msg);\n" " auto& from = static_cast(from_msg);\n"); } @@ -4421,6 +4469,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) { p->Emit( R"cc( PROTOBUF_NOINLINE ::size_t $classname$::ByteSizeLong() const { + $WeakDescriptorSelfPin$; $annotate_bytesize$; // @@protoc_insertion_point(message_set_byte_size_start:$full_name$) ::size_t total_size = $extensions$.MessageSetByteSize(); @@ -4437,6 +4486,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) { Formatter format(p); format( "::size_t $classname$::ByteSizeLong() const {\n" + "$WeakDescriptorSelfPin$" "$annotate_bytesize$" "// @@protoc_insertion_point(message_byte_size_start:$full_name$)\n"); format.Indent(); @@ -4689,6 +4739,7 @@ void MessageGenerator::GenerateIsInitialized(io::Printer* p) { }, R"cc( PROTOBUF_NOINLINE bool $classname$::IsInitialized() const { + $WeakDescriptorSelfPin$; $test_extensions$; $test_required_fields$; $test_ordinary_fields$;