forked from wolfSSL/wolfssl
Added support for static memory with wolfCrypt. Adds new "wc_LoadStaticMemory" function and moves "wolfSSL_init_memory_heap" into wolfCrypt layer. Enhanced wolfCrypt test and benchmark to use the static memory tool if enabled. Added support for static memory with "WOLFSSL_DEBUG_MEMORY" defined. Fixed issue with have-iopool and XMALLOC/XFREE. Added check to prevent using WOLFSSL_STATIC_MEMORY with HAVE_IO_POOL, XMALLOC_USER or NO_WOLFSSL_MEMORY defined.
This commit is contained in:
@ -659,7 +659,7 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args)
|
|||||||
err_sys("unable to load static memory and create ctx");
|
err_sys("unable to load static memory and create ctx");
|
||||||
#else
|
#else
|
||||||
ctx = SSL_CTX_new(method(NULL));
|
ctx = SSL_CTX_new(method(NULL));
|
||||||
#endif
|
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||||
if (ctx == NULL)
|
if (ctx == NULL)
|
||||||
err_sys("unable to get ctx");
|
err_sys("unable to get ctx");
|
||||||
|
|
||||||
|
51
src/ssl.c
51
src/ssl.c
@ -638,32 +638,10 @@ int wolfSSL_GetObjectSize(void)
|
|||||||
return sizeof(WOLFSSL);
|
return sizeof(WOLFSSL);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef WOLFSSL_STATIC_MEMORY
|
#ifdef WOLFSSL_STATIC_MEMORY
|
||||||
|
|
||||||
|
|
||||||
int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap)
|
|
||||||
{
|
|
||||||
word32 wc_MemSz[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
|
|
||||||
word32 wc_Dist[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
|
|
||||||
|
|
||||||
if (heap == NULL) {
|
|
||||||
return BAD_FUNC_ARG;
|
|
||||||
}
|
|
||||||
|
|
||||||
XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
|
|
||||||
|
|
||||||
XMEMCPY(heap->sizeList, wc_MemSz, sizeof(wc_MemSz));
|
|
||||||
XMEMCPY(heap->distList, wc_Dist, sizeof(wc_Dist));
|
|
||||||
|
|
||||||
if (InitMutex(&(heap->memory_mutex)) != 0) {
|
|
||||||
WOLFSSL_MSG("Error creating heap memory mutex");
|
|
||||||
return BAD_MUTEX_E;
|
|
||||||
}
|
|
||||||
|
|
||||||
return SSL_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method,
|
int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method,
|
||||||
unsigned char* buf, unsigned int sz,
|
unsigned char* buf, unsigned int sz,
|
||||||
int flag, int max)
|
int flag, int max)
|
||||||
@ -680,34 +658,23 @@ int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method
|
|||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*ctx == NULL) {
|
if (*ctx == NULL || (*ctx)->heap == NULL) {
|
||||||
if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
|
if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
|
||||||
return BUFFER_E; /* not enough memory for structures */
|
return BUFFER_E; /* not enough memory for structures */
|
||||||
}
|
}
|
||||||
heap = (WOLFSSL_HEAP*)buf;
|
heap = (WOLFSSL_HEAP*)buf;
|
||||||
idx += sizeof(WOLFSSL_HEAP);
|
idx += sizeof(WOLFSSL_HEAP);
|
||||||
if (wolfSSL_init_memory_heap(heap) != SSL_SUCCESS) {
|
if (wolfSSL_init_memory_heap(heap) != 0) {
|
||||||
return SSL_FAILURE;
|
return SSL_FAILURE;
|
||||||
}
|
}
|
||||||
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
||||||
idx += sizeof(WOLFSSL_HEAP_HINT);
|
idx += sizeof(WOLFSSL_HEAP_HINT);
|
||||||
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
||||||
hint->memory = heap;
|
hint->memory = heap;
|
||||||
}
|
|
||||||
else if ((*ctx)->heap == NULL) {
|
if (*ctx && (*ctx)->heap == NULL) {
|
||||||
if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
|
(*ctx)->heap = (void*)hint;
|
||||||
return BUFFER_E; /* not enough memory for structures */
|
|
||||||
}
|
}
|
||||||
heap = (WOLFSSL_HEAP*)buf;
|
|
||||||
idx += sizeof(WOLFSSL_HEAP);
|
|
||||||
if (wolfSSL_init_memory_heap(heap) != SSL_SUCCESS) {
|
|
||||||
return SSL_FAILURE;
|
|
||||||
}
|
|
||||||
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
|
||||||
idx += sizeof(WOLFSSL_HEAP_HINT);
|
|
||||||
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
|
||||||
hint->memory = heap;
|
|
||||||
(*ctx)->heap = (void*)hint;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#ifdef WOLFSSL_HEAP_TEST
|
#ifdef WOLFSSL_HEAP_TEST
|
||||||
@ -767,7 +734,7 @@ int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (ssl->heap)? 1 : 0;
|
return (ssl->heap) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -786,7 +753,7 @@ int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (ctx->heap)? 1 : 0;
|
return (ctx->heap) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* WOLFSSL_STATIC_MEMORY */
|
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||||
|
@ -32,6 +32,13 @@
|
|||||||
/* Macro to disable benchmark */
|
/* Macro to disable benchmark */
|
||||||
#ifndef NO_CRYPT_BENCHMARK
|
#ifndef NO_CRYPT_BENCHMARK
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_STATIC_MEMORY
|
||||||
|
#include <wolfssl/wolfcrypt/memory.h>
|
||||||
|
static WOLFSSL_HEAP_HINT* HEAP_HINT;
|
||||||
|
#else
|
||||||
|
#define HEAP_HINT NULL
|
||||||
|
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#ifdef FREESCALE_MQX
|
#ifdef FREESCALE_MQX
|
||||||
@ -85,8 +92,8 @@
|
|||||||
|
|
||||||
#ifdef WOLFSSL_ASYNC_CRYPT
|
#ifdef WOLFSSL_ASYNC_CRYPT
|
||||||
#include <wolfssl/wolfcrypt/async.h>
|
#include <wolfssl/wolfcrypt/async.h>
|
||||||
static int devId = INVALID_DEVID;
|
|
||||||
#endif
|
#endif
|
||||||
|
static int devId = INVALID_DEVID;
|
||||||
|
|
||||||
#ifdef HAVE_WNR
|
#ifdef HAVE_WNR
|
||||||
const char* wnrConfigFile = "wnr-example.conf";
|
const char* wnrConfigFile = "wnr-example.conf";
|
||||||
@ -249,6 +256,20 @@ int benchmark_test(void *args)
|
|||||||
(void)args;
|
(void)args;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_STATIC_MEMORY
|
||||||
|
#ifdef BENCH_EMBEDDED
|
||||||
|
byte memory[50000];
|
||||||
|
#else
|
||||||
|
byte memory[400000];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (wc_LoadStaticMemory(&HEAP_HINT, memory, sizeof(memory),
|
||||||
|
WOLFMEM_GENERAL, 1) != 0) {
|
||||||
|
printf("unable to load static memory");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
||||||
InitMemoryTracker();
|
InitMemoryTracker();
|
||||||
#endif
|
#endif
|
||||||
@ -1406,7 +1427,7 @@ void bench_rsa(void)
|
|||||||
#error "need a cert buffer size"
|
#error "need a cert buffer size"
|
||||||
#endif /* USE_CERT_BUFFERS */
|
#endif /* USE_CERT_BUFFERS */
|
||||||
|
|
||||||
if ((ret = wc_InitRsaKey(&rsaKey, 0)) < 0) {
|
if ((ret = wc_InitRsaKey(&rsaKey, HEAP_HINT)) < 0) {
|
||||||
printf("InitRsaKey failed! %d\n", ret);
|
printf("InitRsaKey failed! %d\n", ret);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1440,7 +1461,7 @@ void bench_rsa(void)
|
|||||||
wc_RsaSetRNG(&rsaKey, &rng);
|
wc_RsaSetRNG(&rsaKey, &rng);
|
||||||
#endif
|
#endif
|
||||||
start = current_time(1);
|
start = current_time(1);
|
||||||
|
|
||||||
/* capture resulting encrypt length */
|
/* capture resulting encrypt length */
|
||||||
idx = ret;
|
idx = ret;
|
||||||
|
|
||||||
@ -1509,7 +1530,7 @@ void bench_rsa_async(void)
|
|||||||
/* init events and keys */
|
/* init events and keys */
|
||||||
for (i = 0; i < WOLF_ASYNC_MAX_PENDING; i++) {
|
for (i = 0; i < WOLF_ASYNC_MAX_PENDING; i++) {
|
||||||
/* setup an async context for each key */
|
/* setup an async context for each key */
|
||||||
if ((ret = wc_InitRsaKey_ex(&rsaKey[i], 0, devId)) < 0) {
|
if ((ret = wc_InitRsaKey_ex(&rsaKey[i], HEAP_HINT, devId)) < 0) {
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
#ifdef WC_RSA_BLINDING
|
#ifdef WC_RSA_BLINDING
|
||||||
@ -1538,7 +1559,7 @@ void bench_rsa_async(void)
|
|||||||
|
|
||||||
/* while free pending slots in queue, submit RSA operations */
|
/* while free pending slots in queue, submit RSA operations */
|
||||||
for (evtNum = 0; evtNum < WOLF_ASYNC_MAX_PENDING; evtNum++) {
|
for (evtNum = 0; evtNum < WOLF_ASYNC_MAX_PENDING; evtNum++) {
|
||||||
if (events[evtNum].done || (events[evtNum].pending == 0 &&
|
if (events[evtNum].done || (events[evtNum].pending == 0 &&
|
||||||
(i + asyncPend) < ntimes))
|
(i + asyncPend) < ntimes))
|
||||||
{
|
{
|
||||||
/* check for event error */
|
/* check for event error */
|
||||||
@ -1592,7 +1613,7 @@ void bench_rsa_async(void)
|
|||||||
|
|
||||||
/* begin private async RSA */
|
/* begin private async RSA */
|
||||||
start = current_time(1);
|
start = current_time(1);
|
||||||
|
|
||||||
/* capture resulting encrypt length */
|
/* capture resulting encrypt length */
|
||||||
idx = sizeof(enc); /* fixed at 2048 bit */
|
idx = sizeof(enc); /* fixed at 2048 bit */
|
||||||
|
|
||||||
@ -1602,7 +1623,7 @@ void bench_rsa_async(void)
|
|||||||
|
|
||||||
/* while free pending slots in queue, submit RSA operations */
|
/* while free pending slots in queue, submit RSA operations */
|
||||||
for (evtNum = 0; evtNum < WOLF_ASYNC_MAX_PENDING; evtNum++) {
|
for (evtNum = 0; evtNum < WOLF_ASYNC_MAX_PENDING; evtNum++) {
|
||||||
if (events[evtNum].done || (events[evtNum].pending == 0 &&
|
if (events[evtNum].done || (events[evtNum].pending == 0 &&
|
||||||
(i + asyncPend) < ntimes))
|
(i + asyncPend) < ntimes))
|
||||||
{
|
{
|
||||||
/* check for event error */
|
/* check for event error */
|
||||||
@ -1772,7 +1793,7 @@ void bench_rsaKeyGen(void)
|
|||||||
start = current_time(1);
|
start = current_time(1);
|
||||||
|
|
||||||
for(i = 0; i < genTimes; i++) {
|
for(i = 0; i < genTimes; i++) {
|
||||||
wc_InitRsaKey(&genKey, 0);
|
wc_InitRsaKey(&genKey, HEAP_HINT);
|
||||||
wc_MakeRsaKey(&genKey, 1024, 65537, &rng);
|
wc_MakeRsaKey(&genKey, 1024, 65537, &rng);
|
||||||
wc_FreeRsaKey(&genKey);
|
wc_FreeRsaKey(&genKey);
|
||||||
}
|
}
|
||||||
@ -1788,7 +1809,7 @@ void bench_rsaKeyGen(void)
|
|||||||
start = current_time(1);
|
start = current_time(1);
|
||||||
|
|
||||||
for(i = 0; i < genTimes; i++) {
|
for(i = 0; i < genTimes; i++) {
|
||||||
wc_InitRsaKey(&genKey, 0);
|
wc_InitRsaKey(&genKey, HEAP_HINT);
|
||||||
wc_MakeRsaKey(&genKey, 2048, 65537, &rng);
|
wc_MakeRsaKey(&genKey, 2048, 65537, &rng);
|
||||||
wc_FreeRsaKey(&genKey);
|
wc_FreeRsaKey(&genKey);
|
||||||
}
|
}
|
||||||
@ -2045,7 +2066,7 @@ void bench_eccKeyGen(void)
|
|||||||
start = current_time(1);
|
start = current_time(1);
|
||||||
|
|
||||||
for(i = 0; i < genTimes; i++) {
|
for(i = 0; i < genTimes; i++) {
|
||||||
wc_ecc_init(&genKey);
|
wc_ecc_init_ex(&genKey, HEAP_HINT, devId);
|
||||||
wc_ecc_make_key(&rng, 32, &genKey);
|
wc_ecc_make_key(&rng, 32, &genKey);
|
||||||
wc_ecc_free(&genKey);
|
wc_ecc_free(&genKey);
|
||||||
}
|
}
|
||||||
@ -2071,8 +2092,8 @@ void bench_eccKeyAgree(void)
|
|||||||
byte digest[32];
|
byte digest[32];
|
||||||
word32 x = 0;
|
word32 x = 0;
|
||||||
|
|
||||||
wc_ecc_init(&genKey);
|
wc_ecc_init_ex(&genKey, HEAP_HINT, devId);
|
||||||
wc_ecc_init(&genKey2);
|
wc_ecc_init_ex(&genKey2, HEAP_HINT, devId);
|
||||||
|
|
||||||
ret = wc_ecc_make_key(&rng, 32, &genKey);
|
ret = wc_ecc_make_key(&rng, 32, &genKey);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
@ -2158,8 +2179,8 @@ void bench_eccEncrypt(void)
|
|||||||
int ret, i;
|
int ret, i;
|
||||||
double start, total, each, milliEach;
|
double start, total, each, milliEach;
|
||||||
|
|
||||||
wc_ecc_init(&userA);
|
wc_ecc_init_ex(&userA, HEAP_HINT, devId);
|
||||||
wc_ecc_init(&userB);
|
wc_ecc_init_ex(&userB, HEAP_HINT, devId);
|
||||||
|
|
||||||
wc_ecc_make_key(&rng, 32, &userA);
|
wc_ecc_make_key(&rng, 32, &userA);
|
||||||
wc_ecc_make_key(&rng, 32, &userB);
|
wc_ecc_make_key(&rng, 32, &userB);
|
||||||
@ -2449,7 +2470,7 @@ void bench_ed25519KeySign(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(WOLFSSL_EMBOS)
|
#elif defined(WOLFSSL_EMBOS)
|
||||||
|
|
||||||
#include "RTOS.h"
|
#include "RTOS.h"
|
||||||
|
|
||||||
double current_time(int reset)
|
double current_time(int reset)
|
||||||
|
@ -196,6 +196,92 @@ static int create_memory_buckets(byte* buffer, word32 bufSz,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap)
|
||||||
|
{
|
||||||
|
word32 wc_MemSz[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
|
||||||
|
word32 wc_Dist[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
|
||||||
|
|
||||||
|
if (heap == NULL) {
|
||||||
|
return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
|
||||||
|
|
||||||
|
XMEMCPY(heap->sizeList, wc_MemSz, sizeof(wc_MemSz));
|
||||||
|
XMEMCPY(heap->distList, wc_Dist, sizeof(wc_Dist));
|
||||||
|
|
||||||
|
if (InitMutex(&(heap->memory_mutex)) != 0) {
|
||||||
|
WOLFSSL_MSG("Error creating heap memory mutex");
|
||||||
|
return BAD_MUTEX_E;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
||||||
|
unsigned char* buf, unsigned int sz, int flag, int max)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
WOLFSSL_HEAP* heap;
|
||||||
|
WOLFSSL_HEAP_HINT* hint;
|
||||||
|
word32 idx = 0;
|
||||||
|
|
||||||
|
if (pHint == NULL || buf == NULL) {
|
||||||
|
return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
|
||||||
|
return BUFFER_E; /* not enough memory for structures */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* check if hint has already been assigned */
|
||||||
|
if (*pHint == NULL) {
|
||||||
|
heap = (WOLFSSL_HEAP*)buf;
|
||||||
|
idx += sizeof(WOLFSSL_HEAP);
|
||||||
|
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
||||||
|
idx += sizeof(WOLFSSL_HEAP_HINT);
|
||||||
|
|
||||||
|
ret = wolfSSL_init_memory_heap(heap);
|
||||||
|
if (ret != 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
||||||
|
hint->memory = heap;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
#ifdef WOLFSSL_HEAP_TEST
|
||||||
|
/* do not load in memory if test has been set */
|
||||||
|
if (heap == (void*)WOLFSSL_HEAP_TEST) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
hint = (WOLFSSL_HEAP_HINT*)(*pHint);
|
||||||
|
heap = hint->memory;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap);
|
||||||
|
if (ret != 1) {
|
||||||
|
WOLFSSL_MSG("Error partitioning memory");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* determine what max applies too */
|
||||||
|
if ((flag & WOLFMEM_IO_POOL) || (flag & WOLFMEM_IO_POOL_FIXED)) {
|
||||||
|
heap->maxIO = max;
|
||||||
|
}
|
||||||
|
else { /* general memory used in handshakes */
|
||||||
|
heap->maxHa = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap->flag |= flag;
|
||||||
|
*pHint = hint;
|
||||||
|
|
||||||
|
(void)max;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int wolfSSL_load_static_memory(byte* buffer, word32 sz, int flag,
|
int wolfSSL_load_static_memory(byte* buffer, word32 sz, int flag,
|
||||||
WOLFSSL_HEAP* heap)
|
WOLFSSL_HEAP* heap)
|
||||||
@ -219,6 +305,10 @@ int wolfSSL_load_static_memory(byte* buffer, word32 sz, int flag,
|
|||||||
ava--;
|
ava--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
|
printf("Allocated %d bytes for static memory @ %p\n", ava, pt);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* devide into chunks of memory and add them to available list */
|
/* devide into chunks of memory and add them to available list */
|
||||||
while (ava >= (heap->sizeList[0] + padSz + memSz)) {
|
while (ava >= (heap->sizeList[0] + padSz + memSz)) {
|
||||||
int i;
|
int i;
|
||||||
@ -392,7 +482,11 @@ int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
|
void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
|
||||||
|
#else
|
||||||
void* wolfSSL_Malloc(size_t size, void* heap, int type)
|
void* wolfSSL_Malloc(size_t size, void* heap, int type)
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
void* res = 0;
|
void* res = 0;
|
||||||
wc_Memory* pt = NULL;
|
wc_Memory* pt = NULL;
|
||||||
@ -473,6 +567,10 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
|
|||||||
mem->alloc += 1;
|
mem->alloc += 1;
|
||||||
res = pt->buffer;
|
res = pt->buffer;
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
|
printf("Alloc: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* keep track of connection statistics if flag is set */
|
/* keep track of connection statistics if flag is set */
|
||||||
if (mem->flag & WOLFMEM_TRACK_STATS) {
|
if (mem->flag & WOLFMEM_TRACK_STATS) {
|
||||||
WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
|
WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
|
||||||
@ -512,7 +610,11 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
|
void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line)
|
||||||
|
#else
|
||||||
void wolfSSL_Free(void *ptr, void* heap, int type)
|
void wolfSSL_Free(void *ptr, void* heap, int type)
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
wc_Memory* pt;
|
wc_Memory* pt;
|
||||||
@ -579,6 +681,10 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
|
|||||||
mem->inUse -= pt->sz;
|
mem->inUse -= pt->sz;
|
||||||
mem->frAlc += 1;
|
mem->frAlc += 1;
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
|
printf("Free: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* keep track of connection statistics if flag is set */
|
/* keep track of connection statistics if flag is set */
|
||||||
if (mem->flag & WOLFMEM_TRACK_STATS) {
|
if (mem->flag & WOLFMEM_TRACK_STATS) {
|
||||||
WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
|
WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
|
||||||
@ -606,8 +712,11 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
|
|||||||
(void)type;
|
(void)type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
|
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line)
|
||||||
|
#else
|
||||||
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
|
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
void* res = 0;
|
void* res = 0;
|
||||||
wc_Memory* pt = NULL;
|
wc_Memory* pt = NULL;
|
||||||
@ -678,7 +787,11 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
|
|||||||
|
|
||||||
/* free memory that was previously being used */
|
/* free memory that was previously being used */
|
||||||
UnLockMutex(&(mem->memory_mutex));
|
UnLockMutex(&(mem->memory_mutex));
|
||||||
wolfSSL_Free(ptr, heap, type);
|
wolfSSL_Free(ptr, heap, type
|
||||||
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
|
, func, line
|
||||||
|
#endif
|
||||||
|
);
|
||||||
if (LockMutex(&(mem->memory_mutex)) != 0) {
|
if (LockMutex(&(mem->memory_mutex)) != 0) {
|
||||||
WOLFSSL_MSG("Bad memory_mutex lock");
|
WOLFSSL_MSG("Bad memory_mutex lock");
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -719,7 +832,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
|
|||||||
|
|
||||||
|
|
||||||
/* allow simple per thread in and out pools */
|
/* allow simple per thread in and out pools */
|
||||||
/* use 17k size sense max record size is 16k plus overhead */
|
/* use 17k size since max record size is 16k plus overhead */
|
||||||
static THREAD_LS_T byte pool_in[17*1024];
|
static THREAD_LS_T byte pool_in[17*1024];
|
||||||
static THREAD_LS_T byte pool_out[17*1024];
|
static THREAD_LS_T byte pool_out[17*1024];
|
||||||
|
|
||||||
@ -766,9 +879,7 @@ void* XREALLOC(void *p, size_t n, void* heap, int type)
|
|||||||
return realloc(p, n);
|
return realloc(p, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void XFREE(void *p, void* heap, int type)
|
||||||
/* unit api calls, let's make sure visible with WOLFSSL_API */
|
|
||||||
WOLFSSL_API void XFREE(void *p, void* heap, int type)
|
|
||||||
{
|
{
|
||||||
(void)heap;
|
(void)heap;
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -33,36 +33,42 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
#ifdef WOLFSSL_STATIC_MEMORY
|
||||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
|
typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line);
|
||||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
|
typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line);
|
||||||
|
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
|
||||||
/* Public in case user app wants to use XMALLOC/XFREE */
|
WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line);
|
||||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
|
WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line);
|
||||||
WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line);
|
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
|
||||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
|
|
||||||
|
|
||||||
#else
|
|
||||||
#if defined(WOLFSSL_STATIC_MEMORY)
|
|
||||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type);
|
|
||||||
typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type);
|
|
||||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size,
|
|
||||||
void* heap, int type);
|
|
||||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
|
|
||||||
WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type);
|
|
||||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size,
|
|
||||||
void* heap, int type);
|
|
||||||
#else
|
#else
|
||||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size);
|
typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type);
|
||||||
typedef void (*wolfSSL_Free_cb)(void *ptr);
|
typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type);
|
||||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
|
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type);
|
||||||
/* Public in case user app wants to use XMALLOC/XFREE */
|
WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
|
||||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size);
|
WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type);
|
||||||
WOLFSSL_API void wolfSSL_Free(void *ptr);
|
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
|
||||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
|
#endif /* WOLFSSL_DEBUG_MEMORY */
|
||||||
#endif
|
#else
|
||||||
#endif
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
|
typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
|
||||||
|
typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
|
||||||
|
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
|
||||||
|
|
||||||
|
/* Public in case user app wants to use XMALLOC/XFREE */
|
||||||
|
WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
|
||||||
|
WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line);
|
||||||
|
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
|
||||||
|
#else
|
||||||
|
typedef void *(*wolfSSL_Malloc_cb)(size_t size);
|
||||||
|
typedef void (*wolfSSL_Free_cb)(void *ptr);
|
||||||
|
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
|
||||||
|
/* Public in case user app wants to use XMALLOC/XFREE */
|
||||||
|
WOLFSSL_API void* wolfSSL_Malloc(size_t size);
|
||||||
|
WOLFSSL_API void wolfSSL_Free(void *ptr);
|
||||||
|
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
|
||||||
|
#endif /* WOLFSSL_DEBUG_MEMORY */
|
||||||
|
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||||
|
|
||||||
/* Public set function */
|
/* Public set function */
|
||||||
WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function,
|
WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function,
|
||||||
@ -156,7 +162,10 @@ WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function,
|
|||||||
byte haFlag; /* flag used for checking handshake count */
|
byte haFlag; /* flag used for checking handshake count */
|
||||||
} WOLFSSL_HEAP_HINT;
|
} WOLFSSL_HEAP_HINT;
|
||||||
|
|
||||||
|
WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
||||||
|
unsigned char* buf, unsigned int sz, int flag, int max);
|
||||||
|
|
||||||
|
WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap);
|
||||||
WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz,
|
WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz,
|
||||||
int flag, WOLFSSL_HEAP* heap);
|
int flag, WOLFSSL_HEAP* heap);
|
||||||
WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap,
|
WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap,
|
||||||
|
@ -1251,6 +1251,9 @@ static char *fgets(char *buff, int sz, FILE *fp)
|
|||||||
|
|
||||||
/* restriction with static memory */
|
/* restriction with static memory */
|
||||||
#ifdef WOLFSSL_STATIC_MEMORY
|
#ifdef WOLFSSL_STATIC_MEMORY
|
||||||
|
#if defined(HAVE_IO_POOL) || defined(XMALLOC_USER) || defined(NO_WOLFSSL_MEMORY)
|
||||||
|
#error static memory cannot be used with HAVE_IO_POOL, XMALLOC_USER or NO_WOLFSSL_MEMORY
|
||||||
|
#endif
|
||||||
#ifndef USE_FAST_MATH
|
#ifndef USE_FAST_MATH
|
||||||
#error static memory requires fast math please define USE_FAST_MATH
|
#error static memory requires fast math please define USE_FAST_MATH
|
||||||
#endif
|
#endif
|
||||||
@ -1258,6 +1261,8 @@ static char *fgets(char *buff, int sz, FILE *fp)
|
|||||||
#error static memory does not support small stack please undefine
|
#error static memory does not support small stack please undefine
|
||||||
#endif
|
#endif
|
||||||
#endif /* WOLFSSL_STATIC_MEMORY */
|
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||||
|
|
||||||
|
|
||||||
/* Place any other flags or defines here */
|
/* Place any other flags or defines here */
|
||||||
|
|
||||||
#if defined(WOLFSSL_MYSQL_COMPATIBLE) && defined(_WIN32) \
|
#if defined(WOLFSSL_MYSQL_COMPATIBLE) && defined(_WIN32) \
|
||||||
|
@ -171,24 +171,22 @@
|
|||||||
/* default to libc stuff */
|
/* default to libc stuff */
|
||||||
/* XREALLOC is used once in normal math lib, not in fast math lib */
|
/* XREALLOC is used once in normal math lib, not in fast math lib */
|
||||||
/* XFREE on some embeded systems doesn't like free(0) so test */
|
/* XFREE on some embeded systems doesn't like free(0) so test */
|
||||||
#if defined(XMALLOC_USER)
|
#if defined(HAVE_IO_POOL)
|
||||||
|
WOLFSSL_API void* XMALLOC(size_t n, void* heap, int type);
|
||||||
|
WOLFSSL_API void* XREALLOC(void *p, size_t n, void* heap, int type);
|
||||||
|
WOLFSSL_API void XFREE(void *p, void* heap, int type);
|
||||||
|
#elif defined(XMALLOC_USER)
|
||||||
/* prototypes for user heap override functions */
|
/* prototypes for user heap override functions */
|
||||||
#include <stddef.h> /* for size_t */
|
#include <stddef.h> /* for size_t */
|
||||||
extern void *XMALLOC(size_t n, void* heap, int type);
|
extern void *XMALLOC(size_t n, void* heap, int type);
|
||||||
extern void *XREALLOC(void *p, size_t n, void* heap, int type);
|
extern void *XREALLOC(void *p, size_t n, void* heap, int type);
|
||||||
extern void XFREE(void *p, void* heap, int type);
|
extern void XFREE(void *p, void* heap, int type);
|
||||||
#ifdef WOLFSSL_STATIC_MEMORY /* don't use wolfSSL static memory either*/
|
|
||||||
#undef WOLFSSL_STATIC_MEMORY
|
|
||||||
#endif
|
|
||||||
#elif defined(NO_WOLFSSL_MEMORY)
|
#elif defined(NO_WOLFSSL_MEMORY)
|
||||||
/* just use plain C stdlib stuff if desired */
|
/* just use plain C stdlib stuff if desired */
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#define XMALLOC(s, h, t) ((void)h, (void)t, malloc((s)))
|
#define XMALLOC(s, h, t) ((void)h, (void)t, malloc((s)))
|
||||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));}
|
#define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));}
|
||||||
#define XREALLOC(p, n, h, t) realloc((p), (n))
|
#define XREALLOC(p, n, h, t) realloc((p), (n))
|
||||||
#ifdef WOLFSSL_STATIC_MEMORY /* don't use wolfSSL static memory either*/
|
|
||||||
#undef WOLFSSL_STATIC_MEMORY
|
|
||||||
#endif
|
|
||||||
#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
|
#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
|
||||||
&& !defined(WOLFSSL_SAFERTOS) && !defined(FREESCALE_MQX) \
|
&& !defined(WOLFSSL_SAFERTOS) && !defined(FREESCALE_MQX) \
|
||||||
&& !defined(FREESCALE_KSDK_MQX) && !defined(FREESCALE_FREE_RTOS) \
|
&& !defined(FREESCALE_KSDK_MQX) && !defined(FREESCALE_FREE_RTOS) \
|
||||||
@ -196,19 +194,27 @@
|
|||||||
&& !defined(WOLFSSL_uITRON4) && !defined(WOLFSSL_uTKERNEL2)
|
&& !defined(WOLFSSL_uITRON4) && !defined(WOLFSSL_uTKERNEL2)
|
||||||
/* default C runtime, can install different routines at runtime via cbs */
|
/* default C runtime, can install different routines at runtime via cbs */
|
||||||
#include <wolfssl/wolfcrypt/memory.h>
|
#include <wolfssl/wolfcrypt/memory.h>
|
||||||
#if defined(WOLFSSL_DEBUG_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
|
#ifdef WOLFSSL_STATIC_MEMORY
|
||||||
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s), __func__, __LINE__))
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), __func__, __LINE__);}
|
#define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t), __func__, __LINE__)
|
||||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), __func__, __LINE__)
|
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), (h), (t), __func__, __LINE__);}
|
||||||
#elif defined(WOLFSSL_STATIC_MEMORY)
|
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t), __func__, __LINE__)
|
||||||
#define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t))
|
#else
|
||||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), (h), (t));}
|
#define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t))
|
||||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t))
|
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), (h), (t));}
|
||||||
#else
|
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t))
|
||||||
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s)))
|
#endif /* WOLFSSL_DEBUG_MEMORY */
|
||||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp));}
|
#else
|
||||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n))
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||||
#endif
|
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s), __func__, __LINE__))
|
||||||
|
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), __func__, __LINE__);}
|
||||||
|
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), __func__, __LINE__)
|
||||||
|
#else
|
||||||
|
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s)))
|
||||||
|
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp));}
|
||||||
|
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n))
|
||||||
|
#endif /* WOLFSSL_DEBUG_MEMORY */
|
||||||
|
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user