Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/mem.c
Line
Count
Source
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 "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
 */
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;
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 */
33
static TSAN_QUALIFIER int malloc_count;
34
static TSAN_QUALIFIER int realloc_count;
35
static 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
41
static char *md_failstring;
42
static long md_count;
43
static int md_fail_percent = 0;
44
static int md_tracefd = -1;
45
46
static void parseit(void);
47
static int shouldfail(void);
48
49
#define FAILTEST()    \
50
    if (shouldfail()) \
51
    return NULL
52
53
#else
54
55
#define INCREMENT(x) /* empty */
56
#define FAILTEST() /* empty */
57
#endif
58
59
int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn,
60
    CRYPTO_realloc_fn realloc_fn,
61
    CRYPTO_free_fn free_fn)
62
0
{
63
0
    if (!allow_customize)
64
0
        return 0;
65
0
    if (malloc_fn != NULL)
66
0
        malloc_impl = malloc_fn;
67
0
    if (realloc_fn != NULL)
68
0
        realloc_impl = realloc_fn;
69
0
    if (free_fn != NULL)
70
0
        free_impl = free_fn;
71
0
    return 1;
72
0
}
73
74
void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn,
75
    CRYPTO_realloc_fn *realloc_fn,
76
    CRYPTO_free_fn *free_fn)
77
0
{
78
0
    if (malloc_fn != NULL)
79
0
        *malloc_fn = malloc_impl;
80
0
    if (realloc_fn != NULL)
81
0
        *realloc_fn = realloc_impl;
82
0
    if (free_fn != NULL)
83
0
        *free_fn = free_impl;
84
0
}
85
86
#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
87
void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
88
{
89
    if (mcount != NULL)
90
        *mcount = LOAD(malloc_count);
91
    if (rcount != NULL)
92
        *rcount = LOAD(realloc_count);
93
    if (fcount != NULL)
94
        *fcount = LOAD(free_count);
95
}
96
97
/*
98
 * Parse a "malloc failure spec" string.  This likes like a set of fields
99
 * separated by semicolons.  Each field has a count and an optional failure
100
 * percentage.  For example:
101
 *          100@0;100@25;0@0
102
 *    or    100;100@25;0
103
 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
104
 * all remaining (count is zero) succeed.
105
 * The failure percentge can have 2 digits after the comma.  For example:
106
 *          0@0.01
107
 * This means 0.01% of all allocations will fail.
108
 */
109
static void parseit(void)
110
{
111
    char *semi = strchr(md_failstring, ';');
112
    char *atsign;
113
114
    if (semi != NULL)
115
        *semi++ = '\0';
116
117
    /* Get the count (atol will stop at the @ if there), and percentage */
118
    md_count = atol(md_failstring);
119
    atsign = strchr(md_failstring, '@');
120
    md_fail_percent = atsign == NULL ? 0 : (int)(atof(atsign + 1) * 100 + 0.5);
121
122
    if (semi != NULL)
123
        md_failstring = semi;
124
}
125
126
/*
127
 * Windows doesn't have random() and srandom(), but it has rand() and srand().
128
 * Some rand() implementations aren't good, but we're not
129
 * dealing with secure randomness here.
130
 */
131
#ifdef _WIN32
132
#define random() rand()
133
#define srandom(seed) srand(seed)
134
#endif
135
/*
136
 * See if the current malloc should fail.
137
 */
138
static int shouldfail(void)
139
{
140
    int roll = (int)(random() % 10000);
141
    int shoulditfail = roll < md_fail_percent;
142
#ifndef _WIN32
143
    /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
144
    int len;
145
    char buff[80];
146
147
    if (md_tracefd > 0) {
148
        BIO_snprintf(buff, sizeof(buff),
149
            "%c C%ld %%%d R%d\n",
150
            shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
151
        len = strlen(buff);
152
        if (write(md_tracefd, buff, len) != len)
153
            perror("shouldfail write failed");
154
    }
155
#endif
156
157
    if (md_count) {
158
        /* If we used up this one, go to the next. */
159
        if (--md_count == 0)
160
            parseit();
161
    }
162
163
    return shoulditfail;
164
}
165
166
void ossl_malloc_setup_failures(void)
167
{
168
    const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
169
170
    if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
171
        parseit();
172
    if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
173
        md_tracefd = atoi(cp);
174
    if ((cp = getenv("OPENSSL_MALLOC_SEED")) != NULL)
175
        srandom(atoi(cp));
176
}
177
#endif
178
179
void *CRYPTO_malloc(size_t num, const char *file, int line)
180
540M
{
181
540M
    INCREMENT(malloc_count);
182
540M
    if (malloc_impl != CRYPTO_malloc)
183
0
        return malloc_impl(num, file, line);
184
185
540M
    if (num == 0)
186
5
        return NULL;
187
188
540M
    FAILTEST();
189
540M
    if (allow_customize) {
190
        /*
191
         * Disallow customization after the first allocation. We only set this
192
         * if necessary to avoid a store to the same cache line on every
193
         * allocation.
194
         */
195
24
        allow_customize = 0;
196
24
    }
197
198
540M
    return malloc(num);
199
540M
}
200
201
void *CRYPTO_zalloc(size_t num, const char *file, int line)
202
776M
{
203
776M
    void *ret;
204
205
776M
    ret = CRYPTO_malloc(num, file, line);
206
776M
    if (ret != NULL)
207
776M
        memset(ret, 0, num);
208
209
776M
    return ret;
210
776M
}
211
212
void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
213
70.0M
{
214
70.0M
    INCREMENT(realloc_count);
215
70.0M
    if (realloc_impl != CRYPTO_realloc)
216
0
        return realloc_impl(str, num, file, line);
217
218
70.0M
    if (str == NULL)
219
57.5M
        return CRYPTO_malloc(num, file, line);
220
221
12.5M
    if (num == 0) {
222
0
        CRYPTO_free(str, file, line);
223
0
        return NULL;
224
0
    }
225
226
12.5M
    FAILTEST();
227
12.5M
    return realloc(str, num);
228
12.5M
}
229
230
void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
231
    const char *file, int line)
232
34.7M
{
233
34.7M
    void *ret = NULL;
234
235
34.7M
    if (str == NULL)
236
14.8M
        return CRYPTO_malloc(num, file, line);
237
238
19.8M
    if (num == 0) {
239
0
        CRYPTO_clear_free(str, old_len, file, line);
240
0
        return NULL;
241
0
    }
242
243
    /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
244
19.8M
    if (num < old_len) {
245
0
        OPENSSL_cleanse((char *)str + num, old_len - num);
246
0
        return str;
247
0
    }
248
249
19.8M
    ret = CRYPTO_malloc(num, file, line);
250
19.8M
    if (ret != NULL) {
251
19.8M
        memcpy(ret, str, old_len);
252
19.8M
        CRYPTO_clear_free(str, old_len, file, line);
253
19.8M
    }
254
19.8M
    return ret;
255
19.8M
}
256
257
void CRYPTO_free(void *str, const char *file, int line)
258
5.91G
{
259
5.91G
    INCREMENT(free_count);
260
5.91G
    if (free_impl != CRYPTO_free) {
261
0
        free_impl(str, file, line);
262
0
        return;
263
0
    }
264
265
5.91G
    free(str);
266
5.91G
}
267
268
void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
269
98.5M
{
270
98.5M
    if (str == NULL)
271
10.4M
        return;
272
88.0M
    if (num)
273
88.0M
        OPENSSL_cleanse(str, num);
274
88.0M
    CRYPTO_free(str, file, line);
275
88.0M
}
276
277
#if !defined(OPENSSL_NO_CRYPTO_MDEBUG)
278
279
#ifndef OPENSSL_NO_DEPRECATED_3_0
280
int CRYPTO_mem_ctrl(int mode)
281
{
282
    (void)mode;
283
    return -1;
284
}
285
286
int CRYPTO_set_mem_debug(int flag)
287
{
288
    (void)flag;
289
    return -1;
290
}
291
292
int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
293
{
294
    (void)info;
295
    (void)file;
296
    (void)line;
297
    return 0;
298
}
299
300
int CRYPTO_mem_debug_pop(void)
301
{
302
    return 0;
303
}
304
305
void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag,
306
    const char *file, int line)
307
{
308
    (void)addr;
309
    (void)num;
310
    (void)flag;
311
    (void)file;
312
    (void)line;
313
}
314
315
void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag,
316
    const char *file, int line)
317
{
318
    (void)addr1;
319
    (void)addr2;
320
    (void)num;
321
    (void)flag;
322
    (void)file;
323
    (void)line;
324
}
325
326
void CRYPTO_mem_debug_free(void *addr, int flag,
327
    const char *file, int line)
328
{
329
    (void)addr;
330
    (void)flag;
331
    (void)file;
332
    (void)line;
333
}
334
335
int CRYPTO_mem_leaks(BIO *b)
336
{
337
    (void)b;
338
    return -1;
339
}
340
341
#ifndef OPENSSL_NO_STDIO
342
int CRYPTO_mem_leaks_fp(FILE *fp)
343
{
344
    (void)fp;
345
    return -1;
346
}
347
#endif
348
349
int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
350
    void *u)
351
{
352
    (void)cb;
353
    (void)u;
354
    return -1;
355
}
356
357
#endif
358
359
#endif