1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH MCHECK 3 2021-03-22 "GNU" "Linux Programmer's Manual"
7 mcheck, mcheck_check_all, mcheck_pedantic, mprobe \- heap consistency checking
10 .RI ( libc ", " \-lc )
13 .B #include <mcheck.h>
15 .BI "int mcheck(void (*" abortfunc ")(enum mcheck_status " mstatus ));
16 .BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
17 .B void mcheck_check_all(void);
19 .BI "enum mcheck_status mprobe(void *" ptr );
24 function installs a set of debugging hooks for the
26 family of memory-allocation functions.
27 These hooks cause certain consistency checks to be performed
28 on the state of the heap.
29 The checks can detect application errors such as freeing a block of memory
30 more than once or corrupting the bookkeeping data structures
31 that immediately precede a block of allocated memory.
35 function must be called before the first call to
37 or a related function.
38 In cases where this is difficult to ensure, linking the program with
40 inserts an implicit call to
42 (with a NULL argument)
43 before the first call to a memory-allocation function.
46 .BR mcheck_pedantic ()
47 function is similar to
49 but performs checks on all allocated blocks whenever
50 one of the memory-allocation functions is called.
51 This can be very slow!
54 .BR mcheck_check_all ()
55 function causes an immediate check on all allocated blocks.
56 This call is effective only if
60 If the system detects an inconsistency in the heap,
61 the caller-supplied function pointed to by
63 is invoked with a single argument,
65 that indicates what type of inconsistency was detected.
68 is NULL, a default function prints an error message on
75 function performs a consistency check on
76 the block of allocated memory pointed to by
80 function should be called beforehand (otherwise
83 .BR MCHECK_DISABLED ).
85 The following list describes the values returned by
93 .BR MCHECK_DISABLED " (" mprobe "() only)"
95 was not called before the first memory allocation function was called.
96 Consistency checking is not possible.
98 .BR MCHECK_OK " (" mprobe "() only)"
99 No inconsistency detected.
102 Memory preceding an allocated block was clobbered.
105 Memory following an allocated block was clobbered.
109 A block of memory was freed twice.
113 .BR mcheck_pedantic ()
114 return 0 on success, or \-1 on error.
117 .BR mcheck_pedantic ()
119 .BR mcheck_check_all ()
120 functions are available since glibc 2.2.
125 functions are present since at least glibc 2.0
127 For an explanation of the terms used in this section, see
135 Interface Attribute Value
138 .BR mcheck_pedantic (),
139 .BR mcheck_check_all (),
142 MT-Unsafe race:mcheck
150 These functions are GNU extensions.
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)
188 main(int argc, char *argv[])
192 if (mcheck(NULL) != 0) {
193 fprintf(stderr, "mcheck() failed\en");
200 fprintf(stderr, "About to free\en");
202 fprintf(stderr, "\enAbout to free a second time\en");