debugallocation_test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2007, 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: Fred Akalin
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h> // for memcmp
  35. #include <vector>
  36. #include "gperftools/malloc_extension.h"
  37. #include "gperftools/tcmalloc.h"
  38. #include "base/logging.h"
  39. using std::vector;
  40. vector<void (*)()> g_testlist; // the tests to run
  41. #define TEST(a, b) \
  42. struct Test_##a##_##b { \
  43. Test_##a##_##b() { g_testlist.push_back(&Run); } \
  44. static void Run(); \
  45. }; \
  46. static Test_##a##_##b g_test_##a##_##b; \
  47. void Test_##a##_##b::Run()
  48. static int RUN_ALL_TESTS() {
  49. vector<void (*)()>::const_iterator it;
  50. for (it = g_testlist.begin(); it != g_testlist.end(); ++it) {
  51. (*it)(); // The test will error-exit if there's a problem.
  52. }
  53. fprintf(stderr, "\nPassed %d tests\n\nPASS\n",
  54. static_cast<int>(g_testlist.size()));
  55. return 0;
  56. }
  57. // The death tests are meant to be run from a shell-script driver, which
  58. // passes in an integer saying which death test to run. We store that
  59. // test-to-run here, and in the macro use a counter to see when we get
  60. // to that test, so we can run it.
  61. static int test_to_run = 0; // set in main() based on argv
  62. static int test_counter = 0; // incremented every time the macro is called
  63. #define IF_DEBUG_EXPECT_DEATH(statement, regex) do { \
  64. if (test_counter++ == test_to_run) { \
  65. fprintf(stderr, "Expected regex:%s\n", regex); \
  66. statement; \
  67. } \
  68. } while (false)
  69. // This flag won't be compiled in in opt mode.
  70. DECLARE_int32(max_free_queue_size);
  71. // Test match as well as mismatch rules. But do not test on OS X; on
  72. // OS X the OS converts new/new[] to malloc before it gets to us, so
  73. // we are unable to catch these mismatch errors.
  74. #ifndef __APPLE__
  75. TEST(DebugAllocationTest, DeallocMismatch) {
  76. // malloc can be matched only by free
  77. // new can be matched only by delete and delete(nothrow)
  78. // new[] can be matched only by delete[] and delete[](nothrow)
  79. // new(nothrow) can be matched only by delete and delete(nothrow)
  80. // new(nothrow)[] can be matched only by delete[] and delete[](nothrow)
  81. // Allocate with malloc.
  82. {
  83. int* x = static_cast<int*>(malloc(sizeof(*x)));
  84. IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete");
  85. IF_DEBUG_EXPECT_DEATH(delete [] x, "mismatch.*being dealloc.*delete *[[]");
  86. // Should work fine.
  87. free(x);
  88. }
  89. // Allocate with new.
  90. {
  91. int* x = new int;
  92. int* y = new int;
  93. IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free");
  94. IF_DEBUG_EXPECT_DEATH(delete [] x, "mismatch.*being dealloc.*delete *[[]");
  95. delete x;
  96. ::operator delete(y, std::nothrow);
  97. }
  98. // Allocate with new[].
  99. {
  100. int* x = new int[1];
  101. int* y = new int[1];
  102. IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free");
  103. IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete");
  104. delete [] x;
  105. ::operator delete[](y, std::nothrow);
  106. }
  107. // Allocate with new(nothrow).
  108. {
  109. int* x = new(std::nothrow) int;
  110. int* y = new(std::nothrow) int;
  111. IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free");
  112. IF_DEBUG_EXPECT_DEATH(delete [] x, "mismatch.*being dealloc.*delete *[[]");
  113. delete x;
  114. ::operator delete(y, std::nothrow);
  115. }
  116. // Allocate with new(nothrow)[].
  117. {
  118. int* x = new(std::nothrow) int[1];
  119. int* y = new(std::nothrow) int[1];
  120. IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free");
  121. IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete");
  122. delete [] x;
  123. ::operator delete[](y, std::nothrow);
  124. }
  125. }
  126. #endif // #ifdef OS_MACOSX
  127. TEST(DebugAllocationTest, DoubleFree) {
  128. int* pint = new int;
  129. delete pint;
  130. IF_DEBUG_EXPECT_DEATH(delete pint, "has been already deallocated");
  131. }
  132. TEST(DebugAllocationTest, StompBefore) {
  133. int* pint = new int;
  134. #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it
  135. pint[-1] = 5;
  136. IF_DEBUG_EXPECT_DEATH(delete pint, "a word before object");
  137. #endif
  138. }
  139. TEST(DebugAllocationTest, StompAfter) {
  140. int* pint = new int;
  141. #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it
  142. pint[1] = 5;
  143. IF_DEBUG_EXPECT_DEATH(delete pint, "a word after object");
  144. #endif
  145. }
  146. TEST(DebugAllocationTest, FreeQueueTest) {
  147. // Verify that the allocator doesn't return blocks that were recently freed.
  148. int* x = new int;
  149. int* old_x = x;
  150. delete x;
  151. x = new int;
  152. #if 1
  153. // This check should not be read as a universal guarantee of behavior. If
  154. // other threads are executing, it would be theoretically possible for this
  155. // check to fail despite the efforts of debugallocation.cc to the contrary.
  156. // It should always hold under the controlled conditions of this unittest,
  157. // however.
  158. EXPECT_NE(x, old_x); // Allocator shouldn't return recently freed blocks
  159. #else
  160. // The below check passes, but since it isn't *required* to pass, I've left
  161. // it commented out.
  162. // EXPECT_EQ(x, old_x);
  163. #endif
  164. old_x = NULL; // avoid breaking opt build with an unused variable warning.
  165. delete x;
  166. }
  167. TEST(DebugAllocationTest, DanglingPointerWriteTest) {
  168. // This test can only be run if debugging.
  169. //
  170. // If not debugging, the 'new' following the dangling write might not be
  171. // safe. When debugging, we expect the (trashed) deleted block to be on the
  172. // list of recently-freed blocks, so the following 'new' will be safe.
  173. #if 1
  174. int* x = new int;
  175. delete x;
  176. int poisoned_x_value = *x;
  177. *x = 1; // a dangling write.
  178. char* s = new char[FLAGS_max_free_queue_size];
  179. // When we delete s, we push the storage that was previously allocated to x
  180. // off the end of the free queue. At that point, the write to that memory
  181. // will be detected.
  182. IF_DEBUG_EXPECT_DEATH(delete [] s, "Memory was written to after being freed.");
  183. // restore the poisoned value of x so that we can delete s without causing a
  184. // crash.
  185. *x = poisoned_x_value;
  186. delete [] s;
  187. #endif
  188. }
  189. TEST(DebugAllocationTest, DanglingWriteAtExitTest) {
  190. int *x = new int;
  191. delete x;
  192. int old_x_value = *x;
  193. *x = 1;
  194. // verify that dangling writes are caught at program termination if the
  195. // corrupted block never got pushed off of the end of the free queue.
  196. IF_DEBUG_EXPECT_DEATH(exit(0), "Memory was written to after being freed.");
  197. *x = old_x_value; // restore x so that the test can exit successfully.
  198. }
  199. TEST(DebugAllocationTest, StackTraceWithDanglingWriteAtExitTest) {
  200. int *x = new int;
  201. delete x;
  202. int old_x_value = *x;
  203. *x = 1;
  204. // verify that we also get a stack trace when we have a dangling write.
  205. // The " @ " is part of the stack trace output.
  206. IF_DEBUG_EXPECT_DEATH(exit(0), " @ .*main");
  207. *x = old_x_value; // restore x so that the test can exit successfully.
  208. }
  209. static size_t CurrentlyAllocatedBytes() {
  210. size_t value;
  211. CHECK(MallocExtension::instance()->GetNumericProperty(
  212. "generic.current_allocated_bytes", &value));
  213. return value;
  214. }
  215. TEST(DebugAllocationTest, CurrentlyAllocated) {
  216. // Clear the free queue
  217. #if 1
  218. FLAGS_max_free_queue_size = 0;
  219. // Force a round-trip through the queue management code so that the
  220. // new size is seen and the queue of recently-freed blocks is flushed.
  221. free(malloc(1));
  222. FLAGS_max_free_queue_size = 1048576;
  223. #endif
  224. // Free something and check that it disappears from allocated bytes
  225. // immediately.
  226. char* p = new char[1000];
  227. size_t after_malloc = CurrentlyAllocatedBytes();
  228. delete[] p;
  229. size_t after_free = CurrentlyAllocatedBytes();
  230. EXPECT_LE(after_free, after_malloc - 1000);
  231. }
  232. TEST(DebugAllocationTest, GetAllocatedSizeTest) {
  233. #if 1
  234. // When debug_allocation is in effect, GetAllocatedSize should return
  235. // exactly requested size, since debug_allocation doesn't allow users
  236. // to write more than that.
  237. for (int i = 0; i < 10; ++i) {
  238. void *p = malloc(i);
  239. EXPECT_EQ(i, MallocExtension::instance()->GetAllocatedSize(p));
  240. free(p);
  241. }
  242. #endif
  243. void* a = malloc(1000);
  244. EXPECT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000);
  245. // This is just a sanity check. If we allocated too much, alloc is broken
  246. EXPECT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000);
  247. EXPECT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000);
  248. free(a);
  249. }
  250. TEST(DebugAllocationTest, HugeAlloc) {
  251. // This must not be a const variable so it doesn't form an
  252. // integral-constant-expression which can be *statically* rejected by the
  253. // compiler as too large for the allocation.
  254. size_t kTooBig = ~static_cast<size_t>(0);
  255. void* a = NULL;
  256. #ifndef NDEBUG
  257. a = malloc(kTooBig);
  258. EXPECT_EQ(NULL, a);
  259. // kAlsoTooBig is small enough not to get caught by debugallocation's check,
  260. // but will still fall through to tcmalloc's check. This must also be
  261. // a non-const variable. See kTooBig for more details.
  262. size_t kAlsoTooBig = kTooBig - 1024;
  263. a = malloc(kAlsoTooBig);
  264. EXPECT_EQ(NULL, a);
  265. #endif
  266. }
  267. // based on test program contributed by mikesart@gmail.com aka
  268. // mikesart@valvesoftware.com. See issue-464.
  269. TEST(DebugAllocationTest, ReallocAfterMemalign) {
  270. char stuff[50];
  271. memset(stuff, 0x11, sizeof(stuff));
  272. void *p = tc_memalign(16, sizeof(stuff));
  273. EXPECT_NE(p, NULL);
  274. memcpy(stuff, p, sizeof(stuff));
  275. p = realloc(p, sizeof(stuff) + 10);
  276. EXPECT_NE(p, NULL);
  277. int rv = memcmp(stuff, p, sizeof(stuff));
  278. EXPECT_EQ(rv, 0);
  279. }
  280. int main(int argc, char** argv) {
  281. // If you run without args, we run the non-death parts of the test.
  282. // Otherwise, argv[1] should be a number saying which death-test
  283. // to run. We will output a regexp we expect the death-message
  284. // to include, and then run the given death test (which hopefully
  285. // will produce that error message). If argv[1] > the number of
  286. // death tests, we will run only the non-death parts. One way to
  287. // tell when you are done with all tests is when no 'expected
  288. // regexp' message is printed for a given argv[1].
  289. if (argc < 2) {
  290. test_to_run = -1; // will never match
  291. } else {
  292. test_to_run = atoi(argv[1]);
  293. }
  294. return RUN_ALL_TESTS();
  295. }