Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull in some staticmemory features #7595

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -8070,6 +8070,27 @@ AC_ARG_ENABLE([staticmemory],
[ ENABLED_STATICMEMORY=no ]
)

for v in `echo $ENABLED_STATICMEMORY | tr "," " "`
do
case $v in
yes)
;;
no)
;;
small|lean)
ENABLED_STATICMEMORY=yes
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_STATIC_MEMORY_LEAN"
;;
debug)
ENABLED_STATICMEMORY=yes
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK"
;;
*)
AC_MSG_ERROR([Invalid choice for staticmemory.])
break;;
esac
done

if test "x$ENABLED_STATICMEMORY" = "xyes"
then
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_STATIC_MEMORY"
Expand Down
53 changes: 47 additions & 6 deletions examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,39 @@ static int client_srtp_test(WOLFSSL *ssl, func_args *args)
}
#endif /* WOLFSSL_SRTP */

#if defined(WOLFSSL_STATIC_MEMORY) && \
defined(WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK)
static void ExampleDebugMemoryCb(size_t sz, int bucketSz, byte st, int type) {
switch (st) {
case WOLFSSL_DEBUG_MEMORY_ALLOC:
if (type == DYNAMIC_TYPE_IN_BUFFER) {
printf("IN BUFFER: ");
}

if (type == DYNAMIC_TYPE_OUT_BUFFER) {
printf("OUT BUFFER: ");
}

printf("Alloc'd %d bytes using bucket size %d\n", (int)sz,
bucketSz);
break;

case WOLFSSL_DEBUG_MEMORY_FAIL:
printf("Failed when trying to allocate %d bytes\n", (int)sz);
break;

case WOLFSSL_DEBUG_MEMORY_FREE:
printf("Free'ing : %d\n", (int)sz);
break;

case WOLFSSL_DEBUG_MEMORY_INIT:
printf("Creating memory bucket of size : %d\n", bucketSz);
break;
}
}
#endif



THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
{
Expand Down Expand Up @@ -2096,10 +2129,12 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
byte memory[80000];
#endif
byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
#if !defined(WOLFSSL_STATIC_MEMORY_LEAN)
WOLFSSL_MEM_CONN_STATS ssl_stats;
#ifdef DEBUG_WOLFSSL
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MEM_STATS mem_stats;
#endif
#endif
WOLFSSL_HEAP_HINT *heap = NULL;
#endif

Expand Down Expand Up @@ -3026,7 +3061,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)


#ifdef WOLFSSL_STATIC_MEMORY
#ifdef DEBUG_WOLFSSL
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
/* print off helper buffer sizes for use with static memory
* printing to stderr in case of debug mode turned on */
LOG_ERROR("static memory management size = %d\n",
Expand All @@ -3043,6 +3078,10 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
err_sys("unable to load static memory");
}

#if defined(WOLFSSL_STATIC_MEMORY) && \
defined(WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK)
wolfSSL_SetDebugMemoryCb(ExampleDebugMemoryCb);
#endif
ctx = wolfSSL_CTX_new_ex(method(heap), heap);
if (ctx == NULL)
err_sys("unable to get ctx");
Expand Down Expand Up @@ -3584,7 +3623,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
}
#endif

#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
!defined(WOLFSSL_STATIC_MEMORY_LEAN)
LOG_ERROR("Before creating SSL\n");
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
err_sys("ctx not using static memory");
Expand Down Expand Up @@ -3682,7 +3722,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
}
#endif

#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
!defined(WOLFSSL_STATIC_MEMORY_LEAN)
LOG_ERROR("After creating SSL\n");
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
err_sys("ctx not using static memory");
Expand Down Expand Up @@ -4390,7 +4431,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
#endif

/* display collected statistics */
#ifdef WOLFSSL_STATIC_MEMORY
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
err_sys("static memory was not used with ssl");

Expand Down Expand Up @@ -4617,7 +4658,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
wolfSSL_shutdown(sslResume); /* bidirectional shutdown */

/* display collected statistics */
#ifdef WOLFSSL_STATIC_MEMORY
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
if (wolfSSL_is_static_memory(sslResume, &ssl_stats) != 1)
err_sys("static memory was not used with ssl");

Expand Down
14 changes: 9 additions & 5 deletions examples/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1601,10 +1601,12 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
byte memory[80000];
#endif
byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
#if !defined(WOLFSSL_STATIC_MEMORY_LEAN)
WOLFSSL_MEM_CONN_STATS ssl_stats;
#ifdef DEBUG_WOLFSSL
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MEM_STATS mem_stats;
#endif
#endif
#endif
#if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
int onlyKeyShare = 0;
Expand Down Expand Up @@ -2503,7 +2505,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
err_sys_ex(runWithErrors, "unable to get method");

#ifdef WOLFSSL_STATIC_MEMORY
#ifdef DEBUG_WOLFSSL
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
/* print off helper buffer sizes for use with static memory
* printing to stderr in case of debug mode turned on */
LOG_ERROR("static memory management size = %d\n",
Expand Down Expand Up @@ -2964,7 +2966,8 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
err_sys_ex(runWithErrors, "tcp accept failed");
}
}
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
!defined(WOLFSSL_STATIC_MEMORY_LEAN)
LOG_ERROR("Before creating SSL\n");
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
err_sys_ex(runWithErrors, "ctx not using static memory");
Expand Down Expand Up @@ -3053,7 +3056,8 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
}
#endif

#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
!defined(WOLFSSL_STATIC_MEMORY_LEAN)
LOG_ERROR("After creating SSL\n");
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
err_sys_ex(runWithErrors, "ctx not using static memory");
Expand Down Expand Up @@ -3799,7 +3803,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
}

/* display collected statistics */
#ifdef WOLFSSL_STATIC_MEMORY
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
err_sys_ex(runWithErrors, "static memory was not used with ssl");

Expand Down
22 changes: 19 additions & 3 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -7241,6 +7241,8 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
ssl_hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
ctx_hint = ((WOLFSSL_HEAP_HINT*)(ctx->heap));

ssl_hint->memory = ctx_hint->memory;
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
/* lock and check IO count / handshake count */
if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
WOLFSSL_MSG("Bad memory_mutex lock");
Expand Down Expand Up @@ -7268,7 +7270,6 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
}
ctx_hint->memory->curIO++;
ctx_hint->memory->curHa++;
ssl_hint->memory = ctx_hint->memory;
ssl_hint->haFlag = 1;
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));

Expand Down Expand Up @@ -7304,6 +7305,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
}
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
}
#endif /* !WOLFSSL_STATIC_MEMORY_LEAN */
#ifdef WOLFSSL_HEAP_TEST
}
#endif
Expand Down Expand Up @@ -8382,30 +8384,38 @@ void SSL_ResourceFree(WOLFSSL* ssl)
/* avoid dereferencing a test value */
if (ssl->heap != (void*)WOLFSSL_HEAP_TEST) {
#endif
void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap;
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
WOLFSSL_HEAP_HINT* ssl_hint = (WOLFSSL_HEAP_HINT*)ssl->heap;
WOLFSSL_HEAP* ctx_heap;
void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap;

ctx_heap = ssl_hint->memory;
#ifndef SINGLE_THREADED
if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) {
WOLFSSL_MSG("Bad memory_mutex lock");
}
#endif
ctx_heap->curIO--;
if (FreeFixedIO(ctx_heap, &(ssl_hint->outBuf)) != 1) {
WOLFSSL_MSG("Error freeing fixed output buffer");
}
if (FreeFixedIO(ctx_heap, &(ssl_hint->inBuf)) != 1) {
WOLFSSL_MSG("Error freeing fixed output buffer");
}
if (ssl_hint->haFlag && ctx_heap->curHa > 0) { /* check if handshake count has been decreased*/

/* check if handshake count has been decreased*/
if (ssl_hint->haFlag && ctx_heap->curHa > 0) {
ctx_heap->curHa--;
}
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_heap->memory_mutex));
#endif

/* check if tracking stats */
if (ctx_heap->flag & WOLFMEM_TRACK_STATS) {
XFREE(ssl_hint->stats, heap, DYNAMIC_TYPE_SSL);
}
#endif /* !WOLFSSL_STATIC_MEMORY_LEAN */
XFREE(ssl->heap, heap, DYNAMIC_TYPE_SSL);
#ifdef WOLFSSL_HEAP_TEST
}
Expand Down Expand Up @@ -8673,14 +8683,20 @@ void FreeHandshakeResources(WOLFSSL* ssl)
WOLFSSL_HEAP* ctx_heap;

ctx_heap = ssl_hint->memory;
#ifndef SINGLE_THREADED
if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) {
WOLFSSL_MSG("Bad memory_mutex lock");
}
#endif
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
if (ctx_heap->curHa > 0) {
ctx_heap->curHa--;
}
ssl_hint->haFlag = 0; /* set to zero since handshake has been dec */
#endif
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_heap->memory_mutex));
#endif
#ifdef WOLFSSL_HEAP_TEST
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2573,6 +2573,7 @@ int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
}
WOLFSSL_ENTER("wolfSSL_is_static_memory");

#ifndef WOLFSSL_STATIC_MEMORY_LEAN
/* fill out statistics if wanted and WOLFMEM_TRACK_STATS flag */
if (mem_stats != NULL && ssl->heap != NULL) {
WOLFSSL_HEAP_HINT* hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
Expand All @@ -2581,7 +2582,9 @@ int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
XMEMCPY(mem_stats, hint->stats, sizeof(WOLFSSL_MEM_CONN_STATS));
}
}
#endif

(void)mem_stats;
return (ssl->heap) ? 1 : 0;
}

Expand All @@ -2593,14 +2596,17 @@ int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats)
}
WOLFSSL_ENTER("wolfSSL_CTX_is_static_memory");

#ifndef WOLFSSL_STATIC_MEMORY_LEAN
/* fill out statistics if wanted */
if (mem_stats != NULL && ctx->heap != NULL) {
WOLFSSL_HEAP* heap = ((WOLFSSL_HEAP_HINT*)(ctx->heap))->memory;
if (wolfSSL_GetMemStats(heap, mem_stats) != 1) {
return MEMORY_E;
}
}
#endif

(void)mem_stats;
return (ctx->heap) ? 1 : 0;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -66534,15 +66534,15 @@ static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
ExpectNull((ssl3 = wolfSSL_new(ctx)));

if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
#ifdef DEBUG_WOLFSSL
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
wolfSSL_PrintStatsConn(&ssl_stats);
#endif
(void)ssl_stats;
}

/* display collected statistics */
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
#ifdef DEBUG_WOLFSSL
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
wolfSSL_PrintStats(&mem_stats);
#endif
(void)mem_stats;
Expand Down
Loading