| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "internal.h" |
| 16 |
| 17 | #include <chrono> |
| 18 | #include <vector> |
| 19 | #include <thread> |
| 20 |
| 21 | #include <gtest/gtest.h> |
| 22 |
| 23 | #include <openssl/crypto.h> |
| 24 | #include <openssl/rand.h> |
| 25 |
| 26 | #include "test/test_util.h" |
| 27 |
| 28 |
| 29 | #if defined(OPENSSL_THREADS) |
| 30 |
| 31 | static unsigned g_once_init_called = 0; |
| 32 |
| 33 | static void once_init(void) { |
| 34 | g_once_init_called++; |
| 35 |
| 36 | // Sleep briefly so one |call_once_func| instance will call |CRYPTO_once| |
| 37 | // while the other is running this function. |
| 38 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 39 | } |
| 40 |
| 41 | static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT; |
| 42 |
| 43 | TEST(ThreadTest, Once) { |
| 44 | ASSERT_EQ(0u, g_once_init_called) |
| 45 | << "g_once_init_called was non-zero at start."; |
| 46 |
| 47 | auto call_once_func = [] { CRYPTO_once(&g_test_once, once_init); }; |
| 48 | std::thread thread1(call_once_func), thread2(call_once_func); |
| 49 | thread1.join(); |
| 50 | thread2.join(); |
| 51 |
| 52 | CRYPTO_once(&g_test_once, once_init); |
| 53 |
| 54 | EXPECT_EQ(1u, g_once_init_called); |
| 55 | } |
| 56 |
| 57 | static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT; |
| 58 | static CRYPTO_once_t once_bss; |
| 59 |
| 60 | static CRYPTO_MUTEX mutex_init_value = CRYPTO_MUTEX_INIT; |
| 61 | static CRYPTO_MUTEX mutex_bss; |
| 62 |
| 63 | static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT; |
| 64 | static CRYPTO_EX_DATA_CLASS ex_data_class_bss; |
| 65 |
| 66 | TEST(ThreadTest, InitZeros) { |
| 67 | if (FIPS_mode()) { |
| 68 | // Our FIPS tooling currently requires that |CRYPTO_ONCE_INIT|, |
| 69 | // |CRYPTO_MUTEX_INIT| and |CRYPTO_EX_DATA_CLASS| are all zeros and so can |
| 70 | // be placed in the BSS section. |
| 71 | EXPECT_EQ(Bytes((uint8_t *)&once_bss, sizeof(once_bss)), |
| 72 | Bytes((uint8_t *)&once_init_value, sizeof(once_init_value))); |
| 73 | EXPECT_EQ(Bytes((uint8_t *)&mutex_bss, sizeof(mutex_bss)), |
| 74 | Bytes((uint8_t *)&mutex_init_value, sizeof(mutex_init_value))); |
| 75 | EXPECT_EQ( |
| 76 | Bytes((uint8_t *)&ex_data_class_bss, sizeof(ex_data_class_bss)), |
| 77 | Bytes((uint8_t *)&ex_data_class_value, sizeof(ex_data_class_value))); |
| 78 | } |
| 79 | } |
| 80 |
| 81 | static int g_test_thread_ok = 0; |
| 82 | static unsigned g_destructor_called_count = 0; |
| 83 |
| 84 | static void thread_local_destructor(void *arg) { |
| 85 | if (arg == NULL) { |
| 86 | return; |
| 87 | } |
| 88 |
| 89 | unsigned *count = reinterpret_cast<unsigned*>(arg); |
| 90 | (*count)++; |
| 91 | } |
| 92 |
| 93 | TEST(ThreadTest, ThreadLocal) { |
| 94 | ASSERT_EQ(nullptr, CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST)) |
| 95 | << "Thread-local data was non-NULL at start."; |
| 96 |
| 97 | std::thread thread([] { |
| 98 | if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != NULL || |
| 99 | !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST, |
| 100 | &g_destructor_called_count, |
| 101 | thread_local_destructor) || |
| 102 | CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != |
| 103 | &g_destructor_called_count) { |
| 104 | return; |
| 105 | } |
| 106 |
| 107 | g_test_thread_ok = 1; |
| 108 | }); |
| 109 | thread.join(); |
| 110 |
| 111 | EXPECT_TRUE(g_test_thread_ok) << "Thread-local data didn't work in thread."; |
| 112 | EXPECT_EQ(1u, g_destructor_called_count); |
| 113 |
| 114 | // Create a no-op thread to test that the thread destructor function works |
| 115 | // even if thread-local storage wasn't used for a thread. |
| 116 | thread = std::thread([] {}); |
| 117 | thread.join(); |
| 118 | } |
| 119 |
| 120 | TEST(ThreadTest, RandState) { |
| 121 | // In FIPS mode, rand.c maintains a linked-list of thread-local data because |
| 122 | // we're required to clear it on process exit. This test exercises removing a |
| 123 | // value from that list. |
| 124 | uint8_t buf[1]; |
| 125 | RAND_bytes(buf, sizeof(buf)); |
| 126 |
| 127 | std::thread thread([] { |
| 128 | uint8_t buf2[1]; |
| 129 | RAND_bytes(buf2, sizeof(buf2)); |
| 130 | }); |
| 131 | thread.join(); |
| 132 | } |
| 133 |
| 134 | TEST(ThreadTest, PreSandboxInitThreads) { |
| 135 | constexpr size_t kNumThreads = 10; |
| 136 |
| 137 | // |CRYPTO_pre_sandbox_init| is safe to call across threads. |
| 138 | std::vector<std::thread> threads; |
| 139 | threads.reserve(kNumThreads); |
| 140 | for (size_t i = 0; i < kNumThreads; i++) { |
| 141 | threads.emplace_back(&CRYPTO_pre_sandbox_init); |
| 142 | } |
| 143 | for (auto &thread : threads) { |
| 144 | thread.join(); |
| 145 | } |
| 146 | } |
| 147 |
| 148 | #endif // OPENSSL_THREADS |
| 149 |