Skip to content

Commit 7fadd4e

Browse files
authored
Merge pull request #7595 from JacobBarthelmeh/static
Pull in some staticmemory features
2 parents bb57c1d + ebdc8b9 commit 7fadd4e

11 files changed

Lines changed: 285 additions & 49 deletions

File tree

configure.ac

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8070,6 +8070,27 @@ AC_ARG_ENABLE([staticmemory],
80708070
[ ENABLED_STATICMEMORY=no ]
80718071
)
80728072
8073+
for v in `echo $ENABLED_STATICMEMORY | tr "," " "`
8074+
do
8075+
case $v in
8076+
yes)
8077+
;;
8078+
no)
8079+
;;
8080+
small|lean)
8081+
ENABLED_STATICMEMORY=yes
8082+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_STATIC_MEMORY_LEAN"
8083+
;;
8084+
debug)
8085+
ENABLED_STATICMEMORY=yes
8086+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK"
8087+
;;
8088+
*)
8089+
AC_MSG_ERROR([Invalid choice for staticmemory.])
8090+
break;;
8091+
esac
8092+
done
8093+
80738094
if test "x$ENABLED_STATICMEMORY" = "xyes"
80748095
then
80758096
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_STATIC_MEMORY"

examples/client/client.c

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,39 @@ static int client_srtp_test(WOLFSSL *ssl, func_args *args)
18651865
}
18661866
#endif /* WOLFSSL_SRTP */
18671867

1868+
#if defined(WOLFSSL_STATIC_MEMORY) && \
1869+
defined(WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK)
1870+
static void ExampleDebugMemoryCb(size_t sz, int bucketSz, byte st, int type) {
1871+
switch (st) {
1872+
case WOLFSSL_DEBUG_MEMORY_ALLOC:
1873+
if (type == DYNAMIC_TYPE_IN_BUFFER) {
1874+
printf("IN BUFFER: ");
1875+
}
1876+
1877+
if (type == DYNAMIC_TYPE_OUT_BUFFER) {
1878+
printf("OUT BUFFER: ");
1879+
}
1880+
1881+
printf("Alloc'd %d bytes using bucket size %d\n", (int)sz,
1882+
bucketSz);
1883+
break;
1884+
1885+
case WOLFSSL_DEBUG_MEMORY_FAIL:
1886+
printf("Failed when trying to allocate %d bytes\n", (int)sz);
1887+
break;
1888+
1889+
case WOLFSSL_DEBUG_MEMORY_FREE:
1890+
printf("Free'ing : %d\n", (int)sz);
1891+
break;
1892+
1893+
case WOLFSSL_DEBUG_MEMORY_INIT:
1894+
printf("Creating memory bucket of size : %d\n", bucketSz);
1895+
break;
1896+
}
1897+
}
1898+
#endif
1899+
1900+
18681901

18691902
THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
18701903
{
@@ -2096,10 +2129,12 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
20962129
byte memory[80000];
20972130
#endif
20982131
byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
2132+
#if !defined(WOLFSSL_STATIC_MEMORY_LEAN)
20992133
WOLFSSL_MEM_CONN_STATS ssl_stats;
2100-
#ifdef DEBUG_WOLFSSL
2134+
#if defined(DEBUG_WOLFSSL)
21012135
WOLFSSL_MEM_STATS mem_stats;
21022136
#endif
2137+
#endif
21032138
WOLFSSL_HEAP_HINT *heap = NULL;
21042139
#endif
21052140

@@ -3026,7 +3061,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
30263061

30273062

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

3081+
#if defined(WOLFSSL_STATIC_MEMORY) && \
3082+
defined(WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK)
3083+
wolfSSL_SetDebugMemoryCb(ExampleDebugMemoryCb);
3084+
#endif
30463085
ctx = wolfSSL_CTX_new_ex(method(heap), heap);
30473086
if (ctx == NULL)
30483087
err_sys("unable to get ctx");
@@ -3584,7 +3623,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
35843623
}
35853624
#endif
35863625

3587-
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
3626+
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
3627+
!defined(WOLFSSL_STATIC_MEMORY_LEAN)
35883628
LOG_ERROR("Before creating SSL\n");
35893629
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
35903630
err_sys("ctx not using static memory");
@@ -3682,7 +3722,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
36823722
}
36833723
#endif
36843724

3685-
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
3725+
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
3726+
!defined(WOLFSSL_STATIC_MEMORY_LEAN)
36863727
LOG_ERROR("After creating SSL\n");
36873728
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
36883729
err_sys("ctx not using static memory");
@@ -4390,7 +4431,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
43904431
#endif
43914432

43924433
/* display collected statistics */
4393-
#ifdef WOLFSSL_STATIC_MEMORY
4434+
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
43944435
if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
43954436
err_sys("static memory was not used with ssl");
43964437

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

46194660
/* display collected statistics */
4620-
#ifdef WOLFSSL_STATIC_MEMORY
4661+
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
46214662
if (wolfSSL_is_static_memory(sslResume, &ssl_stats) != 1)
46224663
err_sys("static memory was not used with ssl");
46234664

examples/server/server.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,10 +1601,12 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
16011601
byte memory[80000];
16021602
#endif
16031603
byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
1604+
#if !defined(WOLFSSL_STATIC_MEMORY_LEAN)
16041605
WOLFSSL_MEM_CONN_STATS ssl_stats;
1605-
#ifdef DEBUG_WOLFSSL
1606+
#if defined(DEBUG_WOLFSSL)
16061607
WOLFSSL_MEM_STATS mem_stats;
16071608
#endif
1609+
#endif
16081610
#endif
16091611
#if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
16101612
int onlyKeyShare = 0;
@@ -2503,7 +2505,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
25032505
err_sys_ex(runWithErrors, "unable to get method");
25042506

25052507
#ifdef WOLFSSL_STATIC_MEMORY
2506-
#ifdef DEBUG_WOLFSSL
2508+
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
25072509
/* print off helper buffer sizes for use with static memory
25082510
* printing to stderr in case of debug mode turned on */
25092511
LOG_ERROR("static memory management size = %d\n",
@@ -2964,7 +2966,8 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
29642966
err_sys_ex(runWithErrors, "tcp accept failed");
29652967
}
29662968
}
2967-
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
2969+
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
2970+
!defined(WOLFSSL_STATIC_MEMORY_LEAN)
29682971
LOG_ERROR("Before creating SSL\n");
29692972
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
29702973
err_sys_ex(runWithErrors, "ctx not using static memory");
@@ -3053,7 +3056,8 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
30533056
}
30543057
#endif
30553058

3056-
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
3059+
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
3060+
!defined(WOLFSSL_STATIC_MEMORY_LEAN)
30573061
LOG_ERROR("After creating SSL\n");
30583062
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
30593063
err_sys_ex(runWithErrors, "ctx not using static memory");
@@ -3799,7 +3803,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
37993803
}
38003804

38013805
/* display collected statistics */
3802-
#ifdef WOLFSSL_STATIC_MEMORY
3806+
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
38033807
if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
38043808
err_sys_ex(runWithErrors, "static memory was not used with ssl");
38053809

src/internal.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7241,6 +7241,8 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
72417241
ssl_hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
72427242
ctx_hint = ((WOLFSSL_HEAP_HINT*)(ctx->heap));
72437243

7244+
ssl_hint->memory = ctx_hint->memory;
7245+
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
72447246
/* lock and check IO count / handshake count */
72457247
if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
72467248
WOLFSSL_MSG("Bad memory_mutex lock");
@@ -7268,7 +7270,6 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
72687270
}
72697271
ctx_hint->memory->curIO++;
72707272
ctx_hint->memory->curHa++;
7271-
ssl_hint->memory = ctx_hint->memory;
72727273
ssl_hint->haFlag = 1;
72737274
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
72747275

@@ -7304,6 +7305,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
73047305
}
73057306
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
73067307
}
7308+
#endif /* !WOLFSSL_STATIC_MEMORY_LEAN */
73077309
#ifdef WOLFSSL_HEAP_TEST
73087310
}
73097311
#endif
@@ -8382,30 +8384,38 @@ void SSL_ResourceFree(WOLFSSL* ssl)
83828384
/* avoid dereferencing a test value */
83838385
if (ssl->heap != (void*)WOLFSSL_HEAP_TEST) {
83848386
#endif
8387+
void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap;
8388+
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
83858389
WOLFSSL_HEAP_HINT* ssl_hint = (WOLFSSL_HEAP_HINT*)ssl->heap;
83868390
WOLFSSL_HEAP* ctx_heap;
8387-
void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap;
83888391

83898392
ctx_heap = ssl_hint->memory;
8393+
#ifndef SINGLE_THREADED
83908394
if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) {
83918395
WOLFSSL_MSG("Bad memory_mutex lock");
83928396
}
8397+
#endif
83938398
ctx_heap->curIO--;
83948399
if (FreeFixedIO(ctx_heap, &(ssl_hint->outBuf)) != 1) {
83958400
WOLFSSL_MSG("Error freeing fixed output buffer");
83968401
}
83978402
if (FreeFixedIO(ctx_heap, &(ssl_hint->inBuf)) != 1) {
83988403
WOLFSSL_MSG("Error freeing fixed output buffer");
83998404
}
8400-
if (ssl_hint->haFlag && ctx_heap->curHa > 0) { /* check if handshake count has been decreased*/
8405+
8406+
/* check if handshake count has been decreased*/
8407+
if (ssl_hint->haFlag && ctx_heap->curHa > 0) {
84018408
ctx_heap->curHa--;
84028409
}
8410+
#ifndef SINGLE_THREADED
84038411
wc_UnLockMutex(&(ctx_heap->memory_mutex));
8412+
#endif
84048413

84058414
/* check if tracking stats */
84068415
if (ctx_heap->flag & WOLFMEM_TRACK_STATS) {
84078416
XFREE(ssl_hint->stats, heap, DYNAMIC_TYPE_SSL);
84088417
}
8418+
#endif /* !WOLFSSL_STATIC_MEMORY_LEAN */
84098419
XFREE(ssl->heap, heap, DYNAMIC_TYPE_SSL);
84108420
#ifdef WOLFSSL_HEAP_TEST
84118421
}
@@ -8673,14 +8683,20 @@ void FreeHandshakeResources(WOLFSSL* ssl)
86738683
WOLFSSL_HEAP* ctx_heap;
86748684

86758685
ctx_heap = ssl_hint->memory;
8686+
#ifndef SINGLE_THREADED
86768687
if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) {
86778688
WOLFSSL_MSG("Bad memory_mutex lock");
86788689
}
8690+
#endif
8691+
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
86798692
if (ctx_heap->curHa > 0) {
86808693
ctx_heap->curHa--;
86818694
}
86828695
ssl_hint->haFlag = 0; /* set to zero since handshake has been dec */
8696+
#endif
8697+
#ifndef SINGLE_THREADED
86838698
wc_UnLockMutex(&(ctx_heap->memory_mutex));
8699+
#endif
86848700
#ifdef WOLFSSL_HEAP_TEST
86858701
}
86868702
#endif

src/ssl.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,7 @@ int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
25732573
}
25742574
WOLFSSL_ENTER("wolfSSL_is_static_memory");
25752575

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

2587+
(void)mem_stats;
25852588
return (ssl->heap) ? 1 : 0;
25862589
}
25872590

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

2599+
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
25962600
/* fill out statistics if wanted */
25972601
if (mem_stats != NULL && ctx->heap != NULL) {
25982602
WOLFSSL_HEAP* heap = ((WOLFSSL_HEAP_HINT*)(ctx->heap))->memory;
25992603
if (wolfSSL_GetMemStats(heap, mem_stats) != 1) {
26002604
return MEMORY_E;
26012605
}
26022606
}
2607+
#endif
26032608

2609+
(void)mem_stats;
26042610
return (ctx->heap) ? 1 : 0;
26052611
}
26062612

tests/api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66534,15 +66534,15 @@ static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
6653466534
ExpectNull((ssl3 = wolfSSL_new(ctx)));
6653566535

6653666536
if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
66537-
#ifdef DEBUG_WOLFSSL
66537+
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
6653866538
wolfSSL_PrintStatsConn(&ssl_stats);
6653966539
#endif
6654066540
(void)ssl_stats;
6654166541
}
6654266542

6654366543
/* display collected statistics */
6654466544
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
66545-
#ifdef DEBUG_WOLFSSL
66545+
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
6654666546
wolfSSL_PrintStats(&mem_stats);
6654766547
#endif
6654866548
(void)mem_stats;

0 commit comments

Comments
 (0)