]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/mcheck.3
share/mk/: distcheck: Run 'check' after 'build'
[thirdparty/man-pages.git] / man3 / mcheck.3
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 2019-03-06 "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 .PP
34 .BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
35 .PP
36 .B void mcheck_check_all(void);
37 .PP
38 .BI "enum mcheck_status mprobe(void *" ptr );
39 .fi
40 .SH DESCRIPTION
41 The
42 .BR mcheck ()
43 function installs a set of debugging hooks for the
44 .BR malloc (3)
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.
51 .PP
52 To be effective, the
53 .BR mcheck ()
54 function must be called before the first call to
55 .BR malloc (3)
56 or a related function.
57 In cases where this is difficult to ensure, linking the program with
58 .IR \-lmcheck
59 inserts an implicit call to
60 .BR mcheck ()
61 (with a NULL argument)
62 before the first call to a memory-allocation function.
63 .PP
64 The
65 .BR mcheck_pedantic ()
66 function is similar to
67 .BR mcheck (),
68 but performs checks on all allocated blocks whenever
69 one of the memory-allocation functions is called.
70 This can be very slow!
71 .PP
72 The
73 .BR mcheck_check_all ()
74 function causes an immediate check on all allocated blocks.
75 This call is effective only if
76 .BR mcheck ()
77 is called beforehand.
78 .PP
79 If the system detects an inconsistency in the heap,
80 the caller-supplied function pointed to by
81 .I abortfunc
82 is invoked with a single argument,
83 .IR mstatus ,
84 that indicates what type of inconsistency was detected.
85 If
86 .I abortfunc
87 is NULL, a default function prints an error message on
88 .IR stderr
89 and calls
90 .BR abort (3).
91 .PP
92 The
93 .BR mprobe ()
94 function performs a consistency check on
95 the block of allocated memory pointed to by
96 .IR ptr .
97 The
98 .BR mcheck ()
99 function should be called beforehand (otherwise
100 .BR mprobe ()
101 returns
102 .BR MCHECK_DISABLED ).
103 .PP
104 The following list describes the values returned by
105 .BR mprobe ()
106 or passed as the
107 .I mstatus
108 argument when
109 .I abortfunc
110 is invoked:
111 .TP
112 .BR MCHECK_DISABLED " (" mprobe "() only)"
113 .BR mcheck ()
114 was not called before the first memory allocation function was called.
115 Consistency checking is not possible.
116 .TP
117 .BR MCHECK_OK " (" mprobe "() only)"
118 No inconsistency detected.
119 .TP
120 .B MCHECK_HEAD
121 Memory preceding an allocated block was clobbered.
122 .TP
123 .B MCHECK_TAIL
124 Memory following an allocated block was clobbered.
125 .TP
126 .B
127 MCHECK_FREE
128 A block of memory was freed twice.
129 .SH RETURN VALUE
130 .BR mcheck ()
131 and
132 .BR mcheck_pedantic ()
133 return 0 on success, or \-1 on error.
134 .SH VERSIONS
135 The
136 .BR mcheck_pedantic ()
137 and
138 .BR mcheck_check_all ()
139 functions are available since glibc 2.2.
140 The
141 .BR mcheck ()
142 and
143 .BR mprobe ()
144 functions are present since at least glibc 2.0
145 .SH ATTRIBUTES
146 For an explanation of the terms used in this section, see
147 .BR attributes (7).
148 .TS
149 allbox;
150 lbw28 lb lbw21
151 l l l.
152 Interface Attribute Value
153 T{
154 .BR mcheck (),
155 .BR mcheck_pedantic (),
156 .br
157 .BR mcheck_check_all (),
158 .BR mprobe ()
159 T} Thread safety T{
160 MT-Unsafe race:mcheck
161 .br
162 const:malloc_hooks
163 T}
164 .TE
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 EXAMPLE
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:
186 .PP
187 .in +4n
188 .EX
189 .RB "$" " ./a.out"
190 About to free
191
192 About to free a second time
193 block freed twice
194 Aborted (core dumped)
195 .EE
196 .in
197 .SS Program source
198 \&
199 .EX
200 #include <stdlib.h>
201 #include <stdio.h>
202 #include <mcheck.h>
203
204 int
205 main(int argc, char *argv[])
206 {
207 char *p;
208
209 if (mcheck(NULL) != 0) {
210 fprintf(stderr, "mcheck() failed\en");
211
212 exit(EXIT_FAILURE);
213 }
214
215 p = malloc(1000);
216
217 fprintf(stderr, "About to free\en");
218 free(p);
219 fprintf(stderr, "\enAbout to free a second time\en");
220 free(p);
221
222 exit(EXIT_SUCCESS);
223 }
224 .EE
225 .SH SEE ALSO
226 .BR malloc (3),
227 .BR mallopt (3),
228 .BR mtrace (3)