thread_test.cc
Go to the documentation of this file.
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 
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 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 
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 
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) {
95  << "Thread-local data was non-NULL at start.";
96 
97  std::thread thread([] {
100  &g_destructor_called_count,
101  thread_local_destructor) ||
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, 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 
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
CRYPTO_STATIC_MUTEX_INIT
#define CRYPTO_STATIC_MUTEX_INIT
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:536
RAND_bytes
#define RAND_bytes
Definition: boringssl_prefix_symbols.h:2060
CRYPTO_EX_DATA_CLASS_INIT
#define CRYPTO_EX_DATA_CLASS_INIT
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:687
Bytes
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:38
CRYPTO_library_init
#define CRYPTO_library_init
Definition: boringssl_prefix_symbols.h:1175
CRYPTO_EX_DATA_CLASS
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:679
internal.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
CRYPTO_once
#define CRYPTO_once
Definition: boringssl_prefix_symbols.h:1182
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
CRYPTO_get_thread_local
#define CRYPTO_get_thread_local
Definition: boringssl_prefix_symbols.h:1169
threads
static uv_thread_t * threads
Definition: threadpool.c:38
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
CRYPTO_STATIC_MUTEX
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:533
CRYPTO_set_thread_local
#define CRYPTO_set_thread_local
Definition: boringssl_prefix_symbols.h:1199
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
FIPS_mode
#define FIPS_mode
Definition: boringssl_prefix_symbols.h:1761
CRYPTO_ONCE_INIT
#define CRYPTO_ONCE_INIT
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:471
crypto.h
arg
Definition: cmdline.cc:40
CRYPTO_pre_sandbox_init
#define CRYPTO_pre_sandbox_init
Definition: boringssl_prefix_symbols.h:1186
rand.h
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
kNumThreads
const int kNumThreads
Definition: thread_stress_test.cc:46
arg
struct arg arg
buf2
static char buf2[32]
Definition: test-fs.c:127
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
CRYPTO_once_t
uint32_t CRYPTO_once_t
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:470
test_util.h
OPENSSL_THREAD_LOCAL_TEST
@ OPENSSL_THREAD_LOCAL_TEST
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:639
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:37