Coverage Report

Created: 2024-11-21 07:03

/src/wolfssl/wolfcrypt/src/memory.c
Line
Count
Source (jump to first uncovered line)
1
/* memory.c
2
 *
3
 * Copyright (C) 2006-2024 wolfSSL Inc.
4
 *
5
 * This file is part of wolfSSL.
6
 *
7
 * wolfSSL is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * wolfSSL is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20
 */
21
22
23
#ifdef HAVE_CONFIG_H
24
    #include <config.h>
25
#endif
26
27
#ifdef WOLFSSL_LINUXKM
28
    /* inhibit "#undef current" in linuxkm_wc_port.h, included from wc_port.h,
29
     * because needed in linuxkm_memory.c, included below.
30
     */
31
    #define WOLFSSL_NEED_LINUX_CURRENT
32
#endif
33
34
#include <wolfssl/wolfcrypt/types.h>
35
#include <wolfssl/wolfcrypt/error-crypt.h>
36
37
/*
38
Possible memory options:
39
 * NO_WOLFSSL_MEMORY:               Disables wolf memory callback support. When not defined settings.h defines USE_WOLFSSL_MEMORY.
40
 * WOLFSSL_STATIC_MEMORY:           Turns on the use of static memory buffers and functions.
41
                                        This allows for using static memory instead of dynamic.
42
 * WOLFSSL_STATIC_MEMORY_LEAN:      Requires WOLFSSL_STATIC_MEMORY be defined.
43
 *                                  Uses smaller type sizes for structs
44
 *                                  requiring that memory pool sizes be less
45
 *                                  then 65k and limits features available like
46
 *                                  IO buffers to reduce footprint size.
47
 * WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK:
48
 *                                  Enables option to register a debugging
49
 *                                  callback function, useful for
50
 *                                  WOLFSSL_STATIC_MEMORY builds where XMALLOC
51
 *                                  and XFREE are not user defined.
52
 * WOLFSSL_STATIC_ALIGN:            Define defaults to 16 to indicate static memory alignment.
53
 * HAVE_IO_POOL:                    Enables use of static thread safe memory pool for input/output buffers.
54
 * XMALLOC_OVERRIDE:                Allows override of the XMALLOC, XFREE and XREALLOC macros.
55
 * XMALLOC_USER:                    Allows custom XMALLOC, XFREE and XREALLOC functions to be defined.
56
 * WOLFSSL_NO_MALLOC:               Disables the fall-back case to use STDIO malloc/free when no callbacks are set.
57
 * WOLFSSL_TRACK_MEMORY:            Enables memory tracking for total stats and list of allocated memory.
58
 * WOLFSSL_DEBUG_MEMORY:            Enables extra function and line number args for memory callbacks.
59
 * WOLFSSL_DEBUG_MEMORY_PRINT:      Enables printing of each malloc/free.
60
 * WOLFSSL_MALLOC_CHECK:            Reports malloc or alignment failure using WOLFSSL_STATIC_ALIGN
61
 * WOLFSSL_FORCE_MALLOC_FAIL_TEST:  Used for internal testing to induce random malloc failures.
62
 * WOLFSSL_HEAP_TEST:               Used for internal testing of heap hint
63
 * WOLFSSL_MEM_FAIL_COUNT:          Fail memory allocation at a count from
64
 *                                  environment variable: MEM_FAIL_CNT.
65
 */
66
67
#ifdef WOLFSSL_ZEPHYR
68
#undef realloc
69
void *z_realloc(void *ptr, size_t size)
70
{
71
    if (ptr == NULL)
72
        ptr = malloc(size); /* native heap */
73
    else
74
        ptr = realloc(ptr, size); /* native heap */
75
76
    return ptr;
77
}
78
#define realloc z_realloc
79
#endif
80
81
#ifdef USE_WOLFSSL_MEMORY
82
83
#include <wolfssl/wolfcrypt/memory.h>
84
#include <wolfssl/wolfcrypt/error-crypt.h>
85
#include <wolfssl/wolfcrypt/logging.h>
86
87
#if defined(WOLFSSL_DEBUG_MEMORY) && defined(WOLFSSL_DEBUG_MEMORY_PRINT)
88
#include <stdio.h>
89
#endif
90
91
#ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
92
    static int gMemFailCountSeed;
93
    static int gMemFailCount;
94
    void wolfSSL_SetMemFailCount(int memFailCount)
95
    {
96
        if (gMemFailCountSeed == 0) {
97
            gMemFailCountSeed = memFailCount;
98
            gMemFailCount = memFailCount;
99
        }
100
    }
101
#endif
102
#if defined(WOLFSSL_MALLOC_CHECK) || defined(WOLFSSL_TRACK_MEMORY_FULL) || \
103
                                                     defined(WOLFSSL_MEMORY_LOG)
104
    #include <stdio.h>
105
#endif
106
107
108
/* Set these to default values initially. */
109
static wolfSSL_Malloc_cb  malloc_function = NULL;
110
static wolfSSL_Free_cb    free_function = NULL;
111
static wolfSSL_Realloc_cb realloc_function = NULL;
112
113
int wolfSSL_SetAllocators(wolfSSL_Malloc_cb  mf,
114
                          wolfSSL_Free_cb    ff,
115
                          wolfSSL_Realloc_cb rf)
116
4
{
117
4
    malloc_function = mf;
118
4
    free_function = ff;
119
4
    realloc_function = rf;
120
4
    return 0;
121
4
}
122
123
int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*  mf,
124
                          wolfSSL_Free_cb*    ff,
125
                          wolfSSL_Realloc_cb* rf)
126
0
{
127
0
    if (mf) *mf = malloc_function;
128
0
    if (ff) *ff = free_function;
129
0
    if (rf) *rf = realloc_function;
130
0
    return 0;
131
0
}
132
133
#ifdef WOLFSSL_MEM_FAIL_COUNT
134
static wolfSSL_Mutex memFailMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(memFailMutex);
135
int mem_fail_allocs = 0;
136
int mem_fail_frees = 0;
137
int mem_fail_cnt = 0;
138
139
void wc_MemFailCount_Init()
140
{
141
    char* cnt;
142
#ifndef WOLFSSL_MUTEX_INITIALIZER
143
    wc_InitMutex(&memFailMutex);
144
#endif
145
    cnt = getenv("MEM_FAIL_CNT");
146
    if (cnt != NULL) {
147
        fprintf(stderr, "MemFailCount At: %d\n", mem_fail_cnt);
148
        mem_fail_cnt = atoi(cnt);
149
    }
150
}
151
static int wc_MemFailCount_AllocMem(void)
152
{
153
    int ret = 1;
154
155
    wc_LockMutex(&memFailMutex);
156
    if ((mem_fail_cnt > 0) && (mem_fail_cnt <= mem_fail_allocs + 1)) {
157
        ret = 0;
158
    }
159
    else {
160
        mem_fail_allocs++;
161
    }
162
    wc_UnLockMutex(&memFailMutex);
163
164
    return ret;
165
}
166
static void wc_MemFailCount_FreeMem(void)
167
{
168
    wc_LockMutex(&memFailMutex);
169
    mem_fail_frees++;
170
    wc_UnLockMutex(&memFailMutex);
171
}
172
void wc_MemFailCount_Free()
173
{
174
#ifndef WOLFSSL_MUTEX_INITIALIZER
175
    wc_FreeMutex(&memFailMutex);
176
#endif
177
    fprintf(stderr, "MemFailCount Total: %d\n", mem_fail_allocs);
178
    fprintf(stderr, "MemFailCount Frees: %d\n", mem_fail_frees);
179
}
180
#endif
181
182
#ifndef WOLFSSL_STATIC_MEMORY
183
#ifdef WOLFSSL_CHECK_MEM_ZERO
184
185
#ifndef WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN
186
/* Number of entries in table of addresses to check. */
187
#define WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN    256
188
#endif
189
190
/* Alignment to maintain when adding length to allocated pointer.
191
 * Intel x64 wants to use aligned loads of XMM registers.
192
 */
193
#define MEM_ALIGN       16
194
195
/* An address that is meant to be all zeros for its length. */
196
typedef struct MemZero {
197
    /* Name of address to check. */
198
    const char* name;
199
    /* Address to check. */
200
    const void* addr;
201
    /* Length of data that must be zero. */
202
    size_t len;
203
} MemZero;
204
205
/* List of addresses to check. */
206
static MemZero memZero[WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN];
207
/* Next index to place address at.
208
 * -1 indicates uninitialized.
209
 * If nextIdx is equal to WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN then all entries
210
 * have been used.
211
 */
212
static int nextIdx = -1;
213
/* Mutex to protect modifying list of addresses to check. */
214
static wolfSSL_Mutex zeroMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(zeroMutex);
215
216
/* Initialize the table of addresses and the mutex.
217
 */
218
void wc_MemZero_Init()
219
{
220
    /* Clear the table to more easily see what is valid. */
221
    XMEMSET(memZero, 0, sizeof(memZero));
222
    /* Initialize mutex. */
223
#ifndef WOLFSSL_MUTEX_INITIALIZER
224
    wc_InitMutex(&zeroMutex);
225
#endif
226
    /* Next index is first entry. */
227
    nextIdx = 0;
228
}
229
230
/* Free the mutex and check we have not any uncheck addresses.
231
 */
232
void wc_MemZero_Free()
233
{
234
    /* Free mutex. */
235
#ifndef WOLFSSL_MUTEX_INITIALIZER
236
    wc_FreeMutex(&zeroMutex);
237
#endif
238
    /* Make sure we checked all addresses. */
239
    if (nextIdx > 0) {
240
        int i;
241
        fprintf(stderr, "[MEM_ZERO] Unseen: %d\n", nextIdx);
242
        for (i = 0; i < nextIdx; i++) {
243
            fprintf(stderr, "  %s - %p:%ld\n", memZero[i].name, memZero[i].addr,
244
                memZero[i].len);
245
        }
246
    }
247
    /* Uninitialized value in next index. */
248
    nextIdx = -1;
249
}
250
251
/* Add an address to check.
252
 *
253
 * @param [in] name  Name of address to check.
254
 * @param [in] addr  Address that needs to be checked.
255
 * @param [in] len   Length of data that must be zero.
256
 */
257
void wc_MemZero_Add(const char* name, const void* addr, size_t len)
258
{
259
    /* Initialize if not done. */
260
    if (nextIdx == -1) {
261
        wc_MemZero_Init();
262
    }
263
264
    /* Add an entry to the table while locked. */
265
    wc_LockMutex(&zeroMutex);
266
    if (nextIdx < WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN) {
267
        /* Fill in the next entry and update next index. */
268
        memZero[nextIdx].name = name;
269
        memZero[nextIdx].addr = addr;
270
        memZero[nextIdx].len  = len;
271
        nextIdx++;
272
    }
273
    else {
274
        /* Abort when too many entries. */
275
        fprintf(stderr, "\n[MEM_ZERO] Too many addresses to check\n");
276
        fprintf(stderr, "[MEM_ZERO] WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN\n");
277
        abort();
278
    }
279
    wc_UnLockMutex(&zeroMutex);
280
}
281
282
/* Check the memory in the range of the address for memory that must be zero.
283
 *
284
 * @param [in] addr  Start address of memory that is to be checked.
285
 * @param [in] len   Length of data associated with address.
286
 */
287
void wc_MemZero_Check(void* addr, size_t len)
288
{
289
    int i;
290
    size_t j;
291
292
    wc_LockMutex(&zeroMutex);
293
    /* Look at each address for overlap with address passes in. */
294
    for (i = 0; i < nextIdx; i++) {
295
        if ((memZero[i].addr < addr) ||
296
               ((size_t)memZero[i].addr >= (size_t)addr + len)) {
297
            /* Check address not part of memory to check. */
298
            continue;
299
        }
300
301
        /* Address is in range of memory being freed - check each byte zero. */
302
        for (j = 0; j < memZero[i].len; j++) {
303
            if (((unsigned char*)memZero[i].addr)[j] != 0) {
304
                /* Byte not zero - abort! */
305
                fprintf(stderr, "\n[MEM_ZERO] %s:%p + %ld is not zero\n",
306
                    memZero[i].name, memZero[i].addr, j);
307
                fprintf(stderr, "[MEM_ZERO] Checking %p:%ld\n", addr, len);
308
                abort();
309
            }
310
        }
311
        /* Update next index to write to. */
312
        nextIdx--;
313
        if (nextIdx > 0) {
314
            /* Remove entry. */
315
            XMEMCPY(memZero + i, memZero + i + 1,
316
                sizeof(MemZero) * (nextIdx - i));
317
            /* Clear out top to make it easier to see what is to be checked. */
318
            XMEMSET(&memZero[nextIdx], 0, sizeof(MemZero));
319
        }
320
        /* Need to check this index again with new data. */
321
        i--;
322
    }
323
    wc_UnLockMutex(&zeroMutex);
324
}
325
#endif /* WOLFSSL_CHECK_MEM_ZERO */
326
327
#ifdef WOLFSSL_DEBUG_MEMORY
328
void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line)
329
#else
330
void* wolfSSL_Malloc(size_t size)
331
#endif
332
3.50M
{
333
3.50M
    void* res = 0;
334
335
#ifdef WOLFSSL_MEM_FAIL_COUNT
336
    if (!wc_MemFailCount_AllocMem()) {
337
        WOLFSSL_MSG("MemFailCnt: Fail malloc");
338
        return NULL;
339
    }
340
#endif
341
342
#ifdef WOLFSSL_CHECK_MEM_ZERO
343
    /* Space for requested size. */
344
    size += MEM_ALIGN;
345
#endif
346
347
3.50M
    if (malloc_function) {
348
    #ifdef WOLFSSL_DEBUG_MEMORY
349
        res = malloc_function(size, func, line);
350
    #else
351
3.50M
        res = malloc_function(size);
352
3.50M
    #endif
353
3.50M
    }
354
520
    else {
355
520
    #ifndef WOLFSSL_NO_MALLOC
356
        #ifdef WOLFSSL_TRAP_MALLOC_SZ
357
        if (size > WOLFSSL_TRAP_MALLOC_SZ) {
358
            WOLFSSL_MSG("Malloc too big!");
359
            return NULL;
360
        }
361
        #endif
362
363
520
        res = malloc(size); /* native heap */
364
    #else
365
        WOLFSSL_MSG("No malloc available");
366
    #endif
367
520
    }
368
369
#ifdef WOLFSSL_CHECK_MEM_ZERO
370
    /* Restore size to requested value. */
371
    size -= MEM_ALIGN;
372
    if (res != NULL) {
373
        /* Place size at front of allocated data and move pointer passed it. */
374
        *(size_t*)res = size;
375
        res = ((unsigned char*)res) + MEM_ALIGN;
376
    }
377
#endif
378
379
#ifdef WOLFSSL_DEBUG_MEMORY
380
#if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY)
381
    fprintf(stderr, "Alloc: %p -> %u at %s:%u\n", res, (word32)size, func, line);
382
#else
383
    (void)func;
384
    (void)line;
385
#endif
386
#endif
387
388
#ifdef WOLFSSL_MALLOC_CHECK
389
    if (res == NULL)
390
        WOLFSSL_MSG("wolfSSL_malloc failed");
391
#endif
392
393
#ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
394
    if (res && --gMemFailCount == 0) {
395
        fprintf(stderr, "\n---FORCED MEM FAIL TEST---\n");
396
        if (free_function) {
397
        #ifdef WOLFSSL_DEBUG_MEMORY
398
            free_function(res, func, line);
399
        #else
400
            free_function(res);
401
        #endif
402
        }
403
        else {
404
            free(res); /* native heap */
405
        }
406
        gMemFailCount = gMemFailCountSeed; /* reset */
407
        return NULL;
408
    }
409
#endif
410
411
3.50M
    return res;
412
3.50M
}
413
414
#ifdef WOLFSSL_DEBUG_MEMORY
415
void wolfSSL_Free(void *ptr, const char* func, unsigned int line)
416
#else
417
void wolfSSL_Free(void *ptr)
418
#endif
419
3.50M
{
420
#ifdef WOLFSSL_DEBUG_MEMORY
421
#if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY)
422
    fprintf(stderr, "Free: %p at %s:%u\n", ptr, func, line);
423
#else
424
    (void)func;
425
    (void)line;
426
#endif
427
#endif
428
429
#ifdef WOLFSSL_CHECK_MEM_ZERO
430
    /* Move pointer back to originally allocated pointer. */
431
    ptr = ((unsigned char*)ptr) - MEM_ALIGN;
432
    /* Check that the pointer is zero where required. */
433
    wc_MemZero_Check(((unsigned char*)ptr) + MEM_ALIGN, *(size_t*)ptr);
434
#endif
435
#ifdef WOLFSSL_MEM_FAIL_COUNT
436
    wc_MemFailCount_FreeMem();
437
#endif
438
439
3.50M
    if (free_function) {
440
    #ifdef WOLFSSL_DEBUG_MEMORY
441
        free_function(ptr, func, line);
442
    #else
443
3.50M
        free_function(ptr);
444
3.50M
    #endif
445
3.50M
    }
446
512
    else {
447
512
    #ifndef WOLFSSL_NO_MALLOC
448
512
        free(ptr); /* native heap */
449
    #else
450
        WOLFSSL_MSG("No free available");
451
    #endif
452
512
    }
453
3.50M
}
454
455
#ifdef WOLFSSL_DEBUG_MEMORY
456
void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line)
457
#else
458
void* wolfSSL_Realloc(void *ptr, size_t size)
459
#endif
460
0
{
461
#ifdef WOLFSSL_CHECK_MEM_ZERO
462
    /* Can't check data that has been freed during realloc.
463
     * Manually allocated new memory, copy data and free original pointer.
464
     */
465
#ifdef WOLFSSL_DEBUG_MEMORY
466
    void* res = wolfSSL_Malloc(size, func, line);
467
#else
468
    void* res = wolfSSL_Malloc(size);
469
#endif
470
    if (ptr != NULL) {
471
        /* Copy the minimum of old and new size. */
472
        size_t copySize = *(size_t*)(((unsigned char*)ptr) - MEM_ALIGN);
473
        if (size < copySize) {
474
            copySize = size;
475
        }
476
        XMEMCPY(res, ptr, copySize);
477
        /* Dispose of old pointer. */
478
    #ifdef WOLFSSL_DEBUG_MEMORY
479
        wolfSSL_Free(ptr, func, line);
480
    #else
481
        wolfSSL_Free(ptr);
482
    #endif
483
    }
484
485
    /* Return new pointer with data copied into it. */
486
    return res;
487
#else
488
0
    void* res = 0;
489
490
#ifdef WOLFSSL_MEM_FAIL_COUNT
491
    if (!wc_MemFailCount_AllocMem()) {
492
        WOLFSSL_MSG("MemFailCnt: Fail realloc");
493
        return NULL;
494
    }
495
#endif
496
497
0
    if (realloc_function) {
498
    #ifdef WOLFSSL_DEBUG_MEMORY
499
        res = realloc_function(ptr, size, func, line);
500
    #else
501
0
        res = realloc_function(ptr, size);
502
0
    #endif
503
0
    }
504
0
    else {
505
0
    #ifndef WOLFSSL_NO_MALLOC
506
0
        res = realloc(ptr, size); /* native heap */
507
    #else
508
        WOLFSSL_MSG("No realloc available");
509
    #endif
510
0
    }
511
512
#ifdef WOLFSSL_MEM_FAIL_COUNT
513
    if (ptr != NULL) {
514
        wc_MemFailCount_FreeMem();
515
    }
516
#endif
517
518
0
    return res;
519
0
#endif
520
0
}
521
#endif /* WOLFSSL_STATIC_MEMORY */
522
523
#ifdef WOLFSSL_STATIC_MEMORY
524
525
struct wc_Memory {
526
    byte*  buffer;
527
    struct wc_Memory* next;
528
#ifdef WOLFSSL_STATIC_MEMORY_LEAN
529
    /* lean static memory is assumed to be under 65k */
530
    word16 sz;
531
#else
532
    word32 sz;
533
#endif
534
#ifdef WOLFSSL_DEBUG_MEMORY
535
    word16 szUsed;
536
#endif
537
};
538
539
540
#ifdef WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK
541
static DebugMemoryCb DebugCb = NULL;
542
543
/* Used to set a debug memory callback. Helpful in cases where
544
 * printf is not available. */
545
void wolfSSL_SetDebugMemoryCb(DebugMemoryCb cb)
546
{
547
    DebugCb = cb;
548
}
549
#endif
550
551
/* returns amount of memory used on success. On error returns negative value
552
   wc_Memory** list is the list that new buckets are prepended to
553
 */
554
static int wc_create_memory_buckets(byte* buffer, word32 bufSz,
555
                              word32 buckSz, byte buckNum, wc_Memory** list) {
556
    byte*  pt  = buffer;
557
    int    ret = 0;
558
    byte memSz   = (byte)sizeof(wc_Memory);
559
    word16 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
560
    word16 i;
561
562
    /* if not enough space available for bucket size then do not try */
563
    if (buckSz + memSz + padSz > bufSz) {
564
        return ret;
565
    }
566
567
    for (i = 0; i < buckNum; i++) {
568
        if ((buckSz + memSz + padSz) <= (bufSz - ret)) {
569
            /* create a new struct and set its values */
570
            wc_Memory* mem = (struct wc_Memory*)(pt);
571
            mem->sz = buckSz;
572
            mem->buffer = (byte*)pt + padSz + memSz;
573
            mem->next = NULL;
574
575
        #ifdef WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK
576
            if (DebugCb) {
577
                DebugCb(buckSz, buckSz, WOLFSSL_DEBUG_MEMORY_INIT, 0);
578
            }
579
        #endif
580
581
            /* add the newly created struct to front of list */
582
            if (*list == NULL) {
583
                *list = mem;
584
            } else {
585
                mem->next = *list;
586
                *list = mem;
587
            }
588
589
            /* advance pointer and keep track of memory used */
590
            ret += buckSz + padSz + memSz;
591
            pt  += buckSz + padSz + memSz;
592
        }
593
        else {
594
            break; /* not enough space left for more buckets of this size */
595
        }
596
    }
597
598
    return ret;
599
}
600
601
static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
602
                                                             WOLFSSL_HEAP* heap)
603
{
604
    word32 ava = sz;
605
    byte*  pt  = buffer;
606
    int    ret = 0;
607
    byte   memSz = (word32)sizeof(wc_Memory);
608
    byte   padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
609
610
    WOLFSSL_ENTER("wc_partition_static_memory");
611
612
    /* align pt */
613
    while ((wc_ptr_t)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
614
        *pt = 0x00;
615
        pt++;
616
        ava--;
617
    }
618
619
#ifdef WOLFSSL_DEBUG_MEMORY
620
    fprintf(stderr, "Allocated %d bytes for static memory @ %p\n", ava, pt);
621
#endif
622
623
    /* divide into chunks of memory and add them to available list */
624
    while (ava >= (word32)(heap->sizeList[0] + padSz + memSz)) {
625
    #ifndef WOLFSSL_STATIC_MEMORY_LEAN
626
        /* creating only IO buffers from memory passed in, max TLS is 16k */
627
        if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
628
            if ((ret = wc_create_memory_buckets(pt, ava,
629
                            WOLFMEM_IO_SZ, 1, &(heap->io))) < 0) {
630
                WOLFSSL_LEAVE("wc_partition_static_memory", ret);
631
                return ret;
632
            }
633
634
            /* check if no more room left for creating IO buffers */
635
            if (ret == 0) {
636
                break;
637
            }
638
639
            /* advance pointer in buffer for next buckets and keep track
640
               of how much memory is left available */
641
            pt  += ret;
642
            ava -= ret;
643
        }
644
        else
645
    #endif
646
        {
647
            int i;
648
            /* start at largest and move to smaller buckets */
649
            for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
650
                if ((word32)(heap->sizeList[i] + padSz + memSz) <= ava) {
651
                    if ((ret = wc_create_memory_buckets(pt, ava,
652
                                    heap->sizeList[i], heap->distList[i],
653
                                    &(heap->ava[i]))) < 0) {
654
                        WOLFSSL_LEAVE("wc_partition_static_memory", ret);
655
                        return ret;
656
                    }
657
658
                    /* advance pointer in buffer for next buckets and keep track
659
                       of how much memory is left available */
660
                    pt  += ret;
661
                    ava -= ret;
662
                }
663
            }
664
        }
665
    }
666
667
    (void)flag;
668
    return 1;
669
}
670
671
static int wc_init_memory_heap(WOLFSSL_HEAP* heap, unsigned int listSz,
672
        const word32 *sizeList, const word32 *distList)
673
{
674
    unsigned int i;
675
676
    XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
677
678
    /* avoid XMEMCPY for LEAN static memory build */
679
    for (i = 0; i < listSz; i++) {
680
        heap->sizeList[i] = sizeList[i];
681
    }
682
683
    for (i = 0; i < listSz; i++) {
684
        heap->distList[i] = distList[i];
685
    }
686
687
#ifndef SINGLE_THREADED
688
    if (wc_InitMutex(&(heap->memory_mutex)) != 0) {
689
        WOLFSSL_MSG("Error creating heap memory mutex");
690
        return BAD_MUTEX_E;
691
    }
692
#endif
693
694
    return 0;
695
}
696
697
int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
698
        unsigned int listSz, const word32 *sizeList,
699
        const word32 *distList, unsigned char *buf,
700
        unsigned int sz, int flag, int maxSz)
701
{
702
    WOLFSSL_HEAP*      heap = NULL;
703
    WOLFSSL_HEAP_HINT* hint = NULL;
704
    word16 idx = 0;
705
    int ret;
706
707
    WOLFSSL_ENTER("wc_LoadStaticMemory_ex");
708
709
    if (pHint == NULL || buf == NULL || sizeList == NULL || distList == NULL) {
710
        return BAD_FUNC_ARG;
711
    }
712
713
    /* Cap the listSz to the actual number of items allocated in the list. */
714
    if (listSz > WOLFMEM_MAX_BUCKETS) {
715
        WOLFSSL_MSG("Truncating the list of memory buckets");
716
        listSz = WOLFMEM_MAX_BUCKETS;
717
    }
718
719
    if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
720
        WOLFSSL_MSG("Not enough memory for partition tracking");
721
        return BUFFER_E; /* not enough memory for structures */
722
    }
723
724
    /* check if hint has already been assigned */
725
    if (*pHint == NULL) {
726
        heap = (WOLFSSL_HEAP*)buf;
727
        idx += sizeof(WOLFSSL_HEAP);
728
        hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
729
        idx += sizeof(WOLFSSL_HEAP_HINT);
730
731
        ret = wc_init_memory_heap(heap, listSz, sizeList, distList);
732
        if (ret != 0) {
733
            return ret;
734
        }
735
736
        XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
737
        hint->memory = heap;
738
    }
739
    else {
740
    #ifdef WOLFSSL_HEAP_TEST
741
        /* do not load in memory if test has been set */
742
        if (heap == (void*)WOLFSSL_HEAP_TEST) {
743
            return 0;
744
        }
745
    #endif
746
747
        hint = (WOLFSSL_HEAP_HINT*)(*pHint);
748
        heap = hint->memory;
749
    }
750
751
    ret = wc_partition_static_memory(buf + idx, sz - idx, flag, heap);
752
    if (ret != 1) {
753
        WOLFSSL_MSG("Error partitioning memory");
754
        return MEMORY_E;
755
    }
756
757
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
758
    /* determine what max applies too */
759
    if ((flag & WOLFMEM_IO_POOL) || (flag & WOLFMEM_IO_POOL_FIXED)) {
760
        heap->maxIO = maxSz;
761
    }
762
    else { /* general memory used in handshakes */
763
        heap->maxHa = maxSz;
764
    }
765
    heap->flag |= flag;
766
#endif
767
    *pHint = hint;
768
769
    (void)maxSz;
770
    return 0;
771
}
772
773
int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
774
    unsigned char* buf, unsigned int sz, int flag, int maxSz)
775
{
776
    word32 sizeList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
777
    word32 distList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
778
    int ret = 0;
779
780
    WOLFSSL_ENTER("wc_LoadStaticMemory");
781
    ret = wc_LoadStaticMemory_ex(pHint,
782
            WOLFMEM_DEF_BUCKETS, sizeList, distList,
783
            buf, sz, flag, maxSz);
784
    WOLFSSL_LEAVE("wc_LoadStaticMemory", ret);
785
    return ret;
786
}
787
788
789
void wc_UnloadStaticMemory(WOLFSSL_HEAP_HINT* heap)
790
{
791
    WOLFSSL_ENTER("wc_UnloadStaticMemory");
792
#ifndef SINGLE_THREADED
793
    if (heap != NULL && heap->memory != NULL) {
794
        wc_FreeMutex(&heap->memory->memory_mutex);
795
    }
796
#else
797
    (void)heap;
798
#endif
799
}
800
801
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
802
/* returns the size of management memory needed for each bucket.
803
 * This is memory that is used to keep track of and align memory buckets. */
804
int wolfSSL_MemoryPaddingSz(void)
805
{
806
    word32 memSz = (word32)sizeof(wc_Memory);
807
    word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
808
    return memSz + padSz;
809
}
810
811
812
/* Used to calculate memory size for optimum use with buckets.
813
   returns the suggested size rounded down to the nearest bucket. */
814
int wolfSSL_StaticBufferSz_ex(unsigned int listSz,
815
        const word32 *sizeList, const word32 *distList,
816
        byte* buffer, word32 sz, int flag)
817
{
818
    word32 ava = sz;
819
    byte*  pt  = buffer;
820
    word32 memSz = (word32)sizeof(wc_Memory);
821
    word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
822
823
    WOLFSSL_ENTER("wolfSSL_StaticBufferSz_ex");
824
825
    if (buffer == NULL || sizeList == NULL || distList == NULL) {
826
        return BAD_FUNC_ARG;
827
    }
828
829
    /* Cap the listSz to the actual number of items allocated in the list. */
830
    if (listSz > WOLFMEM_MAX_BUCKETS) {
831
        WOLFSSL_MSG("Truncating the list of memory buckets");
832
        listSz = WOLFMEM_MAX_BUCKETS;
833
    }
834
835
    /* align pt */
836
    while ((wc_ptr_t)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
837
        pt++;
838
        ava--;
839
    }
840
841
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
842
    /* creating only IO buffers from memory passed in, max TLS is 16k */
843
    if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
844
        if (ava < (memSz + padSz + WOLFMEM_IO_SZ)) {
845
            return 0; /* not enough room for even one bucket */
846
        }
847
848
        ava = ava % (memSz + padSz + WOLFMEM_IO_SZ);
849
    }
850
    else
851
#endif
852
    {
853
        int i, k;
854
855
        if (ava < (sizeList[0] + padSz + memSz)) {
856
            return 0; /* not enough room for even one bucket */
857
        }
858
859
        while ((ava >= (sizeList[0] + padSz + memSz)) && (ava > 0)) {
860
            /* start at largest and move to smaller buckets */
861
            for (i = (listSz - 1); i >= 0; i--) {
862
                for (k = distList[i]; k > 0; k--) {
863
                    if ((sizeList[i] + padSz + memSz) <= ava) {
864
                        ava -= sizeList[i] + padSz + memSz;
865
                    }
866
                }
867
            }
868
        }
869
    }
870
871
    WOLFSSL_LEAVE("wolfSSL_StaticBufferSz_ex", sz - ava);
872
    return sz - ava; /* round down */
873
}
874
875
876
/* Calls wolfSSL_StaticBufferSz_ex with the static memory pool config
877
 * used by wolfSSL by default. */
878
int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag)
879
{
880
    word32 bucketSz[WOLFMEM_DEF_BUCKETS] = {WOLFMEM_BUCKETS};
881
    word32 distList[WOLFMEM_DEF_BUCKETS] = {WOLFMEM_DIST};
882
883
    return wolfSSL_StaticBufferSz_ex(WOLFMEM_DEF_BUCKETS, bucketSz, distList,
884
        buffer, sz, flag);
885
}
886
887
888
int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
889
{
890
    WOLFSSL_MSG("Freeing fixed IO buffer");
891
892
    /* check if fixed buffer was set */
893
    if (*io == NULL) {
894
        return 1;
895
    }
896
897
    if (heap == NULL) {
898
        WOLFSSL_MSG("No heap to return fixed IO too");
899
    }
900
    else {
901
        /* put IO buffer back into IO pool */
902
        (*io)->next = heap->io;
903
        heap->io    = *io;
904
        *io         = NULL;
905
    }
906
907
    return 1;
908
}
909
910
911
int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
912
{
913
    WOLFSSL_MSG("Setting fixed IO for SSL");
914
    if (heap == NULL) {
915
        return MEMORY_E;
916
    }
917
918
    *io = heap->io;
919
920
    if (*io != NULL) {
921
        heap->io = (*io)->next;
922
        (*io)->next = NULL;
923
    }
924
    else { /* failed to grab an IO buffer */
925
        return 0;
926
    }
927
928
    return 1;
929
}
930
931
932
int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
933
{
934
        word32     i;
935
        wc_Memory* pt;
936
937
        XMEMSET(stats, 0, sizeof(WOLFSSL_MEM_STATS));
938
939
        stats->totalAlloc = heap->alloc;
940
        stats->totalFr    = heap->frAlc;
941
        stats->curAlloc   = stats->totalAlloc - stats->totalFr;
942
        stats->maxHa      = heap->maxHa;
943
        stats->maxIO      = heap->maxIO;
944
        for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
945
            stats->blockSz[i] = heap->sizeList[i];
946
            for (pt = heap->ava[i]; pt != NULL; pt = pt->next) {
947
                stats->avaBlock[i] += 1;
948
            }
949
        }
950
951
        for (pt = heap->io; pt != NULL; pt = pt->next) {
952
            stats->avaIO++;
953
        }
954
955
        stats->flag       = heap->flag; /* flag used */
956
957
    return 1;
958
}
959
#endif /* !WOLFSSL_STATIC_MEMORY_LEAN */
960
961
962
/* global heap hint to fall back on when no heap hint is passed to
963
 * XMALLOC/XFREE
964
 * NOT thread safe, should be set once before any expected XMALLOC XFREE calls
965
 */
966
static void* globalHeapHint = NULL;
967
968
969
/* Used to set a new global heap hint. Returns a pointer to the current global
970
 * heap hint before being set. */
971
void* wolfSSL_SetGlobalHeapHint(void* heap)
972
{
973
    void *oldHint = globalHeapHint;
974
975
    globalHeapHint = heap;
976
    return oldHint;
977
}
978
979
980
/* returns a pointer to the current global heap hint */
981
void* wolfSSL_GetGlobalHeapHint(void)
982
{
983
    return globalHeapHint;
984
}
985
986
987
#ifdef WOLFSSL_DEBUG_MEMORY
988
void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
989
#else
990
void* wolfSSL_Malloc(size_t size, void* heap, int type)
991
#endif
992
{
993
    void* res = 0;
994
    wc_Memory* pt = NULL;
995
    int   i;
996
997
    /* check for testing heap hint was set */
998
#ifdef WOLFSSL_HEAP_TEST
999
    if (heap == (void*)WOLFSSL_HEAP_TEST) {
1000
        return malloc(size); /* native heap */
1001
    }
1002
#endif
1003
1004
    /* if no heap hint then use dynamic memory*/
1005
    if (heap == NULL && globalHeapHint == NULL) {
1006
        #ifdef WOLFSSL_HEAP_TEST
1007
            /* allow using malloc for creating ctx and method */
1008
            if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
1009
                                            type == DYNAMIC_TYPE_CERT_MANAGER) {
1010
                WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method");
1011
                res = malloc(size); /* native heap */
1012
            }
1013
            else {
1014
                WOLFSSL_MSG("ERROR null heap hint passed into XMALLOC");
1015
                res = NULL;
1016
            }
1017
        #else
1018
        #ifndef WOLFSSL_NO_MALLOC
1019
            #ifdef FREERTOS
1020
                res = pvPortMalloc(size); /* native heap */
1021
            #elif defined(WOLFSSL_EMBOS)
1022
                res = OS_HEAP_malloc(size);
1023
            #else
1024
                res = malloc(size); /* native heap */
1025
            #endif
1026
1027
            #ifdef WOLFSSL_DEBUG_MEMORY
1028
                fprintf(stderr, "[HEAP %p] Alloc: %p -> %u at %s:%d\n", heap,
1029
                    res, (word32)size, func, line);
1030
            #endif
1031
        #else
1032
            WOLFSSL_MSG("No heap hint found to use and no malloc");
1033
            #ifdef WOLFSSL_DEBUG_MEMORY
1034
            fprintf(stderr, "ERROR: at %s:%d\n", func, line);
1035
            #endif
1036
        #endif /* WOLFSSL_NO_MALLOC */
1037
        #endif /* WOLFSSL_HEAP_TEST */
1038
    }
1039
    else {
1040
        WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
1041
        WOLFSSL_HEAP*      mem;
1042
1043
        if (hint == NULL) {
1044
            hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1045
        #ifdef WOLFSSL_DEBUG_MEMORY
1046
            fprintf(stderr, "(Using global heap hint %p) ", hint);
1047
        #endif
1048
        }
1049
        mem = hint->memory;
1050
1051
    #ifndef SINGLE_THREADED
1052
        if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
1053
            WOLFSSL_MSG("Bad memory_mutex lock");
1054
            return NULL;
1055
        }
1056
    #endif
1057
1058
    #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1059
        /* case of using fixed IO buffers */
1060
        if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
1061
                                             (type == DYNAMIC_TYPE_OUT_BUFFER ||
1062
                                              type == DYNAMIC_TYPE_IN_BUFFER)) {
1063
            if (type == DYNAMIC_TYPE_OUT_BUFFER) {
1064
                pt = hint->outBuf;
1065
            }
1066
            if (type == DYNAMIC_TYPE_IN_BUFFER) {
1067
                pt = hint->inBuf;
1068
            }
1069
        }
1070
        else
1071
    #endif
1072
        {
1073
        #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1074
            /* check if using IO pool flag */
1075
            if (mem->flag & WOLFMEM_IO_POOL &&
1076
                                             (type == DYNAMIC_TYPE_OUT_BUFFER ||
1077
                                              type == DYNAMIC_TYPE_IN_BUFFER)) {
1078
                if (mem->io != NULL) {
1079
                    pt      = mem->io;
1080
                    mem->io = pt->next;
1081
                }
1082
            }
1083
         #endif
1084
1085
            /* general static memory */
1086
            if (pt == NULL) {
1087
                for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
1088
                    if ((word32)size <= mem->sizeList[i]) {
1089
                        if (mem->ava[i] != NULL) {
1090
                            pt = mem->ava[i];
1091
                            mem->ava[i] = pt->next;
1092
                            break;
1093
                        }
1094
                    #ifdef WOLFSSL_DEBUG_STATIC_MEMORY
1095
                        else {
1096
                            fprintf(stderr, "Size: %lu, Empty: %d\n",
1097
                                (unsigned long) size, mem->sizeList[i]);
1098
                        }
1099
                    #endif
1100
                    }
1101
                }
1102
            }
1103
        }
1104
1105
        if (pt != NULL) {
1106
        #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1107
            mem->alloc += 1;
1108
        #endif
1109
            res = pt->buffer;
1110
1111
        #ifdef WOLFSSL_DEBUG_MEMORY
1112
            pt->szUsed = size;
1113
            fprintf(stderr, "[HEAP %p] Alloc: %p -> %lu at %s:%d\n", heap,
1114
                pt->buffer, size, func, line);
1115
        #endif
1116
        #ifdef WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK
1117
            if (DebugCb) {
1118
                DebugCb(size, pt->sz, WOLFSSL_DEBUG_MEMORY_ALLOC, type);
1119
            }
1120
        #endif
1121
        #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1122
            /* keep track of connection statistics if flag is set */
1123
            if (mem->flag & WOLFMEM_TRACK_STATS) {
1124
                WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
1125
                if (stats != NULL) {
1126
                    stats->curMem += pt->sz;
1127
                    if (stats->peakMem < stats->curMem) {
1128
                        stats->peakMem = stats->curMem;
1129
                    }
1130
                    stats->curAlloc++;
1131
                    if (stats->peakAlloc < stats->curAlloc) {
1132
                        stats->peakAlloc = stats->curAlloc;
1133
                    }
1134
                    stats->totalAlloc++;
1135
                }
1136
            }
1137
        #endif
1138
        }
1139
        else {
1140
            WOLFSSL_MSG("ERROR ran out of static memory");
1141
            res = NULL;
1142
            #ifdef WOLFSSL_DEBUG_MEMORY
1143
                fprintf(stderr, "Looking for %lu bytes at %s:%d\n",
1144
                    (unsigned long) size, func, line);
1145
            #endif
1146
            #ifdef WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK
1147
            if (DebugCb) {
1148
                DebugCb(size, 0, WOLFSSL_DEBUG_MEMORY_FAIL, type);
1149
            }
1150
            #endif
1151
        }
1152
    #ifndef SINGLE_THREADED
1153
        wc_UnLockMutex(&(mem->memory_mutex));
1154
    #endif
1155
    }
1156
1157
    #ifdef WOLFSSL_MALLOC_CHECK
1158
        if ((wc_ptr_t)res % WOLFSSL_STATIC_ALIGN) {
1159
            WOLFSSL_MSG("ERROR memory is not aligned");
1160
            res = NULL;
1161
        }
1162
    #endif
1163
1164
1165
    (void)i;
1166
    (void)pt;
1167
    (void)type;
1168
1169
    return res;
1170
}
1171
1172
1173
#ifdef WOLFSSL_DEBUG_MEMORY
1174
void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line)
1175
#else
1176
void wolfSSL_Free(void *ptr, void* heap, int type)
1177
#endif
1178
{
1179
    int i;
1180
    wc_Memory* pt;
1181
1182
    if (ptr) {
1183
        /* check for testing heap hint was set */
1184
    #ifdef WOLFSSL_HEAP_TEST
1185
        if (heap == (void*)WOLFSSL_HEAP_TEST) {
1186
        #ifdef WOLFSSL_DEBUG_MEMORY
1187
            fprintf(stderr, "[HEAP %p] Free: %p at %s:%d\n", heap, pt, func,
1188
                line);
1189
        #endif
1190
            return free(ptr); /* native heap */
1191
        }
1192
    #endif
1193
1194
        if (heap == NULL && globalHeapHint == NULL) {
1195
        #ifdef WOLFSSL_HEAP_TEST
1196
            /* allow using malloc for creating ctx and method */
1197
            if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
1198
                                            type == DYNAMIC_TYPE_CERT_MANAGER) {
1199
                WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method");
1200
            }
1201
            else {
1202
                WOLFSSL_MSG("ERROR null heap hint passed into XFREE");
1203
            }
1204
        #endif
1205
        #ifndef WOLFSSL_NO_MALLOC
1206
            #ifdef WOLFSSL_DEBUG_MEMORY
1207
            fprintf(stderr, "[HEAP %p] Free: %p at %s:%d\n", heap, pt, func,
1208
                line);
1209
            #endif
1210
            #ifdef FREERTOS
1211
                vPortFree(ptr); /* native heap */
1212
            #elif defined(WOLFSSL_EMBOS)
1213
                OS_HEAP_free(ptr); /* native heap */
1214
            #else
1215
                free(ptr); /* native heap */
1216
            #endif
1217
        #else
1218
            WOLFSSL_MSG("Error trying to call free when turned off");
1219
        #endif /* WOLFSSL_NO_MALLOC */
1220
        }
1221
        else {
1222
            WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
1223
            WOLFSSL_HEAP*      mem;
1224
            word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
1225
1226
            if (hint == NULL) {
1227
                hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1228
            #ifdef WOLFSSL_DEBUG_MEMORY
1229
                fprintf(stderr, "(Using global heap hint %p) ", hint);
1230
            #endif
1231
            }
1232
            mem = hint->memory;
1233
            if (mem == NULL) {
1234
                WOLFSSL_MSG("Bad hint pointer to memory");
1235
                return;
1236
            }
1237
1238
            /* get memory struct and add it to available list */
1239
            pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz);
1240
        #ifndef SINGLE_THREADED
1241
            if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
1242
                WOLFSSL_MSG("Bad memory_mutex lock");
1243
                return;
1244
            }
1245
        #endif
1246
1247
        #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1248
            /* case of using fixed IO buffers */
1249
            if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
1250
                                             (type == DYNAMIC_TYPE_OUT_BUFFER ||
1251
                                              type == DYNAMIC_TYPE_IN_BUFFER)) {
1252
                /* fixed IO pools are free'd at the end of SSL lifetime
1253
                   using FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io) */
1254
            }
1255
            else if (mem->flag & WOLFMEM_IO_POOL && pt->sz == WOLFMEM_IO_SZ &&
1256
                                             (type == DYNAMIC_TYPE_OUT_BUFFER ||
1257
                                              type == DYNAMIC_TYPE_IN_BUFFER)) {
1258
                pt->next = mem->io;
1259
                mem->io  = pt;
1260
            }
1261
            else
1262
       #endif
1263
            { /* general memory free */
1264
                for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
1265
                    if (pt->sz == mem->sizeList[i]) {
1266
                        pt->next = mem->ava[i];
1267
                        mem->ava[i] = pt;
1268
1269
                    #ifdef WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK
1270
                        if (DebugCb) {
1271
                        #ifdef WOLFSSL_DEBUG_MEMORY
1272
                            DebugCb(pt->szUsed, pt->sz, WOLFSSL_DEBUG_MEMORY_FREE, type);
1273
                        #else
1274
                            DebugCb(pt->sz, pt->sz, WOLFSSL_DEBUG_MEMORY_FREE, type);
1275
                        #endif
1276
                        }
1277
                    #endif
1278
                        break;
1279
                    }
1280
                }
1281
            }
1282
        #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1283
            mem->inUse -= pt->sz;
1284
            mem->frAlc += 1;
1285
        #endif
1286
1287
        #ifdef WOLFSSL_DEBUG_MEMORY
1288
            fprintf(stderr, "[HEAP %p] Free: %p -> %u at %s:%d\n", heap,
1289
                pt->buffer, pt->szUsed, func, line);
1290
        #endif
1291
1292
        #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1293
            /* keep track of connection statistics if flag is set */
1294
            if (mem->flag & WOLFMEM_TRACK_STATS) {
1295
                WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
1296
                if (stats != NULL) {
1297
                    /* avoid under flow */
1298
                    if (stats->curMem > pt->sz) {
1299
                        stats->curMem -= pt->sz;
1300
                    }
1301
                    else {
1302
                        stats->curMem = 0;
1303
                    }
1304
1305
                    if (stats->curAlloc > 0) {
1306
                        stats->curAlloc--;
1307
                    }
1308
                    stats->totalFr++;
1309
                }
1310
            }
1311
        #endif
1312
        #ifndef SINGLE_THREADED
1313
            wc_UnLockMutex(&(mem->memory_mutex));
1314
        #endif
1315
        }
1316
    }
1317
1318
    (void)i;
1319
    (void)pt;
1320
    (void)type;
1321
}
1322
1323
#ifndef WOLFSSL_NO_REALLOC
1324
#ifdef WOLFSSL_DEBUG_MEMORY
1325
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line)
1326
#else
1327
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
1328
#endif
1329
{
1330
    void* res = 0;
1331
    wc_Memory* pt = NULL;
1332
    int    i;
1333
1334
    /* check for testing heap hint was set */
1335
#ifdef WOLFSSL_HEAP_TEST
1336
    if (heap == (void*)WOLFSSL_HEAP_TEST) {
1337
        return realloc(ptr, size); /* native heap */
1338
    }
1339
#endif
1340
1341
    if (heap == NULL && globalHeapHint == NULL) {
1342
        #ifdef WOLFSSL_HEAP_TEST
1343
            WOLFSSL_MSG("ERROR null heap hint passed in to XREALLOC");
1344
        #endif
1345
        #ifndef WOLFSSL_NO_MALLOC
1346
            res = realloc(ptr, size); /* native heap */
1347
        #else
1348
            WOLFSSL_MSG("NO heap found to use for realloc");
1349
        #endif /* WOLFSSL_NO_MALLOC */
1350
    }
1351
    else {
1352
        WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
1353
        WOLFSSL_HEAP*      mem;
1354
        word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
1355
1356
        if (hint == NULL) {
1357
            hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1358
        #ifdef WOLFSSL_DEBUG_MEMORY
1359
            fprintf(stderr, "(Using global heap hint %p) ", hint);
1360
        #endif
1361
        }
1362
        mem = hint->memory;
1363
1364
        if (ptr == NULL) {
1365
        #ifdef WOLFSSL_DEBUG_MEMORY
1366
            return wolfSSL_Malloc(size, heap, type, func, line);
1367
        #else
1368
            return wolfSSL_Malloc(size, heap, type);
1369
        #endif
1370
        }
1371
    #ifndef SINGLE_THREADED
1372
        if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
1373
            WOLFSSL_MSG("Bad memory_mutex lock");
1374
            return NULL;
1375
        }
1376
    #endif
1377
1378
    #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1379
        /* case of using fixed IO buffers or IO pool */
1380
        if (((mem->flag & WOLFMEM_IO_POOL)||(mem->flag & WOLFMEM_IO_POOL_FIXED))
1381
                                          && (type == DYNAMIC_TYPE_OUT_BUFFER ||
1382
                                              type == DYNAMIC_TYPE_IN_BUFFER)) {
1383
            /* no realloc, is fixed size */
1384
            pt = (wc_Memory*)((byte*)ptr - padSz - sizeof(wc_Memory));
1385
            if (pt->sz < size) {
1386
                WOLFSSL_MSG("Error IO memory was not large enough");
1387
                res = NULL; /* return NULL in error case */
1388
            }
1389
            res = pt->buffer;
1390
        }
1391
        else
1392
    #endif
1393
        {
1394
        /* general memory */
1395
            for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
1396
                if ((word32)size <= mem->sizeList[i]) {
1397
                    if (mem->ava[i] != NULL) {
1398
                        pt = mem->ava[i];
1399
                        mem->ava[i] = pt->next;
1400
                        break;
1401
                    }
1402
                }
1403
            }
1404
1405
            if (pt != NULL && res == NULL) {
1406
                word32 prvSz;
1407
1408
                res = pt->buffer;
1409
1410
                /* copy over original information and free ptr */
1411
                prvSz = ((wc_Memory*)((byte*)ptr - padSz -
1412
                                               sizeof(wc_Memory)))->sz;
1413
                prvSz = (prvSz > pt->sz)? pt->sz: prvSz;
1414
                XMEMCPY(pt->buffer, ptr, prvSz);
1415
            #ifndef WOLFSSL_STATIC_MEMORY_LEAN
1416
                mem->inUse += pt->sz;
1417
                mem->alloc += 1;
1418
            #endif
1419
1420
                /* free memory that was previously being used */
1421
            #ifndef SINGLE_THREADED
1422
                wc_UnLockMutex(&(mem->memory_mutex));
1423
            #endif
1424
                wolfSSL_Free(ptr, heap, type
1425
            #ifdef WOLFSSL_DEBUG_MEMORY
1426
                    , func, line
1427
            #endif
1428
                );
1429
            #ifndef SINGLE_THREADED
1430
                if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
1431
                    WOLFSSL_MSG("Bad memory_mutex lock");
1432
                    return NULL;
1433
                }
1434
            #endif
1435
            }
1436
        }
1437
    #ifndef SINGLE_THREADED
1438
        wc_UnLockMutex(&(mem->memory_mutex));
1439
    #endif
1440
    }
1441
1442
    #ifdef WOLFSSL_MALLOC_CHECK
1443
        if ((wc_ptr_t)res % WOLFSSL_STATIC_ALIGN) {
1444
            WOLFSSL_MSG("ERROR memory is not aligned");
1445
            res = NULL;
1446
        }
1447
    #endif
1448
1449
    (void)i;
1450
    (void)pt;
1451
    (void)type;
1452
1453
    return res;
1454
}
1455
#endif /* WOLFSSL_STATIC_MEMORY */
1456
#endif /* WOLFSSL_NO_REALLOC */
1457
#endif /* USE_WOLFSSL_MEMORY */
1458
1459
1460
#ifdef HAVE_IO_POOL
1461
1462
/* Example for user io pool, shared build may need definitions in lib proper */
1463
1464
#include <stdlib.h>
1465
1466
#ifndef HAVE_THREAD_LS
1467
    #error "Oops, simple I/O pool example needs thread local storage"
1468
#endif
1469
1470
1471
/* allow simple per thread in and out pools */
1472
/* use 17k size since max record size is 16k plus overhead */
1473
static THREAD_LS_T byte pool_in[17*1024];
1474
static THREAD_LS_T byte pool_out[17*1024];
1475
1476
1477
void* XMALLOC(size_t n, void* heap, int type)
1478
{
1479
    (void)heap;
1480
1481
    if (type == DYNAMIC_TYPE_IN_BUFFER) {
1482
        if (n < sizeof(pool_in))
1483
            return pool_in;
1484
        else
1485
            return NULL;
1486
    }
1487
1488
    if (type == DYNAMIC_TYPE_OUT_BUFFER) {
1489
        if (n < sizeof(pool_out))
1490
            return pool_out;
1491
        else
1492
            return NULL;
1493
    }
1494
1495
    return malloc(n); /* native heap */
1496
}
1497
1498
void* XREALLOC(void *p, size_t n, void* heap, int type)
1499
{
1500
    (void)heap;
1501
1502
    if (type == DYNAMIC_TYPE_IN_BUFFER) {
1503
        if (n < sizeof(pool_in))
1504
            return pool_in;
1505
        else
1506
            return NULL;
1507
    }
1508
1509
    if (type == DYNAMIC_TYPE_OUT_BUFFER) {
1510
        if (n < sizeof(pool_out))
1511
            return pool_out;
1512
        else
1513
            return NULL;
1514
    }
1515
1516
    return realloc(p, n); /* native heap */
1517
}
1518
1519
void XFREE(void *p, void* heap, int type)
1520
{
1521
    (void)heap;
1522
1523
    if (type == DYNAMIC_TYPE_IN_BUFFER)
1524
        return;  /* do nothing, static pool */
1525
1526
    if (type == DYNAMIC_TYPE_OUT_BUFFER)
1527
        return;  /* do nothing, static pool */
1528
1529
    free(p); /* native heap */
1530
}
1531
1532
#endif /* HAVE_IO_POOL */
1533
1534
#ifdef WOLFSSL_MEMORY_LOG
1535
void *xmalloc(size_t n, void* heap, int type, const char* func,
1536
              const char* file, unsigned int line)
1537
{
1538
    void*   p = NULL;
1539
    word32* p32;
1540
1541
#ifdef WOLFSSL_MEM_FAIL_COUNT
1542
    if (!wc_MemFailCount_AllocMem()) {
1543
        WOLFSSL_MSG("MemFailCnt: Fail malloc");
1544
        return NULL;
1545
    }
1546
#endif
1547
1548
    if (malloc_function) {
1549
#ifndef WOLFSSL_STATIC_MEMORY
1550
        p32 = malloc_function(n + sizeof(word32) * 4);
1551
#else
1552
        p32 = malloc_function(n + sizeof(word32) * 4, heap, type);
1553
#endif
1554
    }
1555
    else
1556
        p32 = malloc(n + sizeof(word32) * 4); /* native heap */
1557
1558
    if (p32 != NULL) {
1559
        p32[0] = (word32)n;
1560
        p = (void*)(p32 + 4);
1561
1562
        fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%u\n", p, (word32)n,
1563
                                                        type, func, file, line);
1564
    }
1565
1566
    (void)heap;
1567
1568
    return p;
1569
}
1570
void *xrealloc(void *p, size_t n, void* heap, int type, const char* func,
1571
               const char* file, unsigned int line)
1572
{
1573
    void*   newp = NULL;
1574
    word32* p32;
1575
    word32* oldp32 = NULL;
1576
    word32  oldLen;
1577
1578
#ifdef WOLFSSL_MEM_FAIL_COUNT
1579
    if (!wc_MemFailCount_AllocMem()) {
1580
        WOLFSSL_MSG("MemFailCnt: Fail malloc");
1581
        return NULL;
1582
    }
1583
#endif
1584
1585
    if (p != NULL) {
1586
        oldp32 = (word32*)p;
1587
        oldp32 -= 4;
1588
        oldLen = oldp32[0];
1589
    }
1590
1591
    if (realloc_function) {
1592
#ifndef WOLFSSL_STATIC_MEMORY
1593
        p32 = realloc_function(oldp32, n + sizeof(word32) * 4);
1594
#else
1595
        p32 = realloc_function(oldp32, n + sizeof(word32) * 4, heap, type);
1596
#endif
1597
    }
1598
    else
1599
        p32 = realloc(oldp32, n + sizeof(word32) * 4); /* native heap */
1600
1601
    if (p32 != NULL) {
1602
        p32[0] = (word32)n;
1603
        newp = (void*)(p32 + 4);
1604
1605
        if (p != NULL) {
1606
            fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%u\n", p, oldLen,
1607
                                                        type, func, file, line);
1608
        }
1609
        fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%u\n", newp, (word32)n,
1610
                                                        type, func, file, line);
1611
    }
1612
1613
#ifdef WOLFSSL_MEM_FAIL_COUNT
1614
    if (p != NULL) {
1615
        wc_MemFailCount_FreeMem();
1616
    }
1617
#endif
1618
1619
    (void)heap;
1620
1621
    return newp;
1622
}
1623
void xfree(void *p, void* heap, int type, const char* func, const char* file,
1624
           unsigned int line)
1625
{
1626
    word32* p32 = (word32*)p;
1627
1628
    if (p != NULL) {
1629
    #ifdef WOLFSSL_MEM_FAIL_COUNT
1630
        wc_MemFailCount_FreeMem();
1631
    #endif
1632
        p32 -= 4;
1633
1634
        fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%u\n", p, p32[0], type,
1635
                                                              func, file, line);
1636
1637
        if (free_function) {
1638
#ifndef WOLFSSL_STATIC_MEMORY
1639
            free_function(p32);
1640
#else
1641
            free_function(p32, heap, type);
1642
#endif
1643
        }
1644
        else
1645
            free(p32); /* native heap */
1646
    }
1647
1648
    (void)heap;
1649
}
1650
#endif /* WOLFSSL_MEMORY_LOG */
1651
1652
#ifdef WOLFSSL_STACK_LOG
1653
/* Note: this code only works with GCC using -finstrument-functions. */
1654
void __attribute__((no_instrument_function))
1655
     __cyg_profile_func_enter(void *func,  void *caller)
1656
{
1657
    register void* sp asm("sp");
1658
    fprintf(stderr, "ENTER: %016lx %p\n", (unsigned long)(wc_ptr_t)func, sp);
1659
    (void)caller;
1660
}
1661
1662
void __attribute__((no_instrument_function))
1663
     __cyg_profile_func_exit(void *func, void *caller)
1664
{
1665
    register void* sp asm("sp");
1666
    fprintf(stderr, "EXIT: %016lx %p\n", (unsigned long)(wc_ptr_t)func, sp);
1667
    (void)caller;
1668
}
1669
#endif
1670
1671
#ifdef WC_DEBUG_CIPHER_LIFECYCLE
1672
static const byte wc_debug_cipher_lifecycle_tag_value[] =
1673
    { 'W', 'o', 'l', 'f' };
1674
1675
WOLFSSL_LOCAL int wc_debug_CipherLifecycleInit(
1676
    void **CipherLifecycleTag,
1677
    void *heap)
1678
{
1679
    if (CipherLifecycleTag == NULL)
1680
        return BAD_FUNC_ARG;
1681
    *CipherLifecycleTag = (void *)XMALLOC(
1682
        sizeof(wc_debug_cipher_lifecycle_tag_value),
1683
        heap,
1684
        DYNAMIC_TYPE_DEBUG_TAG);
1685
    if (*CipherLifecycleTag == NULL)
1686
        return MEMORY_E;
1687
    XMEMCPY(*CipherLifecycleTag,
1688
            wc_debug_cipher_lifecycle_tag_value,
1689
            sizeof(wc_debug_cipher_lifecycle_tag_value));
1690
    return 0;
1691
}
1692
1693
WOLFSSL_LOCAL int wc_debug_CipherLifecycleCheck(
1694
    void *CipherLifecycleTag,
1695
    int abort_p)
1696
{
1697
    int ret;
1698
    if (CipherLifecycleTag == NULL) {
1699
        ret = BAD_STATE_E;
1700
        goto out;
1701
    }
1702
    if (XMEMCMP(CipherLifecycleTag,
1703
                wc_debug_cipher_lifecycle_tag_value,
1704
                sizeof(wc_debug_cipher_lifecycle_tag_value)) != 0)
1705
    {
1706
        ret = BAD_STATE_E;
1707
        goto out;
1708
    }
1709
    ret = 0;
1710
1711
out:
1712
    if ((ret < 0) && abort_p)
1713
        abort();
1714
1715
    return ret;
1716
}
1717
1718
WOLFSSL_LOCAL int wc_debug_CipherLifecycleFree(
1719
    void **CipherLifecycleTag,
1720
    void *heap,
1721
    int abort_p)
1722
{
1723
    int ret;
1724
    if (CipherLifecycleTag == NULL)
1725
        return BAD_FUNC_ARG;
1726
    ret = wc_debug_CipherLifecycleCheck(*CipherLifecycleTag, abort_p);
1727
    if (ret != 0)
1728
        return ret;
1729
    XFREE(*CipherLifecycleTag, heap, DYNAMIC_TYPE_DEBUG_TAG);
1730
    *CipherLifecycleTag = NULL;
1731
    return 0;
1732
}
1733
#endif /* WC_DEBUG_CIPHER_LIFECYCLE */
1734
1735
#ifdef DEBUG_VECTOR_REGISTER_ACCESS
1736
THREAD_LS_T int wc_svr_count = 0;
1737
THREAD_LS_T const char *wc_svr_last_file = NULL;
1738
THREAD_LS_T int wc_svr_last_line = -1;
1739
THREAD_LS_T int wc_debug_vector_registers_retval =
1740
    WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL;
1741
#endif
1742
1743
#ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
1744
1745
#ifdef HAVE_THREAD_LS
1746
1747
WOLFSSL_LOCAL int SAVE_VECTOR_REGISTERS2_fuzzer(void) {
1748
    static THREAD_LS_T struct drand48_data wc_svr_fuzzing_state;
1749
    static THREAD_LS_T int wc_svr_fuzzing_seeded = 0;
1750
    long result;
1751
1752
#ifdef DEBUG_VECTOR_REGISTER_ACCESS
1753
    if (wc_debug_vector_registers_retval)
1754
        return wc_debug_vector_registers_retval;
1755
#endif
1756
1757
    if (wc_svr_fuzzing_seeded == 0) {
1758
        long seed = WC_DEBUG_VECTOR_REGISTERS_FUZZING_SEED;
1759
        char *seed_envstr = getenv("WC_DEBUG_VECTOR_REGISTERS_FUZZING_SEED");
1760
        if (seed_envstr)
1761
            seed = strtol(seed_envstr, NULL, 0);
1762
        (void)srand48_r(seed, &wc_svr_fuzzing_state);
1763
        wc_svr_fuzzing_seeded = 1;
1764
    }
1765
    (void)lrand48_r(&wc_svr_fuzzing_state, &result);
1766
    if (result & 1)
1767
        return IO_FAILED_E;
1768
    else
1769
        return 0;
1770
}
1771
1772
#else /* !HAVE_THREAD_LS */
1773
1774
/* alternate implementation useful for testing in the kernel module build, where
1775
 * glibc and thread-local storage are unavailable.
1776
 *
1777
 * note this is not a well-behaved PRNG, but is adequate for fuzzing purposes.
1778
 * the prn sequence is incompressible according to ent and xz, and does not
1779
 * cycle within 10M iterations with various seeds including zero, but the Chi
1780
 * square distribution is poor, and the unconditioned lsb bit balance is ~54%
1781
 * regardless of seed.
1782
 *
1783
 * deterministic only if access is single-threaded, but never degenerate.
1784
 */
1785
1786
WOLFSSL_LOCAL int SAVE_VECTOR_REGISTERS2_fuzzer(void) {
1787
    static unsigned long prn = WC_DEBUG_VECTOR_REGISTERS_FUZZING_SEED;
1788
    static int balance_bit = 0;
1789
    unsigned long new_prn = prn ^ 0xba86943da66ee701ul; /* note this magic
1790
                                                         * random number is
1791
                                                         * bit-balanced.
1792
                                                         */
1793
1794
#ifdef DEBUG_VECTOR_REGISTER_ACCESS
1795
    if (wc_debug_vector_registers_retval)
1796
        return wc_debug_vector_registers_retval;
1797
#endif
1798
1799
    /* barrel-roll using the bottom 6 bits. */
1800
    if (new_prn & 0x3f)
1801
        new_prn = (new_prn << (new_prn & 0x3f)) |
1802
            (new_prn >> (0x40 - (new_prn & 0x3f)));
1803
    prn = new_prn;
1804
1805
    balance_bit = !balance_bit;
1806
1807
    return ((prn & 1) ^ balance_bit) ? IO_FAILED_E : 0;
1808
}
1809
1810
#endif /* !HAVE_THREAD_LS */
1811
1812
#endif /* DEBUG_VECTOR_REGISTER_ACCESS_FUZZING */
1813
1814
#ifdef WOLFSSL_LINUXKM
1815
    #include "../../linuxkm/linuxkm_memory.c"
1816
#endif