generated_message_util.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file contains miscellaneous helper code used by generated code --
  35. // including lite types -- but which should not be used directly by users.
  36. #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__
  37. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__
  38. #include <assert.h>
  39. #include <atomic>
  40. #include <climits>
  41. #include <string>
  42. #include <vector>
  43. #include <google/protobuf/stubs/logging.h>
  44. #include <google/protobuf/stubs/common.h>
  45. #include <google/protobuf/stubs/once.h> // Add direct dep on port for pb.cc
  46. #include <google/protobuf/has_bits.h>
  47. #include <google/protobuf/implicit_weak_message.h>
  48. #include <google/protobuf/map_entry_lite.h>
  49. #include <google/protobuf/message_lite.h>
  50. #include <google/protobuf/wire_format_lite.h>
  51. namespace google {
  52. namespace protobuf {
  53. class Arena;
  54. namespace io { class CodedInputStream; }
  55. namespace internal {
  56. // Annotation for the compiler to emit a deprecation message if a field marked
  57. // with option 'deprecated=true' is used in the code, or for other things in
  58. // generated code which are deprecated.
  59. //
  60. // For internal use in the pb.cc files, deprecation warnings are suppressed
  61. // there.
  62. #undef DEPRECATED_PROTOBUF_FIELD
  63. #define PROTOBUF_DEPRECATED
  64. #define GOOGLE_PROTOBUF_DEPRECATED_ATTR
  65. // Returns the offset of the given field within the given aggregate type.
  66. // This is equivalent to the ANSI C offsetof() macro. However, according
  67. // to the C++ standard, offsetof() only works on POD types, and GCC
  68. // enforces this requirement with a warning. In practice, this rule is
  69. // unnecessarily strict; there is probably no compiler or platform on
  70. // which the offsets of the direct fields of a class are non-constant.
  71. // Fields inherited from superclasses *can* have non-constant offsets,
  72. // but that's not what this macro will be used for.
  73. #if defined(__clang__)
  74. // For Clang we use __builtin_offsetof() and suppress the warning,
  75. // to avoid Control Flow Integrity and UBSan vptr sanitizers from
  76. // crashing while trying to validate the invalid reinterpet_casts.
  77. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \
  78. _Pragma("clang diagnostic push") \
  79. _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \
  80. __builtin_offsetof(TYPE, FIELD) \
  81. _Pragma("clang diagnostic pop")
  82. #else
  83. // Note that we calculate relative to the pointer value 16 here since if we
  84. // just use zero, GCC complains about dereferencing a NULL pointer. We
  85. // choose 16 rather than some other number just in case the compiler would
  86. // be confused by an unaligned pointer.
  87. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \
  88. static_cast< ::google::protobuf::uint32>( \
  89. reinterpret_cast<const char*>( \
  90. &reinterpret_cast<const TYPE*>(16)->FIELD) - \
  91. reinterpret_cast<const char*>(16))
  92. #endif
  93. // Constants for special floating point values.
  94. LIBPROTOBUF_EXPORT double Infinity();
  95. LIBPROTOBUF_EXPORT double NaN();
  96. LIBPROTOBUF_EXPORT void InitProtobufDefaults();
  97. // This used by proto1
  98. inline const std::string& GetEmptyString() {
  99. InitProtobufDefaults();
  100. return GetEmptyStringAlreadyInited();
  101. }
  102. // True if IsInitialized() is true for all elements of t. Type is expected
  103. // to be a RepeatedPtrField<some message type>. It's useful to have this
  104. // helper here to keep the protobuf compiler from ever having to emit loops in
  105. // IsInitialized() methods. We want the C++ compiler to inline this or not
  106. // as it sees fit.
  107. template <class Type> bool AllAreInitialized(const Type& t) {
  108. for (int i = t.size(); --i >= 0; ) {
  109. if (!t.Get(i).IsInitialized()) return false;
  110. }
  111. return true;
  112. }
  113. // "Weak" variant of AllAreInitialized, used to implement implicit weak fields.
  114. // This version operates on MessageLite to avoid introducing a dependency on the
  115. // concrete message type.
  116. template <class T>
  117. bool AllAreInitializedWeak(const ::google::protobuf::RepeatedPtrField<T>& t) {
  118. for (int i = t.size(); --i >= 0;) {
  119. if (!reinterpret_cast<const ::google::protobuf::internal::RepeatedPtrFieldBase&>(t)
  120. .Get<::google::protobuf::internal::ImplicitWeakTypeHandler<T> >(i)
  121. .IsInitialized()) {
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. struct LIBPROTOBUF_EXPORT FieldMetadata {
  128. uint32 offset; // offset of this field in the struct
  129. uint32 tag; // field * 8 + wire_type
  130. // byte offset * 8 + bit_offset;
  131. // if the high bit is set then this is the byte offset of the oneof_case
  132. // for this field.
  133. uint32 has_offset;
  134. uint32 type; // the type of this field.
  135. const void* ptr; // auxiliary data
  136. // From the serializer point of view each fundamental type can occur in
  137. // 4 different ways. For simplicity we treat all combinations as a cartesion
  138. // product although not all combinations are allowed.
  139. enum FieldTypeClass {
  140. kPresence,
  141. kNoPresence,
  142. kRepeated,
  143. kPacked,
  144. kOneOf,
  145. kNumTypeClasses // must be last enum
  146. };
  147. // C++ protobuf has 20 fundamental types, were we added Cord and StringPiece
  148. // and also distinquish the same types if they have different wire format.
  149. enum {
  150. kCordType = 19,
  151. kStringPieceType = 20,
  152. kInlinedType = 21,
  153. kNumTypes = 21,
  154. kSpecial = kNumTypes * kNumTypeClasses,
  155. };
  156. static int CalculateType(int fundamental_type, FieldTypeClass type_class);
  157. };
  158. inline bool IsPresent(const void* base, uint32 hasbit) {
  159. const uint32* has_bits_array = static_cast<const uint32*>(base);
  160. return (has_bits_array[hasbit / 32] & (1u << (hasbit & 31))) != 0;
  161. }
  162. inline bool IsOneofPresent(const void* base, uint32 offset, uint32 tag) {
  163. const uint32* oneof =
  164. reinterpret_cast<const uint32*>(static_cast<const uint8*>(base) + offset);
  165. return *oneof == tag >> 3;
  166. }
  167. typedef void (*SpecialSerializer)(const uint8* base, uint32 offset, uint32 tag,
  168. uint32 has_offset,
  169. ::google::protobuf::io::CodedOutputStream* output);
  170. LIBPROTOBUF_EXPORT void ExtensionSerializer(const uint8* base, uint32 offset, uint32 tag,
  171. uint32 has_offset,
  172. ::google::protobuf::io::CodedOutputStream* output);
  173. LIBPROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8* base, uint32 offset, uint32 tag,
  174. uint32 has_offset,
  175. ::google::protobuf::io::CodedOutputStream* output);
  176. struct SerializationTable {
  177. int num_fields;
  178. const FieldMetadata* field_table;
  179. };
  180. LIBPROTOBUF_EXPORT void SerializeInternal(const uint8* base, const FieldMetadata* table,
  181. int num_fields, ::google::protobuf::io::CodedOutputStream* output);
  182. inline void TableSerialize(const ::google::protobuf::MessageLite& msg,
  183. const SerializationTable* table,
  184. ::google::protobuf::io::CodedOutputStream* output) {
  185. const FieldMetadata* field_table = table->field_table;
  186. int num_fields = table->num_fields - 1;
  187. const uint8* base = reinterpret_cast<const uint8*>(&msg);
  188. // TODO(gerbens) This skips the first test if we could use the fast
  189. // array serialization path, we should make this
  190. // int cached_size =
  191. // *reinterpret_cast<const int32*>(base + field_table->offset);
  192. // SerializeWithCachedSize(msg, field_table + 1, num_fields, cached_size, ...)
  193. // But we keep conformance with the old way for now.
  194. SerializeInternal(base, field_table + 1, num_fields, output);
  195. }
  196. uint8* SerializeInternalToArray(const uint8* base, const FieldMetadata* table,
  197. int num_fields, bool is_deterministic,
  198. uint8* buffer);
  199. inline uint8* TableSerializeToArray(const ::google::protobuf::MessageLite& msg,
  200. const SerializationTable* table,
  201. bool is_deterministic, uint8* buffer) {
  202. const uint8* base = reinterpret_cast<const uint8*>(&msg);
  203. const FieldMetadata* field_table = table->field_table + 1;
  204. int num_fields = table->num_fields - 1;
  205. return SerializeInternalToArray(base, field_table, num_fields,
  206. is_deterministic, buffer);
  207. }
  208. template <typename T>
  209. struct CompareHelper {
  210. bool operator()(const T& a, const T& b) { return a < b; }
  211. };
  212. template <>
  213. struct CompareHelper<ArenaStringPtr> {
  214. bool operator()(const ArenaStringPtr& a, const ArenaStringPtr& b) {
  215. return a.Get() < b.Get();
  216. }
  217. };
  218. struct CompareMapKey {
  219. template <typename T>
  220. bool operator()(const MapEntryHelper<T>& a, const MapEntryHelper<T>& b) {
  221. return Compare(a.key_, b.key_);
  222. }
  223. template <typename T>
  224. bool Compare(const T& a, const T& b) {
  225. return CompareHelper<T>()(a, b);
  226. }
  227. };
  228. template <typename MapFieldType, const SerializationTable* table>
  229. void MapFieldSerializer(const uint8* base, uint32 offset, uint32 tag,
  230. uint32 has_offset,
  231. ::google::protobuf::io::CodedOutputStream* output) {
  232. typedef MapEntryHelper<typename MapFieldType::EntryTypeTrait> Entry;
  233. typedef typename MapFieldType::MapType::const_iterator Iter;
  234. const MapFieldType& map_field =
  235. *reinterpret_cast<const MapFieldType*>(base + offset);
  236. const SerializationTable* t =
  237. table +
  238. has_offset; // has_offset is overloaded for maps to mean table offset
  239. if (!output->IsSerializationDeterministic()) {
  240. for (Iter it = map_field.GetMap().begin(); it != map_field.GetMap().end();
  241. ++it) {
  242. Entry map_entry(*it);
  243. output->WriteVarint32(tag);
  244. output->WriteVarint32(map_entry._cached_size_);
  245. SerializeInternal(reinterpret_cast<const uint8*>(&map_entry),
  246. t->field_table, t->num_fields, output);
  247. }
  248. } else {
  249. std::vector<Entry> v;
  250. for (Iter it = map_field.GetMap().begin(); it != map_field.GetMap().end();
  251. ++it) {
  252. v.push_back(Entry(*it));
  253. }
  254. std::sort(v.begin(), v.end(), CompareMapKey());
  255. for (int i = 0; i < v.size(); i++) {
  256. output->WriteVarint32(tag);
  257. output->WriteVarint32(v[i]._cached_size_);
  258. SerializeInternal(reinterpret_cast<const uint8*>(&v[i]), t->field_table,
  259. t->num_fields, output);
  260. }
  261. }
  262. }
  263. LIBPROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message);
  264. LIBPROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena,
  265. MessageLite* submessage,
  266. Arena* submessage_arena);
  267. template <typename T>
  268. T* DuplicateIfNonNull(T* message) {
  269. // The casts must be reinterpret_cast<> because T might be a forward-declared
  270. // type that the compiler doesn't know is related to MessageLite.
  271. return reinterpret_cast<T*>(
  272. DuplicateIfNonNullInternal(reinterpret_cast<MessageLite*>(message)));
  273. }
  274. template <typename T>
  275. T* GetOwnedMessage(Arena* message_arena, T* submessage,
  276. Arena* submessage_arena) {
  277. // The casts must be reinterpret_cast<> because T might be a forward-declared
  278. // type that the compiler doesn't know is related to MessageLite.
  279. return reinterpret_cast<T*>(GetOwnedMessageInternal(
  280. message_arena, reinterpret_cast<MessageLite*>(submessage),
  281. submessage_arena));
  282. }
  283. // Hide atomic from the public header and allow easy change to regular int
  284. // on platforms where the atomic might have a perf impact.
  285. class LIBPROTOBUF_EXPORT CachedSize {
  286. public:
  287. int Get() const { return size_.load(std::memory_order_relaxed); }
  288. void Set(int size) { size_.store(size, std::memory_order_relaxed); }
  289. private:
  290. std::atomic<int> size_{0};
  291. };
  292. // SCCInfo represents information of a strongly connected component of
  293. // mutual dependent messages.
  294. struct LIBPROTOBUF_EXPORT SCCInfoBase {
  295. // We use 0 for the Initialized state, because test eax,eax, jnz is smaller
  296. // and is subject to macro fusion.
  297. enum {
  298. kInitialized = 0, // final state
  299. kRunning = 1,
  300. kUninitialized = -1, // initial state
  301. };
  302. #ifndef _MSC_VER
  303. std::atomic<int> visit_status;
  304. #else
  305. // MSVC doesnt make std::atomic constant initialized. This union trick
  306. // makes it so.
  307. union {
  308. int visit_status_to_make_linker_init;
  309. std::atomic<int> visit_status;
  310. };
  311. #endif
  312. int num_deps;
  313. void (*init_func)();
  314. // This is followed by an array of num_deps
  315. // const SCCInfoBase* deps[];
  316. };
  317. template <int N>
  318. struct SCCInfo {
  319. SCCInfoBase base;
  320. // Semantically this is const SCCInfo<T>* which is is a templated type.
  321. // The obvious inheriting from SCCInfoBase mucks with struct initialization.
  322. // Attempts showed the compiler was generating dynamic initialization code.
  323. // Zero length arrays produce warnings with MSVC.
  324. SCCInfoBase* deps[N ? N : 1];
  325. };
  326. LIBPROTOBUF_EXPORT void InitSCCImpl(SCCInfoBase* scc);
  327. inline void InitSCC(SCCInfoBase* scc) {
  328. auto status = scc->visit_status.load(std::memory_order_acquire);
  329. if (GOOGLE_PREDICT_FALSE(status != SCCInfoBase::kInitialized)) InitSCCImpl(scc);
  330. }
  331. LIBPROTOBUF_EXPORT void DestroyMessage(const void* message);
  332. LIBPROTOBUF_EXPORT void DestroyString(const void* s);
  333. // Destroy (not delete) the message
  334. inline void OnShutdownDestroyMessage(const void* ptr) {
  335. OnShutdownRun(DestroyMessage, ptr);
  336. }
  337. // Destroy the string (call string destructor)
  338. inline void OnShutdownDestroyString(const std::string* ptr) {
  339. OnShutdownRun(DestroyString, ptr);
  340. }
  341. } // namespace internal
  342. } // namespace protobuf
  343. } // namespace google
  344. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__