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.
50
io::StringOutputStream
output
(&
s
);
51
io::CodedOutputStream
out(&
output
);
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
:
81
NoHeapChecker
() {
capture_alloc
.
Hook
(); }
82
~NoHeapChecker
();
83
84
private
:
85
class
NewDeleteCapture
{
86
public
:
87
// TOOD(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
}
// namespace internal
96
}
// namespace protobuf
97
}
// namespace google
98
99
#endif // GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__
google::protobuf.internal::NoHeapChecker::NoHeapChecker
NoHeapChecker()
Definition:
arena_test_util.h:81
google::protobuf.internal::NoHeapChecker::NewDeleteCapture::Unhook
void Unhook()
Definition:
arena_test_util.h:89
s
XmlRpcServer s
string
GLsizei const GLchar *const * string
Definition:
glcorearb.h:3083
google::protobuf::io::CodedOutputStream::SetSerializationDeterministic
void SetSerializationDeterministic(bool value)
Definition:
coded_stream.h:855
T
#define T(upbtypeconst, upbtype, ctype, default_value)
GOOGLE_CHECK_GT
#define GOOGLE_CHECK_GT(A, B)
Definition:
logging.h:160
google::protobuf::TestParseCorruptedString
void TestParseCorruptedString(const T &message)
Definition:
arena_test_util.h:44
google::protobuf.internal::NoHeapChecker::NewDeleteCapture::Hook
void Hook()
Definition:
arena_test_util.h:88
google::protobuf.internal::NoHeapChecker::NewDeleteCapture
Definition:
arena_test_util.h:85
stride
GLint GLenum GLboolean GLsizei stride
Definition:
glcorearb.h:3141
coded_stream.h
start
GLuint start
Definition:
glcorearb.h:2858
google::protobuf.internal::NoHeapChecker::NewDeleteCapture::alloc_count
int alloc_count()
Definition:
arena_test_util.h:90
google::protobuf.internal::NoHeapChecker::~NoHeapChecker
~NoHeapChecker()
Definition:
arena_test_util.cc:42
google::protobuf::io::CodedOutputStream
Definition:
coded_stream.h:693
i
int i
Definition:
gmock-matchers_test.cc:764
zero_copy_stream_impl_lite.h
google::protobuf::io::StringOutputStream
Definition:
zero_copy_stream_impl_lite.h:131
common.h
arena.h
logging.h
internal
Definition:
any.pb.h:40
google::protobuf.internal::NoHeapChecker
Definition:
arena_test_util.h:79
google::protobuf.internal::NoHeapChecker::capture_alloc
class google::protobuf::internal::NoHeapChecker::NewDeleteCapture capture_alloc
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition:
ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf.internal::NoHeapChecker::NewDeleteCapture::free_count
int free_count()
Definition:
arena_test_util.h:91
google
Definition:
data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition:
glcorearb.h:2695
libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48