Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/thread_test.cc

raw
1 // Copyright (c) 2015, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include "internal.h"
5
6 #include <chrono>
7 #include <vector>
8 #include <thread>
9
10 #include <gtest/gtest.h>
11
12 #include <openssl/crypto.h>
13 #include <openssl/rand.h>
14
15 #include "test/test_util.h"
16
17
18 #if defined(OPENSSL_THREADS)
19
20 static unsigned g_once_init_called = 0;
21
22 static void once_init(void) {
23 g_once_init_called++;
24
25 // Sleep briefly so one |call_once_func| instance will call |CRYPTO_once|
26 // while the other is running this function.
27 std::this_thread::sleep_for(std::chrono::milliseconds(1));
28 }
29
30 static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT;
31
32 TEST(ThreadTest, Once) {
33 ASSERT_EQ(0u, g_once_init_called)
34 << "g_once_init_called was non-zero at start.";
35
36 auto call_once_func = [] { CRYPTO_once(&g_test_once, once_init); };
37 std::thread thread1(call_once_func), thread2(call_once_func);
38 thread1.join();
39 thread2.join();
40
41 CRYPTO_once(&g_test_once, once_init);
42
43 EXPECT_EQ(1u, g_once_init_called);
44 }
45
46 #if !defined(OPENSSL_APPLE)
47 // The |InitZeros| test is excluded from the macOS and iOS FIPS builds.
48 // The following three macros are defined in |crypto/internal.h|:
49 // #define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT
50 // #define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER }
51 // #define CRYPTO_EX_DATA_CLASS_INIT {CRYPTO_STATIC_MUTEX_INIT, NULL, 0}
52 // On Linux, the above macros all resolve to a zero (or an array of zeroes).
53 // On macOS, the macros are prefixed with a "signature". For example,
54 // |PTHREAD_ONCE_INIT| is defined in |pthread.h| as:
55 // #define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}}
56 // where, |pthread_impl.h| defines:
57 // #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA
58 // Therefore, on macOS, |CRYPTO_ONCE_INIT| and the other two macros don't
59 // resolve to a zero and the |InitZeros| test fails.
60 //
61 // Rather than making the test work on macOS/iOS by handling the "signature",
62 // we disable it because the file |pthread_impl.h| that contains the macro
63 // in question has the following note:
64 // /* This whole header file will disappear, so don't depend on it... */
65
66 static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT;
67 static CRYPTO_once_t once_bss;
68
69 static struct CRYPTO_STATIC_MUTEX mutex_init_value = CRYPTO_STATIC_MUTEX_INIT;
70 static struct CRYPTO_STATIC_MUTEX mutex_bss;
71
72 static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT;
73 static CRYPTO_EX_DATA_CLASS ex_data_class_bss;
74
75 TEST(ThreadTest, InitZeros) {
76 if (FIPS_mode()) {
77 // Our FIPS tooling currently requires that |CRYPTO_ONCE_INIT|,
78 // |CRYPTO_STATIC_MUTEX_INIT| and |CRYPTO_EX_DATA_CLASS| are all zeros and
79 // so can be placed in the BSS section.
80 EXPECT_EQ(Bytes((uint8_t *)&once_bss, sizeof(once_bss)),
81 Bytes((uint8_t *)&once_init_value, sizeof(once_init_value)));
82 EXPECT_EQ(Bytes((uint8_t *)&mutex_bss, sizeof(mutex_bss)),
83 Bytes((uint8_t *)&mutex_init_value, sizeof(mutex_init_value)));
84 EXPECT_EQ(
85 Bytes((uint8_t *)&ex_data_class_bss, sizeof(ex_data_class_bss)),
86 Bytes((uint8_t *)&ex_data_class_value, sizeof(ex_data_class_value)));
87 }
88 }
89 #endif
90
91 static int g_test_thread_ok = 0;
92 static unsigned g_destructor_called_count = 0;
93
94 static void thread_local_destructor(void *arg) {
95 if (arg == NULL) {
96 return;
97 }
98
99 unsigned *count = reinterpret_cast<unsigned*>(arg);
100 (*count)++;
101 }
102
103 TEST(ThreadTest, ThreadLocal) {
104 ASSERT_EQ(nullptr, CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST))
105 << "Thread-local data was non-NULL at start.";
106
107 std::thread thread([] {
108 if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != NULL ||
109 !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST,
110 &g_destructor_called_count,
111 thread_local_destructor) ||
112 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) !=
113 &g_destructor_called_count) {
114 return;
115 }
116
117 g_test_thread_ok = 1;
118 });
119 thread.join();
120
121 EXPECT_TRUE(g_test_thread_ok) << "Thread-local data didn't work in thread.";
122 EXPECT_EQ(1u, g_destructor_called_count);
123
124 // Create a no-op thread to test that the thread destructor function works
125 // even if thread-local storage wasn't used for a thread.
126 thread = std::thread([] {});
127 thread.join();
128 }
129
130 TEST(ThreadTest, RandState) {
131 // In FIPS mode, rand.c maintains a linked-list of thread-local data because
132 // we're required to clear it on process exit. This test exercises removing a
133 // value from that list.
134 uint8_t buf[1];
135 RAND_bytes(buf, sizeof(buf));
136
137 std::thread thread([] {
138 uint8_t buf2[1];
139 RAND_bytes(buf2, sizeof(buf2));
140 });
141 thread.join();
142 }
143
144 #if defined(OPENSSL_PTHREADS)
145
146 static void thread_task(bool *myFlag) {
147 EXPECT_EQ(1, AWSLC_thread_local_clear());
148 EXPECT_EQ(1, AWSLC_thread_local_clear());
149 uint8_t buf[8];
150 EXPECT_EQ(1, RAND_bytes(buf, sizeof(buf)));
151 EXPECT_EQ(1, AWSLC_thread_local_clear());
152 ERR_clear_error();
153 EXPECT_EQ(1, AWSLC_thread_local_clear());
154 EXPECT_EQ(1, AWSLC_thread_local_clear());
155 *myFlag = true;
156 }
157
158 TEST(ThreadTest, ClearState) {
159 const int kNumThreads = 10;
160 bool myFlags[kNumThreads];
161 std::thread myThreads[kNumThreads];
162
163 for (int i = 0; i < kNumThreads; i++) {
164 bool* myFlag = &myFlags[i];
165 *myFlag = false;
166 myThreads[i] = std::thread(thread_task, myFlag);
167 }
168 for (int i = 0; i < kNumThreads; i++) {
169 myThreads[i].join();
170 ASSERT_TRUE(myFlags[i]) << "Thread " << i << " failed.";
171 }
172 }
173
174 #endif // OPENSSL_PTHREADS
175
176 TEST(ThreadTest, InitThreads) {
177 constexpr size_t kNumThreads = 10;
178
179 // |CRYPTO_library_init| is safe to call across threads.
180 std::vector<std::thread> threads;
181 threads.reserve(kNumThreads);
182 for (size_t i = 0; i < kNumThreads; i++) {
183 threads.emplace_back(&CRYPTO_library_init);
184 }
185 for (auto &thread : threads) {
186 thread.join();
187 }
188 }
189
190 TEST(ThreadTest, PreSandboxInitThreads) {
191 constexpr size_t kNumThreads = 10;
192
193 // |CRYPTO_pre_sandbox_init| is safe to call across threads.
194 std::vector<std::thread> threads;
195 threads.reserve(kNumThreads);
196 for (size_t i = 0; i < kNumThreads; i++) {
197 threads.emplace_back(&CRYPTO_pre_sandbox_init);
198 }
199 for (auto &thread : threads) {
200 thread.join();
201 }
202 }
203
204 #endif // OPENSSL_THREADS