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
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
once_init(void)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
TEST(ThreadTest,Once)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 struct CRYPTO_STATIC_MUTEX mutex_init_value = CRYPTO_STATIC_MUTEX_INIT;
61 static struct CRYPTO_STATIC_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
TEST(ThreadTest,InitZeros)66 TEST(ThreadTest, InitZeros) {
67 if (FIPS_mode()) {
68 // Our FIPS tooling currently requires that |CRYPTO_ONCE_INIT|,
69 // |CRYPTO_STATIC_MUTEX_INIT| and |CRYPTO_EX_DATA_CLASS| are all zeros and
70 // so can 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
thread_local_destructor(void * arg)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
TEST(ThreadTest,ThreadLocal)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
TEST(ThreadTest,RandState)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
TEST(ThreadTest,InitThreads)134 TEST(ThreadTest, InitThreads) {
135 constexpr size_t kNumThreads = 10;
136
137 // |CRYPTO_library_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_library_init);
142 }
143 for (auto &thread : threads) {
144 thread.join();
145 }
146 }
147
TEST(ThreadTest,PreSandboxInitThreads)148 TEST(ThreadTest, PreSandboxInitThreads) {
149 constexpr size_t kNumThreads = 10;
150
151 // |CRYPTO_pre_sandbox_init| is safe to call across threads.
152 std::vector<std::thread> threads;
153 threads.reserve(kNumThreads);
154 for (size_t i = 0; i < kNumThreads; i++) {
155 threads.emplace_back(&CRYPTO_pre_sandbox_init);
156 }
157 for (auto &thread : threads) {
158 thread.join();
159 }
160 }
161
162 #endif // OPENSSL_THREADS
163