我得到了
* glibc检测到* malloc():内存损坏
类型错误。在我的例子中,这些调试特别困难,因为
当我使用#if删除我添加的代码时,也会出现错误,尽管报告来自不同的地方(如果我的代码不存在,则提前,如果我的代码存在,则在应用程序出口处)。这并不特别不寻常;众所周知,堆布局问题对内存布局非常敏感。
我认为最初的示例代码可能在某个地方出现了“良性”溢出,在我做了修改之后,它会踩到一些重要的东西。还有可能的是,我从示例中删除的一些代码初始化了一个仍在某处使用的指针。或者没有什么重要的内容被覆盖,但是使用-g启用调试信息会导致更多的问题被报告。
更糟糕的是,这个例子是多线程的,并且调用一个复杂的供应商库,质量也令人怀疑。
我想本地化内存损坏。有没有办法手动调用glibc堆元数据一致性检查,而不是等待下一次对malloc()的调用?
FWIW这个bug不在我写的代码中,我确实相信公开羞辱(一些可怜的灵魂在做同样的事情可以通过发现这一点来节省一天),就这样吧。来自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
* ============================================================================
*
*/发布于 2013-06-17 17:57:55
看起来有一种方法是
#include <mcheck.h>
mcheck_check_all();文档并不是很详细,但是这个mcheck.h似乎是在分配期间已经运行的glibc内置检查。
发布于 2013-06-17 16:39:31
使用-g启用调试信息会导致报告更多问题。
这不是glibc堆损坏检测的工作方式。
是否有任何方法手动调用glibc堆元数据一致性检查?
是的:node/Heap-Consistency-Checking.html也见MALLOC_CHECK_环境变量描述。
https://stackoverflow.com/questions/17151734
复制相似问题