Coverage Report

Created: 2026-02-14 07:18

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