2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 "crypto/cryptlib.h"
16 #include <openssl/crypto.h>
19 * the following pointers may be changed as long as 'allow_customize' is set
21 static int allow_customize
= 1;
22 static CRYPTO_malloc_fn malloc_impl
= CRYPTO_malloc
;
23 static CRYPTO_realloc_fn realloc_impl
= CRYPTO_realloc
;
24 static CRYPTO_free_fn free_impl
= CRYPTO_free
;
26 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
27 # include "internal/tsan_assist.h"
29 static TSAN_QUALIFIER
int malloc_count
;
30 static TSAN_QUALIFIER
int realloc_count
;
31 static TSAN_QUALIFIER
int free_count
;
33 # define INCREMENT(x) tsan_counter(&(x))
35 static char *md_failstring
;
37 static int md_fail_percent
= 0;
38 static int md_tracefd
= -1;
40 static void parseit(void);
41 static int shouldfail(void);
43 # define FAILTEST() if (shouldfail()) return NULL
47 # define INCREMENT(x) /* empty */
48 # define FAILTEST() /* empty */
51 int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn
,
52 CRYPTO_realloc_fn realloc_fn
,
53 CRYPTO_free_fn free_fn
)
57 if (malloc_fn
!= NULL
)
58 malloc_impl
= malloc_fn
;
59 if (realloc_fn
!= NULL
)
60 realloc_impl
= realloc_fn
;
66 void CRYPTO_get_mem_functions(CRYPTO_malloc_fn
*malloc_fn
,
67 CRYPTO_realloc_fn
*realloc_fn
,
68 CRYPTO_free_fn
*free_fn
)
70 if (malloc_fn
!= NULL
)
71 *malloc_fn
= malloc_impl
;
72 if (realloc_fn
!= NULL
)
73 *realloc_fn
= realloc_impl
;
78 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
79 void CRYPTO_get_alloc_counts(int *mcount
, int *rcount
, int *fcount
)
82 *mcount
= tsan_load(&malloc_count
);
84 *rcount
= tsan_load(&realloc_count
);
86 *fcount
= tsan_load(&free_count
);
90 * Parse a "malloc failure spec" string. This likes like a set of fields
91 * separated by semicolons. Each field has a count and an optional failure
92 * percentage. For example:
95 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
96 * all remaining (count is zero) succeed.
98 static void parseit(void)
100 char *semi
= strchr(md_failstring
, ';');
106 /* Get the count (atol will stop at the @ if there), and percentage */
107 md_count
= atol(md_failstring
);
108 atsign
= strchr(md_failstring
, '@');
109 md_fail_percent
= atsign
== NULL
? 0 : atoi(atsign
+ 1);
112 md_failstring
= semi
;
116 * Windows doesn't have random(), but it has rand()
117 * Some rand() implementations aren't good, but we're not
118 * dealing with secure randomness here.
121 # define random() rand()
124 * See if the current malloc should fail.
126 static int shouldfail(void)
128 int roll
= (int)(random() % 100);
129 int shoulditfail
= roll
< md_fail_percent
;
131 /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
135 if (md_tracefd
> 0) {
136 BIO_snprintf(buff
, sizeof(buff
),
137 "%c C%ld %%%d R%d\n",
138 shoulditfail
? '-' : '+', md_count
, md_fail_percent
, roll
);
140 if (write(md_tracefd
, buff
, len
) != len
)
141 perror("shouldfail write failed");
146 /* If we used up this one, go to the next. */
154 void ossl_malloc_setup_failures(void)
156 const char *cp
= getenv("OPENSSL_MALLOC_FAILURES");
158 if (cp
!= NULL
&& (md_failstring
= strdup(cp
)) != NULL
)
160 if ((cp
= getenv("OPENSSL_MALLOC_FD")) != NULL
)
161 md_tracefd
= atoi(cp
);
165 void *CRYPTO_malloc(size_t num
, const char *file
, int line
)
167 INCREMENT(malloc_count
);
168 if (malloc_impl
!= CRYPTO_malloc
)
169 return malloc_impl(num
, file
, line
);
175 if (allow_customize
) {
177 * Disallow customization after the first allocation. We only set this
178 * if necessary to avoid a store to the same cache line on every
187 void *CRYPTO_zalloc(size_t num
, const char *file
, int line
)
191 ret
= CRYPTO_malloc(num
, file
, line
);
199 void *CRYPTO_realloc(void *str
, size_t num
, const char *file
, int line
)
201 INCREMENT(realloc_count
);
202 if (realloc_impl
!= CRYPTO_realloc
)
203 return realloc_impl(str
, num
, file
, line
);
207 return CRYPTO_malloc(num
, file
, line
);
210 CRYPTO_free(str
, file
, line
);
214 return realloc(str
, num
);
217 void *CRYPTO_clear_realloc(void *str
, size_t old_len
, size_t num
,
218 const char *file
, int line
)
223 return CRYPTO_malloc(num
, file
, line
);
226 CRYPTO_clear_free(str
, old_len
, file
, line
);
230 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
232 OPENSSL_cleanse((char*)str
+ num
, old_len
- num
);
236 ret
= CRYPTO_malloc(num
, file
, line
);
238 memcpy(ret
, str
, old_len
);
239 CRYPTO_clear_free(str
, old_len
, file
, line
);
244 void CRYPTO_free(void *str
, const char *file
, int line
)
246 INCREMENT(free_count
);
247 if (free_impl
!= CRYPTO_free
) {
248 free_impl(str
, file
, line
);
255 void CRYPTO_clear_free(void *str
, size_t num
, const char *file
, int line
)
260 OPENSSL_cleanse(str
, num
);
261 CRYPTO_free(str
, file
, line
);
264 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG)
266 # ifndef OPENSSL_NO_DEPRECATED_3_0
267 int CRYPTO_mem_ctrl(int mode
)
273 int CRYPTO_set_mem_debug(int flag
)
279 int CRYPTO_mem_debug_push(const char *info
, const char *file
, int line
)
281 (void)info
; (void)file
; (void)line
;
285 int CRYPTO_mem_debug_pop(void)
290 void CRYPTO_mem_debug_malloc(void *addr
, size_t num
, int flag
,
291 const char *file
, int line
)
293 (void)addr
; (void)num
; (void)flag
; (void)file
; (void)line
;
296 void CRYPTO_mem_debug_realloc(void *addr1
, void *addr2
, size_t num
, int flag
,
297 const char *file
, int line
)
299 (void)addr1
; (void)addr2
; (void)num
; (void)flag
; (void)file
; (void)line
;
302 void CRYPTO_mem_debug_free(void *addr
, int flag
,
303 const char *file
, int line
)
305 (void)addr
; (void)flag
; (void)file
; (void)line
;
308 int CRYPTO_mem_leaks(BIO
*b
)
314 # ifndef OPENSSL_NO_STDIO
315 int CRYPTO_mem_leaks_fp(FILE *fp
)
322 int CRYPTO_mem_leaks_cb(int (*cb
)(const char *str
, size_t len
, void *u
),