[457] | 1 | /* memory.h
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2006-2020 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 | /* submitted by eof */
|
---|
| 24 |
|
---|
| 25 | /*!
|
---|
| 26 | \file wolfssl/wolfcrypt/memory.h
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef WOLFSSL_MEMORY_H
|
---|
| 30 | #define WOLFSSL_MEMORY_H
|
---|
| 31 |
|
---|
| 32 | #ifndef STRING_USER
|
---|
| 33 | #include <stdlib.h>
|
---|
| 34 | #endif
|
---|
| 35 | #include <wolfssl/wolfcrypt/types.h>
|
---|
| 36 |
|
---|
| 37 | #ifdef __cplusplus
|
---|
| 38 | extern "C" {
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
| 41 | #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
|
---|
| 42 | WOLFSSL_API void wolfSSL_SetMemFailCount(int memFailCount);
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
| 45 | #ifdef WOLFSSL_STATIC_MEMORY
|
---|
| 46 | #ifdef WOLFSSL_DEBUG_MEMORY
|
---|
| 47 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line);
|
---|
| 48 | typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line);
|
---|
| 49 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
|
---|
| 50 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line);
|
---|
| 51 | WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line);
|
---|
| 52 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
|
---|
| 53 | #else
|
---|
| 54 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type);
|
---|
| 55 | typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type);
|
---|
| 56 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type);
|
---|
| 57 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
|
---|
| 58 | WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type);
|
---|
| 59 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
|
---|
| 60 | #endif /* WOLFSSL_DEBUG_MEMORY */
|
---|
| 61 | #else
|
---|
| 62 | #ifdef WOLFSSL_DEBUG_MEMORY
|
---|
| 63 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
|
---|
| 64 | typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
|
---|
| 65 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
|
---|
| 66 |
|
---|
| 67 | /* Public in case user app wants to use XMALLOC/XFREE */
|
---|
| 68 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
|
---|
| 69 | WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line);
|
---|
| 70 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
|
---|
| 71 | #else
|
---|
| 72 | typedef void *(*wolfSSL_Malloc_cb)(size_t size);
|
---|
| 73 | typedef void (*wolfSSL_Free_cb)(void *ptr);
|
---|
| 74 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
|
---|
| 75 | /* Public in case user app wants to use XMALLOC/XFREE */
|
---|
| 76 | WOLFSSL_API void* wolfSSL_Malloc(size_t size);
|
---|
| 77 | WOLFSSL_API void wolfSSL_Free(void *ptr);
|
---|
| 78 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
|
---|
| 79 | #endif /* WOLFSSL_DEBUG_MEMORY */
|
---|
| 80 | #endif /* WOLFSSL_STATIC_MEMORY */
|
---|
| 81 |
|
---|
| 82 | /* Public get/set functions */
|
---|
| 83 | WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb,
|
---|
| 84 | wolfSSL_Free_cb,
|
---|
| 85 | wolfSSL_Realloc_cb);
|
---|
| 86 | WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*,
|
---|
| 87 | wolfSSL_Free_cb*,
|
---|
| 88 | wolfSSL_Realloc_cb*);
|
---|
| 89 |
|
---|
| 90 | #ifdef WOLFSSL_STATIC_MEMORY
|
---|
| 91 | #define WOLFSSL_STATIC_TIMEOUT 1
|
---|
| 92 | #ifndef WOLFSSL_STATIC_ALIGN
|
---|
| 93 | #define WOLFSSL_STATIC_ALIGN 16
|
---|
| 94 | #endif
|
---|
| 95 | #ifndef WOLFMEM_MAX_BUCKETS
|
---|
| 96 | #define WOLFMEM_MAX_BUCKETS 9
|
---|
| 97 | #endif
|
---|
| 98 | #define WOLFMEM_DEF_BUCKETS 9 /* number of default memory blocks */
|
---|
| 99 | #ifndef WOLFMEM_IO_SZ
|
---|
| 100 | #define WOLFMEM_IO_SZ 16992 /* 16 byte aligned */
|
---|
| 101 | #endif
|
---|
| 102 | #ifndef WOLFMEM_BUCKETS
|
---|
| 103 | #ifndef SESSION_CERTS
|
---|
| 104 | /* default size of chunks of memory to separate into */
|
---|
| 105 | #ifndef LARGEST_MEM_BUCKET
|
---|
| 106 | #define LARGEST_MEM_BUCKET 16128
|
---|
| 107 | #endif
|
---|
| 108 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
|
---|
| 109 | LARGEST_MEM_BUCKET
|
---|
| 110 | #elif defined (OPENSSL_EXTRA)
|
---|
| 111 | /* extra storage in structs for multiple attributes and order */
|
---|
| 112 | #ifndef LARGEST_MEM_BUCKET
|
---|
| 113 | #define LARGEST_MEM_BUCKET 25600
|
---|
| 114 | #endif
|
---|
| 115 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3360,4480,\
|
---|
| 116 | LARGEST_MEM_BUCKET
|
---|
| 117 | #elif defined (WOLFSSL_CERT_EXT)
|
---|
| 118 | /* certificate extensions requires 24k for the SSL struct */
|
---|
| 119 | #ifndef LARGEST_MEM_BUCKET
|
---|
| 120 | #define LARGEST_MEM_BUCKET 24576
|
---|
| 121 | #endif
|
---|
| 122 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
|
---|
| 123 | LARGEST_MEM_BUCKET
|
---|
| 124 | #else
|
---|
| 125 | /* increase 23k for object member of WOLFSSL_X509_NAME_ENTRY */
|
---|
| 126 | #ifndef LARGEST_MEM_BUCKET
|
---|
| 127 | #define LARGEST_MEM_BUCKET 23440
|
---|
| 128 | #endif
|
---|
| 129 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
|
---|
| 130 | LARGEST_MEM_BUCKET
|
---|
| 131 | #endif
|
---|
| 132 | #endif
|
---|
| 133 | #ifndef WOLFMEM_DIST
|
---|
| 134 | #ifndef WOLFSSL_STATIC_MEMORY_SMALL
|
---|
| 135 | #define WOLFMEM_DIST 49,10,6,14,5,6,9,1,1
|
---|
| 136 | #else
|
---|
| 137 | /* Low resource and not RSA */
|
---|
| 138 | #define WOLFMEM_DIST 29, 7,6, 9,4,4,0,0,0
|
---|
| 139 | #endif
|
---|
| 140 | #endif
|
---|
| 141 |
|
---|
| 142 | /* flags for loading static memory (one hot bit) */
|
---|
| 143 | #define WOLFMEM_GENERAL 0x01
|
---|
| 144 | #define WOLFMEM_IO_POOL 0x02
|
---|
| 145 | #define WOLFMEM_IO_POOL_FIXED 0x04
|
---|
| 146 | #define WOLFMEM_TRACK_STATS 0x08
|
---|
| 147 |
|
---|
| 148 | #ifndef WOLFSSL_MEM_GUARD
|
---|
| 149 | #define WOLFSSL_MEM_GUARD
|
---|
| 150 | typedef struct WOLFSSL_MEM_STATS WOLFSSL_MEM_STATS;
|
---|
| 151 | typedef struct WOLFSSL_MEM_CONN_STATS WOLFSSL_MEM_CONN_STATS;
|
---|
| 152 | #endif
|
---|
| 153 |
|
---|
| 154 | struct WOLFSSL_MEM_CONN_STATS {
|
---|
| 155 | word32 peakMem; /* peak memory usage */
|
---|
| 156 | word32 curMem; /* current memory usage */
|
---|
| 157 | word32 peakAlloc; /* peak memory allocations */
|
---|
| 158 | word32 curAlloc; /* current memory allocations */
|
---|
| 159 | word32 totalAlloc;/* total memory allocations for lifetime */
|
---|
| 160 | word32 totalFr; /* total frees for lifetime */
|
---|
| 161 | };
|
---|
| 162 |
|
---|
| 163 | struct WOLFSSL_MEM_STATS {
|
---|
| 164 | word32 curAlloc; /* current memory allocations */
|
---|
| 165 | word32 totalAlloc;/* total memory allocations for lifetime */
|
---|
| 166 | word32 totalFr; /* total frees for lifetime */
|
---|
| 167 | word32 totalUse; /* total amount of memory used in blocks */
|
---|
| 168 | word32 avaIO; /* available IO specific pools */
|
---|
| 169 | word32 maxHa; /* max number of concurrent handshakes allowed */
|
---|
| 170 | word32 maxIO; /* max number of concurrent IO connections allowed */
|
---|
| 171 | word32 blockSz[WOLFMEM_MAX_BUCKETS]; /* block sizes in stacks */
|
---|
| 172 | word32 avaBlock[WOLFMEM_MAX_BUCKETS];/* ava block sizes */
|
---|
| 173 | word32 usedBlock[WOLFMEM_MAX_BUCKETS];
|
---|
| 174 | int flag; /* flag used */
|
---|
| 175 | };
|
---|
| 176 |
|
---|
| 177 | typedef struct wc_Memory wc_Memory; /* internal structure for mem bucket */
|
---|
| 178 | typedef struct WOLFSSL_HEAP {
|
---|
| 179 | wc_Memory* ava[WOLFMEM_MAX_BUCKETS];
|
---|
| 180 | wc_Memory* io; /* list of buffers to use for IO */
|
---|
| 181 | word32 maxHa; /* max concurrent handshakes */
|
---|
| 182 | word32 curHa;
|
---|
| 183 | word32 maxIO; /* max concurrent IO connections */
|
---|
| 184 | word32 curIO;
|
---|
| 185 | word32 sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */
|
---|
| 186 | word32 distList[WOLFMEM_MAX_BUCKETS];/* general distribution */
|
---|
| 187 | word32 inUse; /* amount of memory currently in use */
|
---|
| 188 | word32 ioUse;
|
---|
| 189 | word32 alloc; /* total number of allocs */
|
---|
| 190 | word32 frAlc; /* total number of frees */
|
---|
| 191 | int flag;
|
---|
| 192 | wolfSSL_Mutex memory_mutex;
|
---|
| 193 | } WOLFSSL_HEAP;
|
---|
| 194 |
|
---|
| 195 | /* structure passed into XMALLOC as heap hint
|
---|
| 196 | * having this abstraction allows tracking statistics of individual ssl's
|
---|
| 197 | */
|
---|
| 198 | typedef struct WOLFSSL_HEAP_HINT {
|
---|
| 199 | WOLFSSL_HEAP* memory;
|
---|
| 200 | WOLFSSL_MEM_CONN_STATS* stats; /* hold individual connection stats */
|
---|
| 201 | wc_Memory* outBuf; /* set if using fixed io buffers */
|
---|
| 202 | wc_Memory* inBuf;
|
---|
| 203 | byte haFlag; /* flag used for checking handshake count */
|
---|
| 204 | } WOLFSSL_HEAP_HINT;
|
---|
| 205 |
|
---|
| 206 | WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
---|
| 207 | unsigned char* buf, unsigned int sz, int flag, int max);
|
---|
| 208 |
|
---|
| 209 | WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap);
|
---|
| 210 | WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz,
|
---|
| 211 | int flag, WOLFSSL_HEAP* heap);
|
---|
| 212 | WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap,
|
---|
| 213 | WOLFSSL_MEM_STATS* stats);
|
---|
| 214 | WOLFSSL_LOCAL int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
|
---|
| 215 | WOLFSSL_LOCAL int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
|
---|
| 216 |
|
---|
| 217 | WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag);
|
---|
| 218 | WOLFSSL_API int wolfSSL_MemoryPaddingSz(void);
|
---|
| 219 | #endif /* WOLFSSL_STATIC_MEMORY */
|
---|
| 220 |
|
---|
| 221 | #ifdef WOLFSSL_STACK_LOG
|
---|
| 222 | WOLFSSL_API void __attribute__((no_instrument_function))
|
---|
| 223 | __cyg_profile_func_enter(void *func, void *caller);
|
---|
| 224 | WOLFSSL_API void __attribute__((no_instrument_function))
|
---|
| 225 | __cyg_profile_func_exit(void *func, void *caller);
|
---|
| 226 | #endif /* WOLFSSL_STACK_LOG */
|
---|
| 227 |
|
---|
| 228 | #ifdef __cplusplus
|
---|
| 229 | } /* extern "C" */
|
---|
| 230 | #endif
|
---|
| 231 |
|
---|
| 232 | #endif /* WOLFSSL_MEMORY_H */
|
---|
| 233 |
|
---|