]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/mem.c
2 * Copyright 1995-2018 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;
23 static void *(*malloc_impl
)(size_t, const char *, int)
25 static void *(*realloc_impl
)(void *, size_t, const char *, int)
27 static void (*free_impl
)(void *, const char *, int)
30 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)
31 # include "internal/tsan_assist.h"
33 static TSAN_QUALIFIER
int malloc_count
;
34 static TSAN_QUALIFIER
int realloc_count
;
35 static TSAN_QUALIFIER
int free_count
;
37 # define INCREMENT(x) tsan_counter(&(x))
39 static char *md_failstring
;
41 static int md_fail_percent
= 0;
42 static int md_tracefd
= -1;
44 static void parseit(void);
45 static int shouldfail(void);
47 # define FAILTEST() if (shouldfail()) return NULL
51 # define INCREMENT(x) /* empty */
52 # define FAILTEST() /* empty */
55 int CRYPTO_set_mem_functions(
56 void *(*m
)(size_t, const char *, int),
57 void *(*r
)(void *, size_t, const char *, int),
58 void (*f
)(void *, const char *, int))
71 void CRYPTO_get_mem_functions(
72 void *(**m
)(size_t, const char *, int),
73 void *(**r
)(void *, size_t, const char *, int),
74 void (**f
)(void *, const char *, int))
84 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)
85 void CRYPTO_get_alloc_counts(int *mcount
, int *rcount
, int *fcount
)
88 *mcount
= tsan_load(&malloc_count
);
90 *rcount
= tsan_load(&realloc_count
);
92 *fcount
= tsan_load(&free_count
);
96 * Parse a "malloc failure spec" string. This likes like a set of fields
97 * separated by semicolons. Each field has a count and an optional failure
98 * percentage. For example:
101 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
102 * all remaining (count is zero) succeed.
104 static void parseit(void)
106 char *semi
= strchr(md_failstring
, ';');
112 /* Get the count (atol will stop at the @ if there), and percentage */
113 md_count
= atol(md_failstring
);
114 atsign
= strchr(md_failstring
, '@');
115 md_fail_percent
= atsign
== NULL
? 0 : atoi(atsign
+ 1);
118 md_failstring
= semi
;
122 * Windows doesn't have random(), but it has rand()
123 * Some rand() implementations aren't good, but we're not
124 * dealing with secure randomness here.
127 # define random() rand()
130 * See if the current malloc should fail.
132 static int shouldfail(void)
134 int roll
= (int)(random() % 100);
135 int shoulditfail
= roll
< md_fail_percent
;
137 /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
141 if (md_tracefd
> 0) {
142 BIO_snprintf(buff
, sizeof(buff
),
143 "%c C%ld %%%d R%d\n",
144 shoulditfail
? '-' : '+', md_count
, md_fail_percent
, roll
);
146 if (write(md_tracefd
, buff
, len
) != len
)
147 perror("shouldfail write failed");
152 /* If we used up this one, go to the next. */
160 void ossl_malloc_setup_failures(void)
162 const char *cp
= getenv("OPENSSL_MALLOC_FAILURES");
164 if (cp
!= NULL
&& (md_failstring
= strdup(cp
)) != NULL
)
166 if ((cp
= getenv("OPENSSL_MALLOC_FD")) != NULL
)
167 md_tracefd
= atoi(cp
);
171 void *CRYPTO_malloc(size_t num
, const char *file
, int line
)
175 INCREMENT(malloc_count
);
176 if (malloc_impl
!= NULL
&& malloc_impl
!= CRYPTO_malloc
)
177 return malloc_impl(num
, file
, line
);
183 if (allow_customize
) {
185 * Disallow customization after the first allocation. We only set this
186 * if necessary to avoid a store to the same cache line on every
191 (void)(file
); (void)(line
);
197 void *CRYPTO_zalloc(size_t num
, const char *file
, int line
)
199 void *ret
= CRYPTO_malloc(num
, file
, line
);
207 void *CRYPTO_realloc(void *str
, size_t num
, const char *file
, int line
)
209 INCREMENT(realloc_count
);
210 if (realloc_impl
!= NULL
&& realloc_impl
!= &CRYPTO_realloc
)
211 return realloc_impl(str
, num
, file
, line
);
215 return CRYPTO_malloc(num
, file
, line
);
218 CRYPTO_free(str
, file
, line
);
222 (void)(file
); (void)(line
);
223 return realloc(str
, num
);
227 void *CRYPTO_clear_realloc(void *str
, size_t old_len
, size_t num
,
228 const char *file
, int line
)
233 return CRYPTO_malloc(num
, file
, line
);
236 CRYPTO_clear_free(str
, old_len
, file
, line
);
240 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
242 OPENSSL_cleanse((char*)str
+ num
, old_len
- num
);
246 ret
= CRYPTO_malloc(num
, file
, line
);
248 memcpy(ret
, str
, old_len
);
249 CRYPTO_clear_free(str
, old_len
, file
, line
);
254 void CRYPTO_free(void *str
, const char *file
, int line
)
256 INCREMENT(free_count
);
257 if (free_impl
!= NULL
&& free_impl
!= &CRYPTO_free
) {
258 free_impl(str
, file
, line
);
265 void CRYPTO_clear_free(void *str
, size_t num
, const char *file
, int line
)
270 OPENSSL_cleanse(str
, num
);
271 CRYPTO_free(str
, file
, line
);
274 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG)
276 # ifndef OPENSSL_NO_DEPRECATED_3_0
277 int CRYPTO_mem_ctrl(int mode
)
283 int CRYPTO_set_mem_debug(int flag
)
289 int CRYPTO_mem_debug_push(const char *info
, const char *file
, int line
)
291 (void)info
; (void)file
; (void)line
;
295 int CRYPTO_mem_debug_pop(void)
300 void CRYPTO_mem_debug_malloc(void *addr
, size_t num
, int flag
,
301 const char *file
, int line
)
303 (void)addr
; (void)num
; (void)flag
; (void)file
; (void)line
;
306 void CRYPTO_mem_debug_realloc(void *addr1
, void *addr2
, size_t num
, int flag
,
307 const char *file
, int line
)
309 (void)addr1
; (void)addr2
; (void)num
; (void)flag
; (void)file
; (void)line
;
312 void CRYPTO_mem_debug_free(void *addr
, int flag
,
313 const char *file
, int line
)
315 (void)addr
; (void)flag
; (void)file
; (void)line
;
318 int CRYPTO_mem_leaks(BIO
*b
)
324 # ifndef OPENSSL_NO_STDIO
325 int CRYPTO_mem_leaks_fp(FILE *fp
)
332 int CRYPTO_mem_leaks_cb(int (*cb
)(const char *str
, size_t len
, void *u
),