VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.1i/crypto/mem.c@ 87808

Last change on this file since 87808 was 87808, checked in by vboxsync, 4 years ago

openssl-1.1.1i: fix typo in mem.c

File size: 7.8 KB
Line 
1/*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include "e_os.h"
11#include "internal/cryptlib.h"
12#include <stdio.h>
13#include <stdlib.h>
14#include <limits.h>
15#include <openssl/crypto.h>
16#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
17# include <execinfo.h>
18#endif
19
20/*
21 * the following pointers may be changed as long as 'allow_customize' is set
22 */
23static int allow_customize = 1;
24
25static void *(*malloc_impl)(size_t, const char *, int)
26 = CRYPTO_malloc;
27static void *(*realloc_impl)(void *, size_t, const char *, int)
28 = CRYPTO_realloc;
29static void (*free_impl)(void *, const char *, int)
30 = CRYPTO_free;
31
32#ifndef OPENSSL_NO_CRYPTO_MDEBUG
33# include "internal/tsan_assist.h"
34
35static TSAN_QUALIFIER int malloc_count;
36static TSAN_QUALIFIER int realloc_count;
37static TSAN_QUALIFIER int free_count;
38
39# define INCREMENT(x) tsan_counter(&(x))
40
41static char *md_failstring;
42static long md_count;
43static int md_fail_percent = 0;
44static int md_tracefd = -1;
45static int call_malloc_debug = 1;
46
47static void parseit(void);
48static int shouldfail(void);
49
50# define FAILTEST() if (shouldfail()) return NULL
51
52#else
53static int call_malloc_debug = 0;
54
55# define INCREMENT(x) /* empty */
56# define FAILTEST() /* empty */
57#endif
58
59int CRYPTO_set_mem_functions(
60 void *(*m)(size_t, const char *, int),
61 void *(*r)(void *, size_t, const char *, int),
62 void (*f)(void *, const char *, int))
63{
64 if (!allow_customize)
65 return 0;
66 if (m)
67 malloc_impl = m;
68 if (r)
69 realloc_impl = r;
70 if (f)
71 free_impl = f;
72 return 1;
73}
74
75int CRYPTO_set_mem_debug(int flag)
76{
77 if (!allow_customize)
78 return 0;
79 call_malloc_debug = flag;
80 return 1;
81}
82
83void CRYPTO_get_mem_functions(
84 void *(**m)(size_t, const char *, int),
85 void *(**r)(void *, size_t, const char *, int),
86 void (**f)(void *, const char *, int))
87{
88 if (m != NULL)
89 *m = malloc_impl;
90 if (r != NULL)
91 *r = realloc_impl;
92 if (f != NULL)
93 *f = free_impl;
94}
95
96#ifndef OPENSSL_NO_CRYPTO_MDEBUG
97void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
98{
99 if (mcount != NULL)
100 *mcount = tsan_load(&malloc_count);
101 if (rcount != NULL)
102 *rcount = tsan_load(&realloc_count);
103 if (fcount != NULL)
104 *fcount = tsan_load(&free_count);
105}
106
107/*
108 * Parse a "malloc failure spec" string. This likes like a set of fields
109 * separated by semicolons. Each field has a count and an optional failure
110 * percentage. For example:
111 * 100@0;100@25;0@0
112 * or 100;100@25;0
113 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
114 * all remaining (count is zero) succeed.
115 */
116static void parseit(void)
117{
118 char *semi = strchr(md_failstring, ';');
119 char *atsign;
120
121 if (semi != NULL)
122 *semi++ = '\0';
123
124 /* Get the count (atol will stop at the @ if there), and percentage */
125 md_count = atol(md_failstring);
126 atsign = strchr(md_failstring, '@');
127 md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
128
129 if (semi != NULL)
130 md_failstring = semi;
131}
132
133/*
134 * Windows doesn't have random(), but it has rand()
135 * Some rand() implementations aren't good, but we're not
136 * dealing with secure randomness here.
137 */
138# ifdef _WIN32
139# define random() rand()
140# endif
141/*
142 * See if the current malloc should fail.
143 */
144static int shouldfail(void)
145{
146 int roll = (int)(random() % 100);
147 int shoulditfail = roll < md_fail_percent;
148# ifndef _WIN32
149/* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
150 int len;
151 char buff[80];
152
153 if (md_tracefd > 0) {
154 BIO_snprintf(buff, sizeof(buff),
155 "%c C%ld %%%d R%d\n",
156 shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
157 len = strlen(buff);
158 if (write(md_tracefd, buff, len) != len)
159 perror("shouldfail write failed");
160# ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
161 if (shoulditfail) {
162 void *addrs[30];
163 int num = backtrace(addrs, OSSL_NELEM(addrs));
164
165 backtrace_symbols_fd(addrs, num, md_tracefd);
166 }
167# endif
168 }
169# endif
170
171 if (md_count) {
172 /* If we used up this one, go to the next. */
173 if (--md_count == 0)
174 parseit();
175 }
176
177 return shoulditfail;
178}
179
180void ossl_malloc_setup_failures(void)
181{
182 const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
183
184 if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
185 parseit();
186 if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
187 md_tracefd = atoi(cp);
188}
189#endif
190
191void *CRYPTO_malloc(size_t num, const char *file, int line)
192{
193 void *ret = NULL;
194
195 INCREMENT(malloc_count);
196 if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
197 return malloc_impl(num, file, line);
198
199 if (num == 0)
200 return NULL;
201
202 FAILTEST();
203 if (allow_customize) {
204 /*
205 * Disallow customization after the first allocation. We only set this
206 * if necessary to avoid a store to the same cache line on every
207 * allocation.
208 */
209 allow_customize = 0;
210 }
211#ifndef OPENSSL_NO_CRYPTO_MDEBUG
212 if (call_malloc_debug) {
213 CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
214 ret = malloc(num);
215 CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
216 } else {
217 ret = malloc(num);
218 }
219#else
220 (void)(file); (void)(line);
221 ret = malloc(num);
222#endif
223
224 return ret;
225}
226
227void *CRYPTO_zalloc(size_t num, const char *file, int line)
228{
229 void *ret = CRYPTO_malloc(num, file, line);
230
231 FAILTEST();
232 if (ret != NULL)
233 memset(ret, 0, num);
234 return ret;
235}
236
237void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
238{
239 INCREMENT(realloc_count);
240 if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
241 return realloc_impl(str, num, file, line);
242
243 FAILTEST();
244 if (str == NULL)
245 return CRYPTO_malloc(num, file, line);
246
247 if (num == 0) {
248 CRYPTO_free(str, file, line);
249 return NULL;
250 }
251
252#ifndef OPENSSL_NO_CRYPTO_MDEBUG
253 if (call_malloc_debug) {
254 void *ret;
255 CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
256 ret = realloc(str, num);
257 CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
258 return ret;
259 }
260#else
261 (void)(file); (void)(line);
262#endif
263 return realloc(str, num);
264
265}
266
267void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
268 const char *file, int line)
269{
270 void *ret = NULL;
271
272 if (str == NULL)
273 return CRYPTO_malloc(num, file, line);
274
275 if (num == 0) {
276 CRYPTO_clear_free(str, old_len, file, line);
277 return NULL;
278 }
279
280 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
281 if (num < old_len) {
282 OPENSSL_cleanse((char*)str + num, old_len - num);
283 return str;
284 }
285
286 ret = CRYPTO_malloc(num, file, line);
287 if (ret != NULL) {
288 memcpy(ret, str, old_len);
289 CRYPTO_clear_free(str, old_len, file, line);
290 }
291 return ret;
292}
293
294void CRYPTO_free(void *str, const char *file, int line)
295{
296 INCREMENT(free_count);
297 if (free_impl != NULL && free_impl != &CRYPTO_free) {
298 free_impl(str, file, line);
299 return;
300 }
301
302#ifndef OPENSSL_NO_CRYPTO_MDEBUG
303 if (call_malloc_debug) {
304 CRYPTO_mem_debug_free(str, 0, file, line);
305 free(str);
306 CRYPTO_mem_debug_free(str, 1, file, line);
307 } else {
308 free(str);
309 }
310#else
311 free(str);
312#endif
313}
314
315void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
316{
317 if (str == NULL)
318 return;
319 if (num)
320 OPENSSL_cleanse(str, num);
321 CRYPTO_free(str, file, line);
322}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette