From 95e694a9d5e2769d8fa58b8cebacb8692f292965 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Thu, 19 Sep 2024 09:58:26 -0700 Subject: [PATCH] Add alignment field to `MessageCreator` It's possible to operate on a `Map` without necessarily knowing at compile time what `K` and `V` are. The way to do this is by using `UntypedMapBase`, which `proto2::Map` inherits from. However, to do anything useful with the map you need two important bits of information: the size of the map entry, which is represented as a `std::pair`, and the offset of the value within that. These are combined together into the `MapNodeSizeInfoT` type. For Rust to wrap C++ maps, we currently need to store in C++ generated code an array of `MapNodeSizeInfoT` for each message type. The Rust code has to call into C++ to ask for the appropriate `MapNodeSizeInfoT` and then pass that back into C++ for map operations. Dealing with these message-specific constants is pretty awkward, so this CL introduces a way around that. By making each message's alignment available through the `MessageLite*` interface, we make it possible to compute the necessary size info on the fly, instead of requiring the Rust implementation to store it in generated code. Luckily the existing padding inside `MessageCreator` provided enough room to store the alignment. Although this change is mostly for the benefit of Rust, it doesn't cost C++ anything. This change doesn't actually simplify the Rust implementation yet, but I will leave that for a follow-up. PiperOrigin-RevId: 676456011 --- src/google/protobuf/compiler/cpp/message.cc | 11 +- .../compiler/java/java_features.pb.cc | 3 +- src/google/protobuf/compiler/plugin.pb.cc | 18 +- src/google/protobuf/cpp_features.pb.cc | 3 +- src/google/protobuf/descriptor.pb.cc | 162 ++++++++++++------ src/google/protobuf/dynamic_message.cc | 2 +- src/google/protobuf/implicit_weak_message.cc | 3 +- src/google/protobuf/message_lite.h | 22 ++- 8 files changed, 152 insertions(+), 72 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index 2f6b341a62..7ab6d680c4 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -3944,7 +3944,7 @@ void MessageGenerator::GenerateClassData(io::Printer* p) { p->Emit(R"cc( constexpr auto $classname$::InternalNewImpl_() { return $pbi$::MessageCreator(&$classname$::PlacementNew_, - sizeof($classname$)); + sizeof($classname$), alignof($classname$)); } )cc"); } else if (new_op.needs_arena_seeding) { @@ -3956,10 +3956,12 @@ void MessageGenerator::GenerateClassData(io::Printer* p) { $arena_offsets$, }); if (arena_bits.has_value()) { - return $pbi$::MessageCreator::$copy_type$(sizeof($classname$), *arena_bits); + return $pbi$::MessageCreator::$copy_type$( + sizeof($classname$), alignof($classname$), *arena_bits); } else { return $pbi$::MessageCreator(&$classname$::PlacementNew_, - sizeof($classname$)); + sizeof($classname$), + alignof($classname$)); } } )cc"); @@ -3968,7 +3970,8 @@ void MessageGenerator::GenerateClassData(io::Printer* p) { {"arena_offsets", [&] { GetNewOp(p); }}}, R"cc( constexpr auto $classname$::InternalNewImpl_() { - return $pbi$::MessageCreator::$copy_type$(sizeof($classname$)); + return $pbi$::MessageCreator::$copy_type$(sizeof($classname$), + alignof($classname$)); } )cc"); } diff --git a/src/google/protobuf/compiler/java/java_features.pb.cc b/src/google/protobuf/compiler/java/java_features.pb.cc index 045a4f061d..fac334ad55 100644 --- a/src/google/protobuf/compiler/java/java_features.pb.cc +++ b/src/google/protobuf/compiler/java/java_features.pb.cc @@ -200,7 +200,8 @@ inline void* JavaFeatures::PlacementNew_(const void*, void* mem, return ::new (mem) JavaFeatures(arena); } constexpr auto JavaFeatures::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(JavaFeatures)); + return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(JavaFeatures), + alignof(JavaFeatures)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index d08ad02fb1..fada033f9f 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -398,7 +398,8 @@ inline void* Version::PlacementNew_(const void*, void* mem, return ::new (mem) Version(arena); } constexpr auto Version::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(Version)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(Version), + alignof(Version)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -755,10 +756,12 @@ constexpr auto CodeGeneratorRequest::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(CodeGeneratorRequest), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(CodeGeneratorRequest), alignof(CodeGeneratorRequest), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&CodeGeneratorRequest::PlacementNew_, - sizeof(CodeGeneratorRequest)); + sizeof(CodeGeneratorRequest), + alignof(CodeGeneratorRequest)); } } PROTOBUF_CONSTINIT @@ -1156,7 +1159,8 @@ inline void* CodeGeneratorResponse_File::PlacementNew_(const void*, void* mem, return ::new (mem) CodeGeneratorResponse_File(arena); } constexpr auto CodeGeneratorResponse_File::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(CodeGeneratorResponse_File)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(CodeGeneratorResponse_File), + alignof(CodeGeneratorResponse_File)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -1515,10 +1519,12 @@ constexpr auto CodeGeneratorResponse::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(CodeGeneratorResponse), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(CodeGeneratorResponse), alignof(CodeGeneratorResponse), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&CodeGeneratorResponse::PlacementNew_, - sizeof(CodeGeneratorResponse)); + sizeof(CodeGeneratorResponse), + alignof(CodeGeneratorResponse)); } } PROTOBUF_CONSTINIT diff --git a/src/google/protobuf/cpp_features.pb.cc b/src/google/protobuf/cpp_features.pb.cc index b98ae6f8b9..83353fea9a 100644 --- a/src/google/protobuf/cpp_features.pb.cc +++ b/src/google/protobuf/cpp_features.pb.cc @@ -200,7 +200,8 @@ inline void* CppFeatures::PlacementNew_(const void*, void* mem, return ::new (mem) CppFeatures(arena); } constexpr auto CppFeatures::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(CppFeatures)); + return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(CppFeatures), + alignof(CppFeatures)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index 7c03d888a4..64dce40723 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -2669,10 +2669,12 @@ constexpr auto FileDescriptorSet::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(FileDescriptorSet), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(FileDescriptorSet), alignof(FileDescriptorSet), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&FileDescriptorSet::PlacementNew_, - sizeof(FileDescriptorSet)); + sizeof(FileDescriptorSet), + alignof(FileDescriptorSet)); } } PROTOBUF_CONSTINIT @@ -2982,10 +2984,12 @@ constexpr auto FileDescriptorProto::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(FileDescriptorProto), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(FileDescriptorProto), alignof(FileDescriptorProto), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&FileDescriptorProto::PlacementNew_, - sizeof(FileDescriptorProto)); + sizeof(FileDescriptorProto), + alignof(FileDescriptorProto)); } } PROTOBUF_CONSTINIT @@ -3609,7 +3613,8 @@ inline void* DescriptorProto_ExtensionRange::PlacementNew_(const void*, void* me return ::new (mem) DescriptorProto_ExtensionRange(arena); } constexpr auto DescriptorProto_ExtensionRange::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(DescriptorProto_ExtensionRange)); + return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(DescriptorProto_ExtensionRange), + alignof(DescriptorProto_ExtensionRange)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -3905,7 +3910,8 @@ inline void* DescriptorProto_ReservedRange::PlacementNew_(const void*, void* mem return ::new (mem) DescriptorProto_ReservedRange(arena); } constexpr auto DescriptorProto_ReservedRange::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(DescriptorProto_ReservedRange)); + return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(DescriptorProto_ReservedRange), + alignof(DescriptorProto_ReservedRange)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -4231,10 +4237,12 @@ constexpr auto DescriptorProto::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(DescriptorProto), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(DescriptorProto), alignof(DescriptorProto), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&DescriptorProto::PlacementNew_, - sizeof(DescriptorProto)); + sizeof(DescriptorProto), + alignof(DescriptorProto)); } } PROTOBUF_CONSTINIT @@ -4791,7 +4799,8 @@ inline void* ExtensionRangeOptions_Declaration::PlacementNew_(const void*, void* return ::new (mem) ExtensionRangeOptions_Declaration(arena); } constexpr auto ExtensionRangeOptions_Declaration::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(ExtensionRangeOptions_Declaration)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(ExtensionRangeOptions_Declaration), + alignof(ExtensionRangeOptions_Declaration)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -5168,10 +5177,12 @@ constexpr auto ExtensionRangeOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(ExtensionRangeOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(ExtensionRangeOptions), alignof(ExtensionRangeOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&ExtensionRangeOptions::PlacementNew_, - sizeof(ExtensionRangeOptions)); + sizeof(ExtensionRangeOptions), + alignof(ExtensionRangeOptions)); } } PROTOBUF_CONSTINIT @@ -5569,7 +5580,8 @@ inline void* FieldDescriptorProto::PlacementNew_(const void*, void* mem, return ::new (mem) FieldDescriptorProto(arena); } constexpr auto FieldDescriptorProto::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(FieldDescriptorProto)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(FieldDescriptorProto), + alignof(FieldDescriptorProto)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -6101,7 +6113,8 @@ inline void* OneofDescriptorProto::PlacementNew_(const void*, void* mem, return ::new (mem) OneofDescriptorProto(arena); } constexpr auto OneofDescriptorProto::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(OneofDescriptorProto)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(OneofDescriptorProto), + alignof(OneofDescriptorProto)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -6377,7 +6390,8 @@ inline void* EnumDescriptorProto_EnumReservedRange::PlacementNew_(const void*, v return ::new (mem) EnumDescriptorProto_EnumReservedRange(arena); } constexpr auto EnumDescriptorProto_EnumReservedRange::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(EnumDescriptorProto_EnumReservedRange)); + return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(EnumDescriptorProto_EnumReservedRange), + alignof(EnumDescriptorProto_EnumReservedRange)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -6673,10 +6687,12 @@ constexpr auto EnumDescriptorProto::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(EnumDescriptorProto), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(EnumDescriptorProto), alignof(EnumDescriptorProto), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&EnumDescriptorProto::PlacementNew_, - sizeof(EnumDescriptorProto)); + sizeof(EnumDescriptorProto), + alignof(EnumDescriptorProto)); } } PROTOBUF_CONSTINIT @@ -7071,7 +7087,8 @@ inline void* EnumValueDescriptorProto::PlacementNew_(const void*, void* mem, return ::new (mem) EnumValueDescriptorProto(arena); } constexpr auto EnumValueDescriptorProto::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(EnumValueDescriptorProto)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(EnumValueDescriptorProto), + alignof(EnumValueDescriptorProto)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -7404,10 +7421,12 @@ constexpr auto ServiceDescriptorProto::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(ServiceDescriptorProto), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(ServiceDescriptorProto), alignof(ServiceDescriptorProto), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&ServiceDescriptorProto::PlacementNew_, - sizeof(ServiceDescriptorProto)); + sizeof(ServiceDescriptorProto), + alignof(ServiceDescriptorProto)); } } PROTOBUF_CONSTINIT @@ -7756,7 +7775,8 @@ inline void* MethodDescriptorProto::PlacementNew_(const void*, void* mem, return ::new (mem) MethodDescriptorProto(arena); } constexpr auto MethodDescriptorProto::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(MethodDescriptorProto)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(MethodDescriptorProto), + alignof(MethodDescriptorProto)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -8212,10 +8232,12 @@ constexpr auto FileOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(FileOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(FileOptions), alignof(FileOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&FileOptions::PlacementNew_, - sizeof(FileOptions)); + sizeof(FileOptions), + alignof(FileOptions)); } } PROTOBUF_CONSTINIT @@ -9038,10 +9060,12 @@ constexpr auto MessageOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(MessageOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(MessageOptions), alignof(MessageOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&MessageOptions::PlacementNew_, - sizeof(MessageOptions)); + sizeof(MessageOptions), + alignof(MessageOptions)); } } PROTOBUF_CONSTINIT @@ -9454,7 +9478,8 @@ inline void* FieldOptions_EditionDefault::PlacementNew_(const void*, void* mem, return ::new (mem) FieldOptions_EditionDefault(arena); } constexpr auto FieldOptions_EditionDefault::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(FieldOptions_EditionDefault)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(FieldOptions_EditionDefault), + alignof(FieldOptions_EditionDefault)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -9737,7 +9762,8 @@ inline void* FieldOptions_FeatureSupport::PlacementNew_(const void*, void* mem, return ::new (mem) FieldOptions_FeatureSupport(arena); } constexpr auto FieldOptions_FeatureSupport::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(FieldOptions_FeatureSupport)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(FieldOptions_FeatureSupport), + alignof(FieldOptions_FeatureSupport)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -10106,10 +10132,12 @@ constexpr auto FieldOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(FieldOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(FieldOptions), alignof(FieldOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&FieldOptions::PlacementNew_, - sizeof(FieldOptions)); + sizeof(FieldOptions), + alignof(FieldOptions)); } } PROTOBUF_CONSTINIT @@ -10719,10 +10747,12 @@ constexpr auto OneofOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(OneofOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(OneofOptions), alignof(OneofOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&OneofOptions::PlacementNew_, - sizeof(OneofOptions)); + sizeof(OneofOptions), + alignof(OneofOptions)); } } PROTOBUF_CONSTINIT @@ -11056,10 +11086,12 @@ constexpr auto EnumOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(EnumOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(EnumOptions), alignof(EnumOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&EnumOptions::PlacementNew_, - sizeof(EnumOptions)); + sizeof(EnumOptions), + alignof(EnumOptions)); } } PROTOBUF_CONSTINIT @@ -11466,10 +11498,12 @@ constexpr auto EnumValueOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(EnumValueOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(EnumValueOptions), alignof(EnumValueOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&EnumValueOptions::PlacementNew_, - sizeof(EnumValueOptions)); + sizeof(EnumValueOptions), + alignof(EnumValueOptions)); } } PROTOBUF_CONSTINIT @@ -11882,10 +11916,12 @@ constexpr auto ServiceOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(ServiceOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(ServiceOptions), alignof(ServiceOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&ServiceOptions::PlacementNew_, - sizeof(ServiceOptions)); + sizeof(ServiceOptions), + alignof(ServiceOptions)); } } PROTOBUF_CONSTINIT @@ -12247,10 +12283,12 @@ constexpr auto MethodOptions::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(MethodOptions), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(MethodOptions), alignof(MethodOptions), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&MethodOptions::PlacementNew_, - sizeof(MethodOptions)); + sizeof(MethodOptions), + alignof(MethodOptions)); } } PROTOBUF_CONSTINIT @@ -12616,7 +12654,8 @@ inline void* UninterpretedOption_NamePart::PlacementNew_(const void*, void* mem, return ::new (mem) UninterpretedOption_NamePart(arena); } constexpr auto UninterpretedOption_NamePart::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(UninterpretedOption_NamePart)); + return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(UninterpretedOption_NamePart), + alignof(UninterpretedOption_NamePart)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -12921,10 +12960,12 @@ constexpr auto UninterpretedOption::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(UninterpretedOption), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(UninterpretedOption), alignof(UninterpretedOption), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&UninterpretedOption::PlacementNew_, - sizeof(UninterpretedOption)); + sizeof(UninterpretedOption), + alignof(UninterpretedOption)); } } PROTOBUF_CONSTINIT @@ -13355,10 +13396,12 @@ constexpr auto FeatureSet::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(FeatureSet), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(FeatureSet), alignof(FeatureSet), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&FeatureSet::PlacementNew_, - sizeof(FeatureSet)); + sizeof(FeatureSet), + alignof(FeatureSet)); } } PROTOBUF_CONSTINIT @@ -13748,7 +13791,8 @@ inline void* FeatureSetDefaults_FeatureSetEditionDefault::PlacementNew_(const vo return ::new (mem) FeatureSetDefaults_FeatureSetEditionDefault(arena); } constexpr auto FeatureSetDefaults_FeatureSetEditionDefault::InternalNewImpl_() { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(FeatureSetDefaults_FeatureSetEditionDefault)); + return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(FeatureSetDefaults_FeatureSetEditionDefault), + alignof(FeatureSetDefaults_FeatureSetEditionDefault)); } PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 @@ -14090,10 +14134,12 @@ constexpr auto FeatureSetDefaults::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(FeatureSetDefaults), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(FeatureSetDefaults), alignof(FeatureSetDefaults), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&FeatureSetDefaults::PlacementNew_, - sizeof(FeatureSetDefaults)); + sizeof(FeatureSetDefaults), + alignof(FeatureSetDefaults)); } } PROTOBUF_CONSTINIT @@ -14427,10 +14473,12 @@ constexpr auto SourceCodeInfo_Location::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(SourceCodeInfo_Location), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(SourceCodeInfo_Location), alignof(SourceCodeInfo_Location), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&SourceCodeInfo_Location::PlacementNew_, - sizeof(SourceCodeInfo_Location)); + sizeof(SourceCodeInfo_Location), + alignof(SourceCodeInfo_Location)); } } PROTOBUF_CONSTINIT @@ -14790,10 +14838,12 @@ constexpr auto SourceCodeInfo::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(SourceCodeInfo), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(SourceCodeInfo), alignof(SourceCodeInfo), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&SourceCodeInfo::PlacementNew_, - sizeof(SourceCodeInfo)); + sizeof(SourceCodeInfo), + alignof(SourceCodeInfo)); } } PROTOBUF_CONSTINIT @@ -15053,10 +15103,12 @@ constexpr auto GeneratedCodeInfo_Annotation::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::CopyInit(sizeof(GeneratedCodeInfo_Annotation), *arena_bits); + return ::google::protobuf::internal::MessageCreator::CopyInit( + sizeof(GeneratedCodeInfo_Annotation), alignof(GeneratedCodeInfo_Annotation), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&GeneratedCodeInfo_Annotation::PlacementNew_, - sizeof(GeneratedCodeInfo_Annotation)); + sizeof(GeneratedCodeInfo_Annotation), + alignof(GeneratedCodeInfo_Annotation)); } } PROTOBUF_CONSTINIT @@ -15409,10 +15461,12 @@ constexpr auto GeneratedCodeInfo::InternalNewImpl_() { ::google::protobuf::Message::internal_visibility()), }); if (arena_bits.has_value()) { - return ::google::protobuf::internal::MessageCreator::ZeroInit(sizeof(GeneratedCodeInfo), *arena_bits); + return ::google::protobuf::internal::MessageCreator::ZeroInit( + sizeof(GeneratedCodeInfo), alignof(GeneratedCodeInfo), *arena_bits); } else { return ::google::protobuf::internal::MessageCreator(&GeneratedCodeInfo::PlacementNew_, - sizeof(GeneratedCodeInfo)); + sizeof(GeneratedCodeInfo), + alignof(GeneratedCodeInfo)); } } PROTOBUF_CONSTINIT diff --git a/src/google/protobuf/dynamic_message.cc b/src/google/protobuf/dynamic_message.cc index 12434fc606..89c15c4edc 100644 --- a/src/google/protobuf/dynamic_message.cc +++ b/src/google/protobuf/dynamic_message.cc @@ -767,7 +767,7 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( type_info->weak_field_map_offset = -1; type_info->class_data.message_creator = - internal::MessageCreator(DynamicMessage::NewImpl, size); + internal::MessageCreator(DynamicMessage::NewImpl, size, kSafeAlignment); // Construct the reflection object. diff --git a/src/google/protobuf/implicit_weak_message.cc b/src/google/protobuf/implicit_weak_message.cc index e9144be390..7a1f55b970 100644 --- a/src/google/protobuf/implicit_weak_message.cc +++ b/src/google/protobuf/implicit_weak_message.cc @@ -70,7 +70,8 @@ constexpr MessageLite::ClassDataLite<1> ImplicitWeakMessage::class_data_ = { nullptr, // is_initialized (always true) MergeImpl, internal::MessageCreator(NewImpl, - sizeof(ImplicitWeakMessage)), + sizeof(ImplicitWeakMessage), + alignof(ImplicitWeakMessage)), &DestroyImpl, GetClearImpl(), &ByteSizeLongImpl, diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index c02f7b6735..e164274fed 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -89,26 +89,34 @@ class MessageCreator { }; constexpr MessageCreator() - : allocation_size_(), tag_(), arena_bits_(uintptr_t{}) {} + : allocation_size_(), tag_(), alignment_(), arena_bits_(uintptr_t{}) {} static constexpr MessageCreator ZeroInit(uint32_t allocation_size, + uint8_t alignment, uintptr_t arena_bits = 0) { MessageCreator out; out.allocation_size_ = allocation_size; out.tag_ = kZeroInit; + out.alignment_ = alignment; out.arena_bits_ = arena_bits; return out; } static constexpr MessageCreator CopyInit(uint32_t allocation_size, + uint8_t alignment, uintptr_t arena_bits = 0) { MessageCreator out; out.allocation_size_ = allocation_size; out.tag_ = kMemcpy; + out.alignment_ = alignment; out.arena_bits_ = arena_bits; return out; } - constexpr MessageCreator(Func func, uint32_t allocation_size) - : allocation_size_(allocation_size), tag_(kFunc), func_(func) {} + constexpr MessageCreator(Func func, uint32_t allocation_size, + uint8_t alignment) + : allocation_size_(allocation_size), + tag_(kFunc), + alignment_(alignment), + func_(func) {} // Template for testing. template @@ -124,6 +132,8 @@ class MessageCreator { uint32_t allocation_size() const { return allocation_size_; } + uint8_t alignment() const { return alignment_; } + uintptr_t arena_bits() const { ABSL_DCHECK_NE(+tag(), +kFunc); return arena_bits_; @@ -132,6 +142,7 @@ class MessageCreator { private: uint32_t allocation_size_; Tag tag_; + uint8_t alignment_; union { Func func_; uintptr_t arena_bits_; @@ -638,7 +649,7 @@ class PROTOBUF_EXPORT MessageLite { #endif return T::InternalNewImpl_(); } else { - return internal::MessageCreator(&T::PlacementNew_, sizeof(T)); + return internal::MessageCreator(&T::PlacementNew_, sizeof(T), alignof(T)); } } @@ -789,6 +800,8 @@ class PROTOBUF_EXPORT MessageLite { uint32_t allocation_size() const { return message_creator.allocation_size(); } + + uint8_t alignment() const { return message_creator.alignment(); } }; template struct ClassDataLite { @@ -1170,6 +1183,7 @@ template PROTOBUF_ALWAYS_INLINE inline MessageLite* MessageCreator::PlacementNew( const MessageLite* prototype_for_func, const MessageLite* prototype_for_copy, void* mem, Arena* arena) const { + ABSL_DCHECK_EQ(reinterpret_cast(mem) % alignment_, 0u); const Tag as_tag = tag(); // When the feature is not enabled we skip the `as_tag` check since it is // unnecessary. Except for testing, where we want to test the copy logic even