From: Matt Caswell Date: Wed, 23 Dec 2020 11:35:54 +0000 (+0000) Subject: Add a test for the new CRYPTO_atomic_* functions X-Git-Tag: openssl-3.0.0-alpha10~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ea08f8b294d129371536649463c76a81dc4d4e55;p=thirdparty%2Fopenssl.git Add a test for the new CRYPTO_atomic_* functions Also tests the older CRYPTO_atomic_add() which was without a test Reviewed-by: Dmitry Belyavskiy (Merged from https://github.com/openssl/openssl/pull/13733) --- diff --git a/test/threadstest.c b/test/threadstest.c index 5f373fe75f2..d7ed59781d9 100644 --- a/test/threadstest.c +++ b/test/threadstest.c @@ -184,10 +184,86 @@ static int test_thread_local(void) return 1; } +static int test_atomic(void) +{ + int val = 0, ret = 0, testresult = 0; + uint64_t val64 = 1, ret64 = 0; + CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new(); + + if (!TEST_ptr(lock)) + return 0; + + if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) { + /* This succeeds therefore we're on a platform with lockless atomics */ + if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret)) + goto err; + } else { + /* This failed therefore we're on a platform without lockless atomics */ + if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret)) + goto err; + } + val = 0; + ret = 0; + + if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock))) + goto err; + if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret)) + goto err; + + if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) { + /* This succeeds therefore we're on a platform with lockless atomics */ + if (!TEST_uint_eq((unsigned int)val64, 3) + || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64)) + goto err; + } else { + /* This failed therefore we're on a platform without lockless atomics */ + if (!TEST_uint_eq((unsigned int)val64, 1) + || !TEST_int_eq((unsigned int)ret64, 0)) + goto err; + } + val64 = 1; + ret64 = 0; + + if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock))) + goto err; + + if (!TEST_uint_eq((unsigned int)val64, 3) + || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64)) + goto err; + + ret64 = 0; + if (CRYPTO_atomic_load(&val64, &ret64, NULL)) { + /* This succeeds therefore we're on a platform with lockless atomics */ + if (!TEST_uint_eq((unsigned int)val64, 3) + || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64)) + goto err; + } else { + /* This failed therefore we're on a platform without lockless atomics */ + if (!TEST_uint_eq((unsigned int)val64, 3) + || !TEST_int_eq((unsigned int)ret64, 0)) + goto err; + } + + ret64 = 0; + if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock))) + goto err; + + if (!TEST_uint_eq((unsigned int)val64, 3) + || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64)) + goto err; + + testresult = 1; + err: + + CRYPTO_THREAD_lock_free(lock); + return testresult; +} + int setup_tests(void) { ADD_TEST(test_lock); ADD_TEST(test_once); ADD_TEST(test_thread_local); + ADD_TEST(test_atomic); return 1; }