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