2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include "internal/cryptlib_int.h"
16 #include <openssl/crypto.h>
17 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
18 # include <execinfo.h>
22 * the following pointers may be changed as long as 'allow_customize' is set
24 static int allow_customize = 1;
26 static void *(*malloc_impl)(size_t, const char *, int)
28 static void *(*realloc_impl)(void *, size_t, const char *, int)
30 static void (*free_impl)(void *, const char *, int)
33 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
34 static int malloc_count;
35 static int realloc_count;
36 static int free_count;
39 # define INCREMENT(x) CRYPTO_atomic_add(&x, 1, &dummy, memdbg_lock)
40 # define GET(ret, val) CRYPTO_atomic_read(&val, ret, memdbg_lock)
42 static char *md_failstring;
44 static int md_fail_percent = 0;
45 static int md_tracefd = -1;
46 static int call_malloc_debug = 1;
48 static void parseit(void);
49 static int shouldfail(void);
51 # define FAILTEST() if (shouldfail()) return NULL
54 static int call_malloc_debug = 0;
56 # define INCREMENT(x) /* empty */
57 # define FAILTEST() /* empty */
60 int CRYPTO_set_mem_functions(
61 void *(*m)(size_t, const char *, int),
62 void *(*r)(void *, size_t, const char *, int),
63 void (*f)(void *, const char *, int))
76 int CRYPTO_set_mem_debug(int flag)
80 call_malloc_debug = flag;
84 void CRYPTO_get_mem_functions(
85 void *(**m)(size_t, const char *, int),
86 void *(**r)(void *, size_t, const char *, int),
87 void (**f)(void *, const char *, int))
97 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
98 void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
101 GET(mcount, malloc_count);
103 GET(rcount, realloc_count);
105 GET(fcount, free_count);
109 * Parse a "malloc failure spec" string. This likes like a set of fields
110 * separated by semicolons. Each field has a count and an optional failure
111 * percentage. For example:
114 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
115 * all remaining (count is zero) succeed.
117 static void parseit(void)
119 char *semi = strchr(md_failstring, ';');
125 /* Get the count (atol will stop at the @ if there), and percentage */
126 md_count = atol(md_failstring);
127 atsign = strchr(md_failstring, '@');
128 md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
131 md_failstring = semi;
135 * Windows doesn't have random(), but it has rand()
136 * Some rand() implementations aren't good, but we're not
137 * dealing with secure randomness here.
140 # define random() rand()
143 * See if the current malloc should fail.
145 static int shouldfail(void)
147 int roll = (int)(random() % 100);
148 int shoulditfail = roll < md_fail_percent;
150 /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
154 if (md_tracefd > 0) {
155 BIO_snprintf(buff, sizeof(buff),
156 "%c C%ld %%%d R%d\n",
157 shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
159 if (write(md_tracefd, buff, len) != len)
160 perror("shouldfail write failed");
161 # ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
164 int num = backtrace(addrs, OSSL_NELEM(addrs));
166 backtrace_symbols_fd(addrs, num, md_tracefd);
173 /* If we used up this one, go to the next. */
181 void ossl_malloc_setup_failures(void)
183 const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
185 if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
187 if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
188 md_tracefd = atoi(cp);
192 void *CRYPTO_malloc(size_t num, const char *file, int line)
196 INCREMENT(malloc_count);
197 if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
198 return malloc_impl(num, file, line);
205 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
206 if (call_malloc_debug) {
207 CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
209 CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
214 (void)(file); (void)(line);
221 void *CRYPTO_zalloc(size_t num, const char *file, int line)
223 void *ret = CRYPTO_malloc(num, file, line);
231 void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
233 INCREMENT(realloc_count);
234 if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
235 return realloc_impl(str, num, file, line);
239 return CRYPTO_malloc(num, file, line);
242 CRYPTO_free(str, file, line);
247 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
248 if (call_malloc_debug) {
250 CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
251 ret = realloc(str, num);
252 CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
256 (void)(file); (void)(line);
258 return realloc(str, num);
262 void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
263 const char *file, int line)
268 return CRYPTO_malloc(num, file, line);
271 CRYPTO_clear_free(str, old_len, file, line);
275 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
277 OPENSSL_cleanse((char*)str + num, old_len - num);
281 ret = CRYPTO_malloc(num, file, line);
283 memcpy(ret, str, old_len);
284 CRYPTO_clear_free(str, old_len, file, line);
289 void CRYPTO_free(void *str, const char *file, int line)
291 INCREMENT(free_count);
292 if (free_impl != NULL && free_impl != &CRYPTO_free) {
293 free_impl(str, file, line);
297 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
298 if (call_malloc_debug) {
299 CRYPTO_mem_debug_free(str, 0, file, line);
301 CRYPTO_mem_debug_free(str, 1, file, line);
310 void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
315 OPENSSL_cleanse(str, num);
316 CRYPTO_free(str, file, line);