Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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