From 1c0df612476ae4025f6ddf0db0e8e128b40bf5fe Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Tue, 8 Dec 2020 00:19:18 -0600 Subject: [PATCH] wolfssl/test.h and wolfcrypt/test/test.c: add STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(), recognize macro WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES, and add to wolfcrypt_test() runtime settability of relative cumulative stack depth assert threshold using "-s stacksize". --- wolfcrypt/test/test.c | 54 +++++++++++++++++++++++++++++++++++-------- wolfssl/test.h | 40 +++++++++++++++++++++----------- 2 files changed, 71 insertions(+), 23 deletions(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 9cde77829..18b492384 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -33,14 +33,25 @@ #ifndef NO_CRYPT_TEST -/* only for stack size check */ -#ifdef HAVE_STACK_SIZE +#if defined(HAVE_STACK_SIZE) && !defined(HAVE_WOLFCRYPT_TEST_OPTIONS) + #define HAVE_WOLFCRYPT_TEST_OPTIONS +#endif + +#ifdef HAVE_WOLFCRYPT_TEST_OPTIONS #include #define err_sys err_sys_remap /* remap err_sys */ #include #undef err_sys +#endif + +#if defined(HAVE_STACK_SIZE_VERBOSE) +#ifdef WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES + static ssize_t max_relative_stack = WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES; #else - #define STACK_SIZE_CHECKPOINT(...) (__VA_ARGS__) + static ssize_t max_relative_stack = -1; +#endif +#else + #define STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max, ...) (__VA_ARGS__, 0) #define STACK_SIZE_INIT() #endif @@ -468,14 +479,14 @@ static int err_sys(const char* msg, int es) EXIT_TEST(-1); } -#ifndef HAVE_STACK_SIZE +#ifndef HAVE_WOLFCRYPT_TEST_OPTIONS /* func_args from test.h, so don't have to pull in other stuff */ typedef struct func_args { int argc; char** argv; int return_code; } func_args; -#endif /* !HAVE_STACK_SIZE */ +#endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */ #ifdef HAVE_FIPS @@ -529,13 +540,13 @@ static int wolfssl_pb_print(const char* msg, ...) { va_list args; va_start(args, fmt); - STACK_SIZE_CHECKPOINT(printf(fmt, args)); + STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args)); va_end(args); TEST_SLEEP(); } #else /* redirect to printf */ - #define test_pass(...) STACK_SIZE_CHECKPOINT(printf(__VA_ARGS__)) + #define test_pass(...) { if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, printf(__VA_ARGS__)) < 0) { return err_sys("post-test check failed", -1); }} /* stub the sleep macro */ #define TEST_SLEEP() #endif @@ -553,13 +564,32 @@ int wolfcrypt_test(void* args) printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING); printf("------------------------------------------------------------------------------\n"); - if (args) + if (args) { +#ifdef HAVE_WOLFCRYPT_TEST_OPTIONS + int ch; +#endif ((func_args*)args)->return_code = -1; /* error state */ +#ifdef HAVE_WOLFCRYPT_TEST_OPTIONS + while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:")) != -1) { + switch(ch) { + case 's': +#ifdef HAVE_STACK_SIZE_VERBOSE + max_relative_stack = (ssize_t)atoi(myoptarg); + break; +#else + return err_sys("-s (max relative stack size) requires HAVE_STACK_SIZE_VERBOSE.", -1); +#endif + default: + return err_sys("unknown test option.", -1); + } + } +#endif + } #ifdef WOLFSSL_STATIC_MEMORY if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory), WOLFMEM_GENERAL, 1) != 0) { - printf("unable to load static memory"); + printf("unable to load static memory.\n"); return(EXIT_FAILURE); } #endif @@ -1261,6 +1291,12 @@ initDefaultName(); #ifdef WOLFSSL_ESPIDF void app_main( ) #else + +#ifdef HAVE_WOLFCRYPT_TEST_OPTIONS + int myoptind = 0; + char* myoptarg = NULL; +#endif + int main(int argc, char** argv) #endif { diff --git a/wolfssl/test.h b/wolfssl/test.h index af4de0ab4..f3e6ae0be 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -2049,7 +2049,7 @@ struct stack_size_debug_context { * * enable with * - * CFLAGS='-g -DHAVE_STACK_SIZE_VERBOSE' ./configure --enable-stacksize [...] + * ./configure --enable-stacksize=verbose [...] */ static void *debug_stack_size_verbose_shim(struct stack_size_debug_context *shim_args) { @@ -2123,12 +2123,24 @@ int StackSizeHWMReset(void) #define STACK_SIZE_CHECKPOINT(...) ({ \ ssize_t HWM = StackSizeHWM_OffsetCorrected(); \ - int _ret = (__VA_ARGS__); \ - printf("relative stack used = %ld\n", HWM); \ - StackSizeHWMReset(); \ - _ret; \ + __VA_ARGS__; \ + printf("relative stack used = %ld\n", HWM); \ + StackSizeHWMReset(); \ }) +#define STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max, ...) ({ \ + ssize_t HWM = StackSizeHWM_OffsetCorrected(); \ + __VA_ARGS__; \ + printf("relative stack used = %ld\n", HWM); \ + int _ret = StackSizeHWMReset(); \ + if ((max >= 0) && (HWM > (ssize_t)(max))) { \ + printf("relative stack usage at %s L%d exceeds designated max %ld.\n", __FILE__, __LINE__, (ssize_t)(max)); \ + _ret = -1; \ + } \ + _ret; \ + }) + + #ifdef __GNUC__ #define STACK_SIZE_INIT() (void)StackSizeSetOffset(__FUNCTION__, __builtin_frame_address(0)) #endif @@ -2144,6 +2156,9 @@ static WC_INLINE int StackSizeCheck(func_args* args, thread_func tf) size_t stackSize = 1024*1024; pthread_attr_t myAttr; pthread_t threadId; +#ifdef HAVE_STACK_SIZE_VERBOSE + struct stack_size_debug_context shim_args; +#endif #ifdef PTHREAD_STACK_MIN if (stackSize < PTHREAD_STACK_MIN) @@ -2166,15 +2181,12 @@ static WC_INLINE int StackSizeCheck(func_args* args, thread_func tf) #ifdef HAVE_STACK_SIZE_VERBOSE StackSizeCheck_stackSizeHWM = 0; - { - struct stack_size_debug_context shim_args; - shim_args.myStack = myStack; - shim_args.stackSize = stackSize; - shim_args.stackSizeHWM_ptr = &StackSizeCheck_stackSizeHWM; - shim_args.fn = tf; - shim_args.args = args; - ret = pthread_create(&threadId, &myAttr, (thread_func)debug_stack_size_verbose_shim, (void *)&shim_args); - } + shim_args.myStack = myStack; + shim_args.stackSize = stackSize; + shim_args.stackSizeHWM_ptr = &StackSizeCheck_stackSizeHWM; + shim_args.fn = tf; + shim_args.args = args; + ret = pthread_create(&threadId, &myAttr, (thread_func)debug_stack_size_verbose_shim, (void *)&shim_args); #else ret = pthread_create(&threadId, &myAttr, tf, args); #endif