25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

203 lines
4.9 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 <stdio.h>
  16. #if !defined(OPENSSL_NO_THREADS)
  17. #if defined(OPENSSL_WINDOWS)
  18. #pragma warning(push, 3)
  19. #include <windows.h>
  20. #pragma warning(pop)
  21. typedef HANDLE thread_t;
  22. static DWORD WINAPI thread_run(LPVOID arg) {
  23. void (*thread_func)(void);
  24. /* VC really doesn't like casting between data and function pointers. */
  25. memcpy(&thread_func, &arg, sizeof(thread_func));
  26. thread_func();
  27. return 0;
  28. }
  29. static int run_thread(thread_t *out_thread, void (*thread_func)(void)) {
  30. void *arg;
  31. /* VC really doesn't like casting between data and function pointers. */
  32. memcpy(&arg, &thread_func, sizeof(arg));
  33. *out_thread = CreateThread(NULL /* security attributes */,
  34. 0 /* default stack size */, thread_run, arg,
  35. 0 /* run immediately */, NULL /* ignore id */);
  36. return *out_thread != NULL;
  37. }
  38. static int wait_for_thread(thread_t thread) {
  39. return WaitForSingleObject(thread, INFINITE) == 0;
  40. }
  41. #else
  42. #include <pthread.h>
  43. typedef pthread_t thread_t;
  44. static void *thread_run(void *arg) {
  45. void (*thread_func)(void) = arg;
  46. thread_func();
  47. return NULL;
  48. }
  49. static int run_thread(thread_t *out_thread, void (*thread_func)(void)) {
  50. return pthread_create(out_thread, NULL /* default attributes */, thread_run,
  51. thread_func) == 0;
  52. }
  53. static int wait_for_thread(thread_t thread) {
  54. return pthread_join(thread, NULL) == 0;
  55. }
  56. #endif /* OPENSSL_WINDOWS */
  57. static unsigned g_once_init_called = 0;
  58. static void once_init(void) {
  59. g_once_init_called++;
  60. }
  61. static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT;
  62. static void call_once_thread(void) {
  63. CRYPTO_once(&g_test_once, once_init);
  64. }
  65. static int test_once(void) {
  66. if (g_once_init_called != 0) {
  67. fprintf(stderr, "g_once_init_called was non-zero at start.\n");
  68. return 0;
  69. }
  70. thread_t thread;
  71. if (!run_thread(&thread, call_once_thread) ||
  72. !wait_for_thread(thread)) {
  73. fprintf(stderr, "thread failed.\n");
  74. return 0;
  75. }
  76. CRYPTO_once(&g_test_once, once_init);
  77. if (g_once_init_called != 1) {
  78. fprintf(stderr, "Expected init function to be called once, but found %u.\n",
  79. g_once_init_called);
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. static int g_test_thread_ok = 0;
  85. static unsigned g_destructor_called_count = 0;
  86. static void thread_local_destructor(void *arg) {
  87. if (arg == NULL) {
  88. return;
  89. }
  90. unsigned *count = arg;
  91. (*count)++;
  92. }
  93. static void thread_local_test_thread(void) {
  94. void *ptr = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST);
  95. if (ptr != NULL) {
  96. return;
  97. }
  98. if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST,
  99. &g_destructor_called_count,
  100. thread_local_destructor)) {
  101. return;
  102. }
  103. if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) !=
  104. &g_destructor_called_count) {
  105. return;
  106. }
  107. g_test_thread_ok = 1;
  108. }
  109. static void thread_local_test2_thread(void) {}
  110. static int test_thread_local(void) {
  111. void *ptr = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST);
  112. if (ptr != NULL) {
  113. fprintf(stderr, "Thread-local data was non-NULL at start.\n");
  114. }
  115. thread_t thread;
  116. if (!run_thread(&thread, thread_local_test_thread) ||
  117. !wait_for_thread(thread)) {
  118. fprintf(stderr, "thread failed.\n");
  119. return 0;
  120. }
  121. if (!g_test_thread_ok) {
  122. fprintf(stderr, "Thread-local data didn't work in thread.\n");
  123. return 0;
  124. }
  125. if (g_destructor_called_count != 1) {
  126. fprintf(stderr,
  127. "Destructor should have been called once, but actually called %u "
  128. "times.\n",
  129. g_destructor_called_count);
  130. return 0;
  131. }
  132. /* thread_local_test2_thread doesn't do anything, but it tests that the
  133. * thread destructor function works even if thread-local storage wasn't used
  134. * for a thread. */
  135. if (!run_thread(&thread, thread_local_test2_thread) ||
  136. !wait_for_thread(thread)) {
  137. fprintf(stderr, "thread failed.\n");
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. int main(int argc, char **argv) {
  143. if (!test_once() ||
  144. !test_thread_local()) {
  145. return 1;
  146. }
  147. printf("PASS\n");
  148. return 0;
  149. }
  150. #else /* OPENSSL_NO_THREADS */
  151. int main(int argc, char **argv) {
  152. printf("PASS\n");
  153. return 0;
  154. }
  155. #endif