blob: f9fad9beba78a77cc2d846e80cb200404fd6d1c9 [file] [log] [blame]
Robert Sloan8ff03552017-06-14 12:40:58 -07001/* 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 Vartanianbfcf3a72018-08-10 14:55:24 +010017#include <chrono>
18#include <thread>
19
Robert Sloan8ff03552017-06-14 12:40:58 -070020#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 Sloanf068def2018-10-10 18:45:40 -070028#if defined(OPENSSL_THREADS)
Robert Sloan8ff03552017-06-14 12:40:58 -070029
Robert Sloan8ff03552017-06-14 12:40:58 -070030static unsigned g_once_init_called = 0;
31
32static void once_init(void) {
33 g_once_init_called++;
34
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010035 // Sleep briefly so one |call_once_func| instance will call |CRYPTO_once|
Robert Sloan8f860b12017-08-28 07:37:06 -070036 // while the other is running this function.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010037 std::this_thread::sleep_for(std::chrono::milliseconds(1));
Robert Sloan8ff03552017-06-14 12:40:58 -070038}
39
40static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT;
41
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010042TEST(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 Sloan8ff03552017-06-14 12:40:58 -070051 CRYPTO_once(&g_test_once, once_init);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010052
53 EXPECT_EQ(1u, g_once_init_called);
Robert Sloan8ff03552017-06-14 12:40:58 -070054}
55
56static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT;
57static CRYPTO_once_t once_bss;
58
59static struct CRYPTO_STATIC_MUTEX mutex_init_value = CRYPTO_STATIC_MUTEX_INIT;
60static struct CRYPTO_STATIC_MUTEX mutex_bss;
61
62static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT;
63static CRYPTO_EX_DATA_CLASS ex_data_class_bss;
64
Robert Sloan8ff03552017-06-14 12:40:58 -070065TEST(ThreadTest, InitZeros) {
66 if (FIPS_mode()) {
Robert Sloan8f860b12017-08-28 07:37:06 -070067 // 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 Sloan8ff03552017-06-14 12:40:58 -070070 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
80static int g_test_thread_ok = 0;
81static unsigned g_destructor_called_count = 0;
82
83static 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
92TEST(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 Vartanianbfcf3a72018-08-10 14:55:24 +010096 std::thread thread([] {
Robert Sloan8ff03552017-06-14 12:40:58 -070097 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 Vartanianbfcf3a72018-08-10 14:55:24 +0100107 });
108 thread.join();
Robert Sloan8ff03552017-06-14 12:40:58 -0700109
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 Vartanianbfcf3a72018-08-10 14:55:24 +0100113 // 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 Sloan8ff03552017-06-14 12:40:58 -0700117}
118
119TEST(ThreadTest, RandState) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700120 // 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 Sloan8ff03552017-06-14 12:40:58 -0700123 uint8_t buf[1];
124 RAND_bytes(buf, sizeof(buf));
125
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100126 std::thread thread([] {
Robert Sloan8ff03552017-06-14 12:40:58 -0700127 uint8_t buf2[1];
128 RAND_bytes(buf2, sizeof(buf2));
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100129 });
130 thread.join();
Robert Sloan8ff03552017-06-14 12:40:58 -0700131}
132
Robert Sloanf068def2018-10-10 18:45:40 -0700133#endif // OPENSSL_THREADS