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> |
34 | 37 |
35 namespace google { | 38 namespace google { |
36 namespace protobuf { | 39 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 |
37 namespace internal { | 68 namespace internal { |
38 | 69 |
39 class NoHeapChecker { | 70 class NoHeapChecker { |
40 public: | 71 public: |
41 NoHeapChecker() { | 72 NoHeapChecker() { |
42 capture_alloc.Hook(); | 73 capture_alloc.Hook(); |
43 } | 74 } |
44 ~NoHeapChecker(); | 75 ~NoHeapChecker(); |
45 private: | 76 private: |
46 class NewDeleteCapture { | 77 class NewDeleteCapture { |
47 public: | 78 public: |
48 // TOOD(xiaofeng): Implement this for opensource protobuf. | 79 // TOOD(xiaofeng): Implement this for opensource protobuf. |
49 void Hook() {} | 80 void Hook() {} |
50 void Unhook() {} | 81 void Unhook() {} |
51 int alloc_count() { return 0; } | 82 int alloc_count() { return 0; } |
52 int free_count() { return 0; } | 83 int free_count() { return 0; } |
53 } capture_alloc; | 84 } capture_alloc; |
54 }; | 85 }; |
55 | 86 |
56 } // namespace internal | 87 } // namespace internal |
57 } // namespace protobuf | 88 } // namespace protobuf |
58 | 89 |
59 } // namespace google | 90 } // namespace google |
60 #endif // GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ | 91 #endif // GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ |
OLD | NEW |