2 * Copyright 1995-2016 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
13 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
15 #include "internal/cryptlib_int.h"
16 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
17 # include <execinfo.h>
21 * the following pointers may be changed as long as 'allow_customize' is set
23 static int allow_customize = 1;
25 static void *(*malloc_impl)(size_t, const char *, int)
27 static void *(*realloc_impl)(void *, size_t, const char *, int)
29 static void (*free_impl)(void *, const char *, int)
32 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
33 static char *md_failstring;
35 static int md_percent = 100;
36 static int md_tracefd = -1;
37 static int call_malloc_debug = 1;
39 static void parseit(void);
40 static int shouldfail(void);
42 # define FAILTEST() if (shouldfail()) return NULL
45 static int call_malloc_debug = 0;
47 # define FAILTEST() /* empty */
50 int CRYPTO_set_mem_functions(
51 void *(*m)(size_t, const char *, int),
52 void *(*r)(void *, size_t, const char *, int),
53 void (*f)(void *, const char *, int))
66 int CRYPTO_set_mem_debug(int flag)
70 call_malloc_debug = flag;
74 void CRYPTO_get_mem_functions(
75 void *(**m)(size_t, const char *, int),
76 void *(**r)(void *, size_t, const char *, int),
77 void (**f)(void *, const char *, int))
87 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
89 * Parse a "malloc failure spec" string. This likes like a set of fields
90 * separated by semicolons. Each field has a count and an optional failure
91 * percentage. For example:
93 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
94 * all remaining (count is zero) succeed.
96 static void parseit(void)
98 char *semi = strchr(md_failstring, ';');
104 /* Get the count (atol will stop at the @ if there), and percentage */
105 md_count = atol(md_failstring);
106 atsign = strchr(md_failstring, '@');
107 md_percent = atsign == NULL ? 100 : atoi(atsign + 1);
110 md_failstring = semi;
114 * See if the current malloc should fail.
116 static int shouldfail(void)
118 int roll = (int)(random() % 100);
119 int shouldfail = roll > md_percent;
122 if (md_tracefd > 0) {
123 BIO_snprintf(buff, sizeof(buff),
124 "%c C%ld %%%d R%d\n",
125 shouldfail ? '-' : '+', md_count, md_percent, roll);
126 write(md_tracefd, buff, strlen(buff));
127 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
130 int num = backtrace(addrs, OSSL_NELEM(addrs));
132 backtrace_symbols_fd(addrs, num, md_tracefd);
138 /* If we used up this one, go to the next. */
146 void ossl_malloc_setup_failures(void)
148 const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
150 if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
152 if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
153 md_tracefd = atoi(cp);
157 void *CRYPTO_malloc(size_t num, const char *file, int line)
161 if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
162 return malloc_impl(num, file, line);
169 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
170 if (call_malloc_debug) {
171 CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
173 CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
178 osslargused(file); osslargused(line);
185 void *CRYPTO_zalloc(size_t num, const char *file, int line)
187 void *ret = CRYPTO_malloc(num, file, line);
195 void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
197 if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
198 return realloc_impl(str, num, file, line);
202 return CRYPTO_malloc(num, file, line);
205 CRYPTO_free(str, file, line);
210 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
211 if (call_malloc_debug) {
213 CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
214 ret = realloc(str, num);
215 CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
219 osslargused(file); osslargused(line);
221 return realloc(str, num);
225 void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
226 const char *file, int line)
231 return CRYPTO_malloc(num, file, line);
234 CRYPTO_clear_free(str, old_len, file, line);
238 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
240 OPENSSL_cleanse((char*)str + num, old_len - num);
244 ret = CRYPTO_malloc(num, file, line);
246 memcpy(ret, str, old_len);
247 CRYPTO_clear_free(str, old_len, file, line);
252 void CRYPTO_free(void *str, const char *file, int line)
254 if (free_impl != NULL && free_impl != &CRYPTO_free) {
255 free_impl(str, file, line);
259 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
260 if (call_malloc_debug) {
261 CRYPTO_mem_debug_free(str, 0, file, line);
263 CRYPTO_mem_debug_free(str, 1, file, line);
272 void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
277 OPENSSL_cleanse(str, num);
278 CRYPTO_free(str, file, line);