1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #ifndef GOOGLE_PROTOBUF_ARENASTRING_H__
9 #define GOOGLE_PROTOBUF_ARENASTRING_H__
10
11 #include <algorithm>
12 #include <cstdint>
13 #include <string>
14 #include <type_traits>
15 #include <utility>
16
17 #include "absl/log/absl_check.h"
18 #include "absl/strings/string_view.h"
19 #include "google/protobuf/arena.h"
20 #include "google/protobuf/explicitly_constructed.h"
21 #include "google/protobuf/port.h"
22
23 // must be last:
24 #include "google/protobuf/port_def.inc"
25
26 #ifdef SWIG
27 #error "You cannot SWIG proto headers"
28 #endif
29
30
31 namespace google {
32 namespace protobuf {
33 namespace internal {
34 class EpsCopyInputStream;
35
36 class SwapFieldHelper;
37
38 // Declared in message_lite.h
39 PROTOBUF_EXPORT extern ExplicitlyConstructedArenaString
40 fixed_address_empty_string;
41
42 // Lazy string instance to support string fields with non-empty default.
43 // These are initialized on the first call to .get().
44 class PROTOBUF_EXPORT LazyString {
45 public:
46 // We explicitly make LazyString an aggregate so that MSVC can do constant
47 // initialization on it without marking it `constexpr`.
48 // We do not want to use `constexpr` because it makes it harder to have extern
49 // storage for it and causes library bloat.
50 struct InitValue {
51 const char* ptr;
52 size_t size;
53 };
54 // We keep a union of the initialization value and the std::string to save on
55 // space. We don't need the string array after Init() is done.
56 union {
57 mutable InitValue init_value_;
58 alignas(std::string) mutable char string_buf_[sizeof(std::string)];
59 };
60 mutable std::atomic<const std::string*> inited_;
61
get()62 const std::string& get() const {
63 // This check generates less code than a call-once invocation.
64 auto* res = inited_.load(std::memory_order_acquire);
65 if (PROTOBUF_PREDICT_FALSE(res == nullptr)) return Init();
66 return *res;
67 }
68
69 private:
70 // Initialize the string in `string_buf_`, update `inited_` and return it.
71 // We return it here to avoid having to read it again in the inlined code.
72 const std::string& Init() const;
73 };
74
75 class PROTOBUF_EXPORT TaggedStringPtr {
76 public:
77 // Bit flags qualifying string properties. We can use 2 bits as
78 // ptr_ is guaranteed and enforced to be aligned on 4 byte boundaries.
79 enum Flags {
80 kArenaBit = 0x1, // ptr is arena allocated
81 kMutableBit = 0x2, // ptr contents are fully mutable
82 kMask = 0x3 // Bit mask
83 };
84
85 // Composed logical types
86 enum Type {
87 // Default strings are immutable and never owned.
88 kDefault = 0,
89
90 // Allocated strings are mutable and (as the name implies) owned.
91 // A heap allocated string must be deleted.
92 kAllocated = kMutableBit,
93
94 // Mutable arena strings are strings where the string instance is owned
95 // by the arena, but the string contents itself are owned by the string
96 // instance. Mutable arena string instances need to be destroyed which is
97 // typically done through a cleanup action added to the arena owning it.
98 kMutableArena = kArenaBit | kMutableBit,
99
100 // Fixed size arena strings are strings where both the string instance and
101 // the string contents are fully owned by the arena. Fixed size arena
102 // strings are a platform and c++ library specific customization. Fixed
103 // size arena strings are immutable, with the exception of custom internal
104 // updates to the content that fit inside the existing capacity.
105 // Fixed size arena strings must never be deleted or destroyed.
106 kFixedSizeArena = kArenaBit,
107 };
108
109 TaggedStringPtr() = default;
TaggedStringPtr(ExplicitlyConstructedArenaString * ptr)110 explicit constexpr TaggedStringPtr(ExplicitlyConstructedArenaString* ptr)
111 : ptr_(ptr) {}
112
113 // Sets the value to `p`, tagging the value as being a 'default' value.
114 // See documentation for kDefault for more info.
SetDefault(const std::string * p)115 inline const std::string* SetDefault(const std::string* p) {
116 return TagAs(kDefault, const_cast<std::string*>(p));
117 }
118
119 // Sets the value to `p`, tagging the value as a heap allocated value.
120 // Allocated strings are mutable and (as the name implies) owned.
121 // `p` must not be null
SetAllocated(std::string * p)122 inline std::string* SetAllocated(std::string* p) {
123 return TagAs(kAllocated, p);
124 }
125
126 // Sets the value to `p`, tagging the value as a fixed size arena string.
127 // See documentation for kFixedSizeArena for more info.
128 // `p` must not be null
SetFixedSizeArena(std::string * p)129 inline std::string* SetFixedSizeArena(std::string* p) {
130 return TagAs(kFixedSizeArena, p);
131 }
132
133 // Sets the value to `p`, tagging the value as a mutable arena string.
134 // See documentation for kMutableArena for more info.
135 // `p` must not be null
SetMutableArena(std::string * p)136 inline std::string* SetMutableArena(std::string* p) {
137 return TagAs(kMutableArena, p);
138 }
139
140 // Returns true if the contents of the current string are fully mutable.
IsMutable()141 inline bool IsMutable() const { return as_int() & kMutableBit; }
142
143 // Returns true if the current string is an immutable default value.
IsDefault()144 inline bool IsDefault() const { return (as_int() & kMask) == kDefault; }
145
146 // If the current string is a heap-allocated mutable value, returns a pointer
147 // to it. Returns nullptr otherwise.
GetIfAllocated()148 inline std::string* GetIfAllocated() const {
149 auto allocated = as_int() ^ kAllocated;
150 if (allocated & kMask) return nullptr;
151
152 auto ptr = reinterpret_cast<std::string*>(allocated);
153 PROTOBUF_ASSUME(ptr != nullptr);
154 return ptr;
155 }
156
157 // Returns true if the current string is an arena allocated value.
158 // This means it's either a mutable or fixed size arena string.
IsArena()159 inline bool IsArena() const { return as_int() & kArenaBit; }
160
161 // Returns true if the current string is a fixed size arena allocated value.
IsFixedSizeArena()162 inline bool IsFixedSizeArena() const {
163 return (as_int() & kMask) == kFixedSizeArena;
164 }
165
166 // Returns the contained string pointer.
Get()167 inline std::string* Get() const {
168 return reinterpret_cast<std::string*>(as_int() & ~kMask);
169 }
170
171 // Returns true if the contained pointer is null, indicating some error.
172 // The Null value is only used during parsing for temporary values.
173 // A persisted ArenaStringPtr value is never null.
IsNull()174 inline bool IsNull() const { return ptr_ == nullptr; }
175
176 // Returns a copy of this instance. In debug builds, the returned value may be
177 // a forced copy regardless if the current instance is a compile time default.
178 TaggedStringPtr Copy(Arena* arena) const;
179
180 // Identical to the above `Copy` function except that in debug builds,
181 // `default_value` can be used to substitute an empty default with a
182 // hardened copy of the default value.
183 TaggedStringPtr Copy(Arena* arena, const LazyString& default_value) const;
184
185 private:
assert_aligned(const void * p)186 static inline void assert_aligned(const void* p) {
187 static_assert(kMask <= alignof(void*), "Pointer underaligned for bit mask");
188 static_assert(kMask <= alignof(std::string),
189 "std::string underaligned for bit mask");
190 ABSL_DCHECK_EQ(reinterpret_cast<uintptr_t>(p) & kMask, 0UL);
191 }
192
193 // Creates a heap or arena allocated copy of this instance.
194 TaggedStringPtr ForceCopy(Arena* arena) const;
195
TagAs(Type type,std::string * p)196 inline std::string* TagAs(Type type, std::string* p) {
197 ABSL_DCHECK(p != nullptr);
198 assert_aligned(p);
199 ptr_ = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(p) | type);
200 return p;
201 }
202
as_int()203 uintptr_t as_int() const { return reinterpret_cast<uintptr_t>(ptr_); }
204 void* ptr_;
205 };
206
207 static_assert(std::is_trivial<TaggedStringPtr>::value,
208 "TaggedStringPtr must be trivial");
209
210 // This class encapsulates a pointer to a std::string with or without arena
211 // owned contents, tagged by the bottom bits of the string pointer. It is a
212 // high-level wrapper that almost directly corresponds to the interface required
213 // by string fields in generated code. It replaces the old std::string* pointer
214 // in such cases.
215 //
216 // The string pointer is tagged to be either a default, externally owned value,
217 // a mutable heap allocated value, or an arena allocated value. The object uses
218 // a single global instance of an empty string that is used as the initial
219 // default value. Fields that have empty default values directly use this global
220 // default. Fields that have non empty default values are supported through
221 // lazily initialized default values managed by the LazyString class.
222 //
223 // Generated code and reflection code both ensure that ptr_ is never null.
224 // Because ArenaStringPtr is used in oneof unions, its constructor is a NOP and
225 // the field is always manually initialized via method calls.
226 //
227 // See TaggedStringPtr for more information about the types of string values
228 // being held, and the mutable and ownership invariants for each type.
229 struct PROTOBUF_EXPORT ArenaStringPtr {
230 // Default constructor, leaves current instance uninitialized (does nothing)
231 ArenaStringPtr() = default;
232
233 // Constexpr constructor, initializes to a constexpr, empty string value.
ArenaStringPtrArenaStringPtr234 constexpr ArenaStringPtr(ExplicitlyConstructedArenaString* default_value,
235 ConstantInitialized)
236 : tagged_ptr_(default_value) {}
237
238 // Arena enabled constructor for strings without a default value.
239 // Initializes this instance to a constexpr, empty string value, unless debug
240 // hardening is enabled, in which case this instance will hold a forced copy.
ArenaStringPtrArenaStringPtr241 explicit ArenaStringPtr(Arena* arena)
242 : tagged_ptr_(&fixed_address_empty_string) {
243 if (DebugHardenForceCopyDefaultString()) {
244 Set(absl::string_view(""), arena);
245 }
246 }
247
248 // Arena enabled constructor for strings with a non-empty default value.
249 // Initializes this instance to a constexpr, empty string value, unless debug
250 // hardening is enabled, in which case this instance will be forced to hold a
251 // forced copy of the value in `default_value`.
ArenaStringPtrArenaStringPtr252 ArenaStringPtr(Arena* arena, const LazyString& default_value)
253 : tagged_ptr_(&fixed_address_empty_string) {
254 if (DebugHardenForceCopyDefaultString()) {
255 Set(absl::string_view(default_value.get()), arena);
256 }
257 }
258
259 // Arena enabled copy constructor for strings without a default value.
260 // This instance will be initialized with a copy of the value in `rhs`.
261 // If `rhs` holds a default (empty) value, then this instance will also be
262 // initialized with the default empty value, unless debug hardening is
263 // enabled, in which case this instance will be forced to hold a copy of
264 // an empty default value.
ArenaStringPtrArenaStringPtr265 ArenaStringPtr(Arena* arena, const ArenaStringPtr& rhs)
266 : tagged_ptr_(rhs.tagged_ptr_.Copy(arena)) {}
267
268 // Arena enabled copy constructor for strings with a non-empty default value.
269 // This instance will be initialized with a copy of the value in `rhs`.
270 // If `rhs` holds a default (empty) value, then this instance will also be
271 // initialized with the default empty value, unless debug hardening is
272 // enabled, in which case this instance will be forced to hold forced copy
273 // of the value in `default_value`.
ArenaStringPtrArenaStringPtr274 ArenaStringPtr(Arena* arena, const ArenaStringPtr& rhs,
275 const LazyString& default_value)
276 : tagged_ptr_(rhs.tagged_ptr_.Copy(arena, default_value)) {}
277
278 // Called from generated code / reflection runtime only. Resets value to point
279 // to a default string pointer, with the semantics that this ArenaStringPtr
280 // does not own the pointed-to memory. Disregards initial value of ptr_ (so
281 // this is the *ONLY* safe method to call after construction or when
282 // reinitializing after becoming the active field in a oneof union).
283 inline void InitDefault();
284
285 // Similar to `InitDefault` except that it allows the default value to be
286 // initialized to an externally owned string. This method is called from
287 // parsing code. `str` must not be null and outlive this instance.
288 inline void InitExternal(const std::string* str);
289
290 // Called from generated code / reflection runtime only. Resets the value of
291 // this instances to the heap allocated value in `str`. `str` must not be
292 // null. Invokes `arena->Own(str)` to transfer ownership into the arena if
293 // `arena` is not null, else, `str` will be owned by ArenaStringPtr. This
294 // function should only be used to initialize a ArenaStringPtr or on an
295 // instance known to not carry any heap allocated value.
296 inline void InitAllocated(std::string* str, Arena* arena);
297
298 void Set(absl::string_view value, Arena* arena);
299 void Set(std::string&& value, Arena* arena);
300 template <typename... OverloadDisambiguator>
301 void Set(const std::string& value, Arena* arena);
302 void Set(const char* s, Arena* arena);
303 void Set(const char* s, size_t n, Arena* arena);
304
305 void SetBytes(absl::string_view value, Arena* arena);
306 void SetBytes(std::string&& value, Arena* arena);
307 template <typename... OverloadDisambiguator>
308 void SetBytes(const std::string& value, Arena* arena);
309 void SetBytes(const char* s, Arena* arena);
310 void SetBytes(const void* p, size_t n, Arena* arena);
311
312 template <typename RefWrappedType>
SetArenaStringPtr313 void Set(std::reference_wrapper<RefWrappedType> const_string_ref,
314 ::google::protobuf::Arena* arena) {
315 Set(const_string_ref.get(), arena);
316 }
317
318 // Returns a mutable std::string reference.
319 // The version accepting a `LazyString` value is used in the generated code to
320 // initialize mutable copies for fields with a non-empty default where the
321 // default value is lazily initialized.
322 std::string* Mutable(Arena* arena);
323 std::string* Mutable(const LazyString& default_value, Arena* arena);
324
325 // Gets a mutable pointer with unspecified contents.
326 // This function is identical to Mutable(), except it is optimized for the
327 // case where the caller is not interested in the current contents. For
328 // example, if the current field is not mutable, it will re-initialize the
329 // value with an empty string rather than a (non-empty) default value.
330 // Likewise, if the current value is a fixed size arena string with contents,
331 // it will be initialized into an empty mutable arena string.
332 std::string* MutableNoCopy(Arena* arena);
333
334 // Basic accessors.
GetArenaStringPtr335 PROTOBUF_NDEBUG_INLINE const std::string& Get() const {
336 // Unconditionally mask away the tag.
337 return *tagged_ptr_.Get();
338 }
339
340 // Returns a pointer to the stored contents for this instance.
341 // This method is for internal debugging and tracking purposes only.
UnsafeGetPointerArenaStringPtr342 PROTOBUF_NDEBUG_INLINE const std::string* UnsafeGetPointer() const
343 ABSL_ATTRIBUTE_RETURNS_NONNULL {
344 return tagged_ptr_.Get();
345 }
346
347 // Release returns a std::string* instance that is heap-allocated and is not
348 // Own()'d by any arena. If the field is not set, this returns nullptr. The
349 // caller retains ownership. Clears this field back to the default state.
350 // Used to implement release_<field>() methods on generated classes.
351 PROTOBUF_NODISCARD std::string* Release();
352
353 // Takes a std::string that is heap-allocated, and takes ownership. The
354 // std::string's destructor is registered with the arena. Used to implement
355 // set_allocated_<field> in generated classes.
356 void SetAllocated(std::string* value, Arena* arena);
357
358 // Frees storage (if not on an arena).
359 void Destroy();
360
361 // Clears content, but keeps allocated std::string, to avoid the overhead of
362 // heap operations. After this returns, the content (as seen by the user) will
363 // always be the empty std::string. Assumes that |default_value| is an empty
364 // std::string.
365 void ClearToEmpty();
366
367 // Clears content, assuming that the current value is not the empty
368 // string default.
369 void ClearNonDefaultToEmpty();
370
371 // Clears content, but keeps allocated std::string if arena != nullptr, to
372 // avoid the overhead of heap operations. After this returns, the content
373 // (as seen by the user) will always be equal to |default_value|.
374 void ClearToDefault(const LazyString& default_value, ::google::protobuf::Arena* arena);
375
376 // Swaps internal pointers. Arena-safety semantics: this is guarded by the
377 // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is
378 // 'unsafe' if called directly.
379 inline PROTOBUF_NDEBUG_INLINE static void InternalSwap(ArenaStringPtr* rhs,
380 ArenaStringPtr* lhs,
381 Arena* arena);
382
383 // Internal setter used only at parse time to directly set a donated string
384 // value.
UnsafeSetTaggedPointerArenaStringPtr385 void UnsafeSetTaggedPointer(TaggedStringPtr value) { tagged_ptr_ = value; }
386 // Generated code only! An optimization, in certain cases the generated
387 // code is certain we can obtain a std::string with no default checks and
388 // tag tests.
389 std::string* UnsafeMutablePointer() ABSL_ATTRIBUTE_RETURNS_NONNULL;
390
391 // Returns true if this instances holds an immutable default value.
IsDefaultArenaStringPtr392 inline bool IsDefault() const { return tagged_ptr_.IsDefault(); }
393
394 private:
395 template <typename... Args>
NewStringArenaStringPtr396 inline std::string* NewString(Arena* arena, Args&&... args) {
397 if (arena == nullptr) {
398 auto* s = new std::string(std::forward<Args>(args)...);
399 return tagged_ptr_.SetAllocated(s);
400 } else {
401 auto* s = Arena::Create<std::string>(arena, std::forward<Args>(args)...);
402 return tagged_ptr_.SetMutableArena(s);
403 }
404 }
405
406 TaggedStringPtr tagged_ptr_;
407
IsFixedSizeArenaArenaStringPtr408 bool IsFixedSizeArena() const { return false; }
409
410 // Swaps tagged pointer without debug hardening. This is to allow python
411 // protobuf to maintain pointer stability even in DEBUG builds.
UnsafeShallowSwapArenaStringPtr412 inline PROTOBUF_NDEBUG_INLINE static void UnsafeShallowSwap(
413 ArenaStringPtr* rhs, ArenaStringPtr* lhs) {
414 std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
415 }
416
417 friend class ::google::protobuf::internal::SwapFieldHelper;
418 friend class TcParser;
419
420 // Slow paths.
421
422 // MutableSlow requires that !IsString() || IsDefault
423 // Variadic to support 0 args for empty default and 1 arg for LazyString.
424 template <typename... Lazy>
425 std::string* MutableSlow(::google::protobuf::Arena* arena, const Lazy&... lazy_default);
426
427 friend class EpsCopyInputStream;
428 };
429
Copy(Arena * arena)430 inline TaggedStringPtr TaggedStringPtr::Copy(Arena* arena) const {
431 if (DebugHardenForceCopyDefaultString()) {
432 // Harden by forcing an allocated string value.
433 return IsNull() ? *this : ForceCopy(arena);
434 }
435 return IsDefault() ? *this : ForceCopy(arena);
436 }
437
Copy(Arena * arena,const LazyString & default_value)438 inline TaggedStringPtr TaggedStringPtr::Copy(
439 Arena* arena, const LazyString& default_value) const {
440 if (DebugHardenForceCopyDefaultString()) {
441 // Harden by forcing an allocated string value.
442 TaggedStringPtr hardened(*this);
443 if (IsDefault()) {
444 hardened.SetDefault(&default_value.get());
445 }
446 return hardened.ForceCopy(arena);
447 }
448 return IsDefault() ? *this : ForceCopy(arena);
449 }
450
InitDefault()451 inline void ArenaStringPtr::InitDefault() {
452 tagged_ptr_ = TaggedStringPtr(&fixed_address_empty_string);
453 }
454
InitExternal(const std::string * str)455 inline void ArenaStringPtr::InitExternal(const std::string* str) {
456 tagged_ptr_.SetDefault(str);
457 }
458
InitAllocated(std::string * str,Arena * arena)459 inline void ArenaStringPtr::InitAllocated(std::string* str, Arena* arena) {
460 if (arena != nullptr) {
461 tagged_ptr_.SetMutableArena(str);
462 arena->Own(str);
463 } else {
464 tagged_ptr_.SetAllocated(str);
465 }
466 }
467
Set(const char * s,Arena * arena)468 inline void ArenaStringPtr::Set(const char* s, Arena* arena) {
469 Set(absl::string_view{s}, arena);
470 }
471
Set(const char * s,size_t n,Arena * arena)472 inline void ArenaStringPtr::Set(const char* s, size_t n, Arena* arena) {
473 Set(absl::string_view{s, n}, arena);
474 }
475
SetBytes(absl::string_view value,Arena * arena)476 inline void ArenaStringPtr::SetBytes(absl::string_view value, Arena* arena) {
477 Set(value, arena);
478 }
479
480 template <>
481 PROTOBUF_EXPORT void ArenaStringPtr::Set(const std::string& value,
482 Arena* arena);
483
484 template <>
SetBytes(const std::string & value,Arena * arena)485 inline void ArenaStringPtr::SetBytes(const std::string& value, Arena* arena) {
486 Set(value, arena);
487 }
488
SetBytes(std::string && value,Arena * arena)489 inline void ArenaStringPtr::SetBytes(std::string&& value, Arena* arena) {
490 Set(std::move(value), arena);
491 }
492
SetBytes(const char * s,Arena * arena)493 inline void ArenaStringPtr::SetBytes(const char* s, Arena* arena) {
494 Set(s, arena);
495 }
496
SetBytes(const void * p,size_t n,Arena * arena)497 inline void ArenaStringPtr::SetBytes(const void* p, size_t n, Arena* arena) {
498 Set(absl::string_view{static_cast<const char*>(p), n}, arena);
499 }
500
InternalSwap(ArenaStringPtr * rhs,ArenaStringPtr * lhs,Arena * arena)501 inline PROTOBUF_NDEBUG_INLINE void ArenaStringPtr::InternalSwap(
502 ArenaStringPtr* rhs, ArenaStringPtr* lhs, Arena* arena) {
503 // Silence unused variable warnings in release buildls.
504 (void)arena;
505 std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
506 if (internal::DebugHardenForceCopyInSwap()) {
507 for (auto* p : {lhs, rhs}) {
508 if (p->IsDefault()) continue;
509 std::string* old_value = p->tagged_ptr_.Get();
510 std::string* new_value =
511 p->IsFixedSizeArena()
512 ? Arena::Create<std::string>(arena, *old_value)
513 : Arena::Create<std::string>(arena, std::move(*old_value));
514 if (arena == nullptr) {
515 delete old_value;
516 p->tagged_ptr_.SetAllocated(new_value);
517 } else {
518 p->tagged_ptr_.SetMutableArena(new_value);
519 }
520 }
521 }
522 }
523
ClearNonDefaultToEmpty()524 inline void ArenaStringPtr::ClearNonDefaultToEmpty() {
525 // Unconditionally mask away the tag.
526 tagged_ptr_.Get()->clear();
527 }
528
UnsafeMutablePointer()529 inline std::string* ArenaStringPtr::UnsafeMutablePointer() {
530 ABSL_DCHECK(tagged_ptr_.IsMutable());
531 ABSL_DCHECK(tagged_ptr_.Get() != nullptr);
532 return tagged_ptr_.Get();
533 }
534
535
536 } // namespace internal
537 } // namespace protobuf
538 } // namespace google
539
540 #include "google/protobuf/port_undef.inc"
541
542 #endif // GOOGLE_PROTOBUF_ARENASTRING_H__
543