问题
glibc seems to have more than one way of doing some heap checking:
- mallopt with the M_CHECK_ACTION parameter
- the MALLOC_CHECK_ environment variable
- the mcheck family of functions
I find the available documentation to be confusing. The manual doesn't list M_CHECK_ACTION at all when describing mallopt. This mallopt man page, however, does describe M_CHECK_ACTION. Additionally, it says it's equivalent to the environment variable 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.
The glibc manual has a page for mcheck and friends and describes them as "heap consistency checking". It is on this page that the manual discusses MALLOC_CHECK_:
Another possibility to check for and guard against bugs in the use of malloc, realloc and free is to set the environment variable MALLOC_CHECK_. When MALLOC_CHECK_ is set, a special (less efficient) implementation is used which is designed to be tolerant against simple errors, such as double calls of free with the same argument, or overruns of a single byte (off-by-one bugs).
So mcheck et al is an alternative to MALLOC_CHECK_/M_CHECK_ACTION?
Furthermore, how does one disable all this super-helpful consistency checking? The man page says setting MALLOC_CHECK_ (and therefore M_CHECK_ACTION) to 0 will not use "a special implementation of the memory-allocation functions." The glibc manual, however, states that "When MALLOC_CHECK_ is set, a special (less efficient) implementation is used." An environment variable set to 0 is still set, so one of these is wrong.
My own experiments (using the example program from this mcheck man page) show that not having MALLOC_CHECK_ set at all results in the same behavior as MALLOC_CHECK_=3 (on RHEL 6.4). And mcheck seems completely unrelated, as it can be set independently.
回答1:
There are four different consistency checks. The following corresponds to glibc 2.25.
- Various asserts. Whether they are active is determined when glibc is built. In the past, Debian and downstreams left asserts switched on. Fedora and downstreams disabled them in the past (but current Fedora leaves them on).
- Consistency checks in the core
malloc
implementation which do not use asserts, butmalloc_printerr
. In the past, they could be switched off usingMALLOC_CHECK_=0
(andmallopt
). However, the I doubt that the error recovery aftermalloc_printerr
is correct in all cases, so this is unlikely to work. And if there is heap corruption, the program might crash soon anyway. - Relatively lightweight heap buffer overflow detection. This is enabled by
MALLOC_CHECK_=3
(or another non-zero value). This cannot be switched on bymallopt
. It is implemented by increasing the size of allocations and storing some padding and checking it in some places. This heap checker should be thread-safe, and it is tightly coupled withmalloc
internals. However, it is rarely used, so there could easily be annoying bugs. - The
mcheck
function, called from__malloc_initialize_hook
by linking withlibmcheck.a
. This is completely different from the previous checks. I think the idea is to verify correct allocation behavior by having a separate set of metadata (independent of the actual allocator), andmcheck
does not depend on theglibc
malloc internals except for themalloc
hooks. However, its use of these hooks completely not thread safe. I don't think anyone usesmcheck
today. It is just included because no one has removed it yet. (It is not even needed for backwards compatibility because there is no guarantee that all heap corruption is detected, and applications which corrupt the heap are still completely undefined, so there is no way to detect whethermcheck
actually performs additional checks.)
In addition to that, there is also MALLOC_PERTURB_
, which can be used to detect some forms of access to uninitialized or freed data.
回答2:
setting the environment variable
MALLOC_CHECK_=n
is the same as calling
mallopt(M_CHECK_ACTION, n)
Since glibc 2.3.4, the default value for the M_CHECK_ACTION parameter is 3.
You can set n to 0
Ignore error conditions; continue execution (with undefined results).
来源:https://stackoverflow.com/questions/18153746/what-is-the-difference-between-glibcs-malloc-check-m-check-action-and-mcheck