Skip to content

Commit fc589d6

Browse files
committed
wolfssl/wolfcrypt/mem_track.h: refactor for linuxkm compatibility, mainly by supporting NO_STDIO_FILESYSTEM.
1 parent cc1ec8a commit fc589d6

1 file changed

Lines changed: 39 additions & 32 deletions

File tree

wolfssl/wolfcrypt/mem_track.h

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,24 @@
6262

6363
#include "wolfssl/wolfcrypt/settings.h"
6464
#include "wolfssl/wolfcrypt/logging.h"
65+
#include "wolfssl/wolfcrypt/memory.h"
6566

6667
#if defined(WOLFSSL_TRACK_MEMORY) || defined(HAVE_STACK_SIZE) || \
6768
defined(HAVE_STACK_SIZE_VERBOSE)
68-
#include <stdio.h>
69+
#ifdef NO_STDIO_FILESYSTEM
70+
/* if wc_port.h/linuxkm_wc_port.h doesn't define printf, then the user
71+
* needs to define it.
72+
*/
73+
#define wc_mem_printf(...) printf(__VA_ARGS__)
74+
#else
75+
#include <stdio.h>
76+
#define wc_mem_printf(...) fprintf(stderr, __VA_ARGS__)
77+
#endif
6978
#endif
7079

7180
#if defined(WOLFSSL_TRACK_MEMORY)
7281
#define DO_MEM_STATS
73-
#if defined(__linux__) || defined(__MACH__)
82+
#if (defined(__linux__) && !defined(WOLFSSL_LINUXKM)) || defined(__MACH__)
7483
#define DO_MEM_LIST
7584
#endif
7685
#endif
@@ -160,7 +169,7 @@ static WC_INLINE void* TrackMalloc(size_t sz)
160169

161170
#ifdef WOLFSSL_DEBUG_MEMORY
162171
#ifdef WOLFSSL_DEBUG_MEMORY_PRINT
163-
fprintf(stderr, "Alloc: %p -> %u at %s:%d\n", header->thisMemory, (word32)sz, func, line);
172+
wc_mem_printf("Alloc: %p -> %u at %s:%d\n", header->thisMemory, (word32)sz, func, line);
164173
#else
165174
(void)func;
166175
(void)line;
@@ -268,7 +277,7 @@ static WC_INLINE void TrackFree(void* ptr)
268277

269278
#ifdef WOLFSSL_DEBUG_MEMORY
270279
#ifdef WOLFSSL_DEBUG_MEMORY_PRINT
271-
fprintf(stderr, "Free: %p -> %u at %s:%d\n", ptr, (word32)sz, func, line);
280+
wc_mem_printf("Free: %p -> %u at %s:%d\n", ptr, (word32)sz, func, line);
272281
#else
273282
(void)func;
274283
(void)line;
@@ -334,11 +343,11 @@ static WC_INLINE int InitMemoryTracker(void)
334343

335344
ret = wolfSSL_GetAllocators(&mfDefault, &ffDefault, &rfDefault);
336345
if (ret < 0) {
337-
fprintf(stderr, "wolfSSL GetAllocators failed to get the defaults\n");
346+
wc_mem_printf("wolfSSL GetAllocators failed to get the defaults\n");
338347
}
339348
ret = wolfSSL_SetAllocators(TrackMalloc, TrackFree, TrackRealloc);
340349
if (ret < 0) {
341-
fprintf(stderr, "wolfSSL SetAllocators failed for track memory\n");
350+
wc_mem_printf("wolfSSL SetAllocators failed for track memory\n");
342351
return ret;
343352
}
344353

@@ -377,28 +386,26 @@ static WC_INLINE void ShowMemoryTracker(void)
377386
#endif
378387

379388
#ifdef DO_MEM_STATS
380-
fprintf(stderr, "total Allocs = %9ld\n", ourMemStats.totalAllocs);
381-
fprintf(stderr, "total Deallocs = %9ld\n", ourMemStats.totalDeallocs);
382-
fprintf(stderr, "total Bytes = %9ld\n", ourMemStats.totalBytes);
383-
fprintf(stderr, "peak Bytes = %9ld\n", ourMemStats.peakBytes);
384-
fprintf(stderr, "current Bytes = %9ld\n", ourMemStats.currentBytes);
389+
wc_mem_printf("total Allocs = %9ld\n", ourMemStats.totalAllocs);
390+
wc_mem_printf("total Deallocs = %9ld\n", ourMemStats.totalDeallocs);
391+
wc_mem_printf("total Bytes = %9ld\n", ourMemStats.totalBytes);
392+
wc_mem_printf("peak Bytes = %9ld\n", ourMemStats.peakBytes);
393+
wc_mem_printf("current Bytes = %9ld\n", ourMemStats.currentBytes);
385394
#endif
386395

387396
#ifdef DO_MEM_LIST
388397
if (ourMemList.count > 0) {
389398
/* print list of allocations */
390399
memHint* header;
391400
for (header = ourMemList.head; header != NULL; header = header->next) {
392-
fprintf(stderr, "Leak: Ptr %p, Size %u"
393401
#ifdef WOLFSSL_DEBUG_MEMORY
394-
", Func %s, Line %d"
395-
#endif
396-
"\n",
397-
(byte*)header + sizeof(memHint), (unsigned int)header->thisSize
398-
#ifdef WOLFSSL_DEBUG_MEMORY
399-
, header->func, header->line
400-
#endif
401-
);
402+
wc_mem_printf("Leak: Ptr %p, Size %u, Func %s, Line %d\n",
403+
(byte*)header + sizeof(memHint), (unsigned int)header->thisSize,
404+
header->func, header->line);
405+
#else
406+
wc_mem_printf("Leak: Ptr %p, Size %u\n",
407+
(byte*)header + sizeof(memHint), (unsigned int)header->thisSize);
408+
#endif
402409
}
403410
}
404411

@@ -538,7 +545,7 @@ int StackSizeHWMReset(void)
538545

539546
#define STACK_SIZE_CHECKPOINT_MSG(msg) ({ \
540547
ssize_t HWM = StackSizeHWM_OffsetCorrected(); \
541-
fprintf(stderr, "%ld\t%s\n", (long int)HWM, msg); \
548+
wc_mem_printf("%ld\t%s\n", (long int)HWM, msg); \
542549
StackSizeHWMReset(); \
543550
})
544551

@@ -549,7 +556,7 @@ int StackSizeHWMReset(void)
549556
printf(" relative stack peak usage = %ld bytes\n", (long int)HWM); \
550557
_ret = StackSizeHWMReset(); \
551558
if ((max >= 0) && (HWM > (ssize_t)(max))) { \
552-
fprintf(stderr, \
559+
wc_mem_printf( \
553560
" relative stack usage at %s L%d exceeds designated max %ld bytes.\n", \
554561
__FILE__, __LINE__, (long int)(max)); \
555562
_ret = -1; \
@@ -585,21 +592,21 @@ static WC_INLINE int StackSizeCheck(struct func_args* args, thread_func tf)
585592

586593
ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize);
587594
if (ret != 0 || myStack == NULL) {
588-
fprintf(stderr, "posix_memalign failed\n");
595+
wc_mem_printf("posix_memalign failed\n");
589596
return -1;
590597
}
591598

592599
XMEMSET(myStack, STACK_CHECK_VAL, stackSize);
593600

594601
ret = pthread_attr_init(&myAttr);
595602
if (ret != 0) {
596-
fprintf(stderr, "attr_init failed\n");
603+
wc_mem_printf("attr_init failed\n");
597604
return ret;
598605
}
599606

600607
ret = pthread_attr_setstack(&myAttr, myStack, stackSize);
601608
if (ret != 0) {
602-
fprintf(stderr, "attr_setstackaddr failed\n");
609+
wc_mem_printf("attr_setstackaddr failed\n");
603610
return ret;
604611
}
605612

@@ -623,7 +630,7 @@ static WC_INLINE int StackSizeCheck(struct func_args* args, thread_func tf)
623630

624631
ret = pthread_join(threadId, &status);
625632
if (ret != 0) {
626-
fprintf(stderr, "pthread_join failed\n");
633+
wc_mem_printf("pthread_join failed\n");
627634
return ret;
628635
}
629636

@@ -672,7 +679,7 @@ static WC_INLINE int StackSizeCheck_launch(struct func_args* args,
672679

673680
ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize);
674681
if (ret != 0 || myStack == NULL) {
675-
fprintf(stderr, "posix_memalign failed\n");
682+
wc_mem_printf("posix_memalign failed\n");
676683
free(shim_args);
677684
return -1;
678685
}
@@ -681,15 +688,15 @@ static WC_INLINE int StackSizeCheck_launch(struct func_args* args,
681688

682689
ret = pthread_attr_init(&myAttr);
683690
if (ret != 0) {
684-
fprintf(stderr, "attr_init failed\n");
691+
wc_mem_printf("attr_init failed\n");
685692
free(shim_args);
686693
free(myStack);
687694
return ret;
688695
}
689696

690697
ret = pthread_attr_setstack(&myAttr, myStack, stackSize);
691698
if (ret != 0) {
692-
fprintf(stderr, "attr_setstackaddr failed\n");
699+
wc_mem_printf("attr_setstackaddr failed\n");
693700
}
694701

695702
shim_args->myStack = myStack;
@@ -721,7 +728,7 @@ static WC_INLINE int StackSizeCheck_reap(pthread_t threadId, void *stack_context
721728
void *status;
722729
int ret = pthread_join(threadId, &status);
723730
if (ret != 0) {
724-
fprintf(stderr, "pthread_join failed\n");
731+
wc_mem_printf("pthread_join failed\n");
725732
return ret;
726733
}
727734

@@ -770,12 +777,12 @@ static WC_INLINE void StackTrap(void)
770777
{
771778
struct rlimit rl;
772779
if (getrlimit(RLIMIT_STACK, &rl) != 0) {
773-
fprintf(stderr, "getrlimit failed\n");
780+
wc_mem_printf("getrlimit failed\n");
774781
}
775782
printf("rlim_cur = %llu\n", rl.rlim_cur);
776783
rl.rlim_cur = 1024*21; /* adjust trap size here */
777784
if (setrlimit(RLIMIT_STACK, &rl) != 0) {
778-
fprintf(stderr, "setrlimit failed\n");
785+
wc_mem_printf("setrlimit failed\n");
779786
}
780787
}
781788

0 commit comments

Comments
 (0)