📄 dmalloc_t.c
字号:
(void)printf(" ERROR: could not allocate %d bytes.\n", amount); } final = 0; continue; } for (alloc_p = pnt; alloc_p < (char *)pnt + amount; alloc_p++) { if (*alloc_p != ALLOC_BLANK_CHAR) { if (! silent_b) { (void)printf(" ERROR: allocation not fully blanked.\n"); } final = 0; dmalloc_errno = ERROR_ALLOC_FAILED; } } free(pnt); } /* * Now test with fence posts enabled */ dmalloc_debug(old_flags | DEBUG_CHECK_FENCE); for (iter_c = 0; iter_c < 20; iter_c++) { do { amount = _dmalloc_rand() % (page_size * 3); } while (amount == 0); pnt = valloc(amount); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not valloc %d bytes.\n", amount); } final = 0; continue; } if ((unsigned long)pnt % page_size != 0) { if (! silent_b) { (void)printf(" ERROR: valloc got %lx which is not page aligned.\n", (unsigned long)pnt); } final = 0; dmalloc_errno = ERROR_NOT_ON_BLOCK; } free(pnt); } dmalloc_debug(old_flags); } /********************/ /* * Make sure realloc copy works right. */ { void *new_pnt; unsigned int amount, old_flags; int iter_c; if (! silent_b) { (void)printf(" Checking realloc_copy token\n"); } old_flags = dmalloc_debug_current(); dmalloc_debug(old_flags | DEBUG_REALLOC_COPY); for (iter_c = 0; iter_c < 20; iter_c++) { do { amount = _dmalloc_rand() % (page_size * 3); } while (amount == 0); pnt = malloc(amount); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not allocate %d bytes.\n", amount); } final = 0; continue; } /* we should get the same pointer */ new_pnt = realloc(pnt, amount); if (new_pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not reallocate %d bytes.\n", amount + 10); } final = 0; continue; } if (new_pnt == pnt) { if (! silent_b) { (void)printf(" ERROR: realloc produced same pointer.\n"); } final = 0; continue; } if (dmalloc_free(__FILE__, __LINE__, new_pnt, DMALLOC_FUNC_FREE) != FREE_NOERROR) { if (! silent_b) { (void)printf(" ERROR: free bad pointer produced: %s\n", dmalloc_strerror(dmalloc_errno)); } final = 0; } } dmalloc_debug(old_flags); } /********************/ /* * Make sure realloc copy works right. */ { void *new_pnt; unsigned int amount, old_flags; int iter_c; if (! silent_b) { (void)printf(" Checking never-reuse token\n"); } old_flags = dmalloc_debug_current(); dmalloc_debug(old_flags | DEBUG_NEVER_REUSE); for (iter_c = 0; iter_c < 20; iter_c++) { do { amount = _dmalloc_rand() % (page_size * 3); } while (amount == 0); pnt = malloc(amount); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not allocate %d bytes.\n", amount); } final = 0; continue; } /* we should get the same pointer */ new_pnt = realloc(pnt, amount); if (new_pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not reallocate %d bytes.\n", amount + 10); } final = 0; continue; } if (new_pnt == pnt) { if (! silent_b) { (void)printf(" ERROR: realloc produced same pointer.\n"); } final = 0; continue; } if (dmalloc_free(__FILE__, __LINE__, new_pnt, DMALLOC_FUNC_FREE) != FREE_NOERROR) { if (! silent_b) { (void)printf(" ERROR: free bad pointer produced: %s\n", dmalloc_strerror(dmalloc_errno)); } final = 0; } } dmalloc_debug(old_flags); } /********************/ /* * Make sure that the dmalloc function checking allows external * pointers. */ { unsigned int old_flags; char buf[20]; if (! silent_b) { (void)printf(" Checking function checking of non-heap pointers\n"); } old_flags = dmalloc_debug_current(); dmalloc_debug(old_flags | DEBUG_CHECK_FUNCS); _dmalloc_memset(buf, 0, sizeof(buf)); if (dmalloc_errno != ERROR_NONE) { if (! silent_b) { (void)printf(" ERROR: dmalloc_memset of non-heap pointer failed: %s\n", dmalloc_strerror(dmalloc_errno)); } final = 0; } dmalloc_debug(old_flags); } /********************/ /* * Make sure that string tokens work in the processing program. */ { unsigned int old_flags, new_flags; if (! silent_b) { (void)printf(" Checking string tokens in dmalloc_debug_setup\n"); } old_flags = dmalloc_debug_current(); dmalloc_debug(0); dmalloc_debug_setup("log-stats,log-non-free,log-bad-space"); new_flags = dmalloc_debug_current(); if (! (new_flags & DEBUG_LOG_STATS && new_flags & DEBUG_LOG_NONFREE && new_flags & DEBUG_LOG_BAD_SPACE)) { if (! silent_b) { (void)printf(" ERROR: dmalloc_debug_setup didn't process comma separated tokens.\n"); } final = 0; } dmalloc_debug(old_flags); } /********************/ /* * Make sure that the calloc macro is valid. */ { DMALLOC_SIZE user_size; if (! silent_b) { (void)printf(" Checking calloc macro arguments\n"); } /* notice that we do not have a () around this operation */#define SIZE_ARG 10 + 1#define COUNT_ARG 2 /* * Initially I was not putting parens around macro arguments in * dmalloc.h. Rookie mistake. This should allocate (10 + 1) * 2 * bytes (22) not 10 + 1 * 2 bytes (12). */ pnt = calloc(SIZE_ARG, COUNT_ARG); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not calloc %d bytes.\n", (SIZE_ARG) * (COUNT_ARG)); } final = 0; } else if (dmalloc_examine(pnt, &user_size, NULL, NULL, NULL, NULL, NULL, NULL) != DMALLOC_NOERROR) { if (! silent_b) { (void)printf(" ERROR: examining pointer %lx failed.\n", (unsigned long)pnt); } final = 0; } else if (user_size != (SIZE_ARG) * (COUNT_ARG)) { if (! silent_b) { (void)printf(" ERROR: calloc size should be %d but was %d.\n", (SIZE_ARG) * (COUNT_ARG), user_size); } final = 0; } } /********************/ /* * Verify that the check-funcs work with check-fence. Thanks to * John Hetherington for reporting this. */ { unsigned int old_flags = dmalloc_debug_current(); int our_errno_hold = dmalloc_errno; dmalloc_debug(DEBUG_CHECK_FUNCS); if (! silent_b) { (void)printf(" Checking check-funcs with check-fence\n"); } #define BUF_SIZE 64 pnt = malloc(BUF_SIZE); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not malloc %d bytes.\n", BLOCK_SIZE); } return 0; } dmalloc_errno = ERROR_NONE; _dmalloc_memset(pnt, 0, BUF_SIZE); if (dmalloc_errno != ERROR_NONE) { if (! silent_b) { (void)printf(" ERROR: memset on buf of %d bytes failed.\n", BUF_SIZE); } final = 0; } free(pnt); /* now turn on the check-fence token */ dmalloc_debug(DEBUG_CHECK_FENCE | DEBUG_CHECK_FUNCS); pnt = malloc(BUF_SIZE); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not malloc %d bytes.\n", BLOCK_SIZE); } return 0; } dmalloc_errno = ERROR_NONE; _dmalloc_memset(pnt, 0, BUF_SIZE); if (dmalloc_errno != ERROR_NONE) { if (! silent_b) { (void)printf(" ERROR: memset of %d bytes with check-fence failed.\n", BUF_SIZE); } final = 0; } free(pnt); dmalloc_debug(old_flags); /* recover the errno if necessary */ if (dmalloc_errno == ERROR_NONE) { dmalloc_errno = our_errno_hold; } } /********************/ /* * Verify the dmalloc_count_changed function. */ { unsigned long size, mem_count, loc_mark; if (! silent_b) { (void)printf(" Checking dmalloc_count_changed function\n"); } /* get a mark */ loc_mark = dmalloc_mark(); /* make sure that nothing has changed */ mem_count = dmalloc_count_changed(loc_mark, 1 /* not-freed */, 1 /* freed */); if (mem_count != 0) { if (! silent_b) { (void)printf(" ERROR: count-changed reported %lu bytes changed.\n", mem_count); } return 0; } /* allocate a pointer */ size = _dmalloc_rand() % MAX_ALLOC + 10; pnt = malloc(size); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not malloc %lu bytes.\n", size); } return 0; } /* make sure that we see the non-freed pointer */ mem_count = dmalloc_count_changed(loc_mark, 1 /* not-freed */, 0 /* no freed */); if (mem_count != size) { if (! silent_b) { (void)printf(" ERROR: count-changed reported %lu bytes changed not %lu.\n", mem_count, size); } return 0; } /* make sure that we see non-freed pointer with non-fred + freed flags */ mem_count = dmalloc_count_changed(loc_mark, 1 /* not-freed */, 1 /* no freed */); if (mem_count != size) { if (! silent_b) { (void)printf(" ERROR: count-changed reported %lu bytes changed not %lu.\n", mem_count, size); } return 0; } /* now free it */ free(pnt); /* make sure that everything is freed */ mem_count = dmalloc_count_changed(loc_mark, 1 /* not-freed */, 0 /* no freed */); if (mem_count != 0) { if (! silent_b) { (void)printf(" ERROR: count-changed reported %lu bytes changed.\n", mem_count); } return 0; } /* make sure that we see the freed pointer */ mem_count = dmalloc_count_changed(loc_mark, 0 /* no not-freed */, 1 /* no freed */); if (mem_count != size) { if (! silent_b) { (void)printf(" ERROR: count-changed report %lu bytes changed not %lu.\n", mem_count, size); } return 0; } /* make sure that we see the freed pointer with both flags */ mem_count = dmalloc_count_changed(loc_mark, 1 /* no not-freed */, 1 /* no freed */); if (mem_count != size) { if (! silent_b) { (void)printf(" ERROR: count-changed report %lu bytes changed not %lu.\n", mem_count, size); } return 0; } } /********************/ /* * Check block rounding by allocator. */ { unsigned long size; unsigned int old_flags = dmalloc_debug_current(); if (! silent_b) { (void)printf(" Checking block-size alignment rounding\n"); } /* enable never-reuse and disable fence checking */ dmalloc_debug((old_flags | DEBUG_NEVER_REUSE) & (~DEBUG_CHECK_FENCE)); size = BLOCK_SIZE / 2 + 1; pnt = malloc(size); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not malloc %lu bytes.\n", size); } return 0; } if ((unsigned long)pnt % BLOCK_SIZE != 0) { if (! silent_b) { (void)printf(" ERROR: alloc of %lu bytes was not block aligned.\n", size); } return 0; } free(pnt); /* * Now we'll allocate some pitifully small chunk from the heap * allocator and it should return a good pointer. In the future * it should re-align the heap. */ size = 10; pnt = _dmalloc_heap_alloc(size); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not heap-alloc %lu bytes.\n", size); } return 0; } size = BLOCK_SIZE / 2 + 1; pnt = malloc(size); if (pnt == NULL) { if (! silent_b) { (void)printf(" ERROR: could not malloc %lu bytes.\n", size); } return 0; } if ((unsigned long)pnt % BLOCK_SIZE != 0) { if (! silent_b) { (void)printf(" ERROR: alloc of %lu bytes was not block aligned.\n", size); } return 0; } free(pnt); /* restore flags */ dmalloc_debug(old_flags); } /********************/ /* * Make sure per-pointer blanking flags work. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -