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 // Authors: wink@google.com (Wink Saville),
32 //          kenton@google.com (Kenton Varda)
33 //  Based on original Protocol Buffers design by
34 //  Sanjay Ghemawat, Jeff Dean, and others.
35 
36 #include <google/protobuf/message_lite.h>
37 
38 #include <climits>
39 #include <cstdint>
40 #include <string>
41 
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/stubs/stringprintf.h>
45 #include <google/protobuf/parse_context.h>
46 #include <google/protobuf/io/coded_stream.h>
47 #include <google/protobuf/io/zero_copy_stream.h>
48 #include <google/protobuf/io/zero_copy_stream_impl.h>
49 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
50 #include <google/protobuf/arena.h>
51 #include <google/protobuf/generated_message_table_driven.h>
52 #include <google/protobuf/generated_message_util.h>
53 #include <google/protobuf/repeated_field.h>
54 #include <google/protobuf/stubs/strutil.h>
55 #include <google/protobuf/stubs/stl_util.h>
56 #include <google/protobuf/stubs/mutex.h>
57 
58 #include <google/protobuf/port_def.inc>
59 
60 namespace google {
61 namespace protobuf {
62 
InitializationErrorString() const63 std::string MessageLite::InitializationErrorString() const {
64   return "(cannot determine missing fields for lite message)";
65 }
66 
DebugString() const67 std::string MessageLite::DebugString() const {
68   std::uintptr_t address = reinterpret_cast<std::uintptr_t>(this);
69   return StrCat("MessageLite at 0x", strings::Hex(address));
70 }
71 
72 namespace {
73 
74 // When serializing, we first compute the byte size, then serialize the message.
75 // If serialization produces a different number of bytes than expected, we
76 // call this function, which crashes.  The problem could be due to a bug in the
77 // protobuf implementation but is more likely caused by concurrent modification
78 // of the message.  This function attempts to distinguish between the two and
79 // provide a useful error message.
ByteSizeConsistencyError(size_t byte_size_before_serialization,size_t byte_size_after_serialization,size_t bytes_produced_by_serialization,const MessageLite & message)80 void ByteSizeConsistencyError(size_t byte_size_before_serialization,
81                               size_t byte_size_after_serialization,
82                               size_t bytes_produced_by_serialization,
83                               const MessageLite& message) {
84   GOOGLE_CHECK_EQ(byte_size_before_serialization, byte_size_after_serialization)
85       << message.GetTypeName()
86       << " was modified concurrently during serialization.";
87   GOOGLE_CHECK_EQ(bytes_produced_by_serialization, byte_size_before_serialization)
88       << "Byte size calculation and serialization were inconsistent.  This "
89          "may indicate a bug in protocol buffers or it may be caused by "
90          "concurrent modification of "
91       << message.GetTypeName() << ".";
92   GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal.";
93 }
94 
InitializationErrorMessage(const char * action,const MessageLite & message)95 std::string InitializationErrorMessage(const char* action,
96                                        const MessageLite& message) {
97   // Note:  We want to avoid depending on strutil in the lite library, otherwise
98   //   we'd use:
99   //
100   // return strings::Substitute(
101   //   "Can't $0 message of type \"$1\" because it is missing required "
102   //   "fields: $2",
103   //   action, message.GetTypeName(),
104   //   message.InitializationErrorString());
105 
106   std::string result;
107   result += "Can't ";
108   result += action;
109   result += " message of type \"";
110   result += message.GetTypeName();
111   result += "\" because it is missing required fields: ";
112   result += message.InitializationErrorString();
113   return result;
114 }
115 
as_string_view(const void * data,int size)116 inline StringPiece as_string_view(const void* data, int size) {
117   return StringPiece(static_cast<const char*>(data), size);
118 }
119 
120 }  // namespace
121 
LogInitializationErrorMessage() const122 void MessageLite::LogInitializationErrorMessage() const {
123   GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this);
124 }
125 
126 namespace internal {
127 
128 template <bool aliasing>
MergePartialFromImpl(StringPiece input,MessageLite * msg)129 bool MergePartialFromImpl(StringPiece input, MessageLite* msg) {
130   const char* ptr;
131   internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
132                              aliasing, &ptr, input);
133   ptr = msg->_InternalParse(ptr, &ctx);
134   // ctx has an explicit limit set (length of string_view).
135   return ptr && ctx.EndedAtLimit();
136 }
137 
138 template <bool aliasing>
MergePartialFromImpl(io::ZeroCopyInputStream * input,MessageLite * msg)139 bool MergePartialFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg) {
140   const char* ptr;
141   internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
142                              aliasing, &ptr, input);
143   ptr = msg->_InternalParse(ptr, &ctx);
144   // ctx has no explicit limit (hence we end on end of stream)
145   return ptr && ctx.EndedAtEndOfStream();
146 }
147 
148 template <bool aliasing>
MergePartialFromImpl(BoundedZCIS input,MessageLite * msg)149 bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg) {
150   const char* ptr;
151   internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
152                              aliasing, &ptr, input.zcis, input.limit);
153   ptr = msg->_InternalParse(ptr, &ctx);
154   if (PROTOBUF_PREDICT_FALSE(!ptr)) return false;
155   ctx.BackUp(ptr);
156   return ctx.EndedAtLimit();
157 }
158 
159 template bool MergePartialFromImpl<false>(StringPiece input,
160                                           MessageLite* msg);
161 template bool MergePartialFromImpl<true>(StringPiece input,
162                                          MessageLite* msg);
163 template bool MergePartialFromImpl<false>(io::ZeroCopyInputStream* input,
164                                           MessageLite* msg);
165 template bool MergePartialFromImpl<true>(io::ZeroCopyInputStream* input,
166                                          MessageLite* msg);
167 template bool MergePartialFromImpl<false>(BoundedZCIS input, MessageLite* msg);
168 template bool MergePartialFromImpl<true>(BoundedZCIS input, MessageLite* msg);
169 
170 }  // namespace internal
171 
New(Arena * arena) const172 MessageLite* MessageLite::New(Arena* arena) const {
173   MessageLite* message = New();
174   if (arena != NULL) {
175     arena->Own(message);
176   }
177   return message;
178 }
179 
180 class ZeroCopyCodedInputStream : public io::ZeroCopyInputStream {
181  public:
ZeroCopyCodedInputStream(io::CodedInputStream * cis)182   ZeroCopyCodedInputStream(io::CodedInputStream* cis) : cis_(cis) {}
Next(const void ** data,int * size)183   bool Next(const void** data, int* size) final {
184     if (!cis_->GetDirectBufferPointer(data, size)) return false;
185     cis_->Skip(*size);
186     return true;
187   }
BackUp(int count)188   void BackUp(int count) final { cis_->Advance(-count); }
Skip(int count)189   bool Skip(int count) final { return cis_->Skip(count); }
ByteCount() const190   int64_t ByteCount() const final { return 0; }
191 
aliasing_enabled()192   bool aliasing_enabled() { return cis_->aliasing_enabled_; }
193 
194  private:
195   io::CodedInputStream* cis_;
196 };
197 
MergePartialFromCodedStream(io::CodedInputStream * input)198 bool MessageLite::MergePartialFromCodedStream(io::CodedInputStream* input) {
199   ZeroCopyCodedInputStream zcis(input);
200   const char* ptr;
201   internal::ParseContext ctx(input->RecursionBudget(), zcis.aliasing_enabled(),
202                              &ptr, &zcis);
203   // MergePartialFromCodedStream allows terminating the wireformat by 0 or
204   // end-group tag. Leaving it up to the caller to verify correct ending by
205   // calling LastTagWas on input. We need to maintain this behavior.
206   ctx.TrackCorrectEnding();
207   ctx.data().pool = input->GetExtensionPool();
208   ctx.data().factory = input->GetExtensionFactory();
209   ptr = _InternalParse(ptr, &ctx);
210   if (PROTOBUF_PREDICT_FALSE(!ptr)) return false;
211   ctx.BackUp(ptr);
212   if (!ctx.EndedAtEndOfStream()) {
213     GOOGLE_DCHECK(ctx.LastTag() != 1);  // We can't end on a pushed limit.
214     if (ctx.IsExceedingLimit(ptr)) return false;
215     input->SetLastTag(ctx.LastTag());
216     return true;
217   }
218   input->SetConsumed();
219   return true;
220 }
221 
MergeFromCodedStream(io::CodedInputStream * input)222 bool MessageLite::MergeFromCodedStream(io::CodedInputStream* input) {
223   return MergePartialFromCodedStream(input) && IsInitializedWithErrors();
224 }
225 
ParseFromCodedStream(io::CodedInputStream * input)226 bool MessageLite::ParseFromCodedStream(io::CodedInputStream* input) {
227   Clear();
228   return MergeFromCodedStream(input);
229 }
230 
ParsePartialFromCodedStream(io::CodedInputStream * input)231 bool MessageLite::ParsePartialFromCodedStream(io::CodedInputStream* input) {
232   Clear();
233   return MergePartialFromCodedStream(input);
234 }
235 
ParseFromZeroCopyStream(io::ZeroCopyInputStream * input)236 bool MessageLite::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
237   return ParseFrom<kParse>(input);
238 }
239 
ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream * input)240 bool MessageLite::ParsePartialFromZeroCopyStream(
241     io::ZeroCopyInputStream* input) {
242   return ParseFrom<kParsePartial>(input);
243 }
244 
ParseFromFileDescriptor(int file_descriptor)245 bool MessageLite::ParseFromFileDescriptor(int file_descriptor) {
246   io::FileInputStream input(file_descriptor);
247   return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
248 }
249 
ParsePartialFromFileDescriptor(int file_descriptor)250 bool MessageLite::ParsePartialFromFileDescriptor(int file_descriptor) {
251   io::FileInputStream input(file_descriptor);
252   return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0;
253 }
254 
ParseFromIstream(std::istream * input)255 bool MessageLite::ParseFromIstream(std::istream* input) {
256   io::IstreamInputStream zero_copy_input(input);
257   return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
258 }
259 
ParsePartialFromIstream(std::istream * input)260 bool MessageLite::ParsePartialFromIstream(std::istream* input) {
261   io::IstreamInputStream zero_copy_input(input);
262   return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
263 }
264 
MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)265 bool MessageLite::MergePartialFromBoundedZeroCopyStream(
266     io::ZeroCopyInputStream* input, int size) {
267   return ParseFrom<kMergePartial>(internal::BoundedZCIS{input, size});
268 }
269 
MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)270 bool MessageLite::MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
271                                                  int size) {
272   return ParseFrom<kMerge>(internal::BoundedZCIS{input, size});
273 }
274 
ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)275 bool MessageLite::ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
276                                                  int size) {
277   return ParseFrom<kParse>(internal::BoundedZCIS{input, size});
278 }
279 
ParsePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)280 bool MessageLite::ParsePartialFromBoundedZeroCopyStream(
281     io::ZeroCopyInputStream* input, int size) {
282   return ParseFrom<kParsePartial>(internal::BoundedZCIS{input, size});
283 }
284 
ParseFromString(const std::string & data)285 bool MessageLite::ParseFromString(const std::string& data) {
286   return ParseFrom<kParse>(data);
287 }
288 
ParsePartialFromString(const std::string & data)289 bool MessageLite::ParsePartialFromString(const std::string& data) {
290   return ParseFrom<kParsePartial>(data);
291 }
292 
ParseFromArray(const void * data,int size)293 bool MessageLite::ParseFromArray(const void* data, int size) {
294   return ParseFrom<kParse>(as_string_view(data, size));
295 }
296 
ParsePartialFromArray(const void * data,int size)297 bool MessageLite::ParsePartialFromArray(const void* data, int size) {
298   return ParseFrom<kParsePartial>(as_string_view(data, size));
299 }
300 
MergeFromString(const std::string & data)301 bool MessageLite::MergeFromString(const std::string& data) {
302   return ParseFrom<kMerge>(data);
303 }
304 
305 
306 // ===================================================================
307 
SerializeToArrayImpl(const MessageLite & msg,uint8 * target,int size)308 inline uint8* SerializeToArrayImpl(const MessageLite& msg, uint8* target,
309                                    int size) {
310   constexpr bool debug = false;
311   if (debug) {
312     // Force serialization to a stream with a block size of 1, which forces
313     // all writes to the stream to cross buffers triggering all fallback paths
314     // in the unittests when serializing to string / array.
315     io::ArrayOutputStream stream(target, size, 1);
316     uint8* ptr;
317     io::EpsCopyOutputStream out(
318         &stream, io::CodedOutputStream::IsDefaultSerializationDeterministic(),
319         &ptr);
320     ptr = msg._InternalSerialize(ptr, &out);
321     out.Trim(ptr);
322     GOOGLE_DCHECK(!out.HadError() && stream.ByteCount() == size);
323     return target + size;
324   } else {
325     io::EpsCopyOutputStream out(
326         target, size,
327         io::CodedOutputStream::IsDefaultSerializationDeterministic());
328     auto res = msg._InternalSerialize(target, &out);
329     GOOGLE_DCHECK(target + size == res);
330     return res;
331   }
332 }
333 
SerializeWithCachedSizesToArray(uint8 * target) const334 uint8* MessageLite::SerializeWithCachedSizesToArray(uint8* target) const {
335   // We only optimize this when using optimize_for = SPEED.  In other cases
336   // we just use the CodedOutputStream path.
337   return SerializeToArrayImpl(*this, target, GetCachedSize());
338 }
339 
SerializeToCodedStream(io::CodedOutputStream * output) const340 bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const {
341   GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
342   return SerializePartialToCodedStream(output);
343 }
344 
SerializePartialToCodedStream(io::CodedOutputStream * output) const345 bool MessageLite::SerializePartialToCodedStream(
346     io::CodedOutputStream* output) const {
347   const size_t size = ByteSizeLong();  // Force size to be cached.
348   if (size > INT_MAX) {
349     GOOGLE_LOG(ERROR) << GetTypeName()
350                << " exceeded maximum protobuf size of 2GB: " << size;
351     return false;
352   }
353 
354   int original_byte_count = output->ByteCount();
355   SerializeWithCachedSizes(output);
356   if (output->HadError()) {
357     return false;
358   }
359   int final_byte_count = output->ByteCount();
360 
361   if (final_byte_count - original_byte_count != size) {
362     ByteSizeConsistencyError(size, ByteSizeLong(),
363                              final_byte_count - original_byte_count, *this);
364   }
365 
366   return true;
367 }
368 
SerializeToZeroCopyStream(io::ZeroCopyOutputStream * output) const369 bool MessageLite::SerializeToZeroCopyStream(
370     io::ZeroCopyOutputStream* output) const {
371   GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
372   return SerializePartialToZeroCopyStream(output);
373 }
374 
SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream * output) const375 bool MessageLite::SerializePartialToZeroCopyStream(
376     io::ZeroCopyOutputStream* output) const {
377   const size_t size = ByteSizeLong();  // Force size to be cached.
378   if (size > INT_MAX) {
379     GOOGLE_LOG(ERROR) << GetTypeName()
380                << " exceeded maximum protobuf size of 2GB: " << size;
381     return false;
382   }
383 
384   uint8* target;
385   io::EpsCopyOutputStream stream(
386       output, io::CodedOutputStream::IsDefaultSerializationDeterministic(),
387       &target);
388   target = _InternalSerialize(target, &stream);
389   stream.Trim(target);
390   if (stream.HadError()) return false;
391   return true;
392 }
393 
SerializeToFileDescriptor(int file_descriptor) const394 bool MessageLite::SerializeToFileDescriptor(int file_descriptor) const {
395   io::FileOutputStream output(file_descriptor);
396   return SerializeToZeroCopyStream(&output) && output.Flush();
397 }
398 
SerializePartialToFileDescriptor(int file_descriptor) const399 bool MessageLite::SerializePartialToFileDescriptor(int file_descriptor) const {
400   io::FileOutputStream output(file_descriptor);
401   return SerializePartialToZeroCopyStream(&output) && output.Flush();
402 }
403 
SerializeToOstream(std::ostream * output) const404 bool MessageLite::SerializeToOstream(std::ostream* output) const {
405   {
406     io::OstreamOutputStream zero_copy_output(output);
407     if (!SerializeToZeroCopyStream(&zero_copy_output)) return false;
408   }
409   return output->good();
410 }
411 
SerializePartialToOstream(std::ostream * output) const412 bool MessageLite::SerializePartialToOstream(std::ostream* output) const {
413   io::OstreamOutputStream zero_copy_output(output);
414   return SerializePartialToZeroCopyStream(&zero_copy_output);
415 }
416 
AppendToString(std::string * output) const417 bool MessageLite::AppendToString(std::string* output) const {
418   GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
419   return AppendPartialToString(output);
420 }
421 
AppendPartialToString(std::string * output) const422 bool MessageLite::AppendPartialToString(std::string* output) const {
423   size_t old_size = output->size();
424   size_t byte_size = ByteSizeLong();
425   if (byte_size > INT_MAX) {
426     GOOGLE_LOG(ERROR) << GetTypeName()
427                << " exceeded maximum protobuf size of 2GB: " << byte_size;
428     return false;
429   }
430 
431   STLStringResizeUninitialized(output, old_size + byte_size);
432   uint8* start =
433       reinterpret_cast<uint8*>(io::mutable_string_data(output) + old_size);
434   SerializeToArrayImpl(*this, start, byte_size);
435   return true;
436 }
437 
SerializeToString(std::string * output) const438 bool MessageLite::SerializeToString(std::string* output) const {
439   output->clear();
440   return AppendToString(output);
441 }
442 
SerializePartialToString(std::string * output) const443 bool MessageLite::SerializePartialToString(std::string* output) const {
444   output->clear();
445   return AppendPartialToString(output);
446 }
447 
SerializeToArray(void * data,int size) const448 bool MessageLite::SerializeToArray(void* data, int size) const {
449   GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
450   return SerializePartialToArray(data, size);
451 }
452 
SerializePartialToArray(void * data,int size) const453 bool MessageLite::SerializePartialToArray(void* data, int size) const {
454   const size_t byte_size = ByteSizeLong();
455   if (byte_size > INT_MAX) {
456     GOOGLE_LOG(ERROR) << GetTypeName()
457                << " exceeded maximum protobuf size of 2GB: " << byte_size;
458     return false;
459   }
460   if (size < byte_size) return false;
461   uint8* start = reinterpret_cast<uint8*>(data);
462   SerializeToArrayImpl(*this, start, byte_size);
463   return true;
464 }
465 
SerializeAsString() const466 std::string MessageLite::SerializeAsString() const {
467   // If the compiler implements the (Named) Return Value Optimization,
468   // the local variable 'output' will not actually reside on the stack
469   // of this function, but will be overlaid with the object that the
470   // caller supplied for the return value to be constructed in.
471   std::string output;
472   if (!AppendToString(&output)) output.clear();
473   return output;
474 }
475 
SerializePartialAsString() const476 std::string MessageLite::SerializePartialAsString() const {
477   std::string output;
478   if (!AppendPartialToString(&output)) output.clear();
479   return output;
480 }
481 
482 
483 namespace internal {
484 
485 template <>
NewFromPrototype(const MessageLite * prototype,Arena * arena)486 MessageLite* GenericTypeHandler<MessageLite>::NewFromPrototype(
487     const MessageLite* prototype, Arena* arena) {
488   return prototype->New(arena);
489 }
490 template <>
Merge(const MessageLite & from,MessageLite * to)491 void GenericTypeHandler<MessageLite>::Merge(const MessageLite& from,
492                                             MessageLite* to) {
493   to->CheckTypeAndMergeFrom(from);
494 }
495 template <>
Merge(const std::string & from,std::string * to)496 void GenericTypeHandler<std::string>::Merge(const std::string& from,
497                                             std::string* to) {
498   *to = from;
499 }
500 
501 }  // namespace internal
502 
503 
504 // ===================================================================
505 // Shutdown support.
506 
507 namespace internal {
508 
509 struct ShutdownData {
~ShutdownDatagoogle::protobuf::internal::ShutdownData510   ~ShutdownData() {
511     std::reverse(functions.begin(), functions.end());
512     for (auto pair : functions) pair.first(pair.second);
513   }
514 
getgoogle::protobuf::internal::ShutdownData515   static ShutdownData* get() {
516     static auto* data = new ShutdownData;
517     return data;
518   }
519 
520   std::vector<std::pair<void (*)(const void*), const void*>> functions;
521   Mutex mutex;
522 };
523 
RunZeroArgFunc(const void * arg)524 static void RunZeroArgFunc(const void* arg) {
525   void (*func)() = reinterpret_cast<void (*)()>(const_cast<void*>(arg));
526   func();
527 }
528 
OnShutdown(void (* func)())529 void OnShutdown(void (*func)()) {
530   OnShutdownRun(RunZeroArgFunc, reinterpret_cast<void*>(func));
531 }
532 
OnShutdownRun(void (* f)(const void *),const void * arg)533 void OnShutdownRun(void (*f)(const void*), const void* arg) {
534   auto shutdown_data = ShutdownData::get();
535   MutexLock lock(&shutdown_data->mutex);
536   shutdown_data->functions.push_back(std::make_pair(f, arg));
537 }
538 
539 }  // namespace internal
540 
ShutdownProtobufLibrary()541 void ShutdownProtobufLibrary() {
542   // This function should be called only once, but accepts multiple calls.
543   static bool is_shutdown = false;
544   if (!is_shutdown) {
545     delete internal::ShutdownData::get();
546     is_shutdown = true;
547   }
548 }
549 
550 
551 }  // namespace protobuf
552 }  // namespace google
553