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>
TestParseCorruptedString(const T & message)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:
NoHeapChecker()81   NoHeapChecker() { capture_alloc.Hook(); }
82   ~NoHeapChecker();
83 
84  private:
85   class NewDeleteCapture {
86    public:
87     // TODO(xiaofeng): Implement this for opensource protobuf.
Hook()88     void Hook() {}
Unhook()89     void Unhook() {}
alloc_count()90     int alloc_count() { return 0; }
free_count()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:
ArenaHolder(Arena * arena)100   explicit ArenaHolder(Arena* arena)
101       : field_(Arena::CreateMessage<T>(arena)),
102         owned_by_arena_(arena != nullptr) {
103     GOOGLE_DCHECK(google::protobuf::Arena::is_arena_constructable<T>::value);
104     GOOGLE_DCHECK(google::protobuf::Arena::is_destructor_skippable<T>::value);
105   }
106 
~ArenaHolder()107   ~ArenaHolder() {
108     if (!owned_by_arena_) {
109       delete field_;
110     }
111   }
112 
get()113   T* get() { return field_; }
114   T* operator->() { return field_; }
115   T& operator*() { return *field_; }
116 
117  private:
118   T* field_;
119   bool owned_by_arena_;
120 };
121 
122 }  // namespace internal
123 }  // namespace protobuf
124 }  // namespace google
125 
126 #endif  // GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__
127