tcmalloc_large_unittest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, Google Inc.
  3. // All rights reserved.
  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. // Author: Michael Chastain
  32. //
  33. // This is a unit test for large allocations in malloc and friends.
  34. // "Large" means "so large that they overflow the address space".
  35. // For 32 bits, this means allocations near 2^32 bytes and 2^31 bytes.
  36. // For 64 bits, this means allocations near 2^64 bytes and 2^63 bytes.
  37. #include <stddef.h> // for size_t, NULL
  38. #include <stdlib.h> // for malloc, free, realloc
  39. #include <stdio.h>
  40. #include <set> // for set, etc
  41. #include "base/logging.h" // for operator<<, CHECK, etc
  42. using std::set;
  43. // Alloc a size that should always fail.
  44. void TryAllocExpectFail(size_t size) {
  45. void* p1 = malloc(size);
  46. CHECK(p1 == NULL);
  47. void* p2 = malloc(1);
  48. CHECK(p2 != NULL);
  49. void* p3 = realloc(p2, size);
  50. CHECK(p3 == NULL);
  51. free(p2);
  52. }
  53. // Alloc a size that might work and might fail.
  54. // If it does work, touch some pages.
  55. void TryAllocMightFail(size_t size) {
  56. unsigned char* p = static_cast<unsigned char*>(malloc(size));
  57. if ( p != NULL ) {
  58. unsigned char volatile* vp = p; // prevent optimizations
  59. static const size_t kPoints = 1024;
  60. for ( size_t i = 0; i < kPoints; ++i ) {
  61. vp[i * (size / kPoints)] = static_cast<unsigned char>(i);
  62. }
  63. for ( size_t i = 0; i < kPoints; ++i ) {
  64. CHECK(vp[i * (size / kPoints)] == static_cast<unsigned char>(i));
  65. }
  66. vp[size-1] = 'M';
  67. CHECK(vp[size-1] == 'M');
  68. }
  69. free(p);
  70. }
  71. int main (int argc, char** argv) {
  72. // Allocate some 0-byte objects. They better be unique.
  73. // 0 bytes is not large but it exercises some paths related to
  74. // large-allocation code.
  75. {
  76. static const int kZeroTimes = 1024;
  77. printf("Test malloc(0) x %d\n", kZeroTimes);
  78. set<char*> p_set;
  79. for ( int i = 0; i < kZeroTimes; ++i ) {
  80. char* p = new char;
  81. CHECK(p != NULL);
  82. CHECK(p_set.find(p) == p_set.end());
  83. p_set.insert(p_set.end(), p);
  84. }
  85. // Just leak the memory.
  86. }
  87. // Grab some memory so that some later allocations are guaranteed to fail.
  88. printf("Test small malloc\n");
  89. void* p_small = malloc(4*1048576);
  90. CHECK(p_small != NULL);
  91. // Test sizes up near the maximum size_t.
  92. // These allocations test the wrap-around code.
  93. printf("Test malloc(0 - N)\n");
  94. const size_t zero = 0;
  95. static const size_t kMinusNTimes = 16384;
  96. for ( size_t i = 1; i < kMinusNTimes; ++i ) {
  97. TryAllocExpectFail(zero - i);
  98. }
  99. // Test sizes a bit smaller.
  100. // The small malloc above guarantees that all these return NULL.
  101. printf("Test malloc(0 - 1048576 - N)\n");
  102. static const size_t kMinusMBMinusNTimes = 16384;
  103. for ( size_t i = 0; i < kMinusMBMinusNTimes; ++i) {
  104. TryAllocExpectFail(zero - 1048576 - i);
  105. }
  106. // Test sizes at half of size_t.
  107. // These might or might not fail to allocate.
  108. printf("Test malloc(max/2 +- N)\n");
  109. static const size_t kHalfPlusMinusTimes = 64;
  110. const size_t half = (zero - 2) / 2 + 1;
  111. for ( size_t i = 0; i < kHalfPlusMinusTimes; ++i) {
  112. TryAllocMightFail(half - i);
  113. TryAllocMightFail(half + i);
  114. }
  115. printf("PASS\n");
  116. return 0;
  117. }