malloc_extension_c_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
  2. /* Copyright (c) 2009, 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. * ---
  32. * Author: Craig Silverstein
  33. *
  34. * This tests the c shims: malloc_extension_c.h and malloc_hook_c.h.
  35. * Mostly, we'll just care that these shims compile under gcc
  36. * (*not* g++!)
  37. *
  38. * NOTE: this is C code, not C++ code!
  39. */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <stddef.h> /* for size_t */
  43. #include <gperftools/malloc_extension_c.h>
  44. #include <gperftools/malloc_hook_c.h>
  45. #define FAIL(msg) do { \
  46. fprintf(stderr, "FATAL ERROR: %s\n", msg); \
  47. exit(1); \
  48. } while (0)
  49. static int g_new_hook_calls = 0;
  50. static int g_delete_hook_calls = 0;
  51. void TestNewHook(const void* ptr, size_t size) {
  52. g_new_hook_calls++;
  53. }
  54. void TestDeleteHook(const void* ptr) {
  55. g_delete_hook_calls++;
  56. }
  57. static
  58. void *forced_malloc(size_t size)
  59. {
  60. extern void *tc_malloc(size_t);
  61. void *rv = tc_malloc(size);
  62. if (!rv) {
  63. FAIL("malloc is not supposed to fail here");
  64. }
  65. return rv;
  66. }
  67. void TestMallocHook(void) {
  68. /* TODO(csilvers): figure out why we get:
  69. * E0100 00:00:00.000000 7383 malloc_hook.cc:244] RAW: google_malloc section is missing, thus InHookCaller is broken!
  70. */
  71. #if 0
  72. void* result[5];
  73. if (MallocHook_GetCallerStackTrace(result, sizeof(result)/sizeof(*result),
  74. 0) < 2) { /* should have this and main */
  75. FAIL("GetCallerStackTrace failed");
  76. }
  77. #endif
  78. if (!MallocHook_AddNewHook(&TestNewHook)) {
  79. FAIL("Failed to add new hook");
  80. }
  81. if (!MallocHook_AddDeleteHook(&TestDeleteHook)) {
  82. FAIL("Failed to add delete hook");
  83. }
  84. free(forced_malloc(10));
  85. free(forced_malloc(20));
  86. if (g_new_hook_calls != 2) {
  87. FAIL("Wrong number of calls to the new hook");
  88. }
  89. if (g_delete_hook_calls != 2) {
  90. FAIL("Wrong number of calls to the delete hook");
  91. }
  92. if (!MallocHook_RemoveNewHook(&TestNewHook)) {
  93. FAIL("Failed to remove new hook");
  94. }
  95. if (!MallocHook_RemoveDeleteHook(&TestDeleteHook)) {
  96. FAIL("Failed to remove delete hook");
  97. }
  98. free(forced_malloc(10));
  99. free(forced_malloc(20));
  100. if (g_new_hook_calls != 2) {
  101. FAIL("Wrong number of calls to the new hook");
  102. }
  103. MallocHook_SetNewHook(&TestNewHook);
  104. MallocHook_SetDeleteHook(&TestDeleteHook);
  105. free(forced_malloc(10));
  106. free(forced_malloc(20));
  107. if (g_new_hook_calls != 4) {
  108. FAIL("Wrong number of calls to the singular new hook");
  109. }
  110. if (MallocHook_SetNewHook(NULL) == NULL) {
  111. FAIL("Failed to set new hook");
  112. }
  113. if (MallocHook_SetDeleteHook(NULL) == NULL) {
  114. FAIL("Failed to set delete hook");
  115. }
  116. }
  117. void TestMallocExtension(void) {
  118. int blocks;
  119. size_t total;
  120. int hist[64];
  121. char buffer[200];
  122. char* x = (char*)malloc(10);
  123. MallocExtension_VerifyAllMemory();
  124. MallocExtension_VerifyMallocMemory(x);
  125. MallocExtension_MallocMemoryStats(&blocks, &total, hist);
  126. MallocExtension_GetStats(buffer, sizeof(buffer));
  127. if (!MallocExtension_GetNumericProperty("generic.current_allocated_bytes",
  128. &total)) {
  129. FAIL("GetNumericProperty failed for generic.current_allocated_bytes");
  130. }
  131. if (total < 10) {
  132. FAIL("GetNumericProperty had bad return for generic.current_allocated_bytes");
  133. }
  134. if (!MallocExtension_GetNumericProperty("generic.current_allocated_bytes",
  135. &total)) {
  136. FAIL("GetNumericProperty failed for generic.current_allocated_bytes");
  137. }
  138. MallocExtension_MarkThreadIdle();
  139. MallocExtension_MarkThreadBusy();
  140. MallocExtension_ReleaseToSystem(1);
  141. MallocExtension_ReleaseFreeMemory();
  142. if (MallocExtension_GetEstimatedAllocatedSize(10) < 10) {
  143. FAIL("GetEstimatedAllocatedSize returned a bad value (too small)");
  144. }
  145. if (MallocExtension_GetAllocatedSize(x) < 10) {
  146. FAIL("GetEstimatedAllocatedSize returned a bad value (too small)");
  147. }
  148. if (MallocExtension_GetOwnership(x) != MallocExtension_kOwned) {
  149. FAIL("DidAllocatePtr returned a bad value (kNotOwned)");
  150. }
  151. /* TODO(csilvers): this relies on undocumented behavior that
  152. GetOwnership works on stack-allocated variables. Use a better test. */
  153. if (MallocExtension_GetOwnership(hist) != MallocExtension_kNotOwned) {
  154. FAIL("DidAllocatePtr returned a bad value (kOwned)");
  155. }
  156. free(x);
  157. }
  158. int main(int argc, char** argv) {
  159. TestMallocHook();
  160. TestMallocExtension();
  161. printf("PASS\n");
  162. return 0;
  163. }