ioctl_tty.2: Document ioctls: TCGETS2, TCSETS2, TCSETSW2, TCSETSF2
[man-pages.git] / man3 / mcheck.3
blobc4a79853debef84abe81f13d0ae404516f9d3194
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH MCHECK 3  2021-03-22 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 mcheck, mcheck_check_all, mcheck_pedantic, mprobe \- heap consistency checking
28 .SH SYNOPSIS
29 .nf
30 .B #include <mcheck.h>
31 .PP
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);
35 .PP
36 .BI "enum mcheck_status mprobe(void *" ptr );
37 .fi
38 .SH DESCRIPTION
39 The
40 .BR mcheck ()
41 function installs a set of debugging hooks for the
42 .BR malloc (3)
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.
49 .PP
50 To be effective, the
51 .BR mcheck ()
52 function must be called before the first call to
53 .BR malloc (3)
54 or a related function.
55 In cases where this is difficult to ensure, linking the program with
56 .IR \-lmcheck
57 inserts an implicit call to
58 .BR mcheck ()
59 (with a NULL argument)
60 before the first call to a memory-allocation function.
61 .PP
62 The
63 .BR mcheck_pedantic ()
64 function is similar to
65 .BR mcheck (),
66 but performs checks on all allocated blocks whenever
67 one of the memory-allocation functions is called.
68 This can be very slow!
69 .PP
70 The
71 .BR mcheck_check_all ()
72 function causes an immediate check on all allocated blocks.
73 This call is effective only if
74 .BR mcheck ()
75 is called beforehand.
76 .PP
77 If the system detects an inconsistency in the heap,
78 the caller-supplied function pointed to by
79 .I abortfunc
80 is invoked with a single argument,
81 .IR mstatus ,
82 that indicates what type of inconsistency was detected.
84 .I abortfunc
85 is NULL, a default function prints an error message on
86 .IR stderr
87 and calls
88 .BR abort (3).
89 .PP
90 The
91 .BR mprobe ()
92 function performs a consistency check on
93 the block of allocated memory pointed to by
94 .IR ptr .
95 The
96 .BR mcheck ()
97 function should be called beforehand (otherwise
98 .BR mprobe ()
99 returns
100 .BR MCHECK_DISABLED ).
102 The following list describes the values returned by
103 .BR mprobe ()
104 or passed as the
105 .I mstatus
106 argument when
107 .I abortfunc
108 is invoked:
110 .BR MCHECK_DISABLED " (" mprobe "() only)"
111 .BR mcheck ()
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.
118 .B MCHECK_HEAD
119 Memory preceding an allocated block was clobbered.
121 .B MCHECK_TAIL
122 Memory following an allocated block was clobbered.
125 MCHECK_FREE
126 A block of memory was freed twice.
127 .SH RETURN VALUE
128 .BR mcheck ()
130 .BR mcheck_pedantic ()
131 return 0 on success, or \-1 on error.
132 .SH VERSIONS
134 .BR mcheck_pedantic ()
136 .BR mcheck_check_all ()
137 functions are available since glibc 2.2.
139 .BR mcheck ()
141 .BR mprobe ()
142 functions are present since at least glibc 2.0
143 .SH ATTRIBUTES
144 For an explanation of the terms used in this section, see
145 .BR attributes (7).
146 .ad l
149 allbox;
150 lbx lb lb
151 l l l.
152 Interface       Attribute       Value
154 .BR mcheck (),
155 .BR mcheck_pedantic (),
156 .BR mcheck_check_all (),
157 .BR mprobe ()
158 T}      Thread safety   T{
159 MT-Unsafe race:mcheck
160 const:malloc_hooks
165 .sp 1
166 .SH CONFORMING TO
167 These functions are GNU extensions.
168 .SH NOTES
169 Linking a program with
170 .I \-lmcheck
171 and using the
172 .B MALLOC_CHECK_
173 environment variable (described in
174 .BR mallopt (3))
175 cause the same kinds of errors to be detected.
176 But, using
177 .B MALLOC_CHECK_
178 does not require the application to be relinked.
179 .\" But is MALLOC_CHECK_ slower?
180 .SH EXAMPLES
181 The program below calls
182 .BR mcheck ()
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:
187 .in +4n
189 .RB "$" " ./a.out"
190 About to free
192 About to free a second time
193 block freed twice
194 Aborted (core dumped)
197 .SS Program source
200 #include <stdlib.h>
201 #include <stdio.h>
202 #include <mcheck.h>
205 main(int argc, char *argv[])
207     char *p;
209     if (mcheck(NULL) != 0) {
210         fprintf(stderr, "mcheck() failed\en");
212         exit(EXIT_FAILURE);
213     }
215     p = malloc(1000);
217     fprintf(stderr, "About to free\en");
218     free(p);
219     fprintf(stderr, "\enAbout to free a second time\en");
220     free(p);
222     exit(EXIT_SUCCESS);
225 .SH SEE ALSO
226 .BR malloc (3),
227 .BR mallopt (3),
228 .BR mtrace (3)