Add `ABSL_ATTRIBUTE_LIFETIME_BOUND` attribute on generated repeated and map field accessors.

This allows the compiler to statically detect use-after-free bugs.

This change touches a subset of field types. More changes to follow.

PiperOrigin-RevId: 564777204
pull/13814/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 3ae5a079fc
commit 9a0a3cf0cd
  1. 14
      src/google/protobuf/compiler/cpp/field_generators/enum_field.cc
  2. 10
      src/google/protobuf/compiler/cpp/field_generators/map_field.cc
  3. 16
      src/google/protobuf/compiler/cpp/field_generators/primitive_field.cc
  4. 36
      src/google/protobuf/descriptor.pb.h

@ -405,12 +405,16 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
// @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return static_cast<$Enum$>(_internal_$name$().Get(index));
}
)cc");
p->Emit(R"cc(
inline void $Msg$::set_$name$(int index, $Enum$ value) {
$assert_valid$;
_internal_mutable_$name$()->Set(index, value);
$annotate_set$
// @@protoc_insertion_point(field_set:$pkg.Msg.field$)
}
)cc");
p->Emit(R"cc(
inline void $Msg$::add_$name$($Enum$ value) {
$assert_valid$;
$TsanDetectConcurrentMutation$;
@ -418,12 +422,18 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
$annotate_add$
// @@protoc_insertion_point(field_add:$pkg.Msg.field$)
}
inline const $pb$::RepeatedField<int>& $Msg$::$name$() const {
)cc");
p->Emit(R"cc(
inline const $pb$::RepeatedField<int>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name$();
}
inline $pb$::RepeatedField<int>* $Msg$::mutable_$name$() {
)cc");
p->Emit(R"cc(
inline $pb$::RepeatedField<int>* $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;

@ -224,17 +224,23 @@ void Map::GenerateInlineAccessorDefinitions(io::Printer* p) const {
$TsanDetectConcurrentRead$;
return $field_$.GetMap();
}
inline const $Map$& $Msg$::$name$() const {
)cc");
p->Emit(R"cc(
inline const $Map$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_get$;
// @@protoc_insertion_point(field_map:$pkg.Msg.field$)
return _internal_$name$();
}
)cc");
p->Emit(R"cc(
inline $Map$* $Msg$::_internal_mutable_$name$() {
$PrepareSplitMessageForWrite$;
$TsanDetectConcurrentMutation$;
return $field_$.MutableMap();
}
inline $Map$* $Msg$::mutable_$name$() {
)cc");
p->Emit(R"cc(
inline $Map$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_mutable$;
// @@protoc_insertion_point(field_mutable_map:$pkg.Msg.field$)
return _internal_mutable_$name$();

@ -474,30 +474,40 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions(
// @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name$().Get(index);
}
)cc");
p->Emit(R"cc(
inline void $Msg$::set_$name$(int index, $Type$ value) {
$annotate_set$;
_internal_mutable_$name$()->Set(index, value);
// @@protoc_insertion_point(field_set:$pkg.Msg.field$)
}
)cc");
p->Emit(R"cc(
inline void $Msg$::add_$name$($Type$ value) {
$TsanDetectConcurrentMutation$;
_internal_mutable_$name$()->Add(value);
$annotate_add$;
// @@protoc_insertion_point(field_add:$pkg.Msg.field$)
}
inline const $pb$::RepeatedField<$Type$>& $Msg$::$name$() const {
)cc");
p->Emit(R"cc(
inline const $pb$::RepeatedField<$Type$>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name$();
}
inline $pb$::RepeatedField<$Type$>* $Msg$::mutable_$name$() {
)cc");
p->Emit(R"cc(
inline $pb$::RepeatedField<$Type$>* $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;
return _internal_mutable_$name$();
}
)cc");
if (should_split()) {
p->Emit(R"cc(
inline const $pb$::RepeatedField<$Type$>& $Msg$::_internal_$name$()

@ -10935,11 +10935,13 @@ inline void FileDescriptorProto::add_public_dependency(::int32_t value) {
_internal_mutable_public_dependency()->Add(value);
// @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.public_dependency)
}
inline const ::google::protobuf::RepeatedField<::int32_t>& FileDescriptorProto::public_dependency() const {
inline const ::google::protobuf::RepeatedField<::int32_t>& FileDescriptorProto::public_dependency() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.public_dependency)
return _internal_public_dependency();
}
inline ::google::protobuf::RepeatedField<::int32_t>* FileDescriptorProto::mutable_public_dependency() {
inline ::google::protobuf::RepeatedField<::int32_t>* FileDescriptorProto::mutable_public_dependency()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.public_dependency)
PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race);
return _internal_mutable_public_dependency();
@ -10978,11 +10980,13 @@ inline void FileDescriptorProto::add_weak_dependency(::int32_t value) {
_internal_mutable_weak_dependency()->Add(value);
// @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.weak_dependency)
}
inline const ::google::protobuf::RepeatedField<::int32_t>& FileDescriptorProto::weak_dependency() const {
inline const ::google::protobuf::RepeatedField<::int32_t>& FileDescriptorProto::weak_dependency() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.weak_dependency)
return _internal_weak_dependency();
}
inline ::google::protobuf::RepeatedField<::int32_t>* FileDescriptorProto::mutable_weak_dependency() {
inline ::google::protobuf::RepeatedField<::int32_t>* FileDescriptorProto::mutable_weak_dependency()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.weak_dependency)
PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race);
return _internal_mutable_weak_dependency();
@ -16732,11 +16736,13 @@ inline void FieldOptions::add_targets(::google::protobuf::FieldOptions_OptionTar
_internal_mutable_targets()->Add(value);
// @@protoc_insertion_point(field_add:google.protobuf.FieldOptions.targets)
}
inline const ::google::protobuf::RepeatedField<int>& FieldOptions::targets() const {
inline const ::google::protobuf::RepeatedField<int>& FieldOptions::targets() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_list:google.protobuf.FieldOptions.targets)
return _internal_targets();
}
inline ::google::protobuf::RepeatedField<int>* FieldOptions::mutable_targets() {
inline ::google::protobuf::RepeatedField<int>* FieldOptions::mutable_targets()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_mutable_list:google.protobuf.FieldOptions.targets)
PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race);
return _internal_mutable_targets();
@ -19004,11 +19010,13 @@ inline void SourceCodeInfo_Location::add_path(::int32_t value) {
_internal_mutable_path()->Add(value);
// @@protoc_insertion_point(field_add:google.protobuf.SourceCodeInfo.Location.path)
}
inline const ::google::protobuf::RepeatedField<::int32_t>& SourceCodeInfo_Location::path() const {
inline const ::google::protobuf::RepeatedField<::int32_t>& SourceCodeInfo_Location::path() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_list:google.protobuf.SourceCodeInfo.Location.path)
return _internal_path();
}
inline ::google::protobuf::RepeatedField<::int32_t>* SourceCodeInfo_Location::mutable_path() {
inline ::google::protobuf::RepeatedField<::int32_t>* SourceCodeInfo_Location::mutable_path()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_mutable_list:google.protobuf.SourceCodeInfo.Location.path)
PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race);
return _internal_mutable_path();
@ -19047,11 +19055,13 @@ inline void SourceCodeInfo_Location::add_span(::int32_t value) {
_internal_mutable_span()->Add(value);
// @@protoc_insertion_point(field_add:google.protobuf.SourceCodeInfo.Location.span)
}
inline const ::google::protobuf::RepeatedField<::int32_t>& SourceCodeInfo_Location::span() const {
inline const ::google::protobuf::RepeatedField<::int32_t>& SourceCodeInfo_Location::span() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_list:google.protobuf.SourceCodeInfo.Location.span)
return _internal_span();
}
inline ::google::protobuf::RepeatedField<::int32_t>* SourceCodeInfo_Location::mutable_span() {
inline ::google::protobuf::RepeatedField<::int32_t>* SourceCodeInfo_Location::mutable_span()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_mutable_list:google.protobuf.SourceCodeInfo.Location.span)
PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race);
return _internal_mutable_span();
@ -19390,11 +19400,13 @@ inline void GeneratedCodeInfo_Annotation::add_path(::int32_t value) {
_internal_mutable_path()->Add(value);
// @@protoc_insertion_point(field_add:google.protobuf.GeneratedCodeInfo.Annotation.path)
}
inline const ::google::protobuf::RepeatedField<::int32_t>& GeneratedCodeInfo_Annotation::path() const {
inline const ::google::protobuf::RepeatedField<::int32_t>& GeneratedCodeInfo_Annotation::path() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_list:google.protobuf.GeneratedCodeInfo.Annotation.path)
return _internal_path();
}
inline ::google::protobuf::RepeatedField<::int32_t>* GeneratedCodeInfo_Annotation::mutable_path() {
inline ::google::protobuf::RepeatedField<::int32_t>* GeneratedCodeInfo_Annotation::mutable_path()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// @@protoc_insertion_point(field_mutable_list:google.protobuf.GeneratedCodeInfo.Annotation.path)
PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race);
return _internal_mutable_path();

Loading…
Cancel
Save