Skip to content

Commit

Permalink
In TSan mode, detect concurrent mutations of repeated/map fields.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 538573264
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jun 7, 2023
1 parent 87a601e commit d800c5f
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/google/protobuf/compiler/cpp/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ std::vector<Sub> FieldVars(const FieldDescriptor* field, const Options& opts) {
{"{", ""},
{"}", ""},

// For TSan validation.
{"TsanDetectConcurrentMutation",
"PROTOBUF_TSAN_WRITE(&_internal_metadata_)"},
{"TsanDetectConcurrentRead", "PROTOBUF_TSAN_READ(&_internal_metadata_)"},

// Old-style names.
{"field", FieldMemberName(field, split)},
{"declared_type", DeclaredTypeMethodName(field->type())},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
return _internal_mutable_$name$();
}
inline const $pb$::RepeatedField<int>& $Msg$::_internal_$name$() const {
$TsanDetectConcurrentRead$;
return $field_$;
}
inline $pb$::RepeatedField<int>* $Msg$::_internal_mutable_$name$() {
$TsanDetectConcurrentMutation$;
return &$field_$;
}
)cc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ void Map::GenerateAccessorDeclarations(io::Printer* p) const {
void Map::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc(
inline const $Map$& $Msg$::_internal_$name$() const {
$TsanDetectConcurrentRead$;
return $field_$.GetMap();
}
inline const $Map$& $Msg$::$name$() const {
Expand All @@ -227,6 +228,7 @@ void Map::GenerateInlineAccessorDefinitions(io::Printer* p) const {
}
inline $Map$* $Msg$::_internal_mutable_$name$() {
$PrepareSplitMessageForWrite$;
$TsanDetectConcurrentMutation$;
return $field_$.MutableMap();
}
inline $Map$* $Msg$::mutable_$name$() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,21 +826,25 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc(
inline const $pb$::RepeatedPtrField<$Submsg$>&
$classname$::_internal_$name$() const {
$TsanDetectConcurrentRead$;
return $field$$.weak$;
}
inline $pb$::RepeatedPtrField<$Submsg$>*
$classname$::_internal_mutable_$name$() {
$TsanDetectConcurrentMutation$;
return &$field$$.weak$;
}
)cc");
if (weak_) {
p->Emit(R"cc(
inline const $pb$::WeakRepeatedPtrField<$Submsg$>&
$Msg$::_internal_weak_$name$() const {
$TsanDetectConcurrentRead$;
return $field$;
}
inline $pb$::WeakRepeatedPtrField<$Submsg$>*
$Msg$::_internal_mutable_weak_$name$() {
$TsanDetectConcurrentMutation$;
return &$field$;
}
)cc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,11 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions(
}
inline const $pb$::RepeatedField<$Type$>& $Msg$::_internal_$name$() const {
$TsanDetectConcurrentRead$;
return $field_$;
}
inline $pb$::RepeatedField<$Type$>* $Msg$::_internal_mutable_$name$() {
$TsanDetectConcurrentMutation$;
return &$field_$;
}
)cc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,12 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
}
inline const ::$proto_ns$::RepeatedPtrField<std::string>&
$Msg$::_internal_$name$() const {
$TsanDetectConcurrentRead$;
return $field_$;
}
inline ::$proto_ns$::RepeatedPtrField<std::string>*
$Msg$::_internal_mutable_$name$() {
$TsanDetectConcurrentMutation$;
return &$field_$;
}
)cc");
Expand Down
6 changes: 6 additions & 0 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d800c5f

Please sign in to comment.