Skip to content

Commit

Permalink
Add locking to atomic operations in rw/rcu tests
Browse files Browse the repository at this point in the history
I neglected to add locks to the calls to CRYPTO_atomic_add in these
test, which on newer compilers is fine, as atomic operations are
defined.  However on older compilers the __ATOMIC_ACQ_REL definition is
missing causing these function to be implemented using an rwlock, which
when NULL causes the locks to fail.

Fix this my creating the lock and using them appropriately
  • Loading branch information
nhorman committed Mar 29, 2024
1 parent 1967539 commit 52f0848
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
11 changes: 10 additions & 1 deletion crypto/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ void OSSL_sleep(uint64_t millis)
ts.tv_sec = (long int) (millis / 1000);
ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
nanosleep(&ts, NULL);
# elif defined(__TANDEM) && !defined(_REENTRANT)
# elif defined(__TANDEM)
# if !defined(_REENTRANT)
# include <cextdecs.h(PROCESS_DELAY_)>

/* HPNS does not support usleep for non threaded apps */
PROCESS_DELAY_(millis * 1000);
# elif defined(_SPT_MODEL_)
# include <spthread.h>
# include <spt_extensions.h>

usleep(millis * 1000);
# else
usleep(millis * 1000);
# endif
# else
unsigned int s = (unsigned int)(millis / 1000);
unsigned int us = (unsigned int)((millis % 1000) * 1000);
Expand Down
21 changes: 13 additions & 8 deletions test/threadstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ static int rwwriter2_iterations = 0;
static int *rwwriter_ptr = NULL;
static int rw_torture_result = 1;
static CRYPTO_RWLOCK *rwtorturelock = NULL;
static CRYPTO_RWLOCK *atomiclock = NULL;

static void rwwriter_fn(int id, int *iterations)
{
Expand Down Expand Up @@ -150,7 +151,7 @@ static void rwwriter1_fn(void)

TEST_info("Starting writer1");
rwwriter_fn(1, &rwwriter1_iterations);
CRYPTO_atomic_add(&rwwriter1_done, 1, &local, NULL);
CRYPTO_atomic_add(&rwwriter1_done, 1, &local, atomiclock);
}

static void rwwriter2_fn(void)
Expand All @@ -159,7 +160,7 @@ static void rwwriter2_fn(void)

TEST_info("Starting writer 2");
rwwriter_fn(2, &rwwriter2_iterations);
CRYPTO_atomic_add(&rwwriter2_done, 1, &local, NULL);
CRYPTO_atomic_add(&rwwriter2_done, 1, &local, atomiclock);
}

static void rwreader_fn(int *iterations)
Expand All @@ -174,8 +175,8 @@ static void rwreader_fn(int *iterations)
abort();

while (lw1 != 1 || lw2 != 1) {
CRYPTO_atomic_add(&rwwriter1_done, 0, &lw1, NULL);
CRYPTO_atomic_add(&rwwriter2_done, 0, &lw2, NULL);
CRYPTO_atomic_add(&rwwriter1_done, 0, &lw1, atomiclock);
CRYPTO_atomic_add(&rwwriter2_done, 0, &lw2, atomiclock);

count++;
if (rwwriter_ptr != NULL && old > *rwwriter_ptr) {
Expand Down Expand Up @@ -223,6 +224,7 @@ static int _torture_rw(void)
struct timeval dtime;

rwtorturelock = CRYPTO_THREAD_lock_new();
atomiclock = CRYPTO_THREAD_lock_new();
rwwriter1_iterations = 0;
rwwriter2_iterations = 0;
rwreader1_iterations = 0;
Expand Down Expand Up @@ -264,6 +266,7 @@ static int _torture_rw(void)
ret = 1;
out:
CRYPTO_THREAD_lock_free(rwtorturelock);
CRYPTO_THREAD_lock_free(atomiclock);
rwtorturelock = NULL;
return ret;
}
Expand Down Expand Up @@ -336,7 +339,7 @@ static void writer1_fn(void)

TEST_info("Starting writer1");
writer_fn(1, &writer1_iterations);
CRYPTO_atomic_add(&writer1_done, 1, &local, NULL);
CRYPTO_atomic_add(&writer1_done, 1, &local, atomiclock);
}

static void writer2_fn(void)
Expand All @@ -345,7 +348,7 @@ static void writer2_fn(void)

TEST_info("Starting writer2");
writer_fn(2, &writer2_iterations);
CRYPTO_atomic_add(&writer2_done, 1, &local, NULL);
CRYPTO_atomic_add(&writer2_done, 1, &local, atomiclock);
}

static void reader_fn(int *iterations)
Expand All @@ -358,8 +361,8 @@ static void reader_fn(int *iterations)
int lw2 = 0;

while (lw1 != 1 || lw2 != 1) {
CRYPTO_atomic_add(&writer1_done, 0, &lw1, NULL);
CRYPTO_atomic_add(&writer2_done, 0, &lw2, NULL);
CRYPTO_atomic_add(&writer1_done, 0, &lw1, atomiclock);
CRYPTO_atomic_add(&writer2_done, 0, &lw2, atomiclock);
count++;
ossl_rcu_read_lock(rcu_lock);
valp = ossl_rcu_deref(&writer_ptr);
Expand Down Expand Up @@ -403,6 +406,7 @@ static int _torture_rcu(void)
double tottime;
double avr, avw;

atomiclock = CRYPTO_THREAD_lock_new();
memset(&writer1, 0, sizeof(thread_t));
memset(&writer2, 0, sizeof(thread_t));
memset(&reader1, 0, sizeof(thread_t));
Expand Down Expand Up @@ -443,6 +447,7 @@ static int _torture_rcu(void)
TEST_info("Average write time %e/write", avw);

ossl_rcu_lock_free(rcu_lock);
CRYPTO_THREAD_lock_free(atomiclock);
if (!TEST_int_eq(rcu_torture_result, 1))
return 0;

Expand Down

0 comments on commit 52f0848

Please sign in to comment.