arena_test_util.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__
  31. #define GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__
  32. #include <google/protobuf/stubs/logging.h>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/arena.h>
  35. namespace google {
  36. namespace protobuf {
  37. template <typename T, bool use_arena>
  38. void TestParseCorruptedString(const T& message) {
  39. int success_count = 0;
  40. string s = message.SerializeAsString();
  41. const int kMaxIters = 900;
  42. const int stride = s.size() <= kMaxIters ? 1 : s.size() / kMaxIters;
  43. const int start = stride == 1 || use_arena ? 0 : (stride + 1) / 2;
  44. for (int i = start; i < s.size(); i += stride) {
  45. for (int c = 1 + (i % 17); c < 256; c += 2 * c + (i & 3)) {
  46. s[i] ^= c;
  47. google::protobuf::Arena arena;
  48. T* message =
  49. google::protobuf::Arena::CreateMessage<T>(use_arena ? &arena : nullptr);
  50. if (message->ParseFromString(s)) {
  51. ++success_count;
  52. }
  53. if (!use_arena) {
  54. delete message;
  55. }
  56. s[i] ^= c; // Restore s to its original state.
  57. }
  58. }
  59. // This next line is a low bar. But getting through the test without crashing
  60. // due to use-after-free or other bugs is a big part of what we're checking.
  61. GOOGLE_CHECK_GT(success_count, 0);
  62. }
  63. namespace internal {
  64. class NoHeapChecker {
  65. public:
  66. NoHeapChecker() {
  67. capture_alloc.Hook();
  68. }
  69. ~NoHeapChecker();
  70. private:
  71. class NewDeleteCapture {
  72. public:
  73. // TOOD(xiaofeng): Implement this for opensource protobuf.
  74. void Hook() {}
  75. void Unhook() {}
  76. int alloc_count() { return 0; }
  77. int free_count() { return 0; }
  78. } capture_alloc;
  79. };
  80. } // namespace internal
  81. } // namespace protobuf
  82. } // namespace google
  83. #endif // GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__