Skip to content

Commit 0afbc1e

Browse files
authored
Merge pull request #9471 from douzzer/20251124-memory_test-wolfSSL_Atomic_Ptr_CompareExchange
20251124-memory_test-wolfSSL_Atomic_Ptr_CompareExchange
2 parents ea0793f + e459b21 commit 0afbc1e

File tree

5 files changed

+26
-20
lines changed

5 files changed

+26
-20
lines changed

src/ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7633,7 +7633,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx)
76337633
#ifdef WOLFSSL_ATOMIC_OPS
76347634
WOLFSSL_EVP_PKEY *current_pkey = NULL;
76357635
if (! wolfSSL_Atomic_Ptr_CompareExchange(
7636-
(void **)&ctx->privateKeyPKey,
7636+
(void * volatile *)&ctx->privateKeyPKey,
76377637
(void **)&current_pkey, res))
76387638
{
76397639
wolfSSL_EVP_PKEY_free(res);

wolfcrypt/src/wc_port.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ int wolfSSL_Atomic_Uint_CompareExchange(
13591359
}
13601360

13611361
int wolfSSL_Atomic_Ptr_CompareExchange(
1362-
void **c, void **expected_ptr, void *new_ptr)
1362+
void * volatile *c, void **expected_ptr, void *new_ptr)
13631363
{
13641364
uintptr_t exp = (uintptr_t)*expected_ptr;
13651365
int ret = atomic_fcmpset_ptr((uintptr_t *)c, &exp, (uintptr_t)new_ptr);
@@ -1456,7 +1456,7 @@ int wolfSSL_Atomic_Uint_CompareExchange(
14561456
}
14571457

14581458
int wolfSSL_Atomic_Ptr_CompareExchange(
1459-
void **c, void **expected_ptr, void *new_ptr)
1459+
void * volatile *c, void **expected_ptr, void *new_ptr)
14601460
{
14611461
/* use gcc-built-in __atomic_compare_exchange_n(), not
14621462
* atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
@@ -1551,7 +1551,7 @@ int wolfSSL_Atomic_Uint_CompareExchange(
15511551
}
15521552

15531553
int wolfSSL_Atomic_Ptr_CompareExchange(
1554-
void **c, void **expected_ptr, void *new_ptr)
1554+
void * volatile *c, void **expected_ptr, void *new_ptr)
15551555
{
15561556
return __atomic_compare_exchange_n(
15571557
c, expected_ptr, new_ptr, 0 /* weak */,
@@ -1651,7 +1651,7 @@ int wolfSSL_Atomic_Uint_CompareExchange(
16511651
}
16521652

16531653
int wolfSSL_Atomic_Ptr_CompareExchange(
1654-
void ** c, void **expected_ptr, void *new_ptr)
1654+
void * volatile * c, void **expected_ptr, void *new_ptr)
16551655
{
16561656
#ifdef _WIN64
16571657
LONG64 actual_ptr = InterlockedCompareExchange64(

wolfcrypt/test/test.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20061,8 +20061,6 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
2006120061
#endif
2006220062
int int_expected;
2006320063
unsigned int uint_expected;
20064-
void * a_ptr = NULL;
20065-
void * ptr_expected = NULL;
2006620064

2006720065
if (WOLFSSL_ATOMIC_LOAD(a_int) != -2)
2006820066
return WC_TEST_RET_ENC_NC;
@@ -20134,12 +20132,17 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
2013420132
if (WOLFSSL_ATOMIC_LOAD(a_uint) != 7)
2013520133
return WC_TEST_RET_ENC_NC;
2013620134

20137-
a_ptr = NULL;
20138-
ptr_expected = NULL;
20139-
if (! wolfSSL_Atomic_Ptr_CompareExchange(&a_ptr, &ptr_expected, &ret))
20140-
return WC_TEST_RET_ENC_NC;
20141-
if (a_ptr != &ret)
20142-
return WC_TEST_RET_ENC_NC;
20135+
{
20136+
void * volatile a_ptr = NULL;
20137+
void * ptr_expected = NULL;
20138+
static const char s[] = "";
20139+
if (! wolfSSL_Atomic_Ptr_CompareExchange(&a_ptr,
20140+
&ptr_expected,
20141+
(void *)&s))
20142+
return WC_TEST_RET_ENC_NC;
20143+
if (a_ptr != s)
20144+
return WC_TEST_RET_ENC_NC;
20145+
}
2014320146
}
2014420147

2014520148
return ret;

wolfssl/internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3806,7 +3806,10 @@ struct WOLFSSL_CTX {
38063806
int altPrivateKeyDevId;
38073807
#endif /* WOLFSSL_DUAL_ALG_CERTS */
38083808
#ifdef OPENSSL_ALL
3809-
WOLFSSL_EVP_PKEY* privateKeyPKey;
3809+
/* note it is the privateKeyPKey pointer that is volatile, not the object it
3810+
* points to:
3811+
*/
3812+
WOLFSSL_EVP_PKEY* volatile privateKeyPKey;
38103813
#endif
38113814
WOLFSSL_CERT_MANAGER* cm; /* our cert manager, ctx owns SSL will use */
38123815
#endif

wolfssl/wolfcrypt/wc_port.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@
552552
#define WOLFSSL_ATOMIC_STORE(x, val) (x) = (val)
553553
#endif /* WOLFSSL_NO_ATOMICS */
554554

555-
/* WOLFSSL_ATOMIC_COERCE_INT() needs to accept either a regular int or an
555+
/* WOLFSSL_ATOMIC_COERCE_INT() needs to accept either a regular int or a
556556
* wolfSSL_Atomic_Int as its argument, and evaluate to a regular int.
557557
* Allows a user-supplied override definition with type introspection.
558558
*/
@@ -593,7 +593,7 @@
593593
WOLFSSL_API int wolfSSL_Atomic_Uint_CompareExchange(
594594
wolfSSL_Atomic_Uint* c, unsigned int *expected_i, unsigned int new_i);
595595
WOLFSSL_API int wolfSSL_Atomic_Ptr_CompareExchange(
596-
void** c, void **expected_ptr, void *new_ptr);
596+
void* volatile * c, void **expected_ptr, void *new_ptr);
597597
#else
598598
/* Code using these fallback implementations in non-SINGLE_THREADED builds
599599
* needs to arrange its own explicit fallback to int for wolfSSL_Atomic_Int
@@ -632,14 +632,14 @@
632632
}
633633
}
634634
static WC_INLINE int wolfSSL_Atomic_Ptr_CompareExchange(
635-
void **c, void *expected_ptr, void *new_ptr)
635+
void * volatile *c, void *expected_ptr, void *new_ptr)
636636
{
637-
if (*(char **)c == *(char **)expected_ptr) {
638-
*(char **)c = (char *)new_ptr;
637+
if (*(char * volatile *)c == *(char **)expected_ptr) {
638+
*(char * volatile *)c = (char *)new_ptr;
639639
return 1;
640640
}
641641
else {
642-
*(char **)expected_ptr = *(char **)c;
642+
*(char * volatile *)expected_ptr = *(char **)c;
643643
return 0;
644644
}
645645
}

0 commit comments

Comments
 (0)