Skip to content

Commit 072c531

Browse files
committed
m4/ax_atomic.m4: fixes for C++ compatibility.
wolfssl/wolfcrypt/wc_port.h: add WOLFSSL_API attribute to wolfSSL_Atomic_Int_Init, wolfSSL_Atomic_Int_FetchAdd, and wolfSSL_Atomic_Int_FetchAdd, and add fallback definitions for them, allowing elimination of SINGLE_THREADED implementations of wolfSSL_Ref*(), and allowing ungated use of wolfSSL_Atomic_* calls in api.c. wolfcrypt/src/dh.c: in wc_DhAgree_ct(), remove frivolous XMEMSET() and stray semicolon. wolfcrypt/benchmark/benchmark.c: fix bench_rsaKeyGen() to skip tests of key sizes below RSA_MIN_SIZE, and add 4096 bit benchmark if RSA_MAX_SIZE is big enough. tests/unit.h: * adopt definitions of TEST_FAIL, TEST_SUCCESS, and TEST_SKIPPED from unit.c, remap TEST_SKIPPED from -7777 to 3, and add TEST_SUCCESS_NO_MSGS, TEST_SKIPPED_NO_MSGS, EXPECT_DECLS_NO_MSGS(), and EXPECT_FAILURE_CODEPOINT_ID, to support existing and future expected-particular-failure test cases without log noise. * rename outer gate from CyaSSL_UNIT_H to TESTS_UNIT_H. tests/api.c: * use EXPECT_DECLS_NO_MSGS() in test_ssl_memio_setup(), test_ssl_memio_read_write(), and test_wolfSSL_client_server_nofail_memio(), and globally update affected expected error codes to correspond. * use atomics for {client,server}SessRemCount{Malloc,free} to fix races in SessRemCtxCb() and SessRemSslSetupCb().
1 parent b990840 commit 072c531

6 files changed

Lines changed: 138 additions & 90 deletions

File tree

m4/ax_atomic.m4

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ AC_DEFUN([AC_C___ATOMIC],
99
[[int
1010
main (int argc, char **argv)
1111
{
12-
volatile unsigned long ul1 = 1, ul2 = 0, ul3 = 2;
12+
volatile unsigned long ul1 = 1;
13+
unsigned long ul2 = 0, ul3 = 2;
1314
__atomic_load_n(&ul1, __ATOMIC_SEQ_CST);
1415
__atomic_compare_exchange(&ul1, &ul2, &ul3, 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
1516
__atomic_fetch_add(&ul1, 1, __ATOMIC_SEQ_CST);
16-
__atomic_fetch_sub(&ul3, 1, __ATOMIC_SEQ_CST);
17+
__atomic_fetch_sub(&ul1, 1, __ATOMIC_SEQ_CST);
1718
__atomic_or_fetch(&ul1, ul2, __ATOMIC_SEQ_CST);
1819
__atomic_and_fetch(&ul1, ul2, __ATOMIC_SEQ_CST);
19-
volatile unsigned long long ull1 = 1, ull2 = 0, ull3 = 2;
20+
volatile unsigned long long ull1 = 1;
21+
unsigned long long ull2 = 0, ull3 = 2;
2022
__atomic_load_n(&ull1, __ATOMIC_SEQ_CST);
2123
__atomic_compare_exchange(&ull1, &ull2, &ull3, 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
2224
__atomic_fetch_add(&ull1, 1, __ATOMIC_SEQ_CST);
23-
__atomic_fetch_sub(&ull3, 1, __ATOMIC_SEQ_CST);
25+
__atomic_fetch_sub(&ull1, 1, __ATOMIC_SEQ_CST);
2426
__atomic_or_fetch(&ull1, ull2, __ATOMIC_SEQ_CST);
2527
__atomic_and_fetch(&ull1, ull2, __ATOMIC_SEQ_CST);
2628
return 0;

tests/api.c

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,6 @@ int tmpDirNameSet = 0;
532532
| Constants
533533
*----------------------------------------------------------------------------*/
534534

535-
/* Test result constants and macros. */
536-
537-
/* Test succeeded. */
538-
#define TEST_SUCCESS (1)
539-
/* Test failed. */
540-
#define TEST_FAIL (0)
541-
/* Test skipped - not run. */
542-
#define TEST_SKIPPED (-7777)
543-
544535
/* Returns the result based on whether check is true.
545536
*
546537
* @param [in] check Condition for success.
@@ -7291,7 +7282,7 @@ static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
72917282

72927283
static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
72937284
{
7294-
EXPECT_DECLS;
7285+
EXPECT_DECLS_NO_MSGS(-2000);
72957286
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
72967287
int c_sharedCtx = 0;
72977288
int s_sharedCtx = 0;
@@ -7564,7 +7555,7 @@ static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
75647555

75657556
static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
75667557
{
7567-
EXPECT_DECLS;
7558+
EXPECT_DECLS_NO_MSGS(-3000);
75687559
char input[1024];
75697560
int idx = 0;
75707561
const char* msg_c = "hello wolfssl!";
@@ -7653,7 +7644,14 @@ static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
76537644
int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
76547645
test_ssl_cbf* server_cb, cbType client_on_handshake)
76557646
{
7656-
EXPECT_DECLS;
7647+
/* We use EXPECT_DECLS_NO_MSGS() here because this helper routine is used
7648+
* for numerous but varied expected-to-fail scenarios that should not emit
7649+
* error messages on the expected failures. Instead, we return a distinct
7650+
* code for each failure point, allowing the caller to assert on a
7651+
* particular mode of expected failure. On success, the usual TEST_SUCCESS
7652+
* is returned.
7653+
*/
7654+
EXPECT_DECLS_NO_MSGS(-1000);
76577655
struct test_ssl_memio_ctx test_ctx;
76587656
#ifdef WOLFSSL_HAVE_TLS_UNIQUE
76597657
size_t msg_len;
@@ -7665,8 +7663,8 @@ int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
76657663

76667664
test_ctx.c_ctx = client_cb->ctx;
76677665
test_ctx.s_ctx = server_cb->ctx;
7668-
test_ctx.c_cb.return_code = TEST_FAIL;
7669-
test_ctx.s_cb.return_code = TEST_FAIL;
7666+
test_ctx.c_cb.return_code = EXPECT_FAILURE_CODEPOINT_ID;
7667+
test_ctx.s_cb.return_code = EXPECT_FAILURE_CODEPOINT_ID;
76707668

76717669
ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
76727670
ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
@@ -9575,10 +9573,10 @@ static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
95759573
* therefore, handshake becomes failure.
95769574
*/
95779575
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
9578-
&server_cbf, NULL), TEST_FAIL);
9576+
&server_cbf, NULL), -1001);
95799577

9580-
ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
9581-
ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
9578+
ExpectIntEQ(client_cbf.return_code, -1000);
9579+
ExpectIntEQ(server_cbf.return_code, -1000);
95829580
ExpectIntEQ(client_cbf.last_err, WC_NO_ERR_TRACE(MAX_CHAIN_ERROR));
95839581
ExpectIntEQ(server_cbf.last_err, WC_NO_ERR_TRACE(FATAL_ERROR));
95849582
#endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
@@ -14120,7 +14118,7 @@ static int test_wolfSSL_X509_TLS_version_test_1(void)
1412014118

1412114119
#ifndef OPENSSL_COMPATIBLE_DEFAULTS
1412214120
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
14123-
&func_cb_server, NULL), TEST_FAIL);
14121+
&func_cb_server, NULL), -1001);
1412414122
#else
1412514123
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
1412614124
&func_cb_server, NULL), TEST_SUCCESS);
@@ -61861,7 +61859,7 @@ static int test_wolfSSL_curves_mismatch(void)
6186161859
func_cb_server.method = test_params[i].server_meth;
6186261860

6186361861
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
61864-
&func_cb_server, NULL), TEST_FAIL);
61862+
&func_cb_server, NULL), -1001);
6186561863
ExpectIntEQ(func_cb_client.last_err, test_params[i].client_last_err);
6186661864
ExpectIntEQ(func_cb_server.last_err, test_params[i].server_last_err);
6186761865

@@ -69656,10 +69654,16 @@ static int test_wolfSSL_SESSION_expire_downgrade(void)
6965669654

6965769655
#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
6965869656
defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
69659-
static int clientSessRemCountMalloc = 0;
69660-
static int serverSessRemCountMalloc = 0;
69661-
static int clientSessRemCountFree = 0;
69662-
static int serverSessRemCountFree = 0;
69657+
#ifdef WOLFSSL_ATOMIC_OPS
69658+
typedef wolfSSL_Atomic_Int SessRemCounter_t;
69659+
#else
69660+
typedef int SessRemCounter_t;
69661+
#endif
69662+
static SessRemCounter_t clientSessRemCountMalloc;
69663+
static SessRemCounter_t serverSessRemCountMalloc;
69664+
static SessRemCounter_t clientSessRemCountFree;
69665+
static SessRemCounter_t serverSessRemCountFree;
69666+
6966369667
static WOLFSSL_CTX* serverSessCtx = NULL;
6966469668
static WOLFSSL_SESSION* serverSess = NULL;
6966569669
#if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
@@ -69680,9 +69684,9 @@ static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
6968069684
side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
6968169685
if (side != NULL) {
6968269686
if (*side == WOLFSSL_CLIENT_END)
69683-
clientSessRemCountFree++;
69687+
(void)wolfSSL_Atomic_Int_FetchAdd(&clientSessRemCountFree, 1);
6968469688
else
69685-
serverSessRemCountFree++;
69689+
(void)wolfSSL_Atomic_Int_FetchAdd(&serverSessRemCountFree, 1);
6968669690

6968769691
SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
6968869692
}
@@ -69719,14 +69723,14 @@ static int SessRemSslSetupCb(WOLFSSL* ssl)
6971969723

6972069724
if (SSL_is_server(ssl)) {
6972169725
side = &sessRemCtx_Server;
69722-
serverSessRemCountMalloc++;
69726+
(void)wolfSSL_Atomic_Int_FetchAdd(&serverSessRemCountMalloc, 1);
6972369727
ExpectNotNull(serverSess = SSL_get1_session(ssl));
6972469728
ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
6972569729
SSL_SUCCESS);
6972669730
}
6972769731
else {
6972869732
side = &sessRemCtx_Client;
69729-
clientSessRemCountMalloc++;
69733+
(void)wolfSSL_Atomic_Int_FetchAdd(&clientSessRemCountMalloc, 1);
6973069734
#if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
6973169735
!defined(NO_SESSION_CACHE_REF)
6973269736
ExpectNotNull(clientSess = SSL_get1_session(ssl));
@@ -69750,6 +69754,11 @@ static int test_wolfSSL_CTX_sess_set_remove_cb(void)
6975069754
* session object */
6975169755
test_ssl_cbf func_cb;
6975269756

69757+
wolfSSL_Atomic_Int_Init(&clientSessRemCountMalloc, 0);
69758+
wolfSSL_Atomic_Int_Init(&serverSessRemCountMalloc, 0);
69759+
wolfSSL_Atomic_Int_Init(&clientSessRemCountFree, 0);
69760+
wolfSSL_Atomic_Int_Init(&serverSessRemCountFree, 0);
69761+
6975369762
XMEMSET(&func_cb, 0, sizeof(func_cb));
6975469763
func_cb.ctx_ready = SessRemCtxSetupCb;
6975569764
func_cb.on_result = SessRemSslSetupCb;
@@ -78615,7 +78624,7 @@ static int test_DhCallbacks(void)
7861578624
func_cb_server.method = wolfTLSv1_2_server_method;
7861678625

7861778626
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
78618-
&func_cb_server, NULL), TEST_FAIL);
78627+
&func_cb_server, NULL), -1001);
7861978628
#endif
7862078629
return EXPECT_RESULT();
7862178630
}
@@ -85792,7 +85801,7 @@ static int test_multiple_crls_same_issuer(void)
8579285801
client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
8579385802

8579485803
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
85795-
&server_cbs, NULL), TEST_FAIL);
85804+
&server_cbs, NULL), -1001);
8579685805
}
8579785806
#endif
8579885807
return EXPECT_RESULT();
@@ -90339,7 +90348,7 @@ static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
9033990348
server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
9034090349

9034190350
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
90342-
&server_cbs, NULL), TEST_FAIL);
90351+
&server_cbs, NULL), -1001);
9034390352

9034490353
return EXPECT_RESULT();
9034590354
}
@@ -91146,7 +91155,7 @@ static int test_override_alt_cert_chain(void)
9114691155
{test_override_alt_cert_chain_client_ctx_ready,
9114791156
test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
9114891157
{test_override_alt_cert_chain_client_ctx_ready2,
91149-
test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
91158+
test_override_alt_cert_chain_server_ctx_ready, -1001},
9115091159
};
9115191160

9115291161
for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
@@ -91162,8 +91171,10 @@ static int test_override_alt_cert_chain(void)
9116291171
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
9116391172
&server_cbs, NULL), params[i].result);
9116491173

91165-
ExpectIntEQ(client_cbs.return_code, params[i].result);
91166-
ExpectIntEQ(server_cbs.return_code, params[i].result);
91174+
ExpectIntEQ(client_cbs.return_code,
91175+
params[i].result <= 0 ? -1000 : TEST_SUCCESS);
91176+
ExpectIntEQ(server_cbs.return_code,
91177+
params[i].result <= 0 ? -1000 : TEST_SUCCESS);
9116791178
}
9116891179

9116991180
return EXPECT_RESULT();
@@ -93766,7 +93777,7 @@ static int test_revoked_loaded_int_cert(void)
9376693777
client_cbf.ctx_ready = test_params[i].client_ctx_ready;
9376793778

9376893779
ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
93769-
&server_cbf, NULL), TEST_FAIL);
93780+
&server_cbf, NULL), -1001);
9377093781
ExpectIntEQ(client_cbf.last_err, WC_NO_ERR_TRACE(CRL_CERT_REVOKED));
9377193782
ExpectIntEQ(server_cbf.last_err, WC_NO_ERR_TRACE(FATAL_ERROR));
9377293783

tests/unit.h

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*/
2121

2222

23-
#ifndef CyaSSL_UNIT_H
24-
#define CyaSSL_UNIT_H
23+
#ifndef TESTS_UNIT_H
24+
#define TESTS_UNIT_H
2525

2626
#include <wolfssl/ssl.h>
2727
#include <wolfssl/test.h> /* thread and tcp stuff */
@@ -121,42 +121,70 @@
121121
#define AssertPtrGE(x, y) AssertPtr(x, y, >=, <)
122122
#define AssertPtrLE(x, y) AssertPtr(x, y, <=, >)
123123

124+
#define TEST_FAIL 0
125+
#define TEST_SUCCESS 1
126+
#define TEST_SUCCESS_NO_MSGS 2
127+
#define TEST_SKIPPED 3 /* Test skipped - not run. */
128+
#define TEST_SKIPPED_NO_MSGS 4 /* Test skipped - not run. */
124129

125130
#define EXPECT_DECLS \
126-
int _ret = TEST_SKIPPED
131+
int _ret = TEST_SKIPPED, _fail_codepoint_id = TEST_FAIL
132+
#define EXPECT_DECLS_NO_MSGS(fail_codepoint_offset) \
133+
int _ret = TEST_SKIPPED_NO_MSGS, \
134+
_fail_codepoint_id = (fail_codepoint_offset)
135+
#define EXPECT_FAILURE_CODEPOINT_ID _fail_codepoint_id
127136
#define EXPECT_RESULT() \
128-
_ret
137+
((void)_fail_codepoint_id, \
138+
_ret == TEST_SUCCESS_NO_MSGS ? TEST_SUCCESS : \
139+
_ret == TEST_SKIPPED_NO_MSGS ? TEST_SKIPPED : \
140+
_ret)
129141
#define EXPECT_SUCCESS() \
130-
((_ret == TEST_SUCCESS) || (_ret == TEST_SKIPPED))
142+
((_ret == TEST_SUCCESS) || \
143+
(_ret == TEST_SKIPPED) || \
144+
(_ret == TEST_SUCCESS_NO_MSGS) || \
145+
(_ret == TEST_SKIPPED_NO_MSGS))
131146
#define EXPECT_FAIL() \
132-
(_ret == TEST_FAIL)
133-
134-
#define ExpFail(description, result) do { \
135-
printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
136-
fputs("\n expected: ", stdout); printf description; \
137-
fputs("\n result: ", stdout); printf result; fputs("\n\n", stdout); \
138-
fflush(stdout); \
139-
_ret = TEST_FAIL; \
147+
(! EXPECT_SUCCESS())
148+
149+
#define ExpFail(description, result) do { \
150+
if ((_ret == TEST_SUCCESS_NO_MSGS) || (_ret == TEST_SKIPPED_NO_MSGS)) \
151+
_ret = _fail_codepoint_id; \
152+
else { \
153+
printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
154+
fputs("\n expected: ", stdout); printf description; \
155+
fputs("\n result: ", stdout); printf result; \
156+
fputs("\n\n", stdout); \
157+
fflush(stdout); \
158+
_ret = TEST_FAIL; \
159+
} \
140160
} while (0)
141161

142-
#define Expect(test, description, result) do { \
143-
if (_ret != TEST_FAIL) { if (!(test)) ExpFail(description, result); \
144-
else _ret = TEST_SUCCESS; } \
162+
#define Expect(test, description, result) do { \
163+
if (EXPECT_SUCCESS()) { \
164+
if (!(test)) \
165+
ExpFail(description, result); \
166+
else if (_ret == TEST_SKIPPED_NO_MSGS) \
167+
_ret = TEST_SUCCESS_NO_MSGS; \
168+
else \
169+
_ret = TEST_SUCCESS; \
170+
} \
171+
if (_ret == TEST_SUCCESS_NO_MSGS) \
172+
--_fail_codepoint_id; \
145173
} while (0)
146174

147175
#define ExpectTrue(x) Expect( (x), ("%s is true", #x), (#x " => FALSE"))
148176
#define ExpectFalse(x) Expect(!(x), ("%s is false", #x), (#x " => TRUE"))
149177
#define ExpectNotNull(x) Expect( (x), ("%s is not null", #x), (#x " => NULL"))
150178

151179
#define ExpectNull(x) do { \
152-
if (_ret != TEST_FAIL) { \
180+
if (EXPECT_SUCCESS()) { \
153181
PEDANTIC_EXTENSION void* _x = (void*)(x); \
154182
Expect(!_x, ("%s is null", #x), (#x " => %p", _x)); \
155183
} \
156184
} while(0)
157185

158186
#define ExpectInt(x, y, op, er) do { \
159-
if (_ret != TEST_FAIL) { \
187+
if (EXPECT_SUCCESS()) { \
160188
int _x = (int)(x); \
161189
int _y = (int)(y); \
162190
Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y));\
@@ -171,7 +199,7 @@
171199
#define ExpectIntLE(x, y) ExpectInt(x, y, <=, >)
172200

173201
#define ExpectStr(x, y, op, er) do { \
174-
if (_ret != TEST_FAIL) { \
202+
if (EXPECT_SUCCESS()) { \
175203
const char* _x = (const char*)(x); \
176204
const char* _y = (const char*)(y); \
177205
int _z = (_x && _y) ? XSTRCMP(_x, _y) : -1; \
@@ -188,7 +216,7 @@
188216
#define ExpectStrLE(x, y) ExpectStr(x, y, <=, >)
189217

190218
#define ExpectPtr(x, y, op, er) do { \
191-
if (_ret != TEST_FAIL) { \
219+
if (EXPECT_SUCCESS()) { \
192220
PRAGMA_DIAG_PUSH \
193221
/* remarkably, without this inhibition, */ \
194222
/* the _Pragma()s make the declarations warn. */ \
@@ -211,7 +239,7 @@
211239
#define ExpectPtrLE(x, y) ExpectPtr(x, y, <=, >)
212240

213241
#define ExpectBuf(x, y, z, op, er) do { \
214-
if (_ret != TEST_FAIL) { \
242+
if (EXPECT_SUCCESS()) { \
215243
const byte* _x = (const byte*)(x); \
216244
const byte* _y = (const byte*)(y); \
217245
int _z = (int)(z); \
@@ -306,4 +334,4 @@ int w64wrapper_test(void);
306334
int QuicTest(void);
307335

308336

309-
#endif /* CyaSSL_UNIT_H */
337+
#endif /* TESTS_UNIT_H */

0 commit comments

Comments
 (0)