Clear the exit code from 'find' in 'make depend'
[openssl.git] / doc / crypto / OPENSSL_malloc.pod
1 =pod
2
3 =head1 NAME
4
5 OPENSSL_malloc_init,
6 OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_free,
7 OPENSSL_clear_realloc, OPENSSL_clear_free, OPENSSL_cleanse
8 CRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
9 OPENSSL_strdup, OPENSSL_strndup,
10 OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat,
11 CRYPTO_clear_realloc, CRYPTO_clear_free,
12 CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
13 CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
14 OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
15 CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
16 CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp - Memory allocation functions
17
18 =head1 SYNOPSIS
19
20  #include <openssl/crypto.h>
21
22  int OPENSSL_malloc_init(void)
23
24  void *OPENSSL_malloc(size_t num)
25  void *OPENSSL_zalloc(size_t num)
26  void *OPENSSL_realloc(void *addr, size_t num)
27  void OPENSSL_free(void *addr)
28  char *OPENSSL_strdup(const char *str)
29  char *OPENSSL_strndup(const char *str, size_t s)
30  void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num)
31  void OPENSSL_clear_free(void *str, size_t num)
32  void OPENSSL_cleanse(void *ptr, size_t len);
33
34  void *CRYPTO_malloc(size_t num, const char *file, int line)
35  void *CRYPTO_zalloc(size_t num, const char *file, int line)
36  void *CRYPTO_realloc(void *p, size_t num, const char *file, int line)
37  void CRYPTO_free(void *str, const char *, int)
38  char *CRYPTO_strdup(const char *p, const char *file, int line)
39  char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line)
40  void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num, const char *file, int line)
41  void CRYPTO_clear_free(void *str, size_t num, const char *, int)
42
43  void CRYPTO_get_mem_functions(
44          void *(**m)(size_t, const char *, int),
45          void *(**r)(void *, size_t, const char *, int),
46          void (**f)(void *, const char *, int))
47  int CRYPTO_set_mem_functions(
48          void *(*m)(size_t, const char *, int),
49          void *(*r)(void *, size_t, const char *, int),
50          void (*f)(void *, const char *, int))
51
52  int CRYPTO_set_mem_debug(int onoff)
53
54  #define CRYPTO_MEM_CHECK_OFF
55  #define CRYPTO_MEM_CHECK_ON
56  #define CRYPTO_MEM_CHECK_DISABLE
57  #define CRYPTO_MEM_CHECK_ENABLE
58
59  int CRYPTO_mem_ctrl(int mode);
60
61  int OPENSSL_mem_debug_push(const char *info)
62  int OPENSLS_mem_debug_pop)(void)
63
64  int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
65
66  void CRYPTO_mem_leaks(BIO *b);
67  void CRYPTO_mem_leaks(FILE *fp);
68
69 =head1 DESCRIPTION
70
71 OpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are
72 generally macro's that add the standard C B<__FILE__> and B<__LINE__>
73 parameters and call a lower-level B<CRYPTO_xxx> API.
74 Some functions do not add those parameters, but exist for consistency.
75
76 OPENSSL_malloc_init() sets the lower-level memory allocation functions
77 to their default implementation.
78 It is generally not necessary to call this, except perhaps in certain
79 shared-library situations.
80
81 OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the
82 C malloc(), realloc(), and free() functions.
83 OPENSSL_zalloc() calls memset() to zero the memory before returning.
84
85 OPENSSL_clear_realloc() and OPENSSL_clear_free() should be used
86 when the buffer at B<addr> holds sensitive information.
87 The old buffer is filled with zero's by calling OPENSSL_cleanse()
88 before ultimately calling OPENSSL_free().
89
90 OPENSSL_cleanse() fills B<ptr> of size B<len> with a string of 0's.
91 Use OPENSSL_cleanse() with care if the memory is a mapping of a file.
92 If the storage controller uses write compression, then its possible 
93 that sensitive tail bytes will survive zeroization because the block of 
94 zeros will be compressed. If the storage controller uses wear leveling, 
95 then the old sensitive data will not be overwritten; rather, a block of 
96 0's will be written at a new physical location.
97
98 OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
99 equivalent C functions, except that memory is allocated by calling the
100 OPENSSL_malloc() and should be releaed by calling OPENSSL_free().
101
102 OPENSSL_strlcpy(),
103 OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
104 library functions and are provided for portability.
105
106 If no allocations have been done, it is possible to "swap out" the default
107 implementations for OPENSSL_malloc(), OPENSSL_realloc and OPENSSL_free()
108 and replace them with alternate versions (hooks).
109 CRYPTO_get_mem_functions() function fills in the given arguments with the
110 function pointers for the current implementations.
111 With CRYPTO_set_mem_functions(), you can specify a different set of functions.
112 If any of B<m>, B<r>, or B<f> are NULL, then the function is not changed.
113
114 The default implementation can include some debugging capability (if enabled
115 at build-time).
116 This adds some overhead by keeping a list of all memory allocations, and
117 removes items from the list when they are free'd.
118 This is most useful for identifying memory leaks.
119 CRYPTO_set_mem_debug() turns this tracking on and off.  It is normally
120 called at startup, but can be called at any time.
121
122 CRYPTO_mem_ctrl() provides fine-grained control of memory leak tracking.
123 To enable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
124 the B<CRYPTO_MEM_CHECK_ON>.
125 To disable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
126 the B<CRYPTO_MEM_CHECK_OFF>.
127 The B<CRYPTO_MEM_CHECK_DISABLE> and B<CRYPTO_MEM_CHECK_ENABLE> modes
128 are used internally within OpenSSL to temporarily suspend and resume
129 tracking.
130
131 While checking memory, it can be useful to store additional context
132 about what is being done.
133 For example, identifying the field names when parsing a complicated
134 data structure.
135 OPENSSL_mem_debug_push() (which calls CRYPTO_mem_debug_push())
136 attachs an identifying string to the allocation stack.
137 This must be a global or other static string; it is not copied.
138 OPENSSL_mem_debug_pop() removes identifying state from the stack.
139
140 At the end of the program, calling CRYPTO_mem_leaks() or
141 CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it
142 to the specified BIO B<b> or FILE B<fp>. These functions return 1 if
143 there are no leaks, 0 if there are leaks and -1 if an error occurred.
144
145 =head1 RETURN VALUES
146
147 OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
148 CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
149 return no value.
150
151 CRYPTO_mem_leaks() and CRYPTO_mem_leaks_fp() return 1 if there
152 are no leaks, 0 if there are leaks and -1 if an error occurred.
153
154 OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
155 OPENSSL_clear_realloc(),
156 CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
157 CRYPTO_clear_realloc(),
158 OPENSSL_strdup(), and OPENSSL_strndup()
159 return a pointer to allocated memory or NULL on error.
160
161 CRYPTO_set_mem_functions() and CRYPTO_set_mem_debug()
162 return 1 on success or 0 on failure (almost
163 always because allocations have already happened).
164
165 CRYPTO_mem_ctrl() returns the previous value of the mode.
166
167 OPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop()
168 return 1 on success or 0 on failure.
169
170 =head1 NOTES
171
172 While it's permitted to swap out only a few and not all the functions
173 with CRYPTO_set_mem_functions(), it's recommended to swap them all out
174 at once.  I<This applies specially if OpenSSL was built with the
175 configuration option> C<crypto-mdebug> I<enabled.  In case, swapping out
176 only, say, the malloc() implementation is outright dangerous.>
177
178 =cut