VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/crypto/mem.c@ 108358

最後變更 在這個檔案從108358是 108206,由 vboxsync 提交於 5 週 前

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.7 KB
 
1/*
2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include "internal/e_os.h"
11#include "internal/cryptlib.h"
12#include "crypto/cryptlib.h"
13#include <stdio.h>
14#include <stdlib.h>
15#include <limits.h>
16#include <openssl/crypto.h>
17
18/*
19 * the following pointers may be changed as long as 'allow_customize' is set
20 */
21static int allow_customize = 1;
22static CRYPTO_malloc_fn malloc_impl = CRYPTO_malloc;
23static CRYPTO_realloc_fn realloc_impl = CRYPTO_realloc;
24static CRYPTO_free_fn free_impl = CRYPTO_free;
25
26#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
27# include "internal/tsan_assist.h"
28
29# ifdef TSAN_REQUIRES_LOCKING
30# define INCREMENT(x) /* empty */
31# define LOAD(x) 0
32# else /* TSAN_REQUIRES_LOCKING */
33static TSAN_QUALIFIER int malloc_count;
34static TSAN_QUALIFIER int realloc_count;
35static TSAN_QUALIFIER int free_count;
36
37# define INCREMENT(x) tsan_counter(&(x))
38# define LOAD(x) tsan_load(&x)
39# endif /* TSAN_REQUIRES_LOCKING */
40
41static char *md_failstring;
42static long md_count;
43static int md_fail_percent = 0;
44static int md_tracefd = -1;
45
46static void parseit(void);
47static int shouldfail(void);
48
49# define FAILTEST() if (shouldfail()) return NULL
50
51#else
52
53# define INCREMENT(x) /* empty */
54# define FAILTEST() /* empty */
55#endif
56
57int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn,
58 CRYPTO_realloc_fn realloc_fn,
59 CRYPTO_free_fn free_fn)
60{
61 if (!allow_customize)
62 return 0;
63 if (malloc_fn != NULL)
64 malloc_impl = malloc_fn;
65 if (realloc_fn != NULL)
66 realloc_impl = realloc_fn;
67 if (free_fn != NULL)
68 free_impl = free_fn;
69 return 1;
70}
71
72void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn,
73 CRYPTO_realloc_fn *realloc_fn,
74 CRYPTO_free_fn *free_fn)
75{
76 if (malloc_fn != NULL)
77 *malloc_fn = malloc_impl;
78 if (realloc_fn != NULL)
79 *realloc_fn = realloc_impl;
80 if (free_fn != NULL)
81 *free_fn = free_impl;
82}
83
84#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
85void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
86{
87 if (mcount != NULL)
88 *mcount = LOAD(malloc_count);
89 if (rcount != NULL)
90 *rcount = LOAD(realloc_count);
91 if (fcount != NULL)
92 *fcount = LOAD(free_count);
93}
94
95/*
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:
99 * 100@0;100@25;0@0
100 * or 100;100@25;0
101 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
102 * all remaining (count is zero) succeed.
103 * The failure percentge can have 2 digits after the comma. For example:
104 * [email protected]
105 * This means 0.01% of all allocations will fail.
106 */
107static void parseit(void)
108{
109 char *semi = strchr(md_failstring, ';');
110 char *atsign;
111
112 if (semi != NULL)
113 *semi++ = '\0';
114
115 /* Get the count (atol will stop at the @ if there), and percentage */
116 md_count = atol(md_failstring);
117 atsign = strchr(md_failstring, '@');
118 md_fail_percent = atsign == NULL ? 0 : (int)(atof(atsign + 1) * 100 + 0.5);
119
120 if (semi != NULL)
121 md_failstring = semi;
122}
123
124/*
125 * Windows doesn't have random() and srandom(), but it has rand() and srand().
126 * Some rand() implementations aren't good, but we're not
127 * dealing with secure randomness here.
128 */
129# ifdef _WIN32
130# define random() rand()
131# define srandom(seed) srand(seed)
132# endif
133/*
134 * See if the current malloc should fail.
135 */
136static int shouldfail(void)
137{
138 int roll = (int)(random() % 10000);
139 int shoulditfail = roll < md_fail_percent;
140# ifndef _WIN32
141/* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
142 int len;
143 char buff[80];
144
145 if (md_tracefd > 0) {
146 BIO_snprintf(buff, sizeof(buff),
147 "%c C%ld %%%d R%d\n",
148 shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
149 len = strlen(buff);
150 if (write(md_tracefd, buff, len) != len)
151 perror("shouldfail write failed");
152 }
153# endif
154
155 if (md_count) {
156 /* If we used up this one, go to the next. */
157 if (--md_count == 0)
158 parseit();
159 }
160
161 return shoulditfail;
162}
163
164void ossl_malloc_setup_failures(void)
165{
166 const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
167
168 if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
169 parseit();
170 if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
171 md_tracefd = atoi(cp);
172 if ((cp = getenv("OPENSSL_MALLOC_SEED")) != NULL)
173 srandom(atoi(cp));
174}
175#endif
176
177void *CRYPTO_malloc(size_t num, const char *file, int line)
178{
179 void *ptr;
180
181 INCREMENT(malloc_count);
182 if (malloc_impl != CRYPTO_malloc) {
183 ptr = malloc_impl(num, file, line);
184 if (ptr != NULL || num == 0)
185 return ptr;
186 goto err;
187 }
188
189 if (num == 0)
190 return NULL;
191
192 FAILTEST();
193 if (allow_customize) {
194 /*
195 * Disallow customization after the first allocation. We only set this
196 * if necessary to avoid a store to the same cache line on every
197 * allocation.
198 */
199 allow_customize = 0;
200 }
201
202 ptr = malloc(num);
203 if (ptr != NULL)
204 return ptr;
205 err:
206 /*
207 * ossl_err_get_state_int() in err.c uses CRYPTO_zalloc(num, NULL, 0) for
208 * ERR_STATE allocation. Prevent mem alloc error loop while reporting error.
209 */
210 if (file != NULL || line != 0) {
211 ERR_new();
212 ERR_set_debug(file, line, NULL);
213 ERR_set_error(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE, NULL);
214 }
215 return NULL;
216}
217
218void *CRYPTO_zalloc(size_t num, const char *file, int line)
219{
220 void *ret;
221
222 ret = CRYPTO_malloc(num, file, line);
223 if (ret != NULL)
224 memset(ret, 0, num);
225
226 return ret;
227}
228
229void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
230{
231 INCREMENT(realloc_count);
232 if (realloc_impl != CRYPTO_realloc)
233 return realloc_impl(str, num, file, line);
234
235 if (str == NULL)
236 return CRYPTO_malloc(num, file, line);
237
238 if (num == 0) {
239 CRYPTO_free(str, file, line);
240 return NULL;
241 }
242
243 FAILTEST();
244 return realloc(str, num);
245}
246
247void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
248 const char *file, int line)
249{
250 void *ret = NULL;
251
252 if (str == NULL)
253 return CRYPTO_malloc(num, file, line);
254
255 if (num == 0) {
256 CRYPTO_clear_free(str, old_len, file, line);
257 return NULL;
258 }
259
260 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
261 if (num < old_len) {
262 OPENSSL_cleanse((char*)str + num, old_len - num);
263 return str;
264 }
265
266 ret = CRYPTO_malloc(num, file, line);
267 if (ret != NULL) {
268 memcpy(ret, str, old_len);
269 CRYPTO_clear_free(str, old_len, file, line);
270 }
271 return ret;
272}
273
274void CRYPTO_free(void *str, const char *file, int line)
275{
276 INCREMENT(free_count);
277 if (free_impl != CRYPTO_free) {
278 free_impl(str, file, line);
279 return;
280 }
281
282 free(str);
283}
284
285void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
286{
287 if (str == NULL)
288 return;
289 if (num)
290 OPENSSL_cleanse(str, num);
291 CRYPTO_free(str, file, line);
292}
293
294#if !defined(OPENSSL_NO_CRYPTO_MDEBUG)
295
296# ifndef OPENSSL_NO_DEPRECATED_3_0
297int CRYPTO_mem_ctrl(int mode)
298{
299 (void)mode;
300 return -1;
301}
302
303int CRYPTO_set_mem_debug(int flag)
304{
305 (void)flag;
306 return -1;
307}
308
309int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
310{
311 (void)info; (void)file; (void)line;
312 return 0;
313}
314
315int CRYPTO_mem_debug_pop(void)
316{
317 return 0;
318}
319
320void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag,
321 const char *file, int line)
322{
323 (void)addr; (void)num; (void)flag; (void)file; (void)line;
324}
325
326void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag,
327 const char *file, int line)
328{
329 (void)addr1; (void)addr2; (void)num; (void)flag; (void)file; (void)line;
330}
331
332void CRYPTO_mem_debug_free(void *addr, int flag,
333 const char *file, int line)
334{
335 (void)addr; (void)flag; (void)file; (void)line;
336}
337
338int CRYPTO_mem_leaks(BIO *b)
339{
340 (void)b;
341 return -1;
342}
343
344# ifndef OPENSSL_NO_STDIO
345int CRYPTO_mem_leaks_fp(FILE *fp)
346{
347 (void)fp;
348 return -1;
349}
350# endif
351
352int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
353 void *u)
354{
355 (void)cb; (void)u;
356 return -1;
357}
358
359# endif
360
361#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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