]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - secure/lib/libcrypto/man/man3/OPENSSL_malloc.3
MFC: r359486
[FreeBSD/FreeBSD.git] / secure / lib / libcrypto / man / man3 / OPENSSL_malloc.3
1 .\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.40)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 .    ds C`
42 .    ds C'
43 'br\}
44 .\"
45 .\" Escape single quotes in literal strings from groff's Unicode transform.
46 .ie \n(.g .ds Aq \(aq
47 .el       .ds Aq '
48 .\"
49 .\" If the F register is >0, we'll generate index entries on stderr for
50 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
51 .\" entries marked with X<> in POD.  Of course, you'll have to process the
52 .\" output yourself in some meaningful fashion.
53 .\"
54 .\" Avoid warning from groff about undefined register 'F'.
55 .de IX
56 ..
57 .nr rF 0
58 .if \n(.g .if rF .nr rF 1
59 .if (\n(rF:(\n(.g==0)) \{\
60 .    if \nF \{\
61 .        de IX
62 .        tm Index:\\$1\t\\n%\t"\\$2"
63 ..
64 .        if !\nF==2 \{\
65 .            nr % 0
66 .            nr F 2
67 .        \}
68 .    \}
69 .\}
70 .rr rF
71 .\"
72 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
73 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
74 .    \" fudge factors for nroff and troff
75 .if n \{\
76 .    ds #H 0
77 .    ds #V .8m
78 .    ds #F .3m
79 .    ds #[ \f1
80 .    ds #] \fP
81 .\}
82 .if t \{\
83 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
84 .    ds #V .6m
85 .    ds #F 0
86 .    ds #[ \&
87 .    ds #] \&
88 .\}
89 .    \" simple accents for nroff and troff
90 .if n \{\
91 .    ds ' \&
92 .    ds ` \&
93 .    ds ^ \&
94 .    ds , \&
95 .    ds ~ ~
96 .    ds /
97 .\}
98 .if t \{\
99 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
100 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
101 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
102 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
103 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
104 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
105 .\}
106 .    \" troff and (daisy-wheel) nroff accents
107 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
108 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
109 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
110 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
111 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
112 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
113 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
114 .ds ae a\h'-(\w'a'u*4/10)'e
115 .ds Ae A\h'-(\w'A'u*4/10)'E
116 .    \" corrections for vroff
117 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
118 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
119 .    \" for low resolution devices (crt and lpr)
120 .if \n(.H>23 .if \n(.V>19 \
121 \{\
122 .    ds : e
123 .    ds 8 ss
124 .    ds o a
125 .    ds d- d\h'-1'\(ga
126 .    ds D- D\h'-1'\(hy
127 .    ds th \o'bp'
128 .    ds Th \o'LP'
129 .    ds ae ae
130 .    ds Ae AE
131 .\}
132 .rm #[ #] #H #V #F C
133 .\" ========================================================================
134 .\"
135 .IX Title "OPENSSL_MALLOC 3"
136 .TH OPENSSL_MALLOC 3 "2020-03-31" "1.1.1f" "OpenSSL"
137 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
138 .\" way too many mistakes in technical documents.
139 .if n .ad l
140 .nh
141 .SH "NAME"
142 OPENSSL_malloc_init, OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_free, OPENSSL_clear_realloc, OPENSSL_clear_free, OPENSSL_cleanse, CRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free, OPENSSL_strdup, OPENSSL_strndup, OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat, OPENSSL_hexstr2buf, OPENSSL_buf2hexstr, OPENSSL_hexchar2int, CRYPTO_strdup, CRYPTO_strndup, OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop, CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop, CRYPTO_clear_realloc, CRYPTO_clear_free, CRYPTO_get_mem_functions, CRYPTO_set_mem_functions, CRYPTO_get_alloc_counts, CRYPTO_set_mem_debug, CRYPTO_mem_ctrl, CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp, CRYPTO_mem_leaks_cb, OPENSSL_MALLOC_FAILURES, OPENSSL_MALLOC_FD \&\- Memory allocation functions
143 .SH "SYNOPSIS"
144 .IX Header "SYNOPSIS"
145 .Vb 1
146 \& #include <openssl/crypto.h>
147 \&
148 \& int OPENSSL_malloc_init(void)
149 \&
150 \& void *OPENSSL_malloc(size_t num)
151 \& void *OPENSSL_zalloc(size_t num)
152 \& void *OPENSSL_realloc(void *addr, size_t num)
153 \& void OPENSSL_free(void *addr)
154 \& char *OPENSSL_strdup(const char *str)
155 \& char *OPENSSL_strndup(const char *str, size_t s)
156 \& size_t OPENSSL_strlcat(char *dst, const char *src, size_t size);
157 \& size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size);
158 \& void *OPENSSL_memdup(void *data, size_t s)
159 \& void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num)
160 \& void OPENSSL_clear_free(void *str, size_t num)
161 \& void OPENSSL_cleanse(void *ptr, size_t len);
162 \&
163 \& unsigned char *OPENSSL_hexstr2buf(const char *str, long *len);
164 \& char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len);
165 \& int OPENSSL_hexchar2int(unsigned char c);
166 \&
167 \& void *CRYPTO_malloc(size_t num, const char *file, int line)
168 \& void *CRYPTO_zalloc(size_t num, const char *file, int line)
169 \& void *CRYPTO_realloc(void *p, size_t num, const char *file, int line)
170 \& void CRYPTO_free(void *str, const char *, int)
171 \& char *CRYPTO_strdup(const char *p, const char *file, int line)
172 \& char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line)
173 \& void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num,
174 \&                            const char *file, int line)
175 \& void CRYPTO_clear_free(void *str, size_t num, const char *, int)
176 \&
177 \& void CRYPTO_get_mem_functions(
178 \&         void *(**m)(size_t, const char *, int),
179 \&         void *(**r)(void *, size_t, const char *, int),
180 \&         void (**f)(void *, const char *, int))
181 \& int CRYPTO_set_mem_functions(
182 \&         void *(*m)(size_t, const char *, int),
183 \&         void *(*r)(void *, size_t, const char *, int),
184 \&         void (*f)(void *, const char *, int))
185 \&
186 \& void CRYPTO_get_alloc_counts(int *m, int *r, int *f)
187 \&
188 \& int CRYPTO_set_mem_debug(int onoff)
189 \&
190 \& env OPENSSL_MALLOC_FAILURES=... <application>
191 \& env OPENSSL_MALLOC_FD=... <application>
192 \&
193 \& int CRYPTO_mem_ctrl(int mode);
194 \&
195 \& int OPENSSL_mem_debug_push(const char *info)
196 \& int OPENSSL_mem_debug_pop(void);
197 \&
198 \& int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
199 \& int CRYPTO_mem_debug_pop(void);
200 \&
201 \& int CRYPTO_mem_leaks(BIO *b);
202 \& int CRYPTO_mem_leaks_fp(FILE *fp);
203 \& int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
204 \&                         void *u);
205 .Ve
206 .SH "DESCRIPTION"
207 .IX Header "DESCRIPTION"
208 OpenSSL memory allocation is handled by the \fBOPENSSL_xxx\fR \s-1API.\s0 These are
209 generally macro's that add the standard C \fB_\|_FILE_\|_\fR and \fB_\|_LINE_\|_\fR
210 parameters and call a lower-level \fBCRYPTO_xxx\fR \s-1API.\s0
211 Some functions do not add those parameters, but exist for consistency.
212 .PP
213 \&\fBOPENSSL_malloc_init()\fR does nothing and does not need to be called. It is
214 included for compatibility with older versions of OpenSSL.
215 .PP
216 \&\fBOPENSSL_malloc()\fR, \fBOPENSSL_realloc()\fR, and \fBOPENSSL_free()\fR are like the
217 C \fBmalloc()\fR, \fBrealloc()\fR, and \fBfree()\fR functions.
218 \&\fBOPENSSL_zalloc()\fR calls \fBmemset()\fR to zero the memory before returning.
219 .PP
220 \&\fBOPENSSL_clear_realloc()\fR and \fBOPENSSL_clear_free()\fR should be used
221 when the buffer at \fBaddr\fR holds sensitive information.
222 The old buffer is filled with zero's by calling \fBOPENSSL_cleanse()\fR
223 before ultimately calling \fBOPENSSL_free()\fR.
224 .PP
225 \&\fBOPENSSL_cleanse()\fR fills \fBptr\fR of size \fBlen\fR with a string of 0's.
226 Use \fBOPENSSL_cleanse()\fR with care if the memory is a mapping of a file.
227 If the storage controller uses write compression, then its possible
228 that sensitive tail bytes will survive zeroization because the block of
229 zeros will be compressed. If the storage controller uses wear leveling,
230 then the old sensitive data will not be overwritten; rather, a block of
231 0's will be written at a new physical location.
232 .PP
233 \&\fBOPENSSL_strdup()\fR, \fBOPENSSL_strndup()\fR and \fBOPENSSL_memdup()\fR are like the
234 equivalent C functions, except that memory is allocated by calling the
235 \&\fBOPENSSL_malloc()\fR and should be released by calling \fBOPENSSL_free()\fR.
236 .PP
237 \&\fBOPENSSL_strlcpy()\fR,
238 \&\fBOPENSSL_strlcat()\fR and \fBOPENSSL_strnlen()\fR are equivalents of the common C
239 library functions and are provided for portability.
240 .PP
241 \&\fBOPENSSL_hexstr2buf()\fR parses \fBstr\fR as a hex string and returns a
242 pointer to the parsed value. The memory is allocated by calling
243 \&\fBOPENSSL_malloc()\fR and should be released by calling \fBOPENSSL_free()\fR.
244 If \fBlen\fR is not \s-1NULL,\s0 it is filled in with the output length.
245 Colons between two-character hex \*(L"bytes\*(R" are ignored.
246 An odd number of hex digits is an error.
247 .PP
248 \&\fBOPENSSL_buf2hexstr()\fR takes the specified buffer and length, and returns
249 a hex string for value, or \s-1NULL\s0 on error.
250 \&\fBBuffer\fR cannot be \s-1NULL\s0; if \fBlen\fR is 0 an empty string is returned.
251 .PP
252 \&\fBOPENSSL_hexchar2int()\fR converts a character to the hexadecimal equivalent,
253 or returns \-1 on error.
254 .PP
255 If no allocations have been done, it is possible to \*(L"swap out\*(R" the default
256 implementations for \fBOPENSSL_malloc()\fR, OPENSSL_realloc and \fBOPENSSL_free()\fR
257 and replace them with alternate versions (hooks).
258 \&\fBCRYPTO_get_mem_functions()\fR function fills in the given arguments with the
259 function pointers for the current implementations.
260 With \fBCRYPTO_set_mem_functions()\fR, you can specify a different set of functions.
261 If any of \fBm\fR, \fBr\fR, or \fBf\fR are \s-1NULL,\s0 then the function is not changed.
262 .PP
263 The default implementation can include some debugging capability (if enabled
264 at build-time).
265 This adds some overhead by keeping a list of all memory allocations, and
266 removes items from the list when they are free'd.
267 This is most useful for identifying memory leaks.
268 \&\fBCRYPTO_set_mem_debug()\fR turns this tracking on and off.  In order to have
269 any effect, is must be called before any of the allocation functions
270 (e.g., \fBCRYPTO_malloc()\fR) are called, and is therefore normally one of the
271 first lines of \fBmain()\fR in an application.
272 \&\fBCRYPTO_mem_ctrl()\fR provides fine-grained control of memory leak tracking.
273 To enable tracking call \fBCRYPTO_mem_ctrl()\fR with a \fBmode\fR argument of
274 the \fB\s-1CRYPTO_MEM_CHECK_ON\s0\fR.
275 To disable tracking call \fBCRYPTO_mem_ctrl()\fR with a \fBmode\fR argument of
276 the \fB\s-1CRYPTO_MEM_CHECK_OFF\s0\fR.
277 .PP
278 While checking memory, it can be useful to store additional context
279 about what is being done.
280 For example, identifying the field names when parsing a complicated
281 data structure.
282 \&\fBOPENSSL_mem_debug_push()\fR (which calls \fBCRYPTO_mem_debug_push()\fR)
283 attaches an identifying string to the allocation stack.
284 This must be a global or other static string; it is not copied.
285 \&\fBOPENSSL_mem_debug_pop()\fR removes identifying state from the stack.
286 .PP
287 At the end of the program, calling \fBCRYPTO_mem_leaks()\fR or
288 \&\fBCRYPTO_mem_leaks_fp()\fR will report all \*(L"leaked\*(R" memory, writing it
289 to the specified \s-1BIO\s0 \fBb\fR or \s-1FILE\s0 \fBfp\fR. These functions return 1 if
290 there are no leaks, 0 if there are leaks and \-1 if an error occurred.
291 .PP
292 \&\fBCRYPTO_mem_leaks_cb()\fR does the same as \fBCRYPTO_mem_leaks()\fR, but instead
293 of writing to a given \s-1BIO,\s0 the callback function is called for each
294 output string with the string, length, and userdata \fBu\fR as the callback
295 parameters.
296 .PP
297 If the library is built with the \f(CW\*(C`crypto\-mdebug\*(C'\fR option, then one
298 function, \fBCRYPTO_get_alloc_counts()\fR, and two additional environment
299 variables, \fB\s-1OPENSSL_MALLOC_FAILURES\s0\fR and \fB\s-1OPENSSL_MALLOC_FD\s0\fR,
300 are available.
301 .PP
302 The function \fBCRYPTO_get_alloc_counts()\fR fills in the number of times
303 each of \fBCRYPTO_malloc()\fR, \fBCRYPTO_realloc()\fR, and \fBCRYPTO_free()\fR have been
304 called, into the values pointed to by \fBmcount\fR, \fBrcount\fR, and \fBfcount\fR,
305 respectively.  If a pointer is \s-1NULL,\s0 then the corresponding count is not stored.
306 .PP
307 The variable
308 \&\fB\s-1OPENSSL_MALLOC_FAILURES\s0\fR controls how often allocations should fail.
309 It is a set of fields separated by semicolons, which each field is a count
310 (defaulting to zero) and an optional atsign and percentage (defaulting
311 to 100).  If the count is zero, then it lasts forever.  For example,
312 \&\f(CW\*(C`100;@25\*(C'\fR or \f(CW\*(C`100@0;0@25\*(C'\fR means the first 100 allocations pass, then all
313 other allocations (until the program exits or crashes) have a 25% chance of
314 failing.
315 .PP
316 If the variable \fB\s-1OPENSSL_MALLOC_FD\s0\fR is parsed as a positive integer, then
317 it is taken as an open file descriptor, and a record of all allocations is
318 written to that descriptor.  If an allocation will fail, and the platform
319 supports it, then a backtrace will be written to the descriptor.  This can
320 be useful because a malloc may fail but not be checked, and problems will
321 only occur later.  The following example in classic shell syntax shows how
322 to use this (will not work on all platforms):
323 .PP
324 .Vb 5
325 \&  OPENSSL_MALLOC_FAILURES=\*(Aq200;@10\*(Aq
326 \&  export OPENSSL_MALLOC_FAILURES
327 \&  OPENSSL_MALLOC_FD=3
328 \&  export OPENSSL_MALLOC_FD
329 \&  ...app invocation... 3>/tmp/log$$
330 .Ve
331 .SH "RETURN VALUES"
332 .IX Header "RETURN VALUES"
333 \&\fBOPENSSL_malloc_init()\fR, \fBOPENSSL_free()\fR, \fBOPENSSL_clear_free()\fR
334 \&\fBCRYPTO_free()\fR, \fBCRYPTO_clear_free()\fR and \fBCRYPTO_get_mem_functions()\fR
335 return no value.
336 .PP
337 \&\fBCRYPTO_mem_leaks()\fR, \fBCRYPTO_mem_leaks_fp()\fR and \fBCRYPTO_mem_leaks_cb()\fR return 1 if
338 there are no leaks, 0 if there are leaks and \-1 if an error occurred.
339 .PP
340 \&\fBOPENSSL_malloc()\fR, \fBOPENSSL_zalloc()\fR, \fBOPENSSL_realloc()\fR,
341 \&\fBOPENSSL_clear_realloc()\fR,
342 \&\fBCRYPTO_malloc()\fR, \fBCRYPTO_zalloc()\fR, \fBCRYPTO_realloc()\fR,
343 \&\fBCRYPTO_clear_realloc()\fR,
344 \&\fBOPENSSL_buf2hexstr()\fR, \fBOPENSSL_hexstr2buf()\fR,
345 \&\fBOPENSSL_strdup()\fR, and \fBOPENSSL_strndup()\fR
346 return a pointer to allocated memory or \s-1NULL\s0 on error.
347 .PP
348 \&\fBCRYPTO_set_mem_functions()\fR and \fBCRYPTO_set_mem_debug()\fR
349 return 1 on success or 0 on failure (almost
350 always because allocations have already happened).
351 .PP
352 \&\fBCRYPTO_mem_ctrl()\fR returns \-1 if an error occurred, otherwise the
353 previous value of the mode.
354 .PP
355 \&\fBOPENSSL_mem_debug_push()\fR and \fBOPENSSL_mem_debug_pop()\fR
356 return 1 on success or 0 on failure.
357 .SH "NOTES"
358 .IX Header "NOTES"
359 While it's permitted to swap out only a few and not all the functions
360 with \fBCRYPTO_set_mem_functions()\fR, it's recommended to swap them all out
361 at once.  \fIThis applies specially if OpenSSL was built with the
362 configuration option\fR \f(CW\*(C`crypto\-mdebug\*(C'\fR \fIenabled.  In case, swapping out
363 only, say, the \f(BImalloc()\fI implementation is outright dangerous.\fR
364 .SH "COPYRIGHT"
365 .IX Header "COPYRIGHT"
366 Copyright 2016\-2020 The OpenSSL Project Authors. All Rights Reserved.
367 .PP
368 Licensed under the OpenSSL license (the \*(L"License\*(R").  You may not use
369 this file except in compliance with the License.  You can obtain a copy
370 in the file \s-1LICENSE\s0 in the source distribution or at
371 <https://www.openssl.org/source/license.html>.