Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include "internal.h" |
| 16 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 17 | #include <chrono> |
| 18 | #include <thread> |
| 19 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include <openssl/crypto.h> |
| 23 | #include <openssl/rand.h> |
| 24 | |
| 25 | #include "test/test_util.h" |
| 26 | |
| 27 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 28 | #if defined(OPENSSL_THREADS) |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 29 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 30 | static unsigned g_once_init_called = 0; |
| 31 | |
| 32 | static void once_init(void) { |
| 33 | g_once_init_called++; |
| 34 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 35 | // Sleep briefly so one |call_once_func| instance will call |CRYPTO_once| |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 36 | // while the other is running this function. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 37 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT; |
| 41 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 42 | TEST(ThreadTest, Once) { |
| 43 | ASSERT_EQ(0u, g_once_init_called) |
| 44 | << "g_once_init_called was non-zero at start."; |
| 45 | |
| 46 | auto call_once_func = [] { CRYPTO_once(&g_test_once, once_init); }; |
| 47 | std::thread thread1(call_once_func), thread2(call_once_func); |
| 48 | thread1.join(); |
| 49 | thread2.join(); |
| 50 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 51 | CRYPTO_once(&g_test_once, once_init); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 52 | |
| 53 | EXPECT_EQ(1u, g_once_init_called); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT; |
| 57 | static CRYPTO_once_t once_bss; |
| 58 | |
| 59 | static struct CRYPTO_STATIC_MUTEX mutex_init_value = CRYPTO_STATIC_MUTEX_INIT; |
| 60 | static struct CRYPTO_STATIC_MUTEX mutex_bss; |
| 61 | |
| 62 | static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT; |
| 63 | static CRYPTO_EX_DATA_CLASS ex_data_class_bss; |
| 64 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 65 | TEST(ThreadTest, InitZeros) { |
| 66 | if (FIPS_mode()) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 67 | // Our FIPS tooling currently requires that |CRYPTO_ONCE_INIT|, |
| 68 | // |CRYPTO_STATIC_MUTEX_INIT| and |CRYPTO_EX_DATA_CLASS| are all zeros and |
| 69 | // so can be placed in the BSS section. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 70 | EXPECT_EQ(Bytes((uint8_t *)&once_bss, sizeof(once_bss)), |
| 71 | Bytes((uint8_t *)&once_init_value, sizeof(once_init_value))); |
| 72 | EXPECT_EQ(Bytes((uint8_t *)&mutex_bss, sizeof(mutex_bss)), |
| 73 | Bytes((uint8_t *)&mutex_init_value, sizeof(mutex_init_value))); |
| 74 | EXPECT_EQ( |
| 75 | Bytes((uint8_t *)&ex_data_class_bss, sizeof(ex_data_class_bss)), |
| 76 | Bytes((uint8_t *)&ex_data_class_value, sizeof(ex_data_class_value))); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static int g_test_thread_ok = 0; |
| 81 | static unsigned g_destructor_called_count = 0; |
| 82 | |
| 83 | static void thread_local_destructor(void *arg) { |
| 84 | if (arg == NULL) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | unsigned *count = reinterpret_cast<unsigned*>(arg); |
| 89 | (*count)++; |
| 90 | } |
| 91 | |
| 92 | TEST(ThreadTest, ThreadLocal) { |
| 93 | ASSERT_EQ(nullptr, CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST)) |
| 94 | << "Thread-local data was non-NULL at start."; |
| 95 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 96 | std::thread thread([] { |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 97 | if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != NULL || |
| 98 | !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST, |
| 99 | &g_destructor_called_count, |
| 100 | thread_local_destructor) || |
| 101 | CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != |
| 102 | &g_destructor_called_count) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | g_test_thread_ok = 1; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 107 | }); |
| 108 | thread.join(); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 109 | |
| 110 | EXPECT_TRUE(g_test_thread_ok) << "Thread-local data didn't work in thread."; |
| 111 | EXPECT_EQ(1u, g_destructor_called_count); |
| 112 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 113 | // Create a no-op thread to test that the thread destructor function works |
| 114 | // even if thread-local storage wasn't used for a thread. |
| 115 | thread = std::thread([] {}); |
| 116 | thread.join(); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST(ThreadTest, RandState) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 120 | // In FIPS mode, rand.c maintains a linked-list of thread-local data because |
| 121 | // we're required to clear it on process exit. This test exercises removing a |
| 122 | // value from that list. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 123 | uint8_t buf[1]; |
| 124 | RAND_bytes(buf, sizeof(buf)); |
| 125 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 126 | std::thread thread([] { |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 127 | uint8_t buf2[1]; |
| 128 | RAND_bytes(buf2, sizeof(buf2)); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 129 | }); |
| 130 | thread.join(); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 133 | #endif // OPENSSL_THREADS |