• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <thread>
19 
20 #include <gtest/gtest.h>
21 
22 #include <openssl/crypto.h>
23 #include <openssl/rand.h>
24 
25 #include "test/test_util.h"
26 
27 
28 #if defined(OPENSSL_THREADS)
29 
30 static unsigned g_once_init_called = 0;
31 
once_init(void)32 static void once_init(void) {
33   g_once_init_called++;
34 
35   // Sleep briefly so one |call_once_func| instance will call |CRYPTO_once|
36   // while the other is running this function.
37   std::this_thread::sleep_for(std::chrono::milliseconds(1));
38 }
39 
40 static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT;
41 
TEST(ThreadTest,Once)42 TEST(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 
51   CRYPTO_once(&g_test_once, once_init);
52 
53   EXPECT_EQ(1u, g_once_init_called);
54 }
55 
56 static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT;
57 static CRYPTO_once_t once_bss;
58 
59 static struct CRYPTO_STATIC_MUTEX mutex_init_value = CRYPTO_STATIC_MUTEX_INIT;
60 static struct CRYPTO_STATIC_MUTEX mutex_bss;
61 
62 static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT;
63 static CRYPTO_EX_DATA_CLASS ex_data_class_bss;
64 
TEST(ThreadTest,InitZeros)65 TEST(ThreadTest, InitZeros) {
66   if (FIPS_mode()) {
67     // 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.
70     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 
80 static int g_test_thread_ok = 0;
81 static unsigned g_destructor_called_count = 0;
82 
thread_local_destructor(void * arg)83 static 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 
TEST(ThreadTest,ThreadLocal)92 TEST(ThreadTest, ThreadLocal) {
93   ASSERT_EQ(nullptr, CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST))
94       << "Thread-local data was non-NULL at start.";
95 
96   std::thread thread([] {
97     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;
107   });
108   thread.join();
109 
110   EXPECT_TRUE(g_test_thread_ok) << "Thread-local data didn't work in thread.";
111   EXPECT_EQ(1u, g_destructor_called_count);
112 
113   // 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();
117 }
118 
TEST(ThreadTest,RandState)119 TEST(ThreadTest, RandState) {
120   // 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.
123   uint8_t buf[1];
124   RAND_bytes(buf, sizeof(buf));
125 
126   std::thread thread([] {
127     uint8_t buf2[1];
128     RAND_bytes(buf2, sizeof(buf2));
129   });
130   thread.join();
131 }
132 
133 #endif  // OPENSSL_THREADS
134