From 85a8c0606258019b3461baab27c8ce984f9d9af5 Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Mon, 18 Oct 2021 23:45:02 -0500 Subject: [PATCH] linuxkm: add DEBUG_VECTOR_REGISTER_ACCESS (debug feature switch), ASSERT_SAVED_VECTOR_REGISTERS, and ASSERT_RESTORED_VECTOR_REGISTERS macros, and move the fallback no-op definitions of the SAVE_VECTOR_REGISTERS and RESTORE_VECTOR_REGISTERS to types.h. also fixed several ASCII TAB characters in types.h. --- wolfcrypt/src/logging.c | 6 ++++++ wolfcrypt/test/test.c | 2 ++ wolfssl/wolfcrypt/types.h | 40 +++++++++++++++++++++++++++---------- wolfssl/wolfcrypt/wc_port.h | 15 -------------- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index 06b3c9e7b..31b321256 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -186,6 +186,12 @@ static struct log mynewt_log; #endif /* DEBUG_WOLFSSL */ +#ifdef DEBUG_VECTOR_REGISTER_ACCESS +THREAD_LS_T int wc_svr_count = 0; +THREAD_LS_T const char *wc_svr_last_file = NULL; +THREAD_LS_T int wc_svr_last_line = -1; +#endif + /* allow this to be set to NULL, so logs can be redirected to default output */ int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index b5dc9784b..aea603659 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -639,6 +639,7 @@ static int wolfssl_pb_print(const char* msg, ...) va_end(args); PRINT_HEAP_CHECKPOINT(); TEST_SLEEP(); + ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); } #else /* redirect to printf */ @@ -648,6 +649,7 @@ static int wolfssl_pb_print(const char* msg, ...) return err_sys("post-test check failed", -1); \ } \ PRINT_HEAP_CHECKPOINT(); \ + ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \ } /* stub the sleep macro */ #define TEST_SLEEP() diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 88f155ad7..8fba2946a 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -443,7 +443,7 @@ decouple library dependencies with standard string, memory and so on. #endif #elif defined(WOLFSSL_LINUXKM) - /* the requisite linux/slab.h is included in wc_port.h, with incompatible warnings masked out. */ + /* the requisite linux/slab.h is included in wc_port.h, with incompatible warnings masked out. */ #define XMALLOC(s, h, t) ({(void)(h); (void)(t); kmalloc(s, GFP_KERNEL);}) #define XFREE(p, h, t) ({void* _xp; (void)(h); _xp = (p); if(_xp) kfree(_xp);}) #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); krealloc((p), (n), GFP_KERNEL);}) @@ -553,17 +553,17 @@ decouple library dependencies with standard string, memory and so on. #define USE_WOLF_STRSEP #endif - #ifndef STRING_USER + #ifndef STRING_USER #if defined(WOLFSSL_LINUXKM) #include #else #include #endif - #define XMEMCPY(d,s,l) memcpy((d),(s),(l)) - #define XMEMSET(b,c,l) memset((b),(c),(l)) - #define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n)) - #define XMEMMOVE(d,s,l) memmove((d),(s),(l)) + #define XMEMCPY(d,s,l) memcpy((d),(s),(l)) + #define XMEMSET(b,c,l) memset((b),(c),(l)) + #define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n)) + #define XMEMMOVE(d,s,l) memmove((d),(s),(l)) #define XSTRLEN(s1) strlen((s1)) #define XSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n)) @@ -720,11 +720,11 @@ decouple library dependencies with standard string, memory and so on. #endif #endif /* OPENSSL_EXTRA */ - #ifndef CTYPE_USER + #ifndef CTYPE_USER #ifndef WOLFSSL_LINUXKM #include #endif - #if defined(HAVE_ECC) || defined(HAVE_OCSP) || \ + #if defined(HAVE_ECC) || defined(HAVE_OCSP) || \ defined(WOLFSSL_KEY_GEN) || !defined(NO_DSA) || \ defined(OPENSSL_EXTRA) #define XTOUPPER(c) toupper((c)) @@ -1179,8 +1179,28 @@ decouple library dependencies with standard string, memory and so on. #define PRAGMA_CLANG_DIAG_POP #endif - - + #ifdef DEBUG_VECTOR_REGISTER_ACCESS + WOLFSSL_API extern THREAD_LS_T int wc_svr_count; + WOLFSSL_API extern THREAD_LS_T const char *wc_svr_last_file; + WOLFSSL_API extern THREAD_LS_T int wc_svr_last_line; + #define SAVE_VECTOR_REGISTERS(...) { ++wc_svr_count; if (wc_svr_count > 5) {fprintf(stderr, "%s @ L%d : incr : wc_svr_count %d (last op %s L%d)\n", __FILE__, __LINE__, wc_svr_count, wc_svr_last_file, wc_svr_last_line);} wc_svr_last_file = __FILE__; wc_svr_last_line = __LINE__; } + #define ASSERT_SAVED_VECTOR_REGISTERS(fail_clause) {if (wc_svr_count <= 0) {fprintf(stderr, "ASSERT_SAVED_VECTOR_REGISTERS : %s @ L%d : wc_svr_count %d (last op %s L%d)\n", __FILE__, __LINE__, wc_svr_count, wc_svr_last_file, wc_svr_last_line); { fail_clause }}} + #define ASSERT_RESTORED_VECTOR_REGISTERS(fail_clause) {if (wc_svr_count != 0) {fprintf(stderr, "ASSERT_RESTORED_VECTOR_REGISTERS : %s @ L%d : wc_svr_count %d (last op %s L%d)\n", __FILE__, __LINE__, wc_svr_count, wc_svr_last_file, wc_svr_last_line); { fail_clause }}} + #define RESTORE_VECTOR_REGISTERS(...) {--wc_svr_count; if (wc_svr_count > 4) {fprintf(stderr, "%s @ L%d : decr : wc_svr_count %d (last op %s L%d)\n", __FILE__, __LINE__, wc_svr_count, wc_svr_last_file, wc_svr_last_line);} wc_svr_last_file = __FILE__; wc_svr_last_line = __LINE__; } + #else + #ifndef SAVE_VECTOR_REGISTERS + #define SAVE_VECTOR_REGISTERS(...) do{}while(0) + #endif + #ifndef ASSERT_SAVED_VECTOR_REGISTERS + #define ASSERT_SAVED_VECTOR_REGISTERS(...) do{}while(0) + #endif + #ifndef ASSERT_RESTORED_VECTOR_REGISTERS + #define ASSERT_RESTORED_VECTOR_REGISTERS(...) do{}while(0) + #endif + #ifndef RESTORE_VECTOR_REGISTERS + #define RESTORE_VECTOR_REGISTERS() do{}while(0) + #endif + #endif #ifdef __cplusplus } /* extern "C" */ diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 596d94da6..e8662d0cb 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -161,12 +161,6 @@ #ifndef WOLFSSL_NO_ASM #define WOLFSSL_NO_ASM #endif - #ifndef SAVE_VECTOR_REGISTERS - #define SAVE_VECTOR_REGISTERS(...) ({}) - #endif - #ifndef RESTORE_VECTOR_REGISTERS - #define RESTORE_VECTOR_REGISTERS() ({}) - #endif #endif _Pragma("GCC diagnostic pop"); @@ -617,15 +611,6 @@ #endif /* BUILDING_WOLFSSL */ -#else /* ! WOLFSSL_LINUXKM */ - - #ifndef SAVE_VECTOR_REGISTERS - #define SAVE_VECTOR_REGISTERS(...) do{}while(0) - #endif - #ifndef RESTORE_VECTOR_REGISTERS - #define RESTORE_VECTOR_REGISTERS() do{}while(0) - #endif - #endif /* WOLFSSL_LINUXKM */ /* THREADING/MUTEX SECTION */