Fix mem leak in threadpool_test.c

Fixes #24104

Added a goto label for cleanup.

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24412)
This commit is contained in:
shridhar kalavagunta 2024-04-29 20:59:57 -05:00 committed by Tomas Mraz
parent 45f5d51b72
commit 4dbd4925df

View file

@ -100,6 +100,7 @@ static int test_thread_internal(void)
uint32_t threads_supported;
size_t i;
void *t[3];
int status = 0;
OSSL_LIB_CTX *cust_ctx = OSSL_LIB_CTX_new();
threads_supported = OSSL_get_thread_support_flags();
@ -107,65 +108,66 @@ static int test_thread_internal(void)
if (threads_supported == 0) {
if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
return 0;
goto cleanup;
if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
return 0;
goto cleanup;
if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 0))
return 0;
goto cleanup;
if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 0))
return 0;
goto cleanup;
if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
return 0;
goto cleanup;
if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
return 0;
goto cleanup;
t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
if (!TEST_ptr_null(t[0]))
return 0;
goto cleanup;
return 1;
status = 1;
goto cleanup;
}
/* fail when not allowed to use threads */
if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
return 0;
goto cleanup;
t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
if (!TEST_ptr_null(t[0]))
return 0;
goto cleanup;
/* fail when enabled on a different context */
if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
return 0;
goto cleanup;
if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 1))
return 0;
goto cleanup;
if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
return 0;
goto cleanup;
if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 1))
return 0;
goto cleanup;
t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
if (!TEST_ptr_null(t[0]))
return 0;
goto cleanup;
if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 0), 1))
return 0;
goto cleanup;
/* sequential startup */
if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 1))
return 0;
goto cleanup;
if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 1))
return 0;
goto cleanup;
if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
return 0;
goto cleanup;
for (i = 0; i < OSSL_NELEM(t); ++i) {
local[0] = i + 1;
t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
if (!TEST_ptr(t[i]))
return 0;
goto cleanup;
/*
* pthread_join results in undefined behaviour if called on a joined
@ -174,70 +176,72 @@ static int test_thread_internal(void)
* if we do).
*/
if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
return 0;
goto cleanup;
if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
return 0;
goto cleanup;
if (!TEST_int_eq(retval[0], i + 1) || !TEST_int_eq(local[0], i + 2))
return 0;
goto cleanup;
if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
return 0;
goto cleanup;
t[i] = NULL;
if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 0))
return 0;
goto cleanup;
}
/* parallel startup */
if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t)), 1))
return 0;
goto cleanup;
for (i = 0; i < OSSL_NELEM(t); ++i) {
local[i] = i + 1;
t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
if (!TEST_ptr(t[i]))
return 0;
goto cleanup;
}
for (i = 0; i < OSSL_NELEM(t); ++i) {
if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
return 0;
goto cleanup;
}
for (i = 0; i < OSSL_NELEM(t); ++i) {
if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
return 0;
goto cleanup;
if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
return 0;
goto cleanup;
}
/* parallel startup, bottleneck */
if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t) - 1), 1))
return 0;
goto cleanup;
for (i = 0; i < OSSL_NELEM(t); ++i) {
local[i] = i + 1;
t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
if (!TEST_ptr(t[i]))
return 0;
goto cleanup;
}
for (i = 0; i < OSSL_NELEM(t); ++i) {
if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
return 0;
goto cleanup;
}
for (i = 0; i < OSSL_NELEM(t); ++i) {
if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
return 0;
goto cleanup;
if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
return 0;
goto cleanup;
}
if (!TEST_int_eq(OSSL_set_max_threads(NULL, 0), 1))
return 0;
goto cleanup;
status = 1;
cleanup:
OSSL_LIB_CTX_free(cust_ctx);
return 1;
return status;
}
# endif