Skip to content

Commit

Permalink
threadstest: use locking for tsan operations if required
Browse files Browse the repository at this point in the history
Not all platforms support tsan operations, those that don't need to have an
alternative locking path.

Fixes openssl#17447

Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from openssl#17479)
  • Loading branch information
paulidale committed Jan 13, 2022
1 parent 8ff861d commit 3d4d530
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions test/threadstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,31 @@ static const char *default_provider[] = { "default", NULL };
static const char *fips_provider[] = { "fips", NULL };
static const char *fips_and_default_providers[] = { "default", "fips", NULL };

/* Grab a globally unique integer value */
#ifdef TSAN_REQUIRES_LOCKING
static CRYPTO_RWLOCK *tsan_lock;
#endif

/* Grab a globally unique integer value, return 0 on failure */
static int get_new_uid(void)
{
/*
* Start with a nice large number to avoid potential conflicts when
* we generate a new OID.
*/
static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2);
#ifdef TSAN_REQUIRES_LOCKING
int r;

if (!TEST_true(CRYPTO_THREAD_write_lock(tsan_lock)))
return 0;
r = ++current_uid;
if (!TEST_true(CRYPTO_THREAD_unlock(tsan_lock)))
return 0;
return r;

#else
return tsan_counter(&current_uid);
#endif
}

static int test_lock(void)
Expand Down Expand Up @@ -626,7 +641,8 @@ static void test_obj_create_one(void)
BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
if (!TEST_true(id = OBJ_create(oid, sn, ln))
if (!TEST_int_ne(id, 0)
|| !TEST_true(id = OBJ_create(oid, sn, ln))
|| !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
multi_success = 0;
}
Expand Down Expand Up @@ -684,6 +700,11 @@ int setup_tests(void)
if (!TEST_ptr(privkey))
return 0;

#ifdef TSAN_REQUIRES_LOCKING
if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new()))
return 0;
#endif

/* Keep first to validate auto creation of default library context */
ADD_TEST(test_multi_default);

Expand All @@ -707,4 +728,7 @@ int setup_tests(void)
void cleanup_tests(void)
{
OPENSSL_free(privkey);
#ifdef TSAN_REQUIRES_LOCKING
CRYPTO_THREAD_lock_free(tsan_lock);
#endif
}

0 comments on commit 3d4d530

Please sign in to comment.