protobuf/src/google/protobuf/arena_test_util.h
Go to the documentation of this file.
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 #ifndef GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__
32 #define GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__
33 
34 #include <google/protobuf/stubs/logging.h>
35 #include <google/protobuf/stubs/common.h>
36 #include <google/protobuf/io/coded_stream.h>
37 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
38 #include <google/protobuf/arena.h>
39 
40 namespace google {
41 namespace protobuf {
42 
43 template <typename T, bool use_arena>
44 void TestParseCorruptedString(const T& message) {
45  int success_count = 0;
46  std::string s;
47  {
48  // Map order is not deterministic. To make the test deterministic we want
49  // to serialize the proto deterministically.
52  out.SetSerializationDeterministic(true);
53  message.SerializePartialToCodedStream(&out);
54  }
55  const int kMaxIters = 900;
56  const int stride = s.size() <= kMaxIters ? 1 : s.size() / kMaxIters;
57  const int start = stride == 1 || use_arena ? 0 : (stride + 1) / 2;
58  for (int i = start; i < s.size(); i += stride) {
59  for (int c = 1 + (i % 17); c < 256; c += 2 * c + (i & 3)) {
60  s[i] ^= c;
61  Arena arena;
62  T* message = Arena::CreateMessage<T>(use_arena ? &arena : nullptr);
63  if (message->ParseFromString(s)) {
64  ++success_count;
65  }
66  if (!use_arena) {
67  delete message;
68  }
69  s[i] ^= c; // Restore s to its original state.
70  }
71  }
72  // This next line is a low bar. But getting through the test without crashing
73  // due to use-after-free or other bugs is a big part of what we're checking.
74  GOOGLE_CHECK_GT(success_count, 0);
75 }
76 
77 namespace internal {
78 
79 class NoHeapChecker {
80  public:
83 
84  private:
85  class NewDeleteCapture {
86  public:
87  // TODO(xiaofeng): Implement this for opensource protobuf.
88  void Hook() {}
89  void Unhook() {}
90  int alloc_count() { return 0; }
91  int free_count() { return 0; }
92  } capture_alloc;
93 };
94 
95 // Owns the internal T only if it's not owned by an arena.
96 // T needs to be arena constructible and destructor skippable.
97 template <typename T>
98 class ArenaHolder {
99  public:
101  : field_(Arena::CreateMessage<T>(arena)),
102  owned_by_arena_(arena != nullptr) {
105  }
106 
108  if (!owned_by_arena_) {
109  delete field_;
110  }
111  }
112 
113  T* get() { return field_; }
114  T* operator->() { return field_; }
115  T& operator*() { return *field_; }
116 
117  private:
120 };
121 
122 } // namespace internal
123 } // namespace protobuf
124 } // namespace google
125 
126 #endif // GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__
google::protobuf.internal::NoHeapChecker::NoHeapChecker
NoHeapChecker()
Definition: protobuf/src/google/protobuf/arena_test_util.h:81
google::protobuf.internal::ArenaHolder::ArenaHolder
ArenaHolder(Arena *arena)
Definition: protobuf/src/google/protobuf/arena_test_util.h:100
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
google::protobuf.internal::ArenaHolder::get
T * get()
Definition: protobuf/src/google/protobuf/arena_test_util.h:113
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
google::protobuf.internal::NoHeapChecker::NewDeleteCapture::Unhook
void Unhook()
Definition: protobuf/src/google/protobuf/arena_test_util.h:89
GOOGLE_DCHECK
#define GOOGLE_DCHECK
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:194
Arena
Definition: arena.c:39
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
absl::FormatConversionChar::s
@ s
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
T
#define T(upbtypeconst, upbtype, ctype, default_value)
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
google::protobuf::TestParseCorruptedString
void TestParseCorruptedString(const T &message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_test_util.h:44
google::protobuf.internal::NoHeapChecker::NewDeleteCapture::Hook
void Hook()
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_test_util.h:88
grpc::protobuf::io::StringOutputStream
GRPC_CUSTOM_STRINGOUTPUTSTREAM StringOutputStream
Definition: src/compiler/config.h:56
google::protobuf.internal::ArenaHolder
Definition: protobuf/src/google/protobuf/arena_test_util.h:98
start
static uint64_t start
Definition: benchmark-pound.c:74
google::protobuf.internal::ArenaHolder::operator*
T & operator*()
Definition: protobuf/src/google/protobuf/arena_test_util.h:115
GOOGLE_CHECK_GT
#define GOOGLE_CHECK_GT(A, B)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:160
google::protobuf.internal::ArenaHolder::operator->
T * operator->()
Definition: protobuf/src/google/protobuf/arena_test_util.h:114
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
google::protobuf.internal::NoHeapChecker::NewDeleteCapture::alloc_count
int alloc_count()
Definition: protobuf/src/google/protobuf/arena_test_util.h:90
google::protobuf.internal::NoHeapChecker::~NoHeapChecker
~NoHeapChecker()
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_test_util.cc:42
grpc::protobuf::io::CodedOutputStream
GRPC_CUSTOM_CODEDOUTPUTSTREAM CodedOutputStream
Definition: src/compiler/config.h:55
google::protobuf.internal::ArenaHolder::field_
T * field_
Definition: protobuf/src/google/protobuf/arena_test_util.h:118
value
const char * value
Definition: hpack_parser_table.cc:165
google::protobuf.internal::ArenaHolder::owned_by_arena_
bool owned_by_arena_
Definition: protobuf/src/google/protobuf/arena_test_util.h:119
internal
Definition: benchmark/test/output_test_helper.cc:20
google::protobuf.internal::NoHeapChecker::capture_alloc
class google::protobuf::internal::NoHeapChecker::NewDeleteCapture capture_alloc
google::protobuf.internal::NoHeapChecker::NewDeleteCapture::free_count
int free_count()
Definition: protobuf/src/google/protobuf/arena_test_util.h:91
google::protobuf.internal::ArenaHolder::~ArenaHolder
~ArenaHolder()
Definition: protobuf/src/google/protobuf/arena_test_util.h:107
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:42