Fix grammar in certificates.txt
[openssl.git] / doc / man3 / OPENSSL_malloc.pod
1 =pod
2
3 =head1 NAME
4
5 OPENSSL_malloc_init,
6 OPENSSL_malloc, OPENSSL_aligned_alloc, OPENSSL_zalloc, OPENSSL_realloc,
7 OPENSSL_free, OPENSSL_clear_realloc, OPENSSL_clear_free, OPENSSL_cleanse,
8 CRYPTO_malloc, CRYPTO_aligned_alloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
9 OPENSSL_strdup, OPENSSL_strndup,
10 OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat,
11 CRYPTO_strdup, CRYPTO_strndup,
12 OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
13 CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
14 CRYPTO_clear_realloc, CRYPTO_clear_free,
15 CRYPTO_malloc_fn, CRYPTO_realloc_fn, CRYPTO_free_fn,
16 CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
17 CRYPTO_get_alloc_counts,
18 CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
19 CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp, CRYPTO_mem_leaks_cb,
20 OPENSSL_MALLOC_FAILURES,
21 OPENSSL_MALLOC_FD
22 - Memory allocation functions
23
24 =head1 SYNOPSIS
25
26  #include <openssl/crypto.h>
27
28  int OPENSSL_malloc_init(void);
29
30  void *OPENSSL_malloc(size_t num);
31  void *OPENSSL_aligned_alloc(size_t num, size_t alignment, void **freeptr);
32  void *OPENSSL_zalloc(size_t num);
33  void *OPENSSL_realloc(void *addr, size_t num);
34  void OPENSSL_free(void *addr);
35  char *OPENSSL_strdup(const char *str);
36  char *OPENSSL_strndup(const char *str, size_t s);
37  size_t OPENSSL_strlcat(char *dst, const char *src, size_t size);
38  size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size);
39  void *OPENSSL_memdup(void *data, size_t s);
40  void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num);
41  void OPENSSL_clear_free(void *str, size_t num);
42  void OPENSSL_cleanse(void *ptr, size_t len);
43
44  void *CRYPTO_malloc(size_t num, const char *file, int line);
45  void *CRYPTO_aligned_alloc(size_t num, size_t align, void **freeptr, 
46                             const char *file, int line);
47  void *CRYPTO_zalloc(size_t num, const char *file, int line);
48  void *CRYPTO_realloc(void *p, size_t num, const char *file, int line);
49  void CRYPTO_free(void *str, const char *, int);
50  char *CRYPTO_strdup(const char *p, const char *file, int line);
51  char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line);
52  void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num,
53                             const char *file, int line);
54  void CRYPTO_clear_free(void *str, size_t num, const char *, int);
55
56  typedef void *(*CRYPTO_malloc_fn)(size_t num, const char *file, int line);
57  typedef void *(*CRYPTO_realloc_fn)(void *addr, size_t num, const char *file,
58                                     int line);
59  typedef void (*CRYPTO_free_fn)(void *addr, const char *file, int line);
60  void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn,
61                                CRYPTO_realloc_fn *realloc_fn,
62                                CRYPTO_free_fn *free_fn);
63  int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn,
64                               CRYPTO_realloc_fn realloc_fn,
65                               CRYPTO_free_fn free_fn);
66
67  void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount);
68
69  env OPENSSL_MALLOC_FAILURES=... <application>
70  env OPENSSL_MALLOC_FD=... <application>
71
72 The following functions have been deprecated since OpenSSL 3.0, and can be
73 hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
74 see L<openssl_user_macros(7)>:
75
76  int CRYPTO_mem_leaks(BIO *b);
77  int CRYPTO_mem_leaks_fp(FILE *fp);
78  int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
79                          void *u);
80
81  int CRYPTO_set_mem_debug(int onoff);
82  int CRYPTO_mem_ctrl(int mode);
83  int OPENSSL_mem_debug_push(const char *info);
84  int OPENSSL_mem_debug_pop(void);
85  int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
86  int CRYPTO_mem_debug_pop(void);
87
88 =head1 DESCRIPTION
89
90 OpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are
91 generally macro's that add the standard C B<__FILE__> and B<__LINE__>
92 parameters and call a lower-level B<CRYPTO_xxx> API.
93 Some functions do not add those parameters, but exist for consistency.
94
95 OPENSSL_malloc_init() does nothing and does not need to be called. It is
96 included for compatibility with older versions of OpenSSL.
97
98 OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the
99 C malloc(), realloc(), and free() functions.
100 OPENSSL_zalloc() calls memset() to zero the memory before returning.
101
102 OPENSSL_aligned_alloc() operates just as OPENSSL_malloc does, but it
103 allows for the caller to specify an alignment value, for instances in
104 which the default alignment of malloc is insufficient for the callers
105 needs.  Note, the alignment value must be a power of 2, and the size
106 specified must be a multiple of the alignment.
107 NOTE: The call to OPENSSL_aligned_alloc() accepts a 3rd argument, I<freeptr>
108 which must point to a void pointer.  On some platforms, there is no available
109 library call to obtain memory allocations greater than what malloc provides.  In
110 this case, OPENSSL_aligned_alloc implements its own alignment routine,
111 allocating additional memory and offsetting the returned pointer to be on the
112 requested alignment boundary.  In order to safely free allocations made by this
113 method, the caller must return the value in the I<freeptr> variable, rather than
114 the returned pointer.
115
116 OPENSSL_clear_realloc() and OPENSSL_clear_free() should be used
117 when the buffer at B<addr> holds sensitive information.
118 The old buffer is filled with zero's by calling OPENSSL_cleanse()
119 before ultimately calling OPENSSL_free().
120
121 OPENSSL_cleanse() fills B<ptr> of size B<len> with a string of 0's.
122 Use OPENSSL_cleanse() with care if the memory is a mapping of a file.
123 If the storage controller uses write compression, then it's possible
124 that sensitive tail bytes will survive zeroization because the block of
125 zeros will be compressed. If the storage controller uses wear leveling,
126 then the old sensitive data will not be overwritten; rather, a block of
127 0's will be written at a new physical location.
128
129 OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
130 equivalent C functions, except that memory is allocated by calling the
131 OPENSSL_malloc() and should be released by calling OPENSSL_free().
132
133 OPENSSL_strlcpy(),
134 OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
135 library functions and are provided for portability.
136
137 If no allocations have been done, it is possible to "swap out" the default
138 implementations for OPENSSL_malloc(), OPENSSL_realloc() and OPENSSL_free()
139 and replace them with alternate versions.
140 CRYPTO_get_mem_functions() function fills in the given arguments with the
141 function pointers for the current implementations.
142 With CRYPTO_set_mem_functions(), you can specify a different set of functions.
143 If any of B<malloc_fn>, B<realloc_fn>, or B<free_fn> are NULL, then
144 the function is not changed.
145 While it's permitted to swap out only a few and not all the functions
146 with CRYPTO_set_mem_functions(), it's recommended to swap them all out
147 at once.
148
149 If the library is built with the C<crypto-mdebug> option, then one
150 function, CRYPTO_get_alloc_counts(), and two additional environment
151 variables, B<OPENSSL_MALLOC_FAILURES> and B<OPENSSL_MALLOC_FD>,
152 are available.
153
154 The function CRYPTO_get_alloc_counts() fills in the number of times
155 each of CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free() have been
156 called, into the values pointed to by B<mcount>, B<rcount>, and B<fcount>,
157 respectively.  If a pointer is NULL, then the corresponding count is not stored.
158
159 The variable
160 B<OPENSSL_MALLOC_FAILURES> controls how often allocations should fail.
161 It is a set of fields separated by semicolons, which each field is a count
162 (defaulting to zero) and an optional atsign and percentage (defaulting
163 to 100).  If the count is zero, then it lasts forever.  For example,
164 C<100;@25> or C<100@0;0@25> means the first 100 allocations pass, then all
165 other allocations (until the program exits or crashes) have a 25% chance of
166 failing.
167
168 If the variable B<OPENSSL_MALLOC_FD> is parsed as a positive integer, then
169 it is taken as an open file descriptor. This is used in conjunction with
170 B<OPENSSL_MALLOC_FAILURES> described above. For every allocation it will log
171 details about how many allocations there have been so far, what percentage
172 chance there is for this allocation failing, and whether it has actually failed.
173 The following example in classic shell syntax shows how to use this (will not
174 work on all platforms):
175
176   OPENSSL_MALLOC_FAILURES='200;@10'
177   export OPENSSL_MALLOC_FAILURES
178   OPENSSL_MALLOC_FD=3
179   export OPENSSL_MALLOC_FD
180   ...app invocation... 3>/tmp/log$$
181
182 =head1 RETURN VALUES
183
184 OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
185 CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
186 return no value.
187
188 OPENSSL_malloc(), OPENSSL_aligned_alloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
189 OPENSSL_clear_realloc(),
190 CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
191 CRYPTO_clear_realloc(),
192 OPENSSL_strdup(), and OPENSSL_strndup()
193 return a pointer to allocated memory or NULL on error.
194
195 CRYPTO_set_mem_functions() returns 1 on success or 0 on failure (almost
196 always because allocations have already happened).
197
198 CRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp(), CRYPTO_mem_leaks_cb(),
199 CRYPTO_set_mem_debug(), and CRYPTO_mem_ctrl() are deprecated and are no-ops that
200 always return -1.
201 OPENSSL_mem_debug_push(), OPENSSL_mem_debug_pop(),
202 CRYPTO_mem_debug_push(), and CRYPTO_mem_debug_pop()
203 are deprecated and are no-ops that always return 0.
204
205 =head1 HISTORY
206
207 OPENSSL_mem_debug_push(), OPENSSL_mem_debug_pop(),
208 CRYPTO_mem_debug_push(), CRYPTO_mem_debug_pop(),
209 CRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp(),
210 CRYPTO_mem_leaks_cb(), CRYPTO_set_mem_debug(), CRYPTO_mem_ctrl()
211 were deprecated in OpenSSL 3.0.
212 The memory-leak checking has been deprecated in OpenSSL 3.0 in favor of
213 clang's memory and leak sanitizer.
214 OPENSSL_aligned_alloc(), CRYPTO_aligned_alloc() were added in OpenSSL 3.4.0
215
216 =head1 COPYRIGHT
217
218 Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
219
220 Licensed under the Apache License 2.0 (the "License").  You may not use
221 this file except in compliance with the License.  You can obtain a copy
222 in the file LICENSE in the source distribution or at
223 L<https://www.openssl.org/source/license.html>.
224
225 =cut