Skip to content
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

Internal Change #19168

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -762,16 +762,35 @@ void RepeatedMessage::GenerateAccessorDeclarations(io::Printer* p) const {

void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
// TODO: move insertion points
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$;
return _internal_mutable_$name_internal$()->Mutable(index);
}
)cc");
p->Emit(
{
{"Mutable",
[&] {
switch (opts_->bounds_check_mode) {
case BoundsCheckMode::kNoEnforcement:
case BoundsCheckMode::kReturnDefaultValue:
p->Emit("_internal_mutable_$name_internal$()->Mutable(index)");
break;
case BoundsCheckMode::kAbort:
p->Emit(
"::google::protobuf::internal::InternalEnforcedBoundsCheckMutable("
"_internal_mutable_$name_internal$(), index)");
break;
}
}},
},
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$;
return $Mutable$;
;
}
)cc");

p->Emit(R"cc(
inline $pb$::RepeatedPtrField<$Submsg$>* $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
Expand All @@ -785,10 +804,24 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc");
p->Emit(
{
{"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"},
{"Get",
[&] {
switch (opts_->bounds_check_mode) {
case BoundsCheckMode::kNoEnforcement:
p->Emit("Get");
break;
case BoundsCheckMode::kReturnDefaultValue:
p->Emit("InternalCheckedGet");
break;
case BoundsCheckMode::kAbort:
p->Emit("InternalEnforcedBoundsCheckGet");
break;
}
}},
{"GetExtraArg",
[&] {
p->Emit(opts_->safe_boundary_check
p->Emit(opts_->bounds_check_mode ==
BoundsCheckMode::kReturnDefaultValue
? ", reinterpret_cast<const $Submsg$&>($kDefault$)"
: "");
}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,24 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
bool bytes = field_->type() == FieldDescriptor::TYPE_BYTES;
p->Emit(
{
{"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"},
{"Get",
[&] {
switch (opts_->bounds_check_mode) {
case BoundsCheckMode::kNoEnforcement:
p->Emit("Get");
break;
case BoundsCheckMode::kReturnDefaultValue:
p->Emit("InternalCheckedGet");
break;
case BoundsCheckMode::kAbort:
p->Emit("InternalEnforcedBoundsCheckGet");
break;
}
}},
{"GetExtraArg",
[&] {
p->Emit(opts_->safe_boundary_check
p->Emit(opts_->bounds_check_mode ==
BoundsCheckMode::kReturnDefaultValue
? ", $pbi$::GetEmptyStringAlreadyInited()"
: "");
}},
Expand All @@ -867,6 +881,20 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(", $pbi$::BytesTag{}");
}
}},
{"Mutable",
[&] {
switch (opts_->bounds_check_mode) {
case BoundsCheckMode::kNoEnforcement:
case BoundsCheckMode::kReturnDefaultValue:
p->Emit("_internal_mutable_$name_internal$()->Mutable(index)");
break;
case BoundsCheckMode::kAbort:
p->Emit(
"::google::protobuf::internal::InternalEnforcedBoundsCheckMutable("
"_internal_mutable_$name_internal$(), index)");
break;
}
}},
},
R"cc(
inline std::string* $Msg$::add_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
Expand All @@ -889,14 +917,14 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
$WeakDescriptorSelfPin$;
$annotate_mutable$;
// @@protoc_insertion_point(field_mutable:$pkg.Msg.field$)
return _internal_mutable_$name_internal$()->Mutable(index);
return $Mutable$;
;
}
template <typename Arg_, typename... Args_>
inline void $Msg$::set_$name$(int index, Arg_&& value, Args_... args) {
$WeakDescriptorSelfPin$;
$pbi$::AssignToString(
*_internal_mutable_$name_internal$()->Mutable(index),
std::forward<Arg_>(value), args... $bytes_tag$);
$pbi$::AssignToString(*$Mutable$, , std::forward<Arg_>(value),
args... $bytes_tag$);
$annotate_set$;
// @@protoc_insertion_point(field_set:$pkg.Msg.field$)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,93 +672,124 @@ void RepeatedStringView::GenerateAccessorDeclarations(io::Printer* p) const {

void RepeatedStringView::GenerateInlineAccessorDefinitions(
io::Printer* p) const {
p->Emit({{"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"},
{"GetExtraArg",
[&] {
p->Emit(opts_->safe_boundary_check
? ", $pbi$::GetEmptyStringAlreadyInited()"
: "");
}}},
R"cc(
inline absl::string_view $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 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$;
// @@protoc_insertion_point(field_set_char:$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);
$annotate_add$;
// @@protoc_insertion_point(field_add_char:$pkg.Msg.field$)
}
inline void $Msg$::add_$name$(absl::string_view value) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add()->assign(value.data(),
value.size());
$annotate_add$;
// @@protoc_insertion_point(field_add_string_piece:$pkg.Msg.field$)
}
inline const ::$proto_ns$::RepeatedPtrField<std::string>&
$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<std::string>*
$Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;
return _internal_mutable_$name_internal$();
}
)cc");
p->Emit(
{
{"Get",
[&] {
switch (opts_->bounds_check_mode) {
case BoundsCheckMode::kNoEnforcement:
p->Emit("Get");
break;
case BoundsCheckMode::kReturnDefaultValue:
p->Emit("InternalCheckedGet");
break;
case BoundsCheckMode::kAbort:
p->Emit("InternalEnforcedBoundsCheckGet");
break;
}
}},
{"GetExtraArg",
[&] {
if (opts_->bounds_check_mode ==
BoundsCheckMode::kReturnDefaultValue) {
p->Emit(", $pbi$::GetEmptyStringAlreadyInited()");
}
}},

{"Mutable",
[&] {
switch (opts_->bounds_check_mode) {
case BoundsCheckMode::kNoEnforcement:
case BoundsCheckMode::kReturnDefaultValue:
p->Emit("_internal_mutable_$name_internal$()->Mutable(index)");
break;
case BoundsCheckMode::kAbort:
p->Emit(
"::google::protobuf::internal::InternalEnforcedBoundsCheckMutable("
"_internal_mutable_$name_internal$(), index)");
break;
}
}},
},
R"cc(
inline absl::string_view $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 void $Msg$::set_$name$(int index, const std::string& value) {
$WeakDescriptorSelfPin$;
$Mutable$->assign(value);
$annotate_set$;
// @@protoc_insertion_point(field_set:$pkg.Msg.field$)
}
inline void $Msg$::set_$name$(int index, std::string&& value) {
$WeakDescriptorSelfPin$;
$Mutable$->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);
$Mutable$->assign(value);
$annotate_set$;
// @@protoc_insertion_point(field_set_char:$pkg.Msg.field$)
}
inline void $Msg$::set_$name$(int index, absl::string_view value) {
$WeakDescriptorSelfPin$;
$Mutable$->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);
$annotate_add$;
// @@protoc_insertion_point(field_add_char:$pkg.Msg.field$)
}
inline void $Msg$::add_$name$(absl::string_view value) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add()->assign(value.data(),
value.size());
$annotate_add$;
// @@protoc_insertion_point(field_add_string_piece:$pkg.Msg.field$)
}
inline const ::$proto_ns$::RepeatedPtrField<std::string>&
$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<std::string>*
$Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;
return _internal_mutable_$name_internal$();
}
)cc");
if (should_split()) {
p->Emit(R"cc(
inline const $pb$::RepeatedPtrField<std::string>&
Expand Down
7 changes: 5 additions & 2 deletions src/google/protobuf/compiler/cpp/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ bool CppGenerator::Generate(const FileDescriptor* file,
if (key == "dllexport_decl") {
file_options.dllexport_decl = value;
} else if (key == "safe_boundary_check") {
file_options.safe_boundary_check = true;
file_options.bounds_check_mode = BoundsCheckMode::kReturnDefaultValue;
} else if (key == "enforced_boundary_check") {
file_options.bounds_check_mode = BoundsCheckMode::kAbort;
} else if (key == "annotate_headers") {
file_options.annotate_headers = true;
} else if (key == "annotation_pragma_name") {
Expand Down Expand Up @@ -192,7 +194,8 @@ bool CppGenerator::Generate(const FileDescriptor* file,

// The safe_boundary_check option controls behavior for Google-internal
// protobuf APIs.
if (file_options.safe_boundary_check && file_options.opensource_runtime) {
if ((file_options.bounds_check_mode != BoundsCheckMode::kNoEnforcement) &&
file_options.opensource_runtime) {
*error =
"The safe_boundary_check option is not supported outside of Google.";
return false;
Expand Down
8 changes: 7 additions & 1 deletion src/google/protobuf/compiler/cpp/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ enum class EnforceOptimizeMode {
kLiteRuntime,
};

enum class BoundsCheckMode {
kNoEnforcement, // No enforcement.
kReturnDefaultValue, // Return default value if out of bounds.
kAbort, // TrapOrAbort if out of bounds.
};

struct FieldListenerOptions {
bool inject_field_listener_events = false;
absl::flat_hash_set<std::string> forbidden_field_listener_events;
Expand All @@ -45,7 +51,7 @@ struct Options {
FieldListenerOptions field_listener_options;
EnforceOptimizeMode enforce_mode = EnforceOptimizeMode::kNoEnforcement;
int num_cc_files = 0;
bool safe_boundary_check = false;
BoundsCheckMode bounds_check_mode = BoundsCheckMode::kNoEnforcement;
bool proto_h = false;
bool transitive_pb_h = true;
bool annotate_headers = false;
Expand Down
Loading
Loading