⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dmalloc_t.c

📁 测试内存泄露工具
💻 C
📖 第 1 页 / 共 5 页
字号:
       * incrementing because the library will never reposition an       * allocation if shrinking.       */      new_pnt = realloc(pnt, amount - 1);      if (new_pnt == NULL) {	if (! silent_b) {	  (void)printf("   ERROR: could not reallocate %d bytes.\n",		       amount + 1);	}	final = 0;	continue;      }            /* should always get a new pointer */      if (new_pnt != pnt) {	if (! silent_b) {	  (void)printf("   ERROR: could not reallocate %d bytes.\n",		       amount + 1);	}	final = 0;	continue;      }            /* check out the pointer */      if (dmalloc_examine(pnt, &ex_user_size, NULL, NULL, NULL,			  NULL, &ex_mark, &ex_seen) != DMALLOC_NOERROR) {	if (! silent_b) {	  (void)printf("   ERROR: examining pointer %lx failed.\n",		       (unsigned long)pnt);	}	final = 0;      }      else if (ex_user_size != amount - 1	       /* +2 on the mark because of the examine */	       || ex_mark != loc_mark + 2	       /* +2 on seen because realloc counts it in and out */	       || ex_seen != old_seen + 2) {	if (! silent_b) {	  (void)printf("   ERROR: examined realloced pointer info invalid.\n");	}	final = 0;      }            free(pnt);            /* this should fail now that we freed the pointer */      if (dmalloc_examine(pnt, NULL, NULL, NULL, NULL, NULL, NULL,			  NULL) != DMALLOC_ERROR) {	if (! silent_b) {	  (void)printf("   ERROR: examining freed pointer %lx did not fail.\n",		       (unsigned long)pnt);	}	final = 0;      }    }        dmalloc_debug(old_flags);  }    /********************/    /*   * Make sure that the start file:line works.   */  {    unsigned int	old_flags = dmalloc_debug_current();    int			our_errno_hold = dmalloc_errno;    char		*loc_file, save_ch;    int			iter_c, loc_line;    void		*pnts[2];    char		setup[128];        /* turn on fence post checking */    dmalloc_debug(DEBUG_CHECK_FENCE);    dmalloc_errno = 0;        if (! silent_b) {      (void)printf("  Checking heap check start at file:line\n");    }    #define BUF_SIZE	64        /* make an allocation */    pnts[0] = malloc(BUF_SIZE);    if (pnts[0] == NULL) {      if (! silent_b) {	(void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);      }      return 0;    }        /* save the character but then overwrite the high fence post */    save_ch = *((char *)pnts[0] + BUF_SIZE);    *((char *)pnts[0] + BUF_SIZE) = '\0';        /* make another allocation */    pnts[1] = malloc(BUF_SIZE);    if (pnts[1] == NULL) {      if (! silent_b) {	(void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);      }      return 0;    }        /* it shouldn't generate an error */    if (dmalloc_errno != 0) {      if (! silent_b) {	(void)printf("   ERROR: should not have gotten an error with no heap checking enabled.\n");      }      return 0;    }        /* restore the overwritten character otherwise we can't free the pointer */    *((char *)pnts[0] + BUF_SIZE) = save_ch;    free(pnts[0]);    free(pnts[1]);        for (iter_c = 0; iter_c < 2; iter_c++) {      /*       * we have to do this loop hack here because we need to know the       * __FILE__ and __LINE__ of a certain location to set the       * variable so then we need to run it again.       */            /*       * Make an allocation recording where we did it.       *       * NOTE: This all needs to be on the same line.       */      loc_file= __FILE__; loc_line = __LINE__; pnts[iter_c] = malloc(BUF_SIZE);      if (pnts[iter_c] == NULL) {	if (! silent_b) {	  (void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);	}	return 0;      }            /* first time through the loop? */      if (iter_c == 0) {	/* save the character and then overwrite the high fence post */	save_ch = *((char *)pnts[0] + BUF_SIZE);	*((char *)pnts[0] + BUF_SIZE) = '\0';		/*	 * build and enable an options string turning on checking at	 * the above allocation	 */	(void)loc_snprintf(setup, sizeof(setup), "debug=%#x,start=%s:%d",			   DEBUG_CHECK_FENCE, loc_file, loc_line);	dmalloc_debug_setup(setup);		continue;      }            /*       * now the 2nd time through the loop we should have seen an       * error because heap checking should have been enabled at the       * 2nd allocation so the heap should have been checked and the       * problem with the 1st allocation detected.       */      if (dmalloc_errno != ERROR_OVER_FENCE) {	if (! silent_b) {	  (void)printf("   ERROR: should have gotten over fence-post error after checking started.\n");	}	return 0;      }            /*       * restore the overwritten character otherwise we can't free       * the pointer       */      *((char *)pnts[0] + BUF_SIZE) = save_ch;    }        free(pnts[0]);    free(pnts[1]);        /* reset the debug flags and errno */    dmalloc_debug(old_flags);    dmalloc_errno = our_errno_hold;  }    /********************/    /*   * Make sure that the start iteration count works.   */  {    unsigned int	old_flags = dmalloc_debug_current();    int			our_errno_hold = dmalloc_errno;    char		save_ch;    void		*pnt2;    char		setup[128];        /* turn on fence post checking */    dmalloc_debug(DEBUG_CHECK_FENCE);    dmalloc_errno = 0;        if (! silent_b) {      (void)printf("  Checking heap check start at iteration count\n");    }    #define BUF_SIZE	64        /* make an allocation */    pnt = malloc(BUF_SIZE);    if (pnt == NULL) {      if (! silent_b) {	(void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);      }      return 0;    }        /* overwrite the high fence post */    save_ch = *((char *)pnt + BUF_SIZE);    *((char *)pnt + BUF_SIZE) = '\0';        /* make another allocation */    pnt2 = malloc(BUF_SIZE);    if (pnt2 == NULL) {      if (! silent_b) {	(void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);      }      return 0;    }        /* it shouldn't generate an error */    if (dmalloc_errno != 0) {      if (! silent_b) {	(void)printf("   ERROR: should not have gotten an error with no heap checking enabled.\n");      }      return 0;    }        /* free the 2nd pointer */    free(pnt2);        /*     * build and enable an options string turning on checking at the     * next transaction     */    (void)loc_snprintf(setup, sizeof(setup), "debug=%#x,start=c1",		       DEBUG_CHECK_FENCE);    dmalloc_debug_setup(setup);	        /*     * make another allocation which should enable heap checking and     * notice the above pointer overwrite     */    pnt2 = malloc(BUF_SIZE);    if (pnt2 == NULL) {      if (! silent_b) {	(void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);      }      return 0;    }        /* now we should see the error */    if (dmalloc_errno != ERROR_OVER_FENCE) {      if (! silent_b) {	(void)printf("   ERROR: should have gotten over fence-post error after checking started.\n");      }      return 0;    }        /* restore the overwritten character otherwise we can't free the pointer */    *((char *)pnt + BUF_SIZE) = save_ch;    free(pnt);    free(pnt2);        /* reset the debug flags and errno */    dmalloc_debug(old_flags);    dmalloc_errno = our_errno_hold;  }    /********************/    /*   * Make sure that the start after memory size allocated.   */  {    unsigned int	old_flags = dmalloc_debug_current();    int			our_errno_hold = dmalloc_errno;    char		save_ch;    void		*pnt2;    char		setup[128];        /* turn on fence post checking */    dmalloc_debug(DEBUG_CHECK_FENCE);    dmalloc_errno = 0;        if (! silent_b) {      (void)printf("  Checking heap check start at memory size\n");    }    #define BUF_SIZE	64        /* make an allocation */    pnt = malloc(BUF_SIZE);    if (pnt == NULL) {      if (! silent_b) {	(void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);      }      return 0;    }        /* overwrite the high fence post */    save_ch = *((char *)pnt + BUF_SIZE);    *((char *)pnt + BUF_SIZE) = '\0';        /* make another allocation */    pnt2 = malloc(BUF_SIZE);    if (pnt2 == NULL) {      if (! silent_b) {	(void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);      }      return 0;    }        /* it shouldn't generate an error */    if (dmalloc_errno != 0) {      if (! silent_b) {	(void)printf("   ERROR: should not have gotten an error with no heap checking enabled.\n");      }      return 0;    }        free(pnt2);        /*     * build and enable an options string turning on checking at the     * next transaction     */    (void)loc_snprintf(setup, sizeof(setup), "debug=%#x,start=s%lu",		       DEBUG_CHECK_FENCE, dmalloc_memory_allocated());    dmalloc_debug_setup(setup);	        /*     * make another allocation which should enable heap checking and     * notice the above pointer overwrite     */    pnt2 = malloc(BUF_SIZE);    if (pnt2 == NULL) {      if (! silent_b) {	(void)printf("   ERROR: could not malloc %d bytes.\n", BLOCK_SIZE);      }      return 0;    }        /* now we should see the error */    if (dmalloc_errno != ERROR_OVER_FENCE) {      if (! silent_b) {	(void)printf("   ERROR: should have gotten over fence-post error after checking started.\n");      }      return 0;    }        /* restore the overwritten character otherwise we can't free the pointer */    *((char *)pnt + BUF_SIZE) = save_ch;    free(pnt);    free(pnt2);        /* reset the debug flags and errno */    dmalloc_debug(old_flags);    dmalloc_errno = our_errno_hold;  }    /********************/   dmalloc_message("NOTE: ignore the errors from the above ----- to here.\n");  dmalloc_message("-------------------------------------------------------\n");    dmalloc_errno = errno_hold;    /*   * The following errors whould not generate a dmalloc_errno nor any   * error messages   */    /********************/    /*   * Make sure that realloc of NULL pointers either works or doesn't   * depending on the proper ALLOW_REALLOC_NULL setting.   */    {    if (! silent_b) {      (void)printf("  Trying to realloc a 0L pointer.\n");    }    pnt = realloc(NULL, 10);#if ALLOW_REALLOC_NULL    if (pnt == NULL) {      if (! silent_b) {	(void)printf("   ERROR: re-allocation of 0L returned error.\n");      }      final = 0;    }    else {      free(pnt);    }#else    if (pnt == NULL) {      dmalloc_errno = ERROR_NONE;    }    else {      if (! silent_b) {	(void)printf("   ERROR: re-allocation of 0L did not return error.\n");      }      free(pnt);      final = 0;    }#endif  }    /********************/    /*   * Make sure that valloc returns properly page-aligned memory.   */    {    int			iter_c, amount;    unsigned int	old_flags;        if (! silent_b) {      (void)printf("  Testing valloc()\n");    }        old_flags = dmalloc_debug_current();        /*     * First check without frence posts     */    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);    }        /*     * 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 that the blanking flags actually blank all of the   * allocated pointer space.   */    {    char		*alloc_p;    unsigned int	old_flags, iter_c, amount;        if (! silent_b) {      (void)printf("  Checking alloc blanking\n");    }        old_flags = dmalloc_debug_current();    /* turn on alloc blanking without fence posts */    dmalloc_debug((old_flags | DEBUG_ALLOC_BLANK) & (~DEBUG_CHECK_FENCE));        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) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -