1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH MCHECK 3 2021-03-22 "GNU" "Linux Programmer's Manual"
27 mcheck, mcheck_check_all, mcheck_pedantic, mprobe \- heap consistency checking
30 .B #include <mcheck.h>
32 .BI "int mcheck(void (*" abortfunc ")(enum mcheck_status " mstatus ));
33 .BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
34 .B void mcheck_check_all(void);
36 .BI "enum mcheck_status mprobe(void *" ptr );
41 function installs a set of debugging hooks for the
43 family of memory-allocation functions.
44 These hooks cause certain consistency checks to be performed
45 on the state of the heap.
46 The checks can detect application errors such as freeing a block of memory
47 more than once or corrupting the bookkeeping data structures
48 that immediately precede a block of allocated memory.
52 function must be called before the first call to
54 or a related function.
55 In cases where this is difficult to ensure, linking the program with
57 inserts an implicit call to
59 (with a NULL argument)
60 before the first call to a memory-allocation function.
63 .BR mcheck_pedantic ()
64 function is similar to
66 but performs checks on all allocated blocks whenever
67 one of the memory-allocation functions is called.
68 This can be very slow!
71 .BR mcheck_check_all ()
72 function causes an immediate check on all allocated blocks.
73 This call is effective only if
77 If the system detects an inconsistency in the heap,
78 the caller-supplied function pointed to by
80 is invoked with a single argument,
82 that indicates what type of inconsistency was detected.
85 is NULL, a default function prints an error message on
92 function performs a consistency check on
93 the block of allocated memory pointed to by
97 function should be called beforehand (otherwise
100 .BR MCHECK_DISABLED ).
102 The following list describes the values returned by
110 .BR MCHECK_DISABLED " (" mprobe "() only)"
112 was not called before the first memory allocation function was called.
113 Consistency checking is not possible.
115 .BR MCHECK_OK " (" mprobe "() only)"
116 No inconsistency detected.
119 Memory preceding an allocated block was clobbered.
122 Memory following an allocated block was clobbered.
126 A block of memory was freed twice.
130 .BR mcheck_pedantic ()
131 return 0 on success, or \-1 on error.
134 .BR mcheck_pedantic ()
136 .BR mcheck_check_all ()
137 functions are available since glibc 2.2.
142 functions are present since at least glibc 2.0
144 For an explanation of the terms used in this section, see
152 Interface Attribute Value
155 .BR mcheck_pedantic (),
156 .BR mcheck_check_all (),
159 MT-Unsafe race:mcheck
167 These functions are GNU extensions.
169 Linking a program with
173 environment variable (described in
175 cause the same kinds of errors to be detected.
178 does not require the application to be relinked.
179 .\" But is MALLOC_CHECK_ slower?
181 The program below calls
183 with a NULL argument and then frees the same block of memory twice.
184 The following shell session demonstrates what happens
185 when running the program:
192 About to free a second time
194 Aborted (core dumped)
205 main(int argc, char *argv[])
209 if (mcheck(NULL) != 0) {
210 fprintf(stderr, "mcheck() failed\en");
217 fprintf(stderr, "About to free\en");
219 fprintf(stderr, "\enAbout to free a second time\en");