1OPENSSL_MALLOC(3)                   OpenSSL                  OPENSSL_MALLOC(3)
2
3
4

NAME

6       OPENSSL_malloc_init, OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc,
7       OPENSSL_free, OPENSSL_clear_realloc, OPENSSL_clear_free,
8       OPENSSL_cleanse, CRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc,
9       CRYPTO_free, OPENSSL_strdup, OPENSSL_strndup, OPENSSL_memdup,
10       OPENSSL_strlcpy, OPENSSL_strlcat, OPENSSL_hexstr2buf,
11       OPENSSL_buf2hexstr, OPENSSL_hexchar2int, CRYPTO_strdup, CRYPTO_strndup,
12       OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop, CRYPTO_mem_debug_push,
13       CRYPTO_mem_debug_pop, CRYPTO_clear_realloc, CRYPTO_clear_free,
14       CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
15       CRYPTO_get_alloc_counts, CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
16       CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp, CRYPTO_mem_leaks_cb,
17       OPENSSL_MALLOC_FAILURES, OPENSSL_MALLOC_FD - Memory allocation
18       functions
19

SYNOPSIS

21        #include <openssl/crypto.h>
22
23        int OPENSSL_malloc_init(void)
24
25        void *OPENSSL_malloc(size_t num)
26        void *OPENSSL_zalloc(size_t num)
27        void *OPENSSL_realloc(void *addr, size_t num)
28        void OPENSSL_free(void *addr)
29        char *OPENSSL_strdup(const char *str)
30        char *OPENSSL_strndup(const char *str, size_t s)
31        size_t OPENSSL_strlcat(char *dst, const char *src, size_t size);
32        size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size);
33        void *OPENSSL_memdup(void *data, size_t s)
34        void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num)
35        void OPENSSL_clear_free(void *str, size_t num)
36        void OPENSSL_cleanse(void *ptr, size_t len);
37
38        unsigned char *OPENSSL_hexstr2buf(const char *str, long *len);
39        char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len);
40        int OPENSSL_hexchar2int(unsigned char c);
41
42        void *CRYPTO_malloc(size_t num, const char *file, int line)
43        void *CRYPTO_zalloc(size_t num, const char *file, int line)
44        void *CRYPTO_realloc(void *p, size_t num, const char *file, int line)
45        void CRYPTO_free(void *str, const char *, int)
46        char *CRYPTO_strdup(const char *p, const char *file, int line)
47        char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line)
48        void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num,
49                                   const char *file, int line)
50        void CRYPTO_clear_free(void *str, size_t num, const char *, int)
51
52        void CRYPTO_get_mem_functions(
53                void *(**m)(size_t, const char *, int),
54                void *(**r)(void *, size_t, const char *, int),
55                void (**f)(void *, const char *, int))
56        int CRYPTO_set_mem_functions(
57                void *(*m)(size_t, const char *, int),
58                void *(*r)(void *, size_t, const char *, int),
59                void (*f)(void *, const char *, int))
60
61        void CRYPTO_get_alloc_counts(int *m, int *r, int *f)
62
63        int CRYPTO_set_mem_debug(int onoff)
64
65        env OPENSSL_MALLOC_FAILURES=... <application>
66        env OPENSSL_MALLOC_FD=... <application>
67
68        int CRYPTO_mem_ctrl(int mode);
69
70        int OPENSSL_mem_debug_push(const char *info)
71        int OPENSSL_mem_debug_pop(void);
72
73        int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
74        int CRYPTO_mem_debug_pop(void);
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

DESCRIPTION

82       OpenSSL memory allocation is handled by the OPENSSL_xxx API. These are
83       generally macro's that add the standard C __FILE__ and __LINE__
84       parameters and call a lower-level CRYPTO_xxx API.  Some functions do
85       not add those parameters, but exist for consistency.
86
87       OPENSSL_malloc_init() sets the lower-level memory allocation functions
88       to their default implementation.  It is generally not necessary to call
89       this, except perhaps in certain shared-library situations.
90
91       OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the C
92       malloc(), realloc(), and free() functions.  OPENSSL_zalloc() calls
93       memset() to zero the memory before returning.
94
95       OPENSSL_clear_realloc() and OPENSSL_clear_free() should be used when
96       the buffer at addr holds sensitive information.  The old buffer is
97       filled with zero's by calling OPENSSL_cleanse() before ultimately
98       calling OPENSSL_free().
99
100       OPENSSL_cleanse() fills ptr of size len with a string of 0's.  Use
101       OPENSSL_cleanse() with care if the memory is a mapping of a file.  If
102       the storage controller uses write compression, then its possible that
103       sensitive tail bytes will survive zeroization because the block of
104       zeros will be compressed. If the storage controller uses wear leveling,
105       then the old sensitive data will not be overwritten; rather, a block of
106       0's will be written at a new physical location.
107
108       OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
109       equivalent C functions, except that memory is allocated by calling the
110       OPENSSL_malloc() and should be released by calling OPENSSL_free().
111
112       OPENSSL_strlcpy(), OPENSSL_strlcat() and OPENSSL_strnlen() are
113       equivalents of the common C library functions and are provided for
114       portability.
115
116       OPENSSL_hexstr2buf() parses str as a hex string and returns a pointer
117       to the parsed value. The memory is allocated by calling
118       OPENSSL_malloc() and should be released by calling OPENSSL_free().  If
119       len is not NULL, it is filled in with the output length.  Colons
120       between two-character hex "bytes" are ignored.  An odd number of hex
121       digits is an error.
122
123       OPENSSL_buf2hexstr() takes the specified buffer and length, and returns
124       a hex string for value, or NULL on error.  Buffer cannot be NULL; if
125       len is 0 an empty string is returned.
126
127       OPENSSL_hexchar2int() converts a character to the hexadecimal
128       equivalent, or returns -1 on error.
129
130       If no allocations have been done, it is possible to "swap out" the
131       default implementations for OPENSSL_malloc(), OPENSSL_realloc and
132       OPENSSL_free() and replace them with alternate versions (hooks).
133       CRYPTO_get_mem_functions() function fills in the given arguments with
134       the function pointers for the current implementations.  With
135       CRYPTO_set_mem_functions(), you can specify a different set of
136       functions.  If any of m, r, or f are NULL, then the function is not
137       changed.
138
139       The default implementation can include some debugging capability (if
140       enabled at build-time).  This adds some overhead by keeping a list of
141       all memory allocations, and removes items from the list when they are
142       free'd.  This is most useful for identifying memory leaks.
143       CRYPTO_set_mem_debug() turns this tracking on and off.  In order to
144       have any effect, is must be called before any of the allocation
145       functions (e.g., CRYPTO_malloc()) are called, and is therefore normally
146       one of the first lines of main() in an application.  CRYPTO_mem_ctrl()
147       provides fine-grained control of memory leak tracking.  To enable
148       tracking call CRYPTO_mem_ctrl() with a mode argument of the
149       CRYPTO_MEM_CHECK_ON.  To disable tracking call CRYPTO_mem_ctrl() with a
150       mode argument of the CRYPTO_MEM_CHECK_OFF.
151
152       While checking memory, it can be useful to store additional context
153       about what is being done.  For example, identifying the field names
154       when parsing a complicated data structure.  OPENSSL_mem_debug_push()
155       (which calls CRYPTO_mem_debug_push()) attachs an identifying string to
156       the allocation stack.  This must be a global or other static string; it
157       is not copied.  OPENSSL_mem_debug_pop() removes identifying state from
158       the stack.
159
160       At the end of the program, calling CRYPTO_mem_leaks() or
161       CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it to
162       the specified BIO b or FILE fp. These functions return 1 if there are
163       no leaks, 0 if there are leaks and -1 if an error occurred.
164
165       CRYPTO_mem_leaks_cb() does the same as CRYPTO_mem_leaks(), but instead
166       of writing to a given BIO, the callback function is called for each
167       output string with the string, length, and userdata u as the callback
168       parameters.
169
170       If the library is built with the "crypto-mdebug" option, then one
171       function, CRYPTO_get_alloc_counts(), and two additional environment
172       variables, OPENSSL_MALLOC_FAILURES and OPENSSL_MALLOC_FD, are
173       available.
174
175       The function CRYPTO_get_alloc_counts() fills in the number of times
176       each of CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free() have been
177       called, into the values pointed to by mcount, rcount, and fcount,
178       respectively.  If a pointer is NULL, then the corresponding count is
179       not stored.
180
181       The variable OPENSSL_MALLOC_FAILURES controls how often allocations
182       should fail.  It is a set of fields separated by semicolons, which each
183       field is a count (defaulting to zero) and an optional atsign and
184       percentage (defaulting to 100).  If the count is zero, then it lasts
185       forever.  For example, "100;@25" or "100@0;0@25" means the first 100
186       allocations pass, then all other allocations (until the program exits
187       or crashes) have a 25% chance of failing.
188
189       If the variable OPENSSL_MALLOC_FD is parsed as a positive integer, then
190       it is taken as an open file descriptor, and a record of all allocations
191       is written to that descriptor.  If an allocation will fail, and the
192       platform supports it, then a backtrace will be written to the
193       descriptor.  This can be useful because a malloc may fail but not be
194       checked, and problems will only occur later.  The following example in
195       classic shell syntax shows how to use this (will not work on all
196       platforms):
197
198         OPENSSL_MALLOC_FAILURES='200;@10'
199         export OPENSSL_MALLOC_FAILURES
200         OPENSSL_MALLOC_FD=3
201         export OPENSSL_MALLOC_FD
202         ...app invocation... 3>/tmp/log$$
203

RETURN VALUES

205       OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
206       CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
207       return no value.
208
209       CRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp() and CRYPTO_mem_leaks_cb()
210       return 1 if there are no leaks, 0 if there are leaks and -1 if an error
211       occurred.
212
213       OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
214       OPENSSL_clear_realloc(), CRYPTO_malloc(), CRYPTO_zalloc(),
215       CRYPTO_realloc(), CRYPTO_clear_realloc(), OPENSSL_buf2hexstr(),
216       OPENSSL_hexstr2buf(), OPENSSL_strdup(), and OPENSSL_strndup() return a
217       pointer to allocated memory or NULL on error.
218
219       CRYPTO_set_mem_functions() and CRYPTO_set_mem_debug() return 1 on
220       success or 0 on failure (almost always because allocations have already
221       happened).
222
223       CRYPTO_mem_ctrl() returns -1 if an error occurred, otherwise the
224       previous value of the mode.
225
226       OPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop() return 1 on
227       success or 0 on failure.
228

NOTES

230       While it's permitted to swap out only a few and not all the functions
231       with CRYPTO_set_mem_functions(), it's recommended to swap them all out
232       at once.  This applies specially if OpenSSL was built with the
233       configuration option "crypto-mdebug" enabled.  In case, swapping out
234       only, say, the malloc() implementation is outright dangerous.
235
237       Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
238
239       Licensed under the OpenSSL license (the "License").  You may not use
240       this file except in compliance with the License.  You can obtain a copy
241       in the file LICENSE in the source distribution or at
242       <https://www.openssl.org/source/license.html>.
243
244
245
2461.1.1                             2018-09-11                 OPENSSL_MALLOC(3)
Impressum