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 | #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
|
---|
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 | #ifdef WOLFSSL_TLS13
|
---|
114 | #define LARGEST_MEM_BUCKET 30400
|
---|
115 | #else
|
---|
116 | #define LARGEST_MEM_BUCKET 25600
|
---|
117 | #endif
|
---|
118 | #endif
|
---|
119 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3360,4480,\
|
---|
120 | LARGEST_MEM_BUCKET
|
---|
121 | #elif defined (WOLFSSL_CERT_EXT)
|
---|
122 | /* certificate extensions requires 24k for the SSL struct */
|
---|
123 | #ifndef LARGEST_MEM_BUCKET
|
---|
124 | #define LARGEST_MEM_BUCKET 24576
|
---|
125 | #endif
|
---|
126 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
|
---|
127 | LARGEST_MEM_BUCKET
|
---|
128 | #else
|
---|
129 | /* increase 23k for object member of WOLFSSL_X509_NAME_ENTRY */
|
---|
130 | #ifndef LARGEST_MEM_BUCKET
|
---|
131 | #define LARGEST_MEM_BUCKET 23440
|
---|
132 | #endif
|
---|
133 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
|
---|
134 | LARGEST_MEM_BUCKET
|
---|
135 | #endif
|
---|
136 | #endif
|
---|
137 | #ifndef WOLFMEM_DIST
|
---|
138 | #ifndef WOLFSSL_STATIC_MEMORY_SMALL
|
---|
139 | #define WOLFMEM_DIST 49,10,6,14,5,6,9,1,1
|
---|
140 | #else
|
---|
141 | /* Low resource and not RSA */
|
---|
142 | #define WOLFMEM_DIST 29, 7,6, 9,4,4,0,0,0
|
---|
143 | #endif
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | /* flags for loading static memory (one hot bit) */
|
---|
147 | #define WOLFMEM_GENERAL 0x01
|
---|
148 | #define WOLFMEM_IO_POOL 0x02
|
---|
149 | #define WOLFMEM_IO_POOL_FIXED 0x04
|
---|
150 | #define WOLFMEM_TRACK_STATS 0x08
|
---|
151 |
|
---|
152 | #ifndef WOLFSSL_MEM_GUARD
|
---|
153 | #define WOLFSSL_MEM_GUARD
|
---|
154 | typedef struct WOLFSSL_MEM_STATS WOLFSSL_MEM_STATS;
|
---|
155 | typedef struct WOLFSSL_MEM_CONN_STATS WOLFSSL_MEM_CONN_STATS;
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | struct WOLFSSL_MEM_CONN_STATS {
|
---|
159 | word32 peakMem; /* peak memory usage */
|
---|
160 | word32 curMem; /* current memory usage */
|
---|
161 | word32 peakAlloc; /* peak memory allocations */
|
---|
162 | word32 curAlloc; /* current memory allocations */
|
---|
163 | word32 totalAlloc;/* total memory allocations for lifetime */
|
---|
164 | word32 totalFr; /* total frees for lifetime */
|
---|
165 | };
|
---|
166 |
|
---|
167 | struct WOLFSSL_MEM_STATS {
|
---|
168 | word32 curAlloc; /* current memory allocations */
|
---|
169 | word32 totalAlloc;/* total memory allocations for lifetime */
|
---|
170 | word32 totalFr; /* total frees for lifetime */
|
---|
171 | word32 totalUse; /* total amount of memory used in blocks */
|
---|
172 | word32 avaIO; /* available IO specific pools */
|
---|
173 | word32 maxHa; /* max number of concurrent handshakes allowed */
|
---|
174 | word32 maxIO; /* max number of concurrent IO connections allowed */
|
---|
175 | word32 blockSz[WOLFMEM_MAX_BUCKETS]; /* block sizes in stacks */
|
---|
176 | word32 avaBlock[WOLFMEM_MAX_BUCKETS];/* ava block sizes */
|
---|
177 | word32 usedBlock[WOLFMEM_MAX_BUCKETS];
|
---|
178 | int flag; /* flag used */
|
---|
179 | };
|
---|
180 |
|
---|
181 | typedef struct wc_Memory wc_Memory; /* internal structure for mem bucket */
|
---|
182 | typedef struct WOLFSSL_HEAP {
|
---|
183 | wc_Memory* ava[WOLFMEM_MAX_BUCKETS];
|
---|
184 | wc_Memory* io; /* list of buffers to use for IO */
|
---|
185 | word32 maxHa; /* max concurrent handshakes */
|
---|
186 | word32 curHa;
|
---|
187 | word32 maxIO; /* max concurrent IO connections */
|
---|
188 | word32 curIO;
|
---|
189 | word32 sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */
|
---|
190 | word32 distList[WOLFMEM_MAX_BUCKETS];/* general distribution */
|
---|
191 | word32 inUse; /* amount of memory currently in use */
|
---|
192 | word32 ioUse;
|
---|
193 | word32 alloc; /* total number of allocs */
|
---|
194 | word32 frAlc; /* total number of frees */
|
---|
195 | int flag;
|
---|
196 | wolfSSL_Mutex memory_mutex;
|
---|
197 | } WOLFSSL_HEAP;
|
---|
198 |
|
---|
199 | /* structure passed into XMALLOC as heap hint
|
---|
200 | * having this abstraction allows tracking statistics of individual ssl's
|
---|
201 | */
|
---|
202 | typedef struct WOLFSSL_HEAP_HINT {
|
---|
203 | WOLFSSL_HEAP* memory;
|
---|
204 | WOLFSSL_MEM_CONN_STATS* stats; /* hold individual connection stats */
|
---|
205 | wc_Memory* outBuf; /* set if using fixed io buffers */
|
---|
206 | wc_Memory* inBuf;
|
---|
207 | byte haFlag; /* flag used for checking handshake count */
|
---|
208 | } WOLFSSL_HEAP_HINT;
|
---|
209 |
|
---|
210 | WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
---|
211 | unsigned char* buf, unsigned int sz, int flag, int max);
|
---|
212 |
|
---|
213 | WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap);
|
---|
214 | WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz,
|
---|
215 | int flag, WOLFSSL_HEAP* heap);
|
---|
216 | WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap,
|
---|
217 | WOLFSSL_MEM_STATS* stats);
|
---|
218 | WOLFSSL_LOCAL int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
|
---|
219 | WOLFSSL_LOCAL int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
|
---|
220 |
|
---|
221 | WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag);
|
---|
222 | WOLFSSL_API int wolfSSL_MemoryPaddingSz(void);
|
---|
223 | #endif /* WOLFSSL_STATIC_MEMORY */
|
---|
224 |
|
---|
225 | #ifdef WOLFSSL_STACK_LOG
|
---|
226 | WOLFSSL_API void __attribute__((no_instrument_function))
|
---|
227 | __cyg_profile_func_enter(void *func, void *caller);
|
---|
228 | WOLFSSL_API void __attribute__((no_instrument_function))
|
---|
229 | __cyg_profile_func_exit(void *func, void *caller);
|
---|
230 | #endif /* WOLFSSL_STACK_LOG */
|
---|
231 |
|
---|
232 | #ifdef __cplusplus
|
---|
233 | } /* extern "C" */
|
---|
234 | #endif
|
---|
235 |
|
---|
236 | #endif /* WOLFSSL_MEMORY_H */
|
---|
237 |
|
---|