memory.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /* memory.c
  2. *
  3. * Copyright (C) 2006-2022 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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. /* check old macros @wc_fips */
  26. #if defined(USE_CYASSL_MEMORY) && !defined(USE_WOLFSSL_MEMORY)
  27. #define USE_WOLFSSL_MEMORY
  28. #endif
  29. #if defined(CYASSL_MALLOC_CHECK) && !defined(WOLFSSL_MALLOC_CHECK)
  30. #define WOLFSSL_MALLOC_CHECK
  31. #endif
  32. /*
  33. Possible memory options:
  34. * NO_WOLFSSL_MEMORY: Disables wolf memory callback support. When not defined settings.h defines USE_WOLFSSL_MEMORY.
  35. * WOLFSSL_STATIC_MEMORY: Turns on the use of static memory buffers and functions.
  36. This allows for using static memory instead of dynamic.
  37. * WOLFSSL_STATIC_ALIGN: Define defaults to 16 to indicate static memory alignment.
  38. * HAVE_IO_POOL: Enables use of static thread safe memory pool for input/output buffers.
  39. * XMALLOC_OVERRIDE: Allows override of the XMALLOC, XFREE and XREALLOC macros.
  40. * XMALLOC_USER: Allows custom XMALLOC, XFREE and XREALLOC functions to be defined.
  41. * WOLFSSL_NO_MALLOC: Disables the fall-back case to use STDIO malloc/free when no callbacks are set.
  42. * WOLFSSL_TRACK_MEMORY: Enables memory tracking for total stats and list of allocated memory.
  43. * WOLFSSL_DEBUG_MEMORY: Enables extra function and line number args for memory callbacks.
  44. * WOLFSSL_DEBUG_MEMORY_PRINT: Enables printing of each malloc/free.
  45. * WOLFSSL_MALLOC_CHECK: Reports malloc or alignment failure using WOLFSSL_STATIC_ALIGN
  46. * WOLFSSL_FORCE_MALLOC_FAIL_TEST: Used for internal testing to induce random malloc failures.
  47. * WOLFSSL_HEAP_TEST: Used for internal testing of heap hint
  48. */
  49. #ifdef WOLFSSL_ZEPHYR
  50. #undef realloc
  51. void *z_realloc(void *ptr, size_t size)
  52. {
  53. if (ptr == NULL)
  54. ptr = malloc(size);
  55. else
  56. ptr = realloc(ptr, size);
  57. return ptr;
  58. }
  59. #define realloc z_realloc
  60. #endif
  61. #ifdef USE_WOLFSSL_MEMORY
  62. #include <wolfssl/wolfcrypt/memory.h>
  63. #include <wolfssl/wolfcrypt/error-crypt.h>
  64. #include <wolfssl/wolfcrypt/logging.h>
  65. #if defined(WOLFSSL_DEBUG_MEMORY) && defined(WOLFSSL_DEBUG_MEMORY_PRINT)
  66. #include <stdio.h>
  67. #endif
  68. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  69. static int gMemFailCountSeed;
  70. static int gMemFailCount;
  71. void wolfSSL_SetMemFailCount(int memFailCount)
  72. {
  73. if (gMemFailCountSeed == 0) {
  74. gMemFailCountSeed = memFailCount;
  75. gMemFailCount = memFailCount;
  76. }
  77. }
  78. #endif
  79. #if defined(WOLFSSL_MALLOC_CHECK) || defined(WOLFSSL_TRACK_MEMORY_FULL) || \
  80. defined(WOLFSSL_MEMORY_LOG)
  81. #include <stdio.h>
  82. #endif
  83. /* Set these to default values initially. */
  84. static wolfSSL_Malloc_cb malloc_function = NULL;
  85. static wolfSSL_Free_cb free_function = NULL;
  86. static wolfSSL_Realloc_cb realloc_function = NULL;
  87. int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
  88. wolfSSL_Free_cb ff,
  89. wolfSSL_Realloc_cb rf)
  90. {
  91. malloc_function = mf;
  92. free_function = ff;
  93. realloc_function = rf;
  94. return 0;
  95. }
  96. int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
  97. wolfSSL_Free_cb* ff,
  98. wolfSSL_Realloc_cb* rf)
  99. {
  100. if (mf) *mf = malloc_function;
  101. if (ff) *ff = free_function;
  102. if (rf) *rf = realloc_function;
  103. return 0;
  104. }
  105. #ifndef WOLFSSL_STATIC_MEMORY
  106. #ifdef WOLFSSL_CHECK_MEM_ZERO
  107. #ifndef WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN
  108. /* Number of entries in table of addresses to check. */
  109. #define WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN 256
  110. #endif
  111. /* Alignment to maintain when adding length to allocated pointer.
  112. * Intel x64 wants to use aligned loads of XMM registers.
  113. */
  114. #define MEM_ALIGN 16
  115. /* An address that is meant to be all zeros for its length. */
  116. typedef struct MemZero {
  117. /* Name of address to check. */
  118. const char* name;
  119. /* Address to check. */
  120. const void* addr;
  121. /* Length of data that must be zero. */
  122. size_t len;
  123. } MemZero;
  124. /* List of addresses to check. */
  125. static MemZero memZero[WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN];
  126. /* Next index to place address at.
  127. * -1 indicates uninitialized.
  128. * If nextIdx is equal to WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN then all entries
  129. * have been used.
  130. */
  131. static int nextIdx = -1;
  132. /* Mutex to protect modifying list of addresses to check. */
  133. static wolfSSL_Mutex zeroMutex;
  134. /* Initialize the table of addresses and the mutex.
  135. */
  136. void wc_MemZero_Init()
  137. {
  138. /* Clear the table to more easily see what is valid. */
  139. XMEMSET(memZero, 0, sizeof(memZero));
  140. /* Initialize mutex. */
  141. wc_InitMutex(&zeroMutex);
  142. /* Next index is first entry. */
  143. nextIdx = 0;
  144. }
  145. /* Free the mutex and check we have not any uncheck addresses.
  146. */
  147. void wc_MemZero_Free()
  148. {
  149. /* Free mutex. */
  150. wc_FreeMutex(&zeroMutex);
  151. /* Make sure we checked all addresses. */
  152. if (nextIdx > 0) {
  153. int i;
  154. fprintf(stderr, "[MEM_ZERO] Unseen: %d\n", nextIdx);
  155. for (i = 0; i < nextIdx; i++) {
  156. fprintf(stderr, " %s - %p:%ld\n", memZero[i].name, memZero[i].addr,
  157. memZero[i].len);
  158. }
  159. }
  160. /* Uninitialized value in next index. */
  161. nextIdx = -1;
  162. }
  163. /* Add an address to check.
  164. *
  165. * @param [in] name Name of address to check.
  166. * @param [in] addr Address that needs to be checked.
  167. * @param [in] len Length of data that must be zero.
  168. */
  169. void wc_MemZero_Add(const char* name, const void* addr, size_t len)
  170. {
  171. /* Initialize if not done. */
  172. if (nextIdx == -1) {
  173. wc_MemZero_Init();
  174. }
  175. /* Add an entry to the table while locked. */
  176. wc_LockMutex(&zeroMutex);
  177. if (nextIdx < WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN) {
  178. /* Fill in the next entry and update next index. */
  179. memZero[nextIdx].name = name;
  180. memZero[nextIdx].addr = addr;
  181. memZero[nextIdx].len = len;
  182. nextIdx++;
  183. }
  184. else {
  185. /* Abort when too many entries. */
  186. fprintf(stderr, "\n[MEM_ZERO] Too many addresses to check\n");
  187. fprintf(stderr, "[MEM_ZERO] WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN\n");
  188. abort();
  189. }
  190. wc_UnLockMutex(&zeroMutex);
  191. }
  192. /* Check the memory in the range of the address for memory that must be zero.
  193. *
  194. * @param [in] addr Start address of memory that is to be checked.
  195. * @param [in] len Length of data associated with address.
  196. */
  197. void wc_MemZero_Check(void* addr, size_t len)
  198. {
  199. int i;
  200. size_t j;
  201. wc_LockMutex(&zeroMutex);
  202. /* Look at each address for overlap with address passes in. */
  203. for (i = 0; i < nextIdx; i++) {
  204. if ((memZero[i].addr < addr) ||
  205. ((size_t)memZero[i].addr >= (size_t)addr + len)) {
  206. /* Check address not part of memory to check. */
  207. continue;
  208. }
  209. /* Address is in range of memory being freed - check each byte zero. */
  210. for (j = 0; j < memZero[i].len; j++) {
  211. if (((unsigned char*)memZero[i].addr)[j] != 0) {
  212. /* Byte not zero - abort! */
  213. fprintf(stderr, "\n[MEM_ZERO] %s:%p + %ld is not zero\n",
  214. memZero[i].name, memZero[i].addr, j);
  215. fprintf(stderr, "[MEM_ZERO] Checking %p:%ld\n", addr, len);
  216. abort();
  217. break;
  218. }
  219. }
  220. /* Update next index to write to. */
  221. nextIdx--;
  222. if (nextIdx > 0) {
  223. /* Remove entry. */
  224. XMEMCPY(memZero + i, memZero + i + 1,
  225. sizeof(MemZero) * (nextIdx - i));
  226. /* Clear out top to make it easier to see what is to be checked. */
  227. XMEMSET(&memZero[nextIdx], 0, sizeof(MemZero));
  228. }
  229. /* Need to check this index again with new data. */
  230. i--;
  231. }
  232. wc_UnLockMutex(&zeroMutex);
  233. }
  234. #endif /* WOLFSSL_CHECK_MEM_ZERO */
  235. #ifdef WOLFSSL_DEBUG_MEMORY
  236. void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line)
  237. #else
  238. void* wolfSSL_Malloc(size_t size)
  239. #endif
  240. {
  241. void* res = 0;
  242. #ifdef WOLFSSL_CHECK_MEM_ZERO
  243. /* Space for requested size. */
  244. size += MEM_ALIGN;
  245. #endif
  246. if (malloc_function) {
  247. #ifdef WOLFSSL_DEBUG_MEMORY
  248. res = malloc_function(size, func, line);
  249. #else
  250. res = malloc_function(size);
  251. #endif
  252. }
  253. else {
  254. #ifndef WOLFSSL_NO_MALLOC
  255. #ifdef WOLFSSL_TRAP_MALLOC_SZ
  256. if (size > WOLFSSL_TRAP_MALLOC_SZ) {
  257. WOLFSSL_MSG("Malloc too big!");
  258. return NULL;
  259. }
  260. #endif
  261. res = malloc(size);
  262. #else
  263. WOLFSSL_MSG("No malloc available");
  264. #endif
  265. }
  266. #ifdef WOLFSSL_CHECK_MEM_ZERO
  267. /* Restore size to requested value. */
  268. size -= MEM_ALIGN;
  269. if (res != NULL) {
  270. /* Place size at front of allocated data and move pointer passed it. */
  271. *(size_t*)res = size;
  272. res = ((unsigned char*)res) + MEM_ALIGN;
  273. }
  274. #endif
  275. #ifdef WOLFSSL_DEBUG_MEMORY
  276. #if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY)
  277. fprintf(stderr, "Alloc: %p -> %u at %s:%u\n", res, (word32)size, func, line);
  278. #else
  279. (void)func;
  280. (void)line;
  281. #endif
  282. #endif
  283. #ifdef WOLFSSL_MALLOC_CHECK
  284. if (res == NULL)
  285. WOLFSSL_MSG("wolfSSL_malloc failed");
  286. #endif
  287. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  288. if (res && --gMemFailCount == 0) {
  289. fprintf(stderr, "\n---FORCED MEM FAIL TEST---\n");
  290. if (free_function) {
  291. #ifdef WOLFSSL_DEBUG_MEMORY
  292. free_function(res, func, line);
  293. #else
  294. free_function(res);
  295. #endif
  296. }
  297. else {
  298. free(res); /* clear */
  299. }
  300. gMemFailCount = gMemFailCountSeed; /* reset */
  301. return NULL;
  302. }
  303. #endif
  304. return res;
  305. }
  306. #ifdef WOLFSSL_DEBUG_MEMORY
  307. void wolfSSL_Free(void *ptr, const char* func, unsigned int line)
  308. #else
  309. void wolfSSL_Free(void *ptr)
  310. #endif
  311. {
  312. #ifdef WOLFSSL_DEBUG_MEMORY
  313. #if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY)
  314. fprintf(stderr, "Free: %p at %s:%u\n", ptr, func, line);
  315. #else
  316. (void)func;
  317. (void)line;
  318. #endif
  319. #endif
  320. #ifdef WOLFSSL_CHECK_MEM_ZERO
  321. /* Move pointer back to originally allocated pointer. */
  322. ptr = ((unsigned char*)ptr) - MEM_ALIGN;
  323. /* Check that the pointer is zero where required. */
  324. wc_MemZero_Check(((unsigned char*)ptr) + MEM_ALIGN, *(size_t*)ptr);
  325. #endif
  326. if (free_function) {
  327. #ifdef WOLFSSL_DEBUG_MEMORY
  328. free_function(ptr, func, line);
  329. #else
  330. free_function(ptr);
  331. #endif
  332. }
  333. else {
  334. #ifndef WOLFSSL_NO_MALLOC
  335. free(ptr);
  336. #else
  337. WOLFSSL_MSG("No free available");
  338. #endif
  339. }
  340. }
  341. #ifdef WOLFSSL_DEBUG_MEMORY
  342. void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line)
  343. #else
  344. void* wolfSSL_Realloc(void *ptr, size_t size)
  345. #endif
  346. {
  347. #ifdef WOLFSSL_CHECK_MEM_ZERO
  348. /* Can't check data that has been freed during realloc.
  349. * Manually allocated new memory, copy data and free original pointer.
  350. */
  351. #ifdef WOLFSSL_DEBUG_MEMORY
  352. void* res = wolfSSL_Malloc(size, func, line);
  353. #else
  354. void* res = wolfSSL_Malloc(size);
  355. #endif
  356. if (ptr != NULL) {
  357. /* Copy the minimum of old and new size. */
  358. size_t copySize = *(size_t*)(((unsigned char*)ptr) - MEM_ALIGN);
  359. if (size < copySize) {
  360. copySize = size;
  361. }
  362. XMEMCPY(res, ptr, copySize);
  363. /* Dispose of old pointer. */
  364. #ifdef WOLFSSL_DEBUG_MEMORY
  365. wolfSSL_Free(ptr, func, line);
  366. #else
  367. wolfSSL_Free(ptr);
  368. #endif
  369. }
  370. /* Return new pointer with data copied into it. */
  371. return res;
  372. #else
  373. void* res = 0;
  374. if (realloc_function) {
  375. #ifdef WOLFSSL_DEBUG_MEMORY
  376. res = realloc_function(ptr, size, func, line);
  377. #else
  378. res = realloc_function(ptr, size);
  379. #endif
  380. }
  381. else {
  382. #ifndef WOLFSSL_NO_MALLOC
  383. res = realloc(ptr, size);
  384. #else
  385. WOLFSSL_MSG("No realloc available");
  386. #endif
  387. }
  388. return res;
  389. #endif
  390. }
  391. #endif /* WOLFSSL_STATIC_MEMORY */
  392. #ifdef WOLFSSL_STATIC_MEMORY
  393. struct wc_Memory {
  394. byte* buffer;
  395. struct wc_Memory* next;
  396. word32 sz;
  397. };
  398. /* returns amount of memory used on success. On error returns negative value
  399. wc_Memory** list is the list that new buckets are prepended to
  400. */
  401. static int create_memory_buckets(byte* buffer, word32 bufSz,
  402. word32 buckSz, word32 buckNum, wc_Memory** list) {
  403. word32 i;
  404. byte* pt = buffer;
  405. int ret = 0;
  406. word32 memSz = (word32)sizeof(wc_Memory);
  407. word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
  408. /* if not enough space available for bucket size then do not try */
  409. if (buckSz + memSz + padSz > bufSz) {
  410. return ret;
  411. }
  412. for (i = 0; i < buckNum; i++) {
  413. if ((buckSz + memSz + padSz) <= (bufSz - ret)) {
  414. /* create a new struct and set its values */
  415. wc_Memory* mem = (struct wc_Memory*)(pt);
  416. mem->sz = buckSz;
  417. mem->buffer = (byte*)pt + padSz + memSz;
  418. mem->next = NULL;
  419. /* add the newly created struct to front of list */
  420. if (*list == NULL) {
  421. *list = mem;
  422. } else {
  423. mem->next = *list;
  424. *list = mem;
  425. }
  426. /* advance pointer and keep track of memory used */
  427. ret += buckSz + padSz + memSz;
  428. pt += buckSz + padSz + memSz;
  429. }
  430. else {
  431. break; /* not enough space left for more buckets of this size */
  432. }
  433. }
  434. return ret;
  435. }
  436. int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap)
  437. {
  438. word32 wc_MemSz[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
  439. word32 wc_Dist[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
  440. if (heap == NULL) {
  441. return BAD_FUNC_ARG;
  442. }
  443. XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
  444. XMEMCPY(heap->sizeList, wc_MemSz, sizeof(wc_MemSz));
  445. XMEMCPY(heap->distList, wc_Dist, sizeof(wc_Dist));
  446. if (wc_InitMutex(&(heap->memory_mutex)) != 0) {
  447. WOLFSSL_MSG("Error creating heap memory mutex");
  448. return BAD_MUTEX_E;
  449. }
  450. return 0;
  451. }
  452. int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
  453. unsigned char* buf, unsigned int sz, int flag, int maxSz)
  454. {
  455. int ret;
  456. WOLFSSL_HEAP* heap;
  457. WOLFSSL_HEAP_HINT* hint;
  458. word32 idx = 0;
  459. if (pHint == NULL || buf == NULL) {
  460. return BAD_FUNC_ARG;
  461. }
  462. if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
  463. return BUFFER_E; /* not enough memory for structures */
  464. }
  465. /* check if hint has already been assigned */
  466. if (*pHint == NULL) {
  467. heap = (WOLFSSL_HEAP*)buf;
  468. idx += sizeof(WOLFSSL_HEAP);
  469. hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
  470. idx += sizeof(WOLFSSL_HEAP_HINT);
  471. ret = wolfSSL_init_memory_heap(heap);
  472. if (ret != 0) {
  473. return ret;
  474. }
  475. XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
  476. hint->memory = heap;
  477. }
  478. else {
  479. #ifdef WOLFSSL_HEAP_TEST
  480. /* do not load in memory if test has been set */
  481. if (heap == (void*)WOLFSSL_HEAP_TEST) {
  482. return 0;
  483. }
  484. #endif
  485. hint = (WOLFSSL_HEAP_HINT*)(*pHint);
  486. heap = hint->memory;
  487. }
  488. ret = wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap);
  489. if (ret != 1) {
  490. WOLFSSL_MSG("Error partitioning memory");
  491. return -1;
  492. }
  493. /* determine what max applies too */
  494. if ((flag & WOLFMEM_IO_POOL) || (flag & WOLFMEM_IO_POOL_FIXED)) {
  495. heap->maxIO = maxSz;
  496. }
  497. else { /* general memory used in handshakes */
  498. heap->maxHa = maxSz;
  499. }
  500. heap->flag |= flag;
  501. *pHint = hint;
  502. (void)maxSz;
  503. return 0;
  504. }
  505. int wolfSSL_load_static_memory(byte* buffer, word32 sz, int flag,
  506. WOLFSSL_HEAP* heap)
  507. {
  508. word32 ava = sz;
  509. byte* pt = buffer;
  510. int ret = 0;
  511. word32 memSz = (word32)sizeof(wc_Memory);
  512. word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
  513. WOLFSSL_ENTER("wolfSSL_load_static_memory");
  514. if (buffer == NULL) {
  515. return BAD_FUNC_ARG;
  516. }
  517. /* align pt */
  518. while ((wc_ptr_t)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
  519. *pt = 0x00;
  520. pt++;
  521. ava--;
  522. }
  523. #ifdef WOLFSSL_DEBUG_MEMORY
  524. fprintf(stderr, "Allocated %d bytes for static memory @ %p\n", ava, pt);
  525. #endif
  526. /* divide into chunks of memory and add them to available list */
  527. while (ava >= (heap->sizeList[0] + padSz + memSz)) {
  528. int i;
  529. /* creating only IO buffers from memory passed in, max TLS is 16k */
  530. if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
  531. if ((ret = create_memory_buckets(pt, ava,
  532. WOLFMEM_IO_SZ, 1, &(heap->io))) < 0) {
  533. WOLFSSL_LEAVE("wolfSSL_load_static_memory", ret);
  534. return ret;
  535. }
  536. /* check if no more room left for creating IO buffers */
  537. if (ret == 0) {
  538. break;
  539. }
  540. /* advance pointer in buffer for next buckets and keep track
  541. of how much memory is left available */
  542. pt += ret;
  543. ava -= ret;
  544. }
  545. else {
  546. /* start at largest and move to smaller buckets */
  547. for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
  548. if ((heap->sizeList[i] + padSz + memSz) <= ava) {
  549. if ((ret = create_memory_buckets(pt, ava, heap->sizeList[i],
  550. heap->distList[i], &(heap->ava[i]))) < 0) {
  551. WOLFSSL_LEAVE("wolfSSL_load_static_memory", ret);
  552. return ret;
  553. }
  554. /* advance pointer in buffer for next buckets and keep track
  555. of how much memory is left available */
  556. pt += ret;
  557. ava -= ret;
  558. }
  559. }
  560. }
  561. }
  562. return 1;
  563. }
  564. /* returns the size of management memory needed for each bucket.
  565. * This is memory that is used to keep track of and align memory buckets. */
  566. int wolfSSL_MemoryPaddingSz(void)
  567. {
  568. word32 memSz = (word32)sizeof(wc_Memory);
  569. word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
  570. return memSz + padSz;
  571. }
  572. /* Used to calculate memory size for optimum use with buckets.
  573. returns the suggested size rounded down to the nearest bucket. */
  574. int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag)
  575. {
  576. word32 bucketSz[WOLFMEM_MAX_BUCKETS] = {WOLFMEM_BUCKETS};
  577. word32 distList[WOLFMEM_MAX_BUCKETS] = {WOLFMEM_DIST};
  578. word32 ava = sz;
  579. byte* pt = buffer;
  580. word32 memSz = (word32)sizeof(wc_Memory);
  581. word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
  582. WOLFSSL_ENTER("wolfSSL_static_size");
  583. if (buffer == NULL) {
  584. return BAD_FUNC_ARG;
  585. }
  586. /* align pt */
  587. while ((wc_ptr_t)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
  588. pt++;
  589. ava--;
  590. }
  591. /* creating only IO buffers from memory passed in, max TLS is 16k */
  592. if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
  593. if (ava < (memSz + padSz + WOLFMEM_IO_SZ)) {
  594. return 0; /* not enough room for even one bucket */
  595. }
  596. ava = ava % (memSz + padSz + WOLFMEM_IO_SZ);
  597. }
  598. else {
  599. int i, k;
  600. if (ava < (bucketSz[0] + padSz + memSz)) {
  601. return 0; /* not enough room for even one bucket */
  602. }
  603. while ((ava >= (bucketSz[0] + padSz + memSz)) && (ava > 0)) {
  604. /* start at largest and move to smaller buckets */
  605. for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
  606. for (k = distList[i]; k > 0; k--) {
  607. if ((bucketSz[i] + padSz + memSz) <= ava) {
  608. ava -= bucketSz[i] + padSz + memSz;
  609. }
  610. }
  611. }
  612. }
  613. }
  614. return sz - ava; /* round down */
  615. }
  616. int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
  617. {
  618. WOLFSSL_MSG("Freeing fixed IO buffer");
  619. /* check if fixed buffer was set */
  620. if (*io == NULL) {
  621. return 1;
  622. }
  623. if (heap == NULL) {
  624. WOLFSSL_MSG("No heap to return fixed IO too");
  625. }
  626. else {
  627. /* put IO buffer back into IO pool */
  628. (*io)->next = heap->io;
  629. heap->io = *io;
  630. *io = NULL;
  631. }
  632. return 1;
  633. }
  634. int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
  635. {
  636. WOLFSSL_MSG("Setting fixed IO for SSL");
  637. if (heap == NULL) {
  638. return MEMORY_E;
  639. }
  640. *io = heap->io;
  641. if (*io != NULL) {
  642. heap->io = (*io)->next;
  643. (*io)->next = NULL;
  644. }
  645. else { /* failed to grab an IO buffer */
  646. return 0;
  647. }
  648. return 1;
  649. }
  650. int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
  651. {
  652. word32 i;
  653. wc_Memory* pt;
  654. XMEMSET(stats, 0, sizeof(WOLFSSL_MEM_STATS));
  655. stats->totalAlloc = heap->alloc;
  656. stats->totalFr = heap->frAlc;
  657. stats->curAlloc = stats->totalAlloc - stats->totalFr;
  658. stats->maxHa = heap->maxHa;
  659. stats->maxIO = heap->maxIO;
  660. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  661. stats->blockSz[i] = heap->sizeList[i];
  662. for (pt = heap->ava[i]; pt != NULL; pt = pt->next) {
  663. stats->avaBlock[i] += 1;
  664. }
  665. }
  666. for (pt = heap->io; pt != NULL; pt = pt->next) {
  667. stats->avaIO++;
  668. }
  669. stats->flag = heap->flag; /* flag used */
  670. return 1;
  671. }
  672. #ifdef WOLFSSL_DEBUG_MEMORY
  673. void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
  674. #else
  675. void* wolfSSL_Malloc(size_t size, void* heap, int type)
  676. #endif
  677. {
  678. void* res = 0;
  679. wc_Memory* pt = NULL;
  680. int i;
  681. /* check for testing heap hint was set */
  682. #ifdef WOLFSSL_HEAP_TEST
  683. if (heap == (void*)WOLFSSL_HEAP_TEST) {
  684. return malloc(size);
  685. }
  686. #endif
  687. /* if no heap hint then use dynamic memory*/
  688. if (heap == NULL) {
  689. #ifdef WOLFSSL_HEAP_TEST
  690. /* allow using malloc for creating ctx and method */
  691. if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
  692. type == DYNAMIC_TYPE_CERT_MANAGER) {
  693. WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method");
  694. res = malloc(size);
  695. }
  696. else {
  697. WOLFSSL_MSG("ERROR null heap hint passed into XMALLOC");
  698. res = NULL;
  699. }
  700. #else
  701. #ifndef WOLFSSL_NO_MALLOC
  702. #ifdef FREERTOS
  703. res = pvPortMalloc(size);
  704. #elif defined(WOLFSSL_EMBOS)
  705. res = OS_HEAP_malloc(size);
  706. #else
  707. res = malloc(size);
  708. #endif
  709. #ifdef WOLFSSL_DEBUG_MEMORY
  710. fprintf(stderr, "Alloc: %p -> %u at %s:%d\n", res, (word32)size, func, line);
  711. #endif
  712. #else
  713. WOLFSSL_MSG("No heap hint found to use and no malloc");
  714. #ifdef WOLFSSL_DEBUG_MEMORY
  715. fprintf(stderr, "ERROR: at %s:%d\n", func, line);
  716. #endif
  717. #endif /* WOLFSSL_NO_MALLOC */
  718. #endif /* WOLFSSL_HEAP_TEST */
  719. }
  720. else {
  721. WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
  722. WOLFSSL_HEAP* mem = hint->memory;
  723. if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
  724. WOLFSSL_MSG("Bad memory_mutex lock");
  725. return NULL;
  726. }
  727. /* case of using fixed IO buffers */
  728. if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
  729. (type == DYNAMIC_TYPE_OUT_BUFFER ||
  730. type == DYNAMIC_TYPE_IN_BUFFER)) {
  731. if (type == DYNAMIC_TYPE_OUT_BUFFER) {
  732. pt = hint->outBuf;
  733. }
  734. if (type == DYNAMIC_TYPE_IN_BUFFER) {
  735. pt = hint->inBuf;
  736. }
  737. }
  738. else {
  739. /* check if using IO pool flag */
  740. if (mem->flag & WOLFMEM_IO_POOL &&
  741. (type == DYNAMIC_TYPE_OUT_BUFFER ||
  742. type == DYNAMIC_TYPE_IN_BUFFER)) {
  743. if (mem->io != NULL) {
  744. pt = mem->io;
  745. mem->io = pt->next;
  746. }
  747. }
  748. /* general static memory */
  749. if (pt == NULL) {
  750. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  751. if ((word32)size <= mem->sizeList[i]) {
  752. if (mem->ava[i] != NULL) {
  753. pt = mem->ava[i];
  754. mem->ava[i] = pt->next;
  755. break;
  756. }
  757. #ifdef WOLFSSL_DEBUG_STATIC_MEMORY
  758. else {
  759. fprintf(stderr, "Size: %ld, Empty: %d\n", size,
  760. mem->sizeList[i]);
  761. }
  762. #endif
  763. }
  764. }
  765. }
  766. }
  767. if (pt != NULL) {
  768. mem->inUse += pt->sz;
  769. mem->alloc += 1;
  770. res = pt->buffer;
  771. #ifdef WOLFSSL_DEBUG_MEMORY
  772. fprintf(stderr, "Alloc: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
  773. #endif
  774. /* keep track of connection statistics if flag is set */
  775. if (mem->flag & WOLFMEM_TRACK_STATS) {
  776. WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
  777. if (stats != NULL) {
  778. stats->curMem += pt->sz;
  779. if (stats->peakMem < stats->curMem) {
  780. stats->peakMem = stats->curMem;
  781. }
  782. stats->curAlloc++;
  783. if (stats->peakAlloc < stats->curAlloc) {
  784. stats->peakAlloc = stats->curAlloc;
  785. }
  786. stats->totalAlloc++;
  787. }
  788. }
  789. }
  790. else {
  791. WOLFSSL_MSG("ERROR ran out of static memory");
  792. #ifdef WOLFSSL_DEBUG_MEMORY
  793. fprintf(stderr, "Looking for %lu bytes at %s:%d\n", size, func, line);
  794. #endif
  795. }
  796. wc_UnLockMutex(&(mem->memory_mutex));
  797. }
  798. #ifdef WOLFSSL_MALLOC_CHECK
  799. if ((wc_ptr_t)res % WOLFSSL_STATIC_ALIGN) {
  800. WOLFSSL_MSG("ERROR memory is not aligned");
  801. res = NULL;
  802. }
  803. #endif
  804. (void)i;
  805. (void)pt;
  806. (void)type;
  807. return res;
  808. }
  809. #ifdef WOLFSSL_DEBUG_MEMORY
  810. void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line)
  811. #else
  812. void wolfSSL_Free(void *ptr, void* heap, int type)
  813. #endif
  814. {
  815. int i;
  816. wc_Memory* pt;
  817. if (ptr) {
  818. /* check for testing heap hint was set */
  819. #ifdef WOLFSSL_HEAP_TEST
  820. if (heap == (void*)WOLFSSL_HEAP_TEST) {
  821. #ifdef WOLFSSL_DEBUG_MEMORY
  822. fprintf(stderr, "Free: %p at %s:%d\n", pt, func, line);
  823. #endif
  824. return free(ptr);
  825. }
  826. #endif
  827. if (heap == NULL) {
  828. #ifdef WOLFSSL_HEAP_TEST
  829. /* allow using malloc for creating ctx and method */
  830. if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
  831. type == DYNAMIC_TYPE_CERT_MANAGER) {
  832. WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method");
  833. }
  834. else {
  835. WOLFSSL_MSG("ERROR null heap hint passed into XFREE");
  836. }
  837. #endif
  838. #ifndef WOLFSSL_NO_MALLOC
  839. #ifdef FREERTOS
  840. vPortFree(ptr);
  841. #elif defined(WOLFSSL_EMBOS)
  842. OS_HEAP_free(ptr);
  843. #else
  844. free(ptr);
  845. #endif
  846. #else
  847. WOLFSSL_MSG("Error trying to call free when turned off");
  848. #endif /* WOLFSSL_NO_MALLOC */
  849. }
  850. else {
  851. WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
  852. WOLFSSL_HEAP* mem = hint->memory;
  853. word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
  854. /* get memory struct and add it to available list */
  855. pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz);
  856. if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
  857. WOLFSSL_MSG("Bad memory_mutex lock");
  858. return;
  859. }
  860. /* case of using fixed IO buffers */
  861. if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
  862. (type == DYNAMIC_TYPE_OUT_BUFFER ||
  863. type == DYNAMIC_TYPE_IN_BUFFER)) {
  864. /* fixed IO pools are free'd at the end of SSL lifetime
  865. using FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io) */
  866. }
  867. else if (mem->flag & WOLFMEM_IO_POOL && pt->sz == WOLFMEM_IO_SZ &&
  868. (type == DYNAMIC_TYPE_OUT_BUFFER ||
  869. type == DYNAMIC_TYPE_IN_BUFFER)) {
  870. pt->next = mem->io;
  871. mem->io = pt;
  872. }
  873. else { /* general memory free */
  874. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  875. if (pt->sz == mem->sizeList[i]) {
  876. pt->next = mem->ava[i];
  877. mem->ava[i] = pt;
  878. break;
  879. }
  880. }
  881. }
  882. mem->inUse -= pt->sz;
  883. mem->frAlc += 1;
  884. #ifdef WOLFSSL_DEBUG_MEMORY
  885. fprintf(stderr, "Free: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
  886. #endif
  887. /* keep track of connection statistics if flag is set */
  888. if (mem->flag & WOLFMEM_TRACK_STATS) {
  889. WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
  890. if (stats != NULL) {
  891. /* avoid under flow */
  892. if (stats->curMem > pt->sz) {
  893. stats->curMem -= pt->sz;
  894. }
  895. else {
  896. stats->curMem = 0;
  897. }
  898. if (stats->curAlloc > 0) {
  899. stats->curAlloc--;
  900. }
  901. stats->totalFr++;
  902. }
  903. }
  904. wc_UnLockMutex(&(mem->memory_mutex));
  905. }
  906. }
  907. (void)i;
  908. (void)pt;
  909. (void)type;
  910. }
  911. #ifdef WOLFSSL_DEBUG_MEMORY
  912. void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line)
  913. #else
  914. void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
  915. #endif
  916. {
  917. void* res = 0;
  918. wc_Memory* pt = NULL;
  919. word32 prvSz;
  920. int i;
  921. /* check for testing heap hint was set */
  922. #ifdef WOLFSSL_HEAP_TEST
  923. if (heap == (void*)WOLFSSL_HEAP_TEST) {
  924. return realloc(ptr, size);
  925. }
  926. #endif
  927. if (heap == NULL) {
  928. #ifdef WOLFSSL_HEAP_TEST
  929. WOLFSSL_MSG("ERROR null heap hint passed in to XREALLOC");
  930. #endif
  931. #ifndef WOLFSSL_NO_MALLOC
  932. res = realloc(ptr, size);
  933. #else
  934. WOLFSSL_MSG("NO heap found to use for realloc");
  935. #endif /* WOLFSSL_NO_MALLOC */
  936. }
  937. else {
  938. WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
  939. WOLFSSL_HEAP* mem = hint->memory;
  940. word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
  941. if (ptr == NULL) {
  942. #ifdef WOLFSSL_DEBUG_MEMORY
  943. return wolfSSL_Malloc(size, heap, type, func, line);
  944. #else
  945. return wolfSSL_Malloc(size, heap, type);
  946. #endif
  947. }
  948. if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
  949. WOLFSSL_MSG("Bad memory_mutex lock");
  950. return NULL;
  951. }
  952. /* case of using fixed IO buffers or IO pool */
  953. if (((mem->flag & WOLFMEM_IO_POOL)||(mem->flag & WOLFMEM_IO_POOL_FIXED))
  954. && (type == DYNAMIC_TYPE_OUT_BUFFER ||
  955. type == DYNAMIC_TYPE_IN_BUFFER)) {
  956. /* no realloc, is fixed size */
  957. pt = (wc_Memory*)((byte*)ptr - padSz - sizeof(wc_Memory));
  958. if (pt->sz < size) {
  959. WOLFSSL_MSG("Error IO memory was not large enough");
  960. res = NULL; /* return NULL in error case */
  961. }
  962. res = pt->buffer;
  963. }
  964. else {
  965. /* general memory */
  966. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  967. if ((word32)size <= mem->sizeList[i]) {
  968. if (mem->ava[i] != NULL) {
  969. pt = mem->ava[i];
  970. mem->ava[i] = pt->next;
  971. break;
  972. }
  973. }
  974. }
  975. if (pt != NULL && res == NULL) {
  976. res = pt->buffer;
  977. /* copy over original information and free ptr */
  978. prvSz = ((wc_Memory*)((byte*)ptr - padSz -
  979. sizeof(wc_Memory)))->sz;
  980. prvSz = (prvSz > pt->sz)? pt->sz: prvSz;
  981. XMEMCPY(pt->buffer, ptr, prvSz);
  982. mem->inUse += pt->sz;
  983. mem->alloc += 1;
  984. /* free memory that was previously being used */
  985. wc_UnLockMutex(&(mem->memory_mutex));
  986. wolfSSL_Free(ptr, heap, type
  987. #ifdef WOLFSSL_DEBUG_MEMORY
  988. , func, line
  989. #endif
  990. );
  991. if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
  992. WOLFSSL_MSG("Bad memory_mutex lock");
  993. return NULL;
  994. }
  995. }
  996. }
  997. wc_UnLockMutex(&(mem->memory_mutex));
  998. }
  999. #ifdef WOLFSSL_MALLOC_CHECK
  1000. if ((wc_ptr_t)res % WOLFSSL_STATIC_ALIGN) {
  1001. WOLFSSL_MSG("ERROR memory is not aligned");
  1002. res = NULL;
  1003. }
  1004. #endif
  1005. (void)i;
  1006. (void)pt;
  1007. (void)type;
  1008. return res;
  1009. }
  1010. #endif /* WOLFSSL_STATIC_MEMORY */
  1011. #endif /* USE_WOLFSSL_MEMORY */
  1012. #ifdef HAVE_IO_POOL
  1013. /* Example for user io pool, shared build may need definitions in lib proper */
  1014. #include <wolfssl/wolfcrypt/types.h>
  1015. #include <stdlib.h>
  1016. #ifndef HAVE_THREAD_LS
  1017. #error "Oops, simple I/O pool example needs thread local storage"
  1018. #endif
  1019. /* allow simple per thread in and out pools */
  1020. /* use 17k size since max record size is 16k plus overhead */
  1021. static THREAD_LS_T byte pool_in[17*1024];
  1022. static THREAD_LS_T byte pool_out[17*1024];
  1023. void* XMALLOC(size_t n, void* heap, int type)
  1024. {
  1025. (void)heap;
  1026. if (type == DYNAMIC_TYPE_IN_BUFFER) {
  1027. if (n < sizeof(pool_in))
  1028. return pool_in;
  1029. else
  1030. return NULL;
  1031. }
  1032. if (type == DYNAMIC_TYPE_OUT_BUFFER) {
  1033. if (n < sizeof(pool_out))
  1034. return pool_out;
  1035. else
  1036. return NULL;
  1037. }
  1038. return malloc(n);
  1039. }
  1040. void* XREALLOC(void *p, size_t n, void* heap, int type)
  1041. {
  1042. (void)heap;
  1043. if (type == DYNAMIC_TYPE_IN_BUFFER) {
  1044. if (n < sizeof(pool_in))
  1045. return pool_in;
  1046. else
  1047. return NULL;
  1048. }
  1049. if (type == DYNAMIC_TYPE_OUT_BUFFER) {
  1050. if (n < sizeof(pool_out))
  1051. return pool_out;
  1052. else
  1053. return NULL;
  1054. }
  1055. return realloc(p, n);
  1056. }
  1057. void XFREE(void *p, void* heap, int type)
  1058. {
  1059. (void)heap;
  1060. if (type == DYNAMIC_TYPE_IN_BUFFER)
  1061. return; /* do nothing, static pool */
  1062. if (type == DYNAMIC_TYPE_OUT_BUFFER)
  1063. return; /* do nothing, static pool */
  1064. free(p);
  1065. }
  1066. #endif /* HAVE_IO_POOL */
  1067. #ifdef WOLFSSL_MEMORY_LOG
  1068. void *xmalloc(size_t n, void* heap, int type, const char* func,
  1069. const char* file, unsigned int line)
  1070. {
  1071. void* p = NULL;
  1072. word32* p32;
  1073. if (malloc_function)
  1074. p32 = malloc_function(n + sizeof(word32) * 4);
  1075. else
  1076. p32 = malloc(n + sizeof(word32) * 4);
  1077. if (p32 != NULL) {
  1078. p32[0] = (word32)n;
  1079. p = (void*)(p32 + 4);
  1080. fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%u\n", p, (word32)n,
  1081. type, func, file, line);
  1082. }
  1083. (void)heap;
  1084. return p;
  1085. }
  1086. void *xrealloc(void *p, size_t n, void* heap, int type, const char* func,
  1087. const char* file, unsigned int line)
  1088. {
  1089. void* newp = NULL;
  1090. word32* p32;
  1091. word32* oldp32 = NULL;
  1092. word32 oldLen;
  1093. if (p != NULL) {
  1094. oldp32 = (word32*)p;
  1095. oldp32 -= 4;
  1096. oldLen = oldp32[0];
  1097. }
  1098. if (realloc_function)
  1099. p32 = realloc_function(oldp32, n + sizeof(word32) * 4);
  1100. else
  1101. p32 = realloc(oldp32, n + sizeof(word32) * 4);
  1102. if (p32 != NULL) {
  1103. p32[0] = (word32)n;
  1104. newp = (void*)(p32 + 4);
  1105. if (p != NULL) {
  1106. fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%u\n", p, oldLen,
  1107. type, func, file, line);
  1108. }
  1109. fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%u\n", newp, (word32)n,
  1110. type, func, file, line);
  1111. }
  1112. (void)heap;
  1113. return newp;
  1114. }
  1115. void xfree(void *p, void* heap, int type, const char* func, const char* file,
  1116. unsigned int line)
  1117. {
  1118. word32* p32 = (word32*)p;
  1119. if (p != NULL) {
  1120. p32 -= 4;
  1121. fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%u\n", p, p32[0], type,
  1122. func, file, line);
  1123. if (free_function)
  1124. free_function(p32);
  1125. else
  1126. free(p32);
  1127. }
  1128. (void)heap;
  1129. }
  1130. #endif /* WOLFSSL_MEMORY_LOG */
  1131. #ifdef WOLFSSL_STACK_LOG
  1132. /* Note: this code only works with GCC using -finstrument-functions. */
  1133. void __attribute__((no_instrument_function))
  1134. __cyg_profile_func_enter(void *func, void *caller)
  1135. {
  1136. register void* sp asm("sp");
  1137. fprintf(stderr, "ENTER: %016lx %p\n", (unsigned long)(wc_ptr_t)func, sp);
  1138. (void)caller;
  1139. }
  1140. void __attribute__((no_instrument_function))
  1141. __cyg_profile_func_exit(void *func, void *caller)
  1142. {
  1143. register void* sp asm("sp");
  1144. fprintf(stderr, "EXIT: %016lx %p\n", (unsigned long)(wc_ptr_t)func, sp);
  1145. (void)caller;
  1146. }
  1147. #endif
  1148. #ifdef WOLFSSL_LINUXKM
  1149. #include "../../linuxkm/linuxkm_memory.c"
  1150. #endif