2015-03-16 19:48:56 +00:00
|
|
|
/* Copyright (c) 2015, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
2017-04-07 04:55:17 +01:00
|
|
|
#include <openssl/crypto.h>
|
2017-05-31 22:05:20 +01:00
|
|
|
#include <openssl/rand.h>
|
2017-04-07 04:55:17 +01:00
|
|
|
|
2015-03-16 19:48:56 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
2015-05-08 02:28:27 +01:00
|
|
|
#if !defined(OPENSSL_NO_THREADS)
|
|
|
|
|
2015-03-16 19:48:56 +00:00
|
|
|
#if defined(OPENSSL_WINDOWS)
|
|
|
|
|
2016-06-09 21:48:33 +01:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
2015-06-09 19:50:45 +01:00
|
|
|
#include <windows.h>
|
2016-06-09 21:48:33 +01:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(pop))
|
2015-03-16 19:48:56 +00:00
|
|
|
|
|
|
|
typedef HANDLE thread_t;
|
|
|
|
|
|
|
|
static DWORD WINAPI thread_run(LPVOID arg) {
|
|
|
|
void (*thread_func)(void);
|
|
|
|
/* VC really doesn't like casting between data and function pointers. */
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(&thread_func, &arg, sizeof(thread_func));
|
2015-03-16 19:48:56 +00:00
|
|
|
thread_func();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_thread(thread_t *out_thread, void (*thread_func)(void)) {
|
|
|
|
void *arg;
|
|
|
|
/* VC really doesn't like casting between data and function pointers. */
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(&arg, &thread_func, sizeof(arg));
|
2015-03-16 19:48:56 +00:00
|
|
|
|
|
|
|
*out_thread = CreateThread(NULL /* security attributes */,
|
|
|
|
0 /* default stack size */, thread_run, arg,
|
|
|
|
0 /* run immediately */, NULL /* ignore id */);
|
|
|
|
return *out_thread != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_for_thread(thread_t thread) {
|
|
|
|
return WaitForSingleObject(thread, INFINITE) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2016-04-19 22:48:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2015-03-16 19:48:56 +00:00
|
|
|
|
|
|
|
typedef pthread_t thread_t;
|
|
|
|
|
|
|
|
static void *thread_run(void *arg) {
|
|
|
|
void (*thread_func)(void) = arg;
|
|
|
|
thread_func();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_thread(thread_t *out_thread, void (*thread_func)(void)) {
|
|
|
|
return pthread_create(out_thread, NULL /* default attributes */, thread_run,
|
|
|
|
thread_func) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_for_thread(thread_t thread) {
|
|
|
|
return pthread_join(thread, NULL) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* OPENSSL_WINDOWS */
|
|
|
|
|
|
|
|
static unsigned g_once_init_called = 0;
|
|
|
|
|
|
|
|
static void once_init(void) {
|
|
|
|
g_once_init_called++;
|
2016-03-31 00:03:45 +01:00
|
|
|
|
|
|
|
/* Sleep briefly so one |call_once_thread| instance will call |CRYPTO_once|
|
|
|
|
* while the other is running this function. */
|
|
|
|
#if defined(OPENSSL_WINDOWS)
|
|
|
|
Sleep(1 /* milliseconds */);
|
|
|
|
#else
|
2016-04-19 22:48:11 +01:00
|
|
|
struct timespec req;
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(&req, 0, sizeof(req));
|
2016-04-19 22:48:11 +01:00
|
|
|
req.tv_nsec = 1000000;
|
|
|
|
nanosleep(&req, NULL);
|
2016-03-31 00:03:45 +01:00
|
|
|
#endif
|
2015-03-16 19:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT;
|
|
|
|
|
|
|
|
static void call_once_thread(void) {
|
|
|
|
CRYPTO_once(&g_test_once, once_init);
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:29:35 +01:00
|
|
|
static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT;
|
|
|
|
static CRYPTO_once_t once_bss;
|
|
|
|
|
2017-04-14 19:16:20 +01:00
|
|
|
static struct CRYPTO_STATIC_MUTEX mutex_init_value = CRYPTO_STATIC_MUTEX_INIT;
|
|
|
|
static struct CRYPTO_STATIC_MUTEX mutex_bss;
|
|
|
|
|
2017-05-02 22:25:39 +01:00
|
|
|
static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT;
|
|
|
|
static CRYPTO_EX_DATA_CLASS ex_data_class_bss;
|
|
|
|
|
2015-03-16 19:48:56 +00:00
|
|
|
static int test_once(void) {
|
|
|
|
if (g_once_init_called != 0) {
|
|
|
|
fprintf(stderr, "g_once_init_called was non-zero at start.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-31 00:03:45 +01:00
|
|
|
thread_t thread1, thread2;
|
|
|
|
if (!run_thread(&thread1, call_once_thread) ||
|
|
|
|
!run_thread(&thread2, call_once_thread) ||
|
|
|
|
!wait_for_thread(thread1) ||
|
|
|
|
!wait_for_thread(thread2)) {
|
2015-03-16 19:48:56 +00:00
|
|
|
fprintf(stderr, "thread failed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRYPTO_once(&g_test_once, once_init);
|
|
|
|
|
|
|
|
if (g_once_init_called != 1) {
|
|
|
|
fprintf(stderr, "Expected init function to be called once, but found %u.\n",
|
|
|
|
g_once_init_called);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-07 04:55:17 +01:00
|
|
|
|
|
|
|
if (FIPS_mode()) {
|
2017-05-02 22:25:39 +01:00
|
|
|
/* Our FIPS tooling currently requires that |CRYPTO_ONCE_INIT|,
|
|
|
|
* |CRYPTO_STATIC_MUTEX_INIT| and |CRYPTO_EX_DATA_CLASS| are all zeros and
|
|
|
|
* so can be placed in the BSS section. */
|
2017-04-07 16:29:35 +01:00
|
|
|
if (OPENSSL_memcmp((void *)&once_init_value, (void *)&once_bss,
|
|
|
|
sizeof(CRYPTO_once_t)) != 0) {
|
2017-04-07 04:55:17 +01:00
|
|
|
fprintf(stderr, "CRYPTO_ONCE_INIT did not expand to all zeros.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-14 19:16:20 +01:00
|
|
|
|
|
|
|
if (OPENSSL_memcmp((void *)&mutex_init_value, (void *)&mutex_bss,
|
|
|
|
sizeof(struct CRYPTO_STATIC_MUTEX)) != 0) {
|
|
|
|
fprintf(stderr, "CRYPTO_STATIC_MUTEX did not expand to all zeros.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-02 22:25:39 +01:00
|
|
|
|
|
|
|
if (OPENSSL_memcmp((void *)&ex_data_class_value, (void *)&ex_data_class_bss,
|
|
|
|
sizeof(CRYPTO_EX_DATA_CLASS))) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"CRYPTO_EX_DATA_CLASS_INIT did not expand to all zeros.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-07 04:55:17 +01:00
|
|
|
}
|
2015-03-16 19:48:56 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int g_test_thread_ok = 0;
|
|
|
|
static unsigned g_destructor_called_count = 0;
|
|
|
|
|
|
|
|
static void thread_local_destructor(void *arg) {
|
|
|
|
if (arg == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned *count = arg;
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_local_test_thread(void) {
|
|
|
|
void *ptr = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST);
|
|
|
|
if (ptr != NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST,
|
|
|
|
&g_destructor_called_count,
|
|
|
|
thread_local_destructor)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) !=
|
|
|
|
&g_destructor_called_count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_test_thread_ok = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_local_test2_thread(void) {}
|
|
|
|
|
|
|
|
static int test_thread_local(void) {
|
|
|
|
void *ptr = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST);
|
|
|
|
if (ptr != NULL) {
|
|
|
|
fprintf(stderr, "Thread-local data was non-NULL at start.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_t thread;
|
|
|
|
if (!run_thread(&thread, thread_local_test_thread) ||
|
|
|
|
!wait_for_thread(thread)) {
|
|
|
|
fprintf(stderr, "thread failed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_test_thread_ok) {
|
|
|
|
fprintf(stderr, "Thread-local data didn't work in thread.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_destructor_called_count != 1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Destructor should have been called once, but actually called %u "
|
|
|
|
"times.\n",
|
|
|
|
g_destructor_called_count);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread_local_test2_thread doesn't do anything, but it tests that the
|
|
|
|
* thread destructor function works even if thread-local storage wasn't used
|
|
|
|
* for a thread. */
|
|
|
|
if (!run_thread(&thread, thread_local_test2_thread) ||
|
|
|
|
!wait_for_thread(thread)) {
|
|
|
|
fprintf(stderr, "thread failed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:05:20 +01:00
|
|
|
static void rand_state_test_thread(void) {
|
|
|
|
uint8_t buf[1];
|
|
|
|
RAND_bytes(buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_rand_state(void) {
|
|
|
|
/* In FIPS mode, rand.c maintains a linked-list of thread-local data because
|
|
|
|
* we're required to clear it on process exit. This test exercises removing a
|
|
|
|
* value from that list. */
|
|
|
|
uint8_t buf[1];
|
|
|
|
RAND_bytes(buf, sizeof(buf));
|
|
|
|
|
|
|
|
thread_t thread;
|
|
|
|
if (!run_thread(&thread, rand_state_test_thread) ||
|
|
|
|
!wait_for_thread(thread)) {
|
|
|
|
fprintf(stderr, "thread failed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-16 19:48:56 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (!test_once() ||
|
2017-05-31 22:05:20 +01:00
|
|
|
!test_thread_local() ||
|
|
|
|
!test_rand_state()) {
|
2015-03-16 19:48:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("PASS\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-08 02:28:27 +01:00
|
|
|
|
|
|
|
#else /* OPENSSL_NO_THREADS */
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
printf("PASS\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|