Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

201 rinda
6.0 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 <gtest/gtest.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/rand.h>
  18. #include "test/test_util.h"
  19. #if !defined(OPENSSL_NO_THREADS)
  20. #if defined(OPENSSL_WINDOWS)
  21. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  22. #include <windows.h>
  23. OPENSSL_MSVC_PRAGMA(warning(pop))
  24. typedef HANDLE thread_t;
  25. static DWORD WINAPI thread_run(LPVOID arg) {
  26. void (*thread_func)(void);
  27. // VC really doesn't like casting between data and function pointers.
  28. OPENSSL_memcpy(&thread_func, &arg, sizeof(thread_func));
  29. thread_func();
  30. return 0;
  31. }
  32. static int run_thread(thread_t *out_thread, void (*thread_func)(void)) {
  33. void *arg;
  34. // VC really doesn't like casting between data and function pointers.
  35. OPENSSL_memcpy(&arg, &thread_func, sizeof(arg));
  36. *out_thread = CreateThread(NULL /* security attributes */,
  37. 0 /* default stack size */, thread_run, arg,
  38. 0 /* run immediately */, NULL /* ignore id */);
  39. return *out_thread != NULL;
  40. }
  41. static int wait_for_thread(thread_t thread) {
  42. return WaitForSingleObject(thread, INFINITE) == 0;
  43. }
  44. #else
  45. #include <pthread.h>
  46. #include <string.h>
  47. #include <time.h>
  48. typedef pthread_t thread_t;
  49. static void *thread_run(void *arg) {
  50. void (*thread_func)(void) = reinterpret_cast<void (*)(void)>(arg);
  51. thread_func();
  52. return NULL;
  53. }
  54. static int run_thread(thread_t *out_thread, void (*thread_func)(void)) {
  55. return pthread_create(out_thread, NULL /* default attributes */, thread_run,
  56. reinterpret_cast<void *>(thread_func)) == 0;
  57. }
  58. static int wait_for_thread(thread_t thread) {
  59. return pthread_join(thread, NULL) == 0;
  60. }
  61. #endif // OPENSSL_WINDOWS
  62. static unsigned g_once_init_called = 0;
  63. static void once_init(void) {
  64. g_once_init_called++;
  65. // Sleep briefly so one |call_once_thread| instance will call |CRYPTO_once|
  66. // while the other is running this function.
  67. #if defined(OPENSSL_WINDOWS)
  68. Sleep(1 /* milliseconds */);
  69. #else
  70. struct timespec req;
  71. OPENSSL_memset(&req, 0, sizeof(req));
  72. req.tv_nsec = 1000000;
  73. nanosleep(&req, NULL);
  74. #endif
  75. }
  76. static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT;
  77. static void call_once_thread(void) {
  78. CRYPTO_once(&g_test_once, once_init);
  79. }
  80. static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT;
  81. static CRYPTO_once_t once_bss;
  82. static struct CRYPTO_STATIC_MUTEX mutex_init_value = CRYPTO_STATIC_MUTEX_INIT;
  83. static struct CRYPTO_STATIC_MUTEX mutex_bss;
  84. static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT;
  85. static CRYPTO_EX_DATA_CLASS ex_data_class_bss;
  86. TEST(ThreadTest, Once) {
  87. ASSERT_EQ(0u, g_once_init_called)
  88. << "g_once_init_called was non-zero at start.";
  89. thread_t thread1, thread2;
  90. ASSERT_TRUE(run_thread(&thread1, call_once_thread));
  91. ASSERT_TRUE(run_thread(&thread2, call_once_thread));
  92. ASSERT_TRUE(wait_for_thread(thread1));
  93. ASSERT_TRUE(wait_for_thread(thread2));
  94. CRYPTO_once(&g_test_once, once_init);
  95. EXPECT_EQ(1u, g_once_init_called);
  96. }
  97. TEST(ThreadTest, InitZeros) {
  98. if (FIPS_mode()) {
  99. // Our FIPS tooling currently requires that |CRYPTO_ONCE_INIT|,
  100. // |CRYPTO_STATIC_MUTEX_INIT| and |CRYPTO_EX_DATA_CLASS| are all zeros and
  101. // so can be placed in the BSS section.
  102. EXPECT_EQ(Bytes((uint8_t *)&once_bss, sizeof(once_bss)),
  103. Bytes((uint8_t *)&once_init_value, sizeof(once_init_value)));
  104. EXPECT_EQ(Bytes((uint8_t *)&mutex_bss, sizeof(mutex_bss)),
  105. Bytes((uint8_t *)&mutex_init_value, sizeof(mutex_init_value)));
  106. EXPECT_EQ(
  107. Bytes((uint8_t *)&ex_data_class_bss, sizeof(ex_data_class_bss)),
  108. Bytes((uint8_t *)&ex_data_class_value, sizeof(ex_data_class_value)));
  109. }
  110. }
  111. static int g_test_thread_ok = 0;
  112. static unsigned g_destructor_called_count = 0;
  113. static void thread_local_destructor(void *arg) {
  114. if (arg == NULL) {
  115. return;
  116. }
  117. unsigned *count = reinterpret_cast<unsigned*>(arg);
  118. (*count)++;
  119. }
  120. TEST(ThreadTest, ThreadLocal) {
  121. ASSERT_EQ(nullptr, CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST))
  122. << "Thread-local data was non-NULL at start.";
  123. thread_t thread;
  124. ASSERT_TRUE(run_thread(&thread, []() {
  125. if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != NULL ||
  126. !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST,
  127. &g_destructor_called_count,
  128. thread_local_destructor) ||
  129. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) !=
  130. &g_destructor_called_count) {
  131. return;
  132. }
  133. g_test_thread_ok = 1;
  134. }));
  135. ASSERT_TRUE(wait_for_thread(thread));
  136. EXPECT_TRUE(g_test_thread_ok) << "Thread-local data didn't work in thread.";
  137. EXPECT_EQ(1u, g_destructor_called_count);
  138. // Create a no-op thread to test test that the thread destructor function
  139. // works even if thread-local storage wasn't used for a thread.
  140. ASSERT_TRUE(run_thread(&thread, []() {}));
  141. ASSERT_TRUE(wait_for_thread(thread));
  142. }
  143. TEST(ThreadTest, RandState) {
  144. // In FIPS mode, rand.c maintains a linked-list of thread-local data because
  145. // we're required to clear it on process exit. This test exercises removing a
  146. // value from that list.
  147. uint8_t buf[1];
  148. RAND_bytes(buf, sizeof(buf));
  149. thread_t thread;
  150. ASSERT_TRUE(run_thread(&thread, []() {
  151. uint8_t buf2[1];
  152. RAND_bytes(buf2, sizeof(buf2));
  153. }));
  154. ASSERT_TRUE(wait_for_thread(thread));
  155. }
  156. #endif // !OPENSSL_NO_THREADS