memory.h 11 KB

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