blob: 04f68a6e0864a25507ba20adece87985d21bf58e [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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 Schuh40c16522018-10-28 20:27:54 -070042#include <atomic>
43#include <climits>
Brian Silverman9c614bc2016-02-15 20:20:02 -050044#include <string>
Austin Schuh40c16522018-10-28 20:27:54 -070045#include <vector>
Brian Silverman9c614bc2016-02-15 20:20:02 -050046
Austin Schuh40c16522018-10-28 20:27:54 -070047#include <google/protobuf/stubs/logging.h>
Brian Silverman9c614bc2016-02-15 20:20:02 -050048#include <google/protobuf/stubs/common.h>
Austin Schuh40c16522018-10-28 20:27:54 -070049#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 Silverman9c614bc2016-02-15 20:20:02 -050056namespace google {
57
58namespace protobuf {
59
60class Arena;
Austin Schuh40c16522018-10-28 20:27:54 -070061
Brian Silverman9c614bc2016-02-15 20:20:02 -050062namespace io { class CodedInputStream; }
63
64namespace 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 Schuh40c16522018-10-28 20:27:54 -070076#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 Silverman9c614bc2016-02-15 20:20:02 -0500107
108// Constants for special floating point values.
109LIBPROTOBUF_EXPORT double Infinity();
110LIBPROTOBUF_EXPORT double NaN();
111
Austin Schuh40c16522018-10-28 20:27:54 -0700112LIBPROTOBUF_EXPORT void InitProtobufDefaults();
Brian Silverman9c614bc2016-02-15 20:20:02 -0500113
Austin Schuh40c16522018-10-28 20:27:54 -0700114// This used by proto1
115inline const std::string& GetEmptyString() {
116 InitProtobufDefaults();
Brian Silverman9c614bc2016-02-15 20:20:02 -0500117 return GetEmptyStringAlreadyInited();
118}
119
Brian Silverman9c614bc2016-02-15 20:20:02 -0500120// 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.
125template <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 Schuh40c16522018-10-28 20:27:54 -0700132// "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.
135template <class T>
136bool 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 Silverman9c614bc2016-02-15 20:20:02 -0500146
Austin Schuh40c16522018-10-28 20:27:54 -0700147struct 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
181inline 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
186inline 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
192typedef void (*SpecialSerializer)(const uint8* base, uint32 offset, uint32 tag,
193 uint32 has_offset,
194 ::google::protobuf::io::CodedOutputStream* output);
195
196LIBPROTOBUF_EXPORT void ExtensionSerializer(const uint8* base, uint32 offset, uint32 tag,
197 uint32 has_offset,
198 ::google::protobuf::io::CodedOutputStream* output);
199LIBPROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8* base, uint32 offset, uint32 tag,
200 uint32 has_offset,
201 ::google::protobuf::io::CodedOutputStream* output);
202
203struct SerializationTable {
204 int num_fields;
205 const FieldMetadata* field_table;
206};
207
208LIBPROTOBUF_EXPORT void SerializeInternal(const uint8* base, const FieldMetadata* table,
209 int num_fields, ::google::protobuf::io::CodedOutputStream* output);
210
211inline 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
226uint8* SerializeInternalToArray(const uint8* base, const FieldMetadata* table,
227 int num_fields, bool is_deterministic,
228 uint8* buffer);
229
230inline 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
240template <typename T>
241struct CompareHelper {
242 bool operator()(const T& a, const T& b) { return a < b; }
243};
244
245template <>
246struct CompareHelper<ArenaStringPtr> {
247 bool operator()(const ArenaStringPtr& a, const ArenaStringPtr& b) {
248 return a.Get() < b.Get();
249 }
250};
251
252struct 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
263template <typename MapFieldType, const SerializationTable* table>
264void 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
300LIBPROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message);
301LIBPROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena,
302 MessageLite* submessage,
303 Arena* submessage_arena);
304
305template <typename T>
306T* 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
313template <typename T>
314T* 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.
325class 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.
335struct 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
359template <int N>
360struct 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
369LIBPROTOBUF_EXPORT void InitSCCImpl(SCCInfoBase* scc);
370
371inline 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
376LIBPROTOBUF_EXPORT void DestroyMessage(const void* message);
377LIBPROTOBUF_EXPORT void DestroyString(const void* s);
378// Destroy (not delete) the message
379inline void OnShutdownDestroyMessage(const void* ptr) {
380 OnShutdownRun(DestroyMessage, ptr);
381}
382// Destroy the string (call string destructor)
383inline void OnShutdownDestroyString(const std::string* ptr) {
384 OnShutdownRun(DestroyString, ptr);
385}
Brian Silverman9c614bc2016-02-15 20:20:02 -0500386
387} // namespace internal
388} // namespace protobuf
389
390} // namespace google
391#endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__