Skip to content

Commit 987a705

Browse files
committed
Fix stack tracking in wolfCrypt benchmark
1 parent 7ad9c25 commit 987a705

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

wolfcrypt/benchmark/benchmark.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,18 @@ static WC_INLINE void bench_heap_checkpoint_measure(long* allocs,
327327
static WC_INLINE void bench_stack_checkpoint_prepare(void)
328328
{
329329
(void)StackSizeHWMReset();
330+
bench_last_stack_bytes = (long)StackSizeHWM_OffsetCorrected();
330331
}
331332

332333
static WC_INLINE long bench_stack_checkpoint_measure(void)
333334
{
334335
long used = (long)StackSizeHWM_OffsetCorrected();
336+
337+
used -= bench_last_stack_bytes;
335338
if (used < 0)
336339
used = 0;
337340
(void)StackSizeHWMReset();
338-
#ifdef WC_BENCH_STACK_TRACKING
339341
bench_last_stack_bytes = used;
340-
#endif
341342
return used;
342343
}
343344
#else
@@ -1345,7 +1346,7 @@ static const bench_pq_hash_sig_alg bench_pq_hash_sig_opt[] = {
13451346
};
13461347
#endif /* BENCH_PQ_STATEFUL_HBS */
13471348

1348-
#ifndef WOLFSSL_BENCHMARK_ALL
1349+
#if !defined(WOLFSSL_BENCHMARK_ALL) && !defined(MAIN_NO_ARGS)
13491350
#if defined(WOLFSSL_HAVE_MLKEM) || defined(HAVE_FALCON) || \
13501351
defined(HAVE_DILITHIUM) || defined(HAVE_SPHINCS)
13511352
/* The post-quantum-specific mapping of command line option to bit values and
@@ -2250,7 +2251,7 @@ static const char* bench_result_words2[][6] = {
22502251

22512252
/* maximum runtime for each benchmark */
22522253
#ifndef BENCH_MIN_RUNTIME_SEC
2253-
#define BENCH_MIN_RUNTIME_SEC 1.0F
2254+
#define BENCH_MIN_RUNTIME_SEC (double)1.0F
22542255
#endif
22552256

22562257
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM) || \
@@ -2349,8 +2350,12 @@ static int numBlocks = NUM_BLOCKS;
23492350
static word32 bench_size = BENCH_SIZE;
23502351
static int base2 = 1;
23512352
static int digest_stream = 1;
2353+
#ifndef NO_HMAC
23522354
static int mac_stream = 1;
2355+
#endif
2356+
#ifdef HAVE_AESGCM
23532357
static int aead_set_key = 0;
2358+
#endif
23542359
#ifdef HAVE_CHACHA
23552360
static int encrypt_only = 0;
23562361
#endif
@@ -3036,8 +3041,7 @@ static void bench_stats_sym_finish(const char* desc, int useDeviceID,
30363041
#ifdef WC_BENCH_STACK_TRACKING
30373042
{
30383043
long stackUsed = bench_stack_checkpoint_measure();
3039-
stackUsed += bench_stats_stack_setup_bytes;
3040-
bench_last_stack_bytes = stackUsed;
3044+
bench_last_stack_bytes = MAX(stackUsed, bench_stats_stack_setup_bytes);
30413045
}
30423046
bench_stats_stack_setup_bytes = 0;
30433047
#else
@@ -3276,8 +3280,7 @@ static void bench_stats_asym_finish_ex(const char* algo, int strength,
32763280
#ifdef WC_BENCH_STACK_TRACKING
32773281
{
32783282
long stackUsed = bench_stack_checkpoint_measure();
3279-
stackUsed += bench_stats_stack_setup_bytes;
3280-
bench_last_stack_bytes = stackUsed;
3283+
bench_last_stack_bytes = MAX(stackUsed, bench_stats_stack_setup_bytes);
32813284
}
32823285
bench_stats_stack_setup_bytes = 0;
32833286
#else
@@ -16417,8 +16420,16 @@ static void Usage(void)
1641716420
e += 3;
1641816421
#endif
1641916422
printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -dgst_full */
16423+
#ifndef NO_HMAC
1642016424
printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -mac_final */
16425+
#else
16426+
e++;
16427+
#endif
16428+
#ifdef HAVE_AESGCM
1642116429
printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -aead_set_key */
16430+
#else
16431+
e++;
16432+
#endif
1642216433
#ifndef NO_RSA
1642316434
printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -rsa_sign */
1642416435
#ifdef WOLFSSL_KEY_GEN
@@ -16625,10 +16636,14 @@ int wolfcrypt_benchmark_main(int argc, char** argv)
1662516636
#endif
1662616637
else if (string_matches(argv[1], "-dgst_full"))
1662716638
digest_stream = 0;
16639+
#ifndef NO_HMAC
1662816640
else if (string_matches(argv[1], "-mac_final"))
1662916641
mac_stream = 0;
16642+
#endif
16643+
#ifdef HAVE_AESGCM
1663016644
else if (string_matches(argv[1], "-aead_set_key"))
1663116645
aead_set_key = 1;
16646+
#endif
1663216647
#ifdef HAVE_CHACHA
1663316648
else if (string_matches(argv[1], "-enc_only"))
1663416649
encrypt_only = 1;

0 commit comments

Comments
 (0)