Skip to content

Commit 2a08d30

Browse files
authored
Merge pull request #7846 from douzzer/20240806-debug-trace-errcodes-backtrace
20240806-debug-trace-errcodes-backtrace
2 parents 0ab1f19 + c25d86c commit 2a08d30

9 files changed

Lines changed: 239 additions & 38 deletions

File tree

configure.ac

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,17 @@ AC_ARG_ENABLE([debug-trace-errcodes],
217217
[ ENABLED_DEBUG_TRACE_ERRCODES=no ]
218218
)
219219

220-
if test "$ENABLED_DEBUG_TRACE_ERRCODES" = "yes"
220+
if test "$ENABLED_DEBUG_TRACE_ERRCODES" != "no"
221221
then
222222
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_DEBUG_TRACE_ERROR_CODES"
223223
fi
224224

225+
if test "$ENABLED_DEBUG_TRACE_ERRCODES" = "backtrace"
226+
then
227+
AM_CFLAGS="$AM_CFLAGS -g -funwind-tables -DWOLFSSL_DEBUG_BACKTRACE_ERROR_CODES"
228+
AM_LDFLAGS="$AM_LDFLAGS -lbacktrace"
229+
fi
230+
225231
# Start without certificates enabled and enable if a certificate algorithm is
226232
# enabled
227233
ENABLED_CERTS="no"
@@ -9981,7 +9987,7 @@ echo "" >> $OPTION_FILE
99819987
echo "#endif /* WOLFSSL_OPTIONS_H */" >> $OPTION_FILE
99829988
echo "" >> $OPTION_FILE
99839989
9984-
if test "$ENABLED_DEBUG_TRACE_ERRCODES" = "yes"
9990+
if test "$ENABLED_DEBUG_TRACE_ERRCODES" != "no"
99859991
then
99869992
support/gen-debug-trace-error-codes.sh || AC_MSG_ERROR([Header generation for debug-trace-errcodes failed.])
99879993
fi

src/internal.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20653,7 +20653,11 @@ static void LogAlert(int type)
2065320653
typeStr = AlertTypeToString(type);
2065420654
if (typeStr != NULL) {
2065520655
char buff[60];
20656-
XSNPRINTF(buff, sizeof(buff), "Alert type: %s", typeStr);
20656+
if (XSNPRINTF(buff, sizeof(buff), "Alert type: %s", typeStr)
20657+
>= (int)sizeof(buff))
20658+
{
20659+
buff[sizeof(buff) - 1] = 0;
20660+
}
2065720661
WOLFSSL_MSG(buff);
2065820662
}
2065920663
#else

src/pk.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4691,7 +4691,9 @@ int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa)
46914691
mp_clear(t);
46924692

46934693
#ifdef WOLFSSL_SMALL_STACK
4694-
XFREE(tmp, rsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
4694+
if (rsa != NULL) {
4695+
XFREE(tmp, rsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
4696+
}
46954697
#endif
46964698

46974699
return ret;

support/gen-debug-trace-error-codes.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ BEGIN {
1212
print("#undef WOLFSSL_DEBUG_TRACE_ERROR_CODES_H") >> "wolfssl/debug-untrace-error-codes.h";
1313
}
1414
{
15-
if (match($0, "^[[:space:]]+([A-Z][A-Z0-9_]+)[[:space:]]*=[[:space:]]*(-[0-9]+)[,[:space:]]", errcode_a)) {
15+
if (match($0, "^[[:space:]]+([A-Z][A-Z0-9_]+)[[:space:]]*=[[:space:]]*(-[0-9]+)[,[:space:]]")) {
16+
17+
# for mawkward compatibility -- gawk allows errcode_a as the 3rd arg to match().
18+
gsub("^[[:space:]]+", "", $0);
19+
split($0, errcode_a, "[[:space:]=,]+");
20+
1621
if ((errcode_a[1] == "MIN_CODE_E") ||
1722
(errcode_a[1] == "WC_LAST_E") ||
1823
(errcode_a[1] == "MAX_CODE_E"))

tests/api.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78089,9 +78089,11 @@ static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
7808978089
(void)encInfo; /* not used in this test */
7809078090

7809178091
#ifdef DEBUG_WOLFSSL
78092-
fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
78093-
(ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
78094-
(*pDer)->length);
78092+
if (*pDer != NULL) {
78093+
fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
78094+
(ret == 0) ? "Success" : "Failure", ret, privKeyFile,
78095+
(int)key_sz, (*pDer)->length);
78096+
}
7809578097
#endif
7809678098

7809778099
return ret;

wolfcrypt/benchmark/benchmark.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,18 +1663,6 @@ static const char* bench_result_words3[][5] = {
16631663
const char *desc_extra);
16641664
#endif
16651665

1666-
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \
1667-
!defined(HAVE_STACK_SIZE)
1668-
#ifdef __cplusplus
1669-
extern "C" {
1670-
#endif
1671-
WOLFSSL_API int wolfSSL_Debugging_ON(void);
1672-
WOLFSSL_API void wolfSSL_Debugging_OFF(void);
1673-
#ifdef __cplusplus
1674-
} /* extern "C" */
1675-
#endif
1676-
#endif
1677-
16781666
#if !defined(WC_NO_RNG) && \
16791667
((!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) \
16801668
|| !defined(NO_DH) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_ECC) \

wolfcrypt/src/logging.c

Lines changed: 188 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -471,26 +471,48 @@ void WOLFSSL_BUFFER(const byte* buffer, word32 length)
471471

472472
while (buflen > 0) {
473473
int bufidx = 0;
474-
XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "\t");
474+
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "\t")
475+
>= (int)sizeof(line) - bufidx)
476+
{
477+
goto errout;
478+
}
475479
bufidx++;
476480

477481
for (i = 0; i < LINE_LEN; i++) {
478482
if (i < buflen) {
479-
XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "%02x ", buffer[i]);
483+
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "%02x ",
484+
buffer[i]) >= (int)sizeof(line) - bufidx)
485+
{
486+
goto errout;
487+
}
480488
}
481489
else {
482-
XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, " ");
490+
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, " ")
491+
>= (int)sizeof(line) - bufidx)
492+
{
493+
goto errout;
494+
}
483495
}
484496
bufidx += 3;
485497
}
486498

487-
XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "| ");
499+
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "| ")
500+
>= (int)sizeof(line) - bufidx)
501+
{
502+
goto errout;
503+
}
488504
bufidx++;
489505

490506
for (i = 0; i < LINE_LEN; i++) {
491507
if (i < buflen) {
492-
XSNPRINTF(&line[bufidx], sizeof(line)-bufidx,
493-
"%c", 31 < buffer[i] && buffer[i] < 127 ? buffer[i] : '.');
508+
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx,
509+
"%c", 31 < buffer[i] && buffer[i] < 127
510+
? buffer[i]
511+
: '.')
512+
>= (int)sizeof(line) - bufidx)
513+
{
514+
goto errout;
515+
}
494516
bufidx++;
495517
}
496518
}
@@ -499,14 +521,24 @@ void WOLFSSL_BUFFER(const byte* buffer, word32 length)
499521
buffer += LINE_LEN;
500522
buflen -= LINE_LEN;
501523
}
524+
525+
return;
526+
527+
errout:
528+
529+
wolfssl_log(INFO_LOG, NULL, 0, "\t[Buffer error while rendering]");
502530
}
503531

504532
#undef WOLFSSL_ENTER /* undo WOLFSSL_DEBUG_CODEPOINTS wrapper */
505533
void WOLFSSL_ENTER(const char* msg)
506534
{
507535
if (loggingEnabled) {
508536
char buffer[WOLFSSL_MAX_ERROR_SZ];
509-
XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg);
537+
if (XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg)
538+
>= (int)sizeof(buffer))
539+
{
540+
buffer[sizeof(buffer) - 1] = 0;
541+
}
510542
wolfssl_log(ENTER_LOG, NULL, 0, buffer);
511543
}
512544
}
@@ -516,7 +548,11 @@ void WOLFSSL_ENTER2(const char *file, int line, const char* msg)
516548
{
517549
if (loggingEnabled) {
518550
char buffer[WOLFSSL_MAX_ERROR_SZ];
519-
XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg);
551+
if (XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg)
552+
>= (int)sizeof(buffer))
553+
{
554+
buffer[sizeof(buffer) - 1] = 0;
555+
}
520556
wolfssl_log(ENTER_LOG, file, line, buffer);
521557
}
522558
}
@@ -527,8 +563,12 @@ void WOLFSSL_LEAVE(const char* msg, int ret)
527563
{
528564
if (loggingEnabled) {
529565
char buffer[WOLFSSL_MAX_ERROR_SZ];
530-
XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
531-
msg, ret);
566+
if (XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
567+
msg, ret)
568+
>= (int)sizeof(buffer))
569+
{
570+
buffer[sizeof(buffer) - 1] = 0;
571+
}
532572
wolfssl_log(LEAVE_LOG, NULL, 0, buffer);
533573
}
534574
}
@@ -538,8 +578,12 @@ void WOLFSSL_LEAVE2(const char *file, int line, const char* msg, int ret)
538578
{
539579
if (loggingEnabled) {
540580
char buffer[WOLFSSL_MAX_ERROR_SZ];
541-
XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
542-
msg, ret);
581+
if (XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
582+
msg, ret)
583+
>= (int)sizeof(buffer))
584+
{
585+
buffer[sizeof(buffer) - 1] = 0;
586+
}
543587
wolfssl_log(LEAVE_LOG, file, line, buffer);
544588
}
545589
}
@@ -1674,3 +1718,135 @@ void WOLFSSL_ERROR_MSG(const char* msg)
16741718
}
16751719

16761720
#endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
1721+
1722+
#ifdef WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES
1723+
1724+
#include <backtrace-supported.h>
1725+
1726+
#if BACKTRACE_SUPPORTED != 1
1727+
#error WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES is defined but BACKTRACE_SUPPORTED is 0.
1728+
#endif
1729+
1730+
#if !defined(WOLFSSL_MUTEX_INITIALIZER) && defined(WOLFSSL_NO_ATOMICS)
1731+
#error WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES requires WOLFSSL_MUTEX_INITIALIZER or wolfSSL_Atomic_Ints.
1732+
#endif
1733+
1734+
#include <backtrace.h>
1735+
1736+
static int backtrace_callback(void *data, uintptr_t pc, const char *filename,
1737+
int lineno, const char *function)
1738+
{
1739+
if (function == NULL)
1740+
return 0;
1741+
/* the first callback is for the call to wc_print_backtrace() -- skip it. */
1742+
if (*(int *)data == 0) {
1743+
*(int *)data = 1;
1744+
return 0;
1745+
}
1746+
#ifdef NO_STDIO_FILESYSTEM
1747+
printf(" #%d %p in %s %s:%d\n", (*(int *)data)++, (void *)pc,
1748+
function, filename, lineno);
1749+
#else
1750+
fprintf(stderr, " #%d %p in %s %s:%d\n", (*(int *)data)++, (void *)pc,
1751+
function, filename, lineno);
1752+
#endif
1753+
return 0;
1754+
}
1755+
1756+
static void backtrace_error(void *data, const char *msg, int errnum) {
1757+
(void)data;
1758+
#ifdef NO_STDIO_FILESYSTEM
1759+
printf("ERR TRACE: error %d while backtracing: %s", errnum, msg);
1760+
#else
1761+
fprintf(stderr, "ERR TRACE: error %d while backtracing: %s", errnum, msg);
1762+
#endif
1763+
}
1764+
1765+
static void backtrace_creation_error(void *data, const char *msg, int errnum) {
1766+
(void)data;
1767+
#ifdef NO_STDIO_FILESYSTEM
1768+
printf("ERR TRACE: internal error %d "
1769+
"while initializing backtrace facility: %s", errnum, msg);
1770+
printf("ERR TRACE: internal error "
1771+
"while initializing backtrace facility");
1772+
#else
1773+
fprintf(stderr, "ERR TRACE: internal error %d "
1774+
"while initializing backtrace facility: %s", errnum, msg);
1775+
#endif
1776+
}
1777+
1778+
static int backtrace_init(struct backtrace_state **backtrace_state) {
1779+
#ifdef WOLFSSL_MUTEX_INITIALIZER
1780+
static wolfSSL_Mutex backtrace_create_state_mutex =
1781+
WOLFSSL_MUTEX_INITIALIZER(backtrace_create_state_mutex);
1782+
if (wc_LockMutex(&backtrace_create_state_mutex) != 0)
1783+
return -1;
1784+
#elif defined(WOLFSSL_ATOMIC_OPS)
1785+
static wolfSSL_Atomic_Int init_count = 0;
1786+
if (wolfSSL_Atomic_Int_FetchAdd(&init_count, 1) != 1)
1787+
return -1;
1788+
#endif
1789+
if (*backtrace_state == NULL) {
1790+
/* passing a NULL filename to backtrace_create_state() tells
1791+
* libbacktrace to use a target-specific strategy to determine the
1792+
* executable. "libbacktrace supports ELF, PE/COFF, Mach-O, and XCOFF
1793+
* executables with DWARF debugging information. In other words, it
1794+
* supports GNU/Linux, *BSD, macOS, Windows, and AIX."
1795+
*/
1796+
*backtrace_state = backtrace_create_state(
1797+
NULL, 0, backtrace_creation_error, NULL);
1798+
}
1799+
#ifdef WOLFSSL_MUTEX_INITIALIZER
1800+
wc_UnLockMutex(&backtrace_create_state_mutex);
1801+
#endif
1802+
if (*backtrace_state == NULL)
1803+
return -1;
1804+
return 0;
1805+
}
1806+
1807+
void wc_backtrace_render(void) {
1808+
static wolfSSL_Mutex backtrace_mutex
1809+
WOLFSSL_MUTEX_INITIALIZER_CLAUSE(backtrace_mutex);
1810+
static struct backtrace_state *backtrace_state = NULL;
1811+
int depth = 0;
1812+
1813+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1814+
static wolfSSL_Atomic_Int init_count = 0;
1815+
if (init_count != 1) {
1816+
int cur_init_count = wolfSSL_Atomic_Int_FetchSub(&init_count, 1);
1817+
if (cur_init_count != 0) {
1818+
(void)wolfSSL_Atomic_Int_FetchAdd(&init_count, 1);
1819+
return;
1820+
}
1821+
if (wc_InitMutex(&backtrace_mutex) != 0)
1822+
return;
1823+
/* set init_count to 1, race-free: (-1) - (0-2) = 1 */
1824+
(void)wolfSSL_Atomic_Int_FetchSub(&init_count, cur_init_count - 2);
1825+
}
1826+
#endif
1827+
1828+
/* backtrace_state can't be shared between threads even when
1829+
* BACKTRACE_SUPPORTS_THREADS == 1, so we serialize the render op. this
1830+
* helpfully mutexes the initialization too.
1831+
*/
1832+
if (wc_LockMutex(&backtrace_mutex) != 0)
1833+
return;
1834+
1835+
if (backtrace_state == NULL) {
1836+
if (backtrace_init(&backtrace_state) < 0) {
1837+
wc_UnLockMutex(&backtrace_mutex);
1838+
return;
1839+
}
1840+
}
1841+
1842+
/* note that the optimizer can produce misleading backtraces, even with
1843+
* -funwind-tables. in contrast, the macro-generated "ERR TRACE" message
1844+
* from WC_ERR_TRACE() always accurately identifies the error code point.
1845+
*/
1846+
backtrace_full(backtrace_state, 0, backtrace_callback, backtrace_error,
1847+
(void *)&depth);
1848+
1849+
wc_UnLockMutex(&backtrace_mutex);
1850+
}
1851+
1852+
#endif /* WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES */

wolfcrypt/src/wc_kyber.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ int wc_KyberKey_MakeKeyWithRandom(KyberKey* key, const unsigned char* rand,
286286
}
287287

288288
/* Free dynamic memory allocated in function. */
289-
XFREE(a, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
289+
if (key != NULL) {
290+
XFREE(a, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
291+
}
290292

291293
return ret;
292294
}
@@ -890,7 +892,9 @@ int wc_KyberKey_Decapsulate(KyberKey* key, unsigned char* ss,
890892

891893
#ifndef USE_INTEL_SPEEDUP
892894
/* Dispose of dynamic memory allocated in function. */
893-
XFREE(cmp, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
895+
if (key != NULL) {
896+
XFREE(cmp, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
897+
}
894898
#endif
895899

896900
return ret;

0 commit comments

Comments
 (0)