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 2019-03-06 "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 ));
34 .BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
36 .B void mcheck_check_all(void);
38 .BI "enum mcheck_status mprobe(void *" ptr );
43 function installs a set of debugging hooks for the
45 family of memory-allocation functions.
46 These hooks cause certain consistency checks to be performed
47 on the state of the heap.
48 The checks can detect application errors such as freeing a block of memory
49 more than once or corrupting the bookkeeping data structures
50 that immediately precede a block of allocated memory.
54 function must be called before the first call to
56 or a related function.
57 In cases where this is difficult to ensure, linking the program with
59 inserts an implicit call to
61 (with a NULL argument)
62 before the first call to a memory-allocation function.
65 .BR mcheck_pedantic ()
66 function is similar to
68 but performs checks on all allocated blocks whenever
69 one of the memory-allocation functions is called.
70 This can be very slow!
73 .BR mcheck_check_all ()
74 function causes an immediate check on all allocated blocks.
75 This call is effective only if
79 If the system detects an inconsistency in the heap,
80 the caller-supplied function pointed to by
82 is invoked with a single argument,
84 that indicates what type of inconsistency was detected.
87 is NULL, a default function prints an error message on
94 function performs a consistency check on
95 the block of allocated memory pointed to by
99 function should be called beforehand (otherwise
102 .BR MCHECK_DISABLED ).
104 The following list describes the values returned by
112 .BR MCHECK_DISABLED " (" mprobe "() only)"
114 was not called before the first memory allocation function was called.
115 Consistency checking is not possible.
117 .BR MCHECK_OK " (" mprobe "() only)"
118 No inconsistency detected.
121 Memory preceding an allocated block was clobbered.
124 Memory following an allocated block was clobbered.
128 A block of memory was freed twice.
132 .BR mcheck_pedantic ()
133 return 0 on success, or \-1 on error.
136 .BR mcheck_pedantic ()
138 .BR mcheck_check_all ()
139 functions are available since glibc 2.2.
144 functions are present since at least glibc 2.0
146 For an explanation of the terms used in this section, see
152 Interface Attribute Value
155 .BR mcheck_pedantic (),
157 .BR mcheck_check_all (),
160 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");