common.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2022 Amazon.com Inc. or its affiliates. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <assert.h>
  8. #include <stdbool.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #if USE_CUSTOM_SYNC_PRIMITIVES != 0
  12. #include "sync_primitives.h"
  13. #else
  14. #include <pthread.h>
  15. #endif
  16. #include "wasi_thread_start.h"
  17. typedef enum {
  18. BLOCKING_TASK_BUSY_WAIT,
  19. BLOCKING_TASK_ATOMIC_WAIT,
  20. BLOCKING_TASK_POLL_ONEOFF
  21. } blocking_task_type_t;
  22. /* Parameter to change test behavior */
  23. static bool termination_by_trap;
  24. static bool termination_in_main_thread;
  25. static blocking_task_type_t blocking_task_type;
  26. #define NUM_THREADS 3
  27. static pthread_barrier_t barrier;
  28. typedef struct {
  29. start_args_t base;
  30. bool throw_exception;
  31. } shared_t;
  32. void
  33. run_long_task()
  34. {
  35. if (blocking_task_type == BLOCKING_TASK_BUSY_WAIT) {
  36. for (;;) {
  37. }
  38. }
  39. else if (blocking_task_type == BLOCKING_TASK_ATOMIC_WAIT) {
  40. __builtin_wasm_memory_atomic_wait32(0, 0, -1);
  41. }
  42. else {
  43. sleep(UINT_MAX);
  44. }
  45. }
  46. void
  47. start_job()
  48. {
  49. /* Wait for all threads (including the main thread) to be ready */
  50. pthread_barrier_wait(&barrier);
  51. run_long_task(); /* Task to be interrupted */
  52. assert(false && "Thread termination test failed");
  53. }
  54. void
  55. terminate_process()
  56. {
  57. /* Wait for all threads (including the main thread) to be ready */
  58. pthread_barrier_wait(&barrier);
  59. if (termination_by_trap)
  60. __builtin_trap();
  61. else
  62. __wasi_proc_exit(33);
  63. }
  64. void
  65. __wasi_thread_start_C(int thread_id, int *start_arg)
  66. {
  67. shared_t *data = (shared_t *)start_arg;
  68. if (data->throw_exception) {
  69. terminate_process();
  70. }
  71. else {
  72. start_job();
  73. }
  74. }
  75. void
  76. test_termination(bool trap, bool main, blocking_task_type_t task_type)
  77. {
  78. termination_by_trap = trap;
  79. termination_in_main_thread = main;
  80. blocking_task_type = task_type;
  81. int thread_id = -1, i;
  82. shared_t data[NUM_THREADS] = { 0 };
  83. assert(pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1) == 0
  84. && "Failed to init barrier");
  85. for (i = 0; i < NUM_THREADS; i++) {
  86. /* No graceful memory free to simplify the test */
  87. assert(start_args_init(&data[i].base)
  88. && "Failed to allocate thread's stack");
  89. }
  90. /* Create a thread that forces termination through trap or `proc_exit` */
  91. data[0].throw_exception = !termination_in_main_thread;
  92. thread_id = __wasi_thread_spawn(&data[0]);
  93. assert(thread_id > 0 && "Failed to create thread");
  94. /* Create two additional threads to test exception propagation */
  95. data[1].throw_exception = false;
  96. thread_id = __wasi_thread_spawn(&data[1]);
  97. assert(thread_id > 0 && "Failed to create thread");
  98. data[2].throw_exception = false;
  99. thread_id = __wasi_thread_spawn(&data[2]);
  100. assert(thread_id > 0 && "Failed to create thread");
  101. if (termination_in_main_thread) {
  102. terminate_process();
  103. }
  104. else {
  105. start_job();
  106. }
  107. }