15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Copyright (c) 2009, Google Inc.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modification, are permitted provided that the following conditions are
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * met:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * in the documentation and/or other materials provided with the
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distribution.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * this software without specific prior written permission.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ---
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Author: Craig Silverstein
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This tests the c shims: malloc_extension_c.h and malloc_hook_c.h.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Mostly, we'll just care that these shims compile under gcc
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (*not* g++!)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * NOTE: this is C code, not C++ code!
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h>
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdlib.h>
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stddef.h>   /* for size_t */
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <gperftools/malloc_extension_c.h>
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <gperftools/malloc_hook_c.h>
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FAIL(msg) do {                          \
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fprintf(stderr, "FATAL ERROR: %s\n", msg);    \
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  exit(1);                                      \
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} while (0)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int g_new_hook_calls = 0;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int g_delete_hook_calls = 0;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestNewHook(const void* ptr, size_t size) {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_new_hook_calls++;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestDeleteHook(const void* ptr) {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_delete_hook_calls++;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestMallocHook(void) {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /* TODO(csilvers): figure out why we get:
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * E0100 00:00:00.000000  7383 malloc_hook.cc:244] RAW: google_malloc section is missing, thus InHookCaller is broken!
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   */
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if 0
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* result[5];
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (MallocHook_GetCallerStackTrace(result, sizeof(result)/sizeof(*result),
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     0) < 2) {  /* should have this and main */
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("GetCallerStackTrace failed");
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocHook_AddNewHook(&TestNewHook)) {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("Failed to add new hook");
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocHook_AddDeleteHook(&TestDeleteHook)) {
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("Failed to add delete hook");
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  free(malloc(10));
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  free(malloc(20));
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (g_new_hook_calls != 2) {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("Wrong number of calls to the new hook");
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (g_delete_hook_calls != 2) {
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("Wrong number of calls to the delete hook");
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocHook_RemoveNewHook(&TestNewHook)) {
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("Failed to remove new hook");
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocHook_RemoveDeleteHook(&TestDeleteHook)) {
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("Failed to remove delete hook");
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestMallocExtension(void) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int blocks;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t total;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int hist[64];
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char buffer[200];
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char* x = (char*)malloc(10);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocExtension_VerifyAllMemory();
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocExtension_VerifyMallocMemory(x);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocExtension_MallocMemoryStats(&blocks, &total, hist);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocExtension_GetStats(buffer, sizeof(buffer));
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocExtension_GetNumericProperty("generic.current_allocated_bytes",
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          &total)) {
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("GetNumericProperty failed for generic.current_allocated_bytes");
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (total < 10) {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("GetNumericProperty had bad return for generic.current_allocated_bytes");
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!MallocExtension_GetNumericProperty("generic.current_allocated_bytes",
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          &total)) {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("GetNumericProperty failed for generic.current_allocated_bytes");
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocExtension_MarkThreadIdle();
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocExtension_MarkThreadBusy();
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocExtension_ReleaseToSystem(1);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MallocExtension_ReleaseFreeMemory();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (MallocExtension_GetEstimatedAllocatedSize(10) < 10) {
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("GetEstimatedAllocatedSize returned a bad value (too small)");
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (MallocExtension_GetAllocatedSize(x) < 10) {
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("GetEstimatedAllocatedSize returned a bad value (too small)");
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (MallocExtension_GetOwnership(x) != MallocExtension_kOwned) {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("DidAllocatePtr returned a bad value (kNotOwned)");
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /* TODO(csilvers): this relies on undocumented behavior that
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     GetOwnership works on stack-allocated variables.  Use a better test. */
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (MallocExtension_GetOwnership(hist) != MallocExtension_kNotOwned) {
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAIL("DidAllocatePtr returned a bad value (kOwned)");
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  free(x);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int main(int argc, char** argv) {
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestMallocHook();
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestMallocExtension();
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printf("PASS\n");
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return 0;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
148