2

我越来越

*** glibc detected *** malloc(): memory corruption

类型错误。在我的情况下,这些特别难以调试,因为

  1. 交叉编译环境,没有 valgrind
  2. libc 是在没有调试信息的情况下构建的,因此将核心转储加载到跨 gdb 不会产生太多
  3. 根文件系统非常脆弱,试图用重建的副本替换 glibc 会导致大量损坏
  4. 导致这些错误的应用程序最初是作为供应商提供的 C 示例开始的,其中到处都有 malloc/free。我添加了我自己的代码,它使用 C++ 智能指针进行自己的分配,但仍然有很多遗留的哑指针。

当我使用#if删除我添加的代码时也会发生错误,尽管报告出现在不同的位置(如果我的代码不存在则早期,如果我的代码存在则在应用程序退出时)。这并不是特别不寻常。众所周知,堆布局问题对内存布局很敏感。

我认为原始示例代码很可能在某处出现“良性”溢出,这在我进行修改后会影响一些重要的事情。我从示例中删除的一些代码也有可能初始化了一个仍在某处使用的指针。或者没有重要的内容被覆盖,但启用调试信息-g会导致报告更多问题。

更糟糕的是,该示例是多线程的,并且调用了质量也很可疑的复杂供应商库。

我想本地化内存损坏。有没有办法手动调用 glibc 堆元数据一致性检查,而不是等待下一次调用malloc()


FWIW 这个错误不在我编写的代码中,而且我确实相信公开羞辱(一些从事同一件事的可怜人可能会通过发现这一点来节省一天的时间),所以就这样吧。来自ilclient_utils.c至少一个德州仪器 OpenMAX 样本:

  pAppDataPtr->capILComp->outPortParams =
    malloc (sizeof (IL_CLIENT_OUTPORT_PARAMS) *
            pAppDataPtr->capILComp->numOutport);

  memset (pAppDataPtr->capILComp->outPortParams, 0x0,
          sizeof (IL_CLIENT_OUTPORT_PARAMS) *
          pAppDataPtr->capILComp->numOutport);

  pAppDataPtr->deiILComp->inPortParams =
    malloc (sizeof (IL_CLIENT_INPORT_PARAMS) *
            pAppDataPtr->deiILComp->numInport);

  memset (pAppDataPtr->deiILComp->inPortParams, 0x0,
          sizeof (IL_CLIENT_INPORT_PARAMS));

  pAppDataPtr->deiILComp->outPortParams =
    malloc (sizeof (IL_CLIENT_INPORT_PARAMS) *
            pAppDataPtr->deiILComp->numOutport);
  memset (pAppDataPtr->deiILComp->outPortParams, 0x0,
          pAppDataPtr->deiILComp->numOutport *
          sizeof (IL_CLIENT_OUTPORT_PARAMS));

  pAppDataPtr->encILComp->inPortParams =
    malloc (sizeof (IL_CLIENT_INPORT_PARAMS) *
            pAppDataPtr->encILComp->numInport);
  memset (pAppDataPtr->encILComp->inPortParams, 0x0,
          sizeof (IL_CLIENT_INPORT_PARAMS));

  pAppDataPtr->encILComp->outPortParams =
    malloc (sizeof (IL_CLIENT_INPORT_PARAMS) *
            pAppDataPtr->encILComp->numOutport);
  memset (pAppDataPtr->encILComp->outPortParams, 0x0,
          sizeof (IL_CLIENT_OUTPORT_PARAMS));

  pAppDataPtr->disILComp->inPortParams =
    malloc (sizeof (IL_CLIENT_INPORT_PARAMS) *
            pAppDataPtr->disILComp->numInport);
  memset (pAppDataPtr->disILComp->inPortParams, 0x0,
          sizeof (IL_CLIENT_INPORT_PARAMS));

请注意,上述两个内存分配使用sizeof (IL_CLIENT_INPORT_PARAMS)但应该使用sizeof (IL_CLIENT_OUTPORT_PARAMS). C++ 和类型安全万岁new []


该代码附有强制性通知:

/*
 *  Copyright (c) 2010-2011, Texas Instruments Incorporated
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  *  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *  *  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *  *  Neither the name of Texas Instruments Incorporated nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  Contact information for paper mail:
 *  Texas Instruments
 *  Post Office Box 655303
 *  Dallas, Texas 75265
 *  Contact information:
 *  http://www-k.ext.ti.com/sc/technical-support/product-information-centers.htm?
 *  DCMP=TIHomeTracking&HQS=Other+OT+home_d_contact
 *  ============================================================================
 *
 */
4

2 回答 2

2

看起来一种方法是

#include <mcheck.h>

mcheck_check_all();

文档不是很详细,但似乎这mcheck.h是在分配期间已经运行的 glibc 内置检查。

于 2013-06-17T17:57:55.440 回答
-1

使用 -g 启用调试信息会导致报告更多问题。

不是glibc 堆损坏检测的工作方式。

有没有办法手动调用glibc堆元数据一致性检查

是:http ://www.gnu.org/software/libc/manual/html_node/Heap-Consistency-Checking.html另见MALLOC_CHECK_环境变量描述。

于 2013-06-17T16:39:31.433 回答