2 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH mcheck 3 (date) "Linux man-pages (unreleased)"
8 mcheck, mcheck_check_all, mcheck_pedantic, mprobe \- heap consistency checking
11 .RI ( libc ", " \-lc )
14 .B #include <mcheck.h>
16 .BI "int mcheck(void (*" abortfunc ")(enum mcheck_status " mstatus ));
17 .BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
18 .B void mcheck_check_all(void);
20 .BI "enum mcheck_status mprobe(void *" ptr );
25 function installs a set of debugging hooks for the
27 family of memory-allocation functions.
28 These hooks cause certain consistency checks to be performed
29 on the state of the heap.
30 The checks can detect application errors such as freeing a block of memory
31 more than once or corrupting the bookkeeping data structures
32 that immediately precede a block of allocated memory.
36 function must be called before the first call to
38 or a related function.
39 In cases where this is difficult to ensure, linking the program with
41 inserts an implicit call to
43 (with a NULL argument)
44 before the first call to a memory-allocation function.
47 .BR mcheck_pedantic ()
48 function is similar to
50 but performs checks on all allocated blocks whenever
51 one of the memory-allocation functions is called.
52 This can be very slow!
55 .BR mcheck_check_all ()
56 function causes an immediate check on all allocated blocks.
57 This call is effective only if
61 If the system detects an inconsistency in the heap,
62 the caller-supplied function pointed to by
64 is invoked with a single argument,
66 that indicates what type of inconsistency was detected.
69 is NULL, a default function prints an error message on
76 function performs a consistency check on
77 the block of allocated memory pointed to by
81 function should be called beforehand (otherwise
84 .BR MCHECK_DISABLED ).
86 The following list describes the values returned by
94 .BR MCHECK_DISABLED " (" mprobe "() only)"
96 was not called before the first memory allocation function was called.
97 Consistency checking is not possible.
99 .BR MCHECK_OK " (" mprobe "() only)"
100 No inconsistency detected.
103 Memory preceding an allocated block was clobbered.
106 Memory following an allocated block was clobbered.
110 A block of memory was freed twice.
114 .BR mcheck_pedantic ()
115 return 0 on success, or \-1 on error.
117 For an explanation of the terms used in this section, see
123 Interface Attribute Value
128 .BR mcheck_pedantic (),
129 .BR mcheck_check_all (),
134 MT-Unsafe race:mcheck
142 .BR mcheck_pedantic ()
144 .BR mcheck_check_all ()
152 Linking a program with
156 environment variable (described in
158 cause the same kinds of errors to be detected.
161 does not require the application to be relinked.
162 .\" But is MALLOC_CHECK_ slower?
164 The program below calls
166 with a NULL argument and then frees the same block of memory twice.
167 The following shell session demonstrates what happens
168 when running the program:
175 About to free a second time
177 Aborted (core dumped)
182 .\" [[memory leak]] SRC BEGIN (mcheck.c)
193 if (mcheck(NULL) != 0) {
194 fprintf(stderr, "mcheck() failed\en");
201 fprintf(stderr, "About to free\en");
203 fprintf(stderr, "\enAbout to free a second time\en");