You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

134 lines
4.2 KiB

  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. #include "internal.h"
  15. #include <chrono>
  16. #include <thread>
  17. #include <gtest/gtest.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/rand.h>
  20. #include "test/test_util.h"
  21. #if defined(OPENSSL_THREADS)
  22. static unsigned g_once_init_called = 0;
  23. static void once_init(void) {
  24. g_once_init_called++;
  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. static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT;
  30. TEST(ThreadTest, Once) {
  31. ASSERT_EQ(0u, g_once_init_called)
  32. << "g_once_init_called was non-zero at start.";
  33. auto call_once_func = [] { CRYPTO_once(&g_test_once, once_init); };
  34. std::thread thread1(call_once_func), thread2(call_once_func);
  35. thread1.join();
  36. thread2.join();
  37. CRYPTO_once(&g_test_once, once_init);
  38. EXPECT_EQ(1u, g_once_init_called);
  39. }
  40. static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT;
  41. static CRYPTO_once_t once_bss;
  42. static struct CRYPTO_STATIC_MUTEX mutex_init_value = CRYPTO_STATIC_MUTEX_INIT;
  43. static struct CRYPTO_STATIC_MUTEX mutex_bss;
  44. static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT;
  45. static CRYPTO_EX_DATA_CLASS ex_data_class_bss;
  46. TEST(ThreadTest, InitZeros) {
  47. if (FIPS_mode()) {
  48. // Our FIPS tooling currently requires that |CRYPTO_ONCE_INIT|,
  49. // |CRYPTO_STATIC_MUTEX_INIT| and |CRYPTO_EX_DATA_CLASS| are all zeros and
  50. // so can be placed in the BSS section.
  51. EXPECT_EQ(Bytes((uint8_t *)&once_bss, sizeof(once_bss)),
  52. Bytes((uint8_t *)&once_init_value, sizeof(once_init_value)));
  53. EXPECT_EQ(Bytes((uint8_t *)&mutex_bss, sizeof(mutex_bss)),
  54. Bytes((uint8_t *)&mutex_init_value, sizeof(mutex_init_value)));
  55. EXPECT_EQ(
  56. Bytes((uint8_t *)&ex_data_class_bss, sizeof(ex_data_class_bss)),
  57. Bytes((uint8_t *)&ex_data_class_value, sizeof(ex_data_class_value)));
  58. }
  59. }
  60. static int g_test_thread_ok = 0;
  61. static unsigned g_destructor_called_count = 0;
  62. static void thread_local_destructor(void *arg) {
  63. if (arg == NULL) {
  64. return;
  65. }
  66. unsigned *count = reinterpret_cast<unsigned*>(arg);
  67. (*count)++;
  68. }
  69. TEST(ThreadTest, ThreadLocal) {
  70. ASSERT_EQ(nullptr, CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST))
  71. << "Thread-local data was non-NULL at start.";
  72. std::thread thread([] {
  73. if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != NULL ||
  74. !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST,
  75. &g_destructor_called_count,
  76. thread_local_destructor) ||
  77. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) !=
  78. &g_destructor_called_count) {
  79. return;
  80. }
  81. g_test_thread_ok = 1;
  82. });
  83. thread.join();
  84. EXPECT_TRUE(g_test_thread_ok) << "Thread-local data didn't work in thread.";
  85. EXPECT_EQ(1u, g_destructor_called_count);
  86. // Create a no-op thread to test that the thread destructor function works
  87. // even if thread-local storage wasn't used for a thread.
  88. thread = std::thread([] {});
  89. thread.join();
  90. }
  91. TEST(ThreadTest, RandState) {
  92. // In FIPS mode, rand.c maintains a linked-list of thread-local data because
  93. // we're required to clear it on process exit. This test exercises removing a
  94. // value from that list.
  95. uint8_t buf[1];
  96. RAND_bytes(buf, sizeof(buf));
  97. std::thread thread([] {
  98. uint8_t buf2[1];
  99. RAND_bytes(buf2, sizeof(buf2));
  100. });
  101. thread.join();
  102. }
  103. #endif // OPENSSL_THREADS