【发布时间】:2013-08-11 19:15:12
【问题描述】:
glibc 似乎有不止一种方法来做一些堆检查:
- mallopt 与 M_CHECK_ACTION 参数
- MALLOC_CHECK_ 环境变量
- mcheck 系列函数
我发现可用的文档令人困惑。 manual 在描述 mallopt 时根本没有列出 M_CHECK_ACTION。 This mallopt man page,然而,确实描述了 M_CHECK_ACTION。此外,它说它相当于环境变量 MALLOC_CHECK_:
MALLOC_CHECK_ This environment variable controls the same parameter as mallopt() M_CHECK_ACTION. If this variable is set to a nonzero value, then a special implementation of the memory- allocation functions is used. (This is accomplished using the malloc_hook(3) feature.) This implementation performs additional error checking, but is slower than the standard set of memory-allocation functions.
glibc 手册有一个mcheck and friends 的页面,并将它们描述为“堆一致性检查”。手册在此页面上讨论了 MALLOC_CHECK_:
在使用 malloc、realloc 和 free 时检查和防范错误的另一种可能性是设置环境变量 MALLOC_CHECK_。当设置 MALLOC_CHECK_ 时,将使用一种特殊的(效率较低的)实现,该实现旨在容忍简单的错误,例如使用相同参数的两次 free 调用,或单个字节的溢出(off-by-one 错误)。
所以 mcheck et al 是 MALLOC_CHECK_/M_CHECK_ACTION 的替代品?
此外,如何禁用所有这些超级有用的一致性检查?手册页说将 MALLOC_CHECK_(因此 M_CHECK_ACTION)设置为 0 不会使用“内存分配函数的特殊实现”。然而,glibc 手册指出“当设置了 MALLOC_CHECK_ 时,将使用特殊的(效率较低的)实现。”设置为 0 的环境变量仍然设置,因此其中一个是错误的。
我自己的实验(使用来自this mcheck man page 的示例程序)表明,完全没有设置 MALLOC_CHECK_ 会导致与 MALLOC_CHECK_=3 相同的行为(在 RHEL 6.4 上)。而mcheck似乎完全无关,因为它可以独立设置。
【问题讨论】:
标签: debugging glibc heap-corruption