OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 13 matching lines...) Expand all Loading... |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 #ifndef GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ | 31 #ifndef GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ |
32 #define GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ | 32 #define GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ |
33 | 33 |
34 #include <google/protobuf/stubs/logging.h> | |
35 #include <google/protobuf/stubs/common.h> | |
36 #include <google/protobuf/arena.h> | |
37 | 34 |
38 namespace google { | 35 namespace google { |
39 namespace protobuf { | 36 namespace protobuf { |
40 | |
41 template <typename T, bool use_arena> | |
42 void TestParseCorruptedString(const T& message) { | |
43 int success_count = 0; | |
44 string s = message.SerializeAsString(); | |
45 const int kMaxIters = 900; | |
46 const int stride = s.size() <= kMaxIters ? 1 : s.size() / kMaxIters; | |
47 const int start = stride == 1 || use_arena ? 0 : (stride + 1) / 2; | |
48 for (int i = start; i < s.size(); i += stride) { | |
49 for (int c = 1 + (i % 17); c < 256; c += 2 * c + (i & 3)) { | |
50 s[i] ^= c; | |
51 google::protobuf::Arena arena; | |
52 T* message = | |
53 google::protobuf::Arena::CreateMessage<T>(use_arena ? &arena : NULL); | |
54 if (message->ParseFromString(s)) { | |
55 ++success_count; | |
56 } | |
57 if (!use_arena) { | |
58 delete message; | |
59 } | |
60 s[i] ^= c; // Restore s to its original state. | |
61 } | |
62 } | |
63 // This next line is a low bar. But getting through the test without crashing | |
64 // due to use-after-free or other bugs is a big part of what we're checking. | |
65 GOOGLE_CHECK_GT(success_count, 0); | |
66 } | |
67 | |
68 namespace internal { | 37 namespace internal { |
69 | 38 |
70 class NoHeapChecker { | 39 class NoHeapChecker { |
71 public: | 40 public: |
72 NoHeapChecker() { | 41 NoHeapChecker() { |
73 capture_alloc.Hook(); | 42 capture_alloc.Hook(); |
74 } | 43 } |
75 ~NoHeapChecker(); | 44 ~NoHeapChecker(); |
76 private: | 45 private: |
77 class NewDeleteCapture { | 46 class NewDeleteCapture { |
78 public: | 47 public: |
79 // TOOD(xiaofeng): Implement this for opensource protobuf. | 48 // TOOD(xiaofeng): Implement this for opensource protobuf. |
80 void Hook() {} | 49 void Hook() {} |
81 void Unhook() {} | 50 void Unhook() {} |
82 int alloc_count() { return 0; } | 51 int alloc_count() { return 0; } |
83 int free_count() { return 0; } | 52 int free_count() { return 0; } |
84 } capture_alloc; | 53 } capture_alloc; |
85 }; | 54 }; |
86 | 55 |
87 } // namespace internal | 56 } // namespace internal |
88 } // namespace protobuf | 57 } // namespace protobuf |
89 | 58 |
90 } // namespace google | 59 } // namespace google |
91 #endif // GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ | 60 #endif // GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ |
OLD | NEW |