04fa0b718c76ba674e25f8c79e180a6896cdb52d
[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,
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 arbitrary data by calling OPENSSL_cleanse()
88 before ultimately calling OPENSSL_free().
89
90 OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
91 equivalent C functions, except that memory is allocated by calling the
92 OPENSSL_malloc() and should be releaed by calling OPENSSL_free().
93
94 OPENSSL_strlcpy(),
95 OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
96 library functions and are provided for portability.
97
98 If no allocations have been done, it is possible to "swap out" the default
99 implementations for OPENSSL_malloc(), OPENSSL_realloc and OPENSSL_free()
100 and replace them with alternate versions (hooks).
101 CRYPTO_get_mem_functions() function fills in the given arguments with the
102 function pointers for the current implementations.
103 With CRYPTO_set_mem_functions(), you can specify a different set of functions.
104 If any of B<m>, B<r>, or B<f> are NULL, then the function is not changed.
105
106 The default implementation can include some debugging capability (if enabled
107 at build-time).
108 This adds some overhead by keeping a list of all memory allocations, and
109 removes items from the list when they are free'd.
110 This is most useful for identifying memory leaks.
111 CRYPTO_set_mem_debug() turns this tracking on and off.  It is normally
112 called at startup, but can be called at any time.
113
114 CRYPTO_mem_ctrl() provides fine-grained control of memory leak tracking.
115 To enable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
116 the B<CRYPTO_MEM_CHECK_ON>.
117 To disable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
118 the B<CRYPTO_MEM_CHECK_OFF>.
119 The B<CRYPTO_MEM_CHECK_DISABLE> and B<CRYPTO_MEM_CHECK_ENABLE> modes
120 are used internally within OpenSSL to temporarily suspend and resume
121 tracking.
122
123 While checking memory, it can be useful to store additional context
124 about what is being done.
125 For example, identifying the field names when parsing a complicated
126 data structure.
127 OPENSSL_mem_debug_push() (which calls CRYPTO_mem_debug_push())
128 attachs an identifying string to the allocation stack.
129 This must be a global or other static string; it is not copied.
130 OPENSSL_mem_debug_pop() removes identifying state from the stack.
131
132 At the end of the program, calling CRYPTO_mem_leaks() or
133 CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it
134 to the specified BIO B<b> or FILE B<fp>. These functions return 1 if
135 there are no leaks, 0 if there are leaks and -1 if an error occurred.
136
137 =head1 RETURN VALUES
138
139 OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
140 CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
141 return no value.
142
143 CRYPTO_mem_leaks() and CRYPTO_mem_leaks_fp() return 1 if there
144 are no leaks, 0 if there are leaks and -1 if an error occurred.
145
146 OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
147 OPENSSL_clear_realloc(),
148 CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
149 CRYPTO_clear_realloc(),
150 OPENSSL_strdup(), and OPENSSL_strndup()
151 return a pointer to allocated memory or NULL on error.
152
153 CRYPTO_set_mem_functions() and CRYPTO_set_mem_debug()
154 return 1 on success or 0 on failure (almost
155 always because allocations have already happened).
156
157 CRYPTO_mem_ctrl() returns the previous value of the mode.
158
159 OPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop()
160 return 1 on success or 0 on failure.
161
162 =head1 NOTES
163
164 While it's permitted to swap out only a few and not all the functions
165 with CRYPTO_set_mem_functions(), it's recommended to swap them all out
166 at once.  I<This applies specially if OpenSSL was built with the
167 configuration option> C<crypto-mdebug> I<enabled.  In case, swapping out
168 only, say, the malloc() implementation is outright dangerous.>
169
170 =cut