Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2015 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 #include <pthread.h> | |
| 8 #include <semaphore.h> | |
| 9 | |
| 10 #include "native_client/src/include/nacl/nacl_signal.h" | |
| 11 #include "native_client/src/include/nacl_assert.h" | |
| 12 #include "native_client/src/public/nonsfi/irt_signal_handling.h" | |
| 13 #include "native_client/src/untrusted/irt/irt.h" | |
| 14 #include "native_client/src/untrusted/nacl/nacl_irt.h" | |
| 15 #include "native_client/src/untrusted/nacl/nacl_thread.h" | |
| 16 #include "native_client/src/untrusted/pthread/pthread_types.h" | |
| 17 | |
| 18 #define CHECK_OK(expr) ASSERT_EQ(expr, 0) | |
| 19 | |
| 20 extern struct nacl_irt_thread_v0_2 __libnacl_irt_thread_v0_2; | |
|
Mark Seaborn
2015/07/16 23:21:17
Use the appropriate #include instead of an extern
Luis Héctor Chávez
2015/07/20 18:13:47
Done.
| |
| 21 volatile int g_signal_count; | |
| 22 volatile int g_signal_arrived; | |
| 23 volatile int g_test_running; | |
| 24 static nacl_irt_tid_t g_child_tid; | |
| 25 void* g_expected_tls; | |
|
Mark Seaborn
2015/07/16 23:21:17
Nit: "*" spacing
Luis Héctor Chávez
2015/07/20 18:13:47
Done.
| |
| 26 sem_t g_sem; | |
| 27 | |
| 28 int thread_create_wrapper(void (*start_func)(void), void *stack, | |
| 29 void *thread_ptr) { | |
| 30 return __libnacl_irt_thread_v0_2.thread_create(start_func, stack, thread_ptr, | |
| 31 &g_child_tid); | |
| 32 } | |
| 33 | |
| 34 /* | |
| 35 * Check that sending a signal before initializing signal support will result in | |
| 36 * an error. | |
| 37 */ | |
| 38 void test_send_signal_before_init() { | |
| 39 int retval = nacl_signal_send_async_signal(0); | |
| 40 ASSERT_EQ(retval, ESRCH); | |
| 41 } | |
| 42 | |
| 43 /* | |
| 44 * Check that nacl_tls_get() is async-signal-safe. | |
| 45 */ | |
| 46 void tls_get_signal_handler(NaClExceptionContext *exc) { | |
| 47 if (!g_test_running) | |
| 48 return; | |
| 49 ASSERT_EQ(nacl_tls_get(), g_expected_tls); | |
| 50 g_signal_count++; | |
| 51 g_signal_arrived = 1; | |
| 52 } | |
| 53 | |
| 54 void *tls_get_thread_func(void *arg) { | |
| 55 g_expected_tls = nacl_tls_get(); | |
| 56 CHECK_OK(sem_post(&g_sem)); | |
| 57 while (g_test_running) { | |
| 58 ASSERT_EQ(nacl_tls_get(), g_expected_tls); | |
| 59 if (__sync_bool_compare_and_swap(&g_signal_arrived, 1, 0)) { | |
| 60 CHECK_OK(sem_post(&g_sem)); | |
| 61 } | |
| 62 } | |
| 63 return NULL; | |
| 64 } | |
| 65 | |
| 66 void test_async_safe_tls_get() { | |
| 67 CHECK_OK(sem_init(&g_sem, 0, 0)); | |
| 68 nacl_signal_set_handler(tls_get_signal_handler); | |
| 69 | |
| 70 pthread_t tid; | |
| 71 g_signal_count = 0; | |
| 72 g_signal_arrived = 0; | |
| 73 g_test_running = true; | |
| 74 CHECK_OK(pthread_create(&tid, NULL, tls_get_thread_func, NULL)); | |
| 75 | |
| 76 CHECK_OK(sem_wait(&g_sem)); | |
| 77 const int kSignalCount = 1000; | |
| 78 for (int i = 0; i < kSignalCount; i++) { | |
| 79 CHECK_OK(nacl_signal_send_async_signal(g_child_tid)); | |
| 80 CHECK_OK(sem_wait(&g_sem)); | |
| 81 } | |
| 82 g_test_running = false; | |
| 83 /* Send a last signal to make sure any waiting syscalls get interrupted. */ | |
| 84 CHECK_OK(nacl_signal_send_async_signal(g_child_tid)); | |
| 85 CHECK_OK(pthread_join(tid, NULL)); | |
| 86 ASSERT_EQ(g_signal_count, kSignalCount); | |
| 87 CHECK_OK(sem_destroy(&g_sem)); | |
| 88 } | |
| 89 | |
| 90 /* | |
| 91 * Check that both futex_wake() and futex_wait_abs() are signal-async-safe. | |
| 92 */ | |
| 93 void futex_signal_handler(NaClExceptionContext *exc) { | |
| 94 int count = 0; | |
| 95 ASSERT_EQ(__sync_bool_compare_and_swap(&g_signal_arrived, 0, 1), 1); | |
| 96 CHECK_OK(__libnacl_irt_futex.futex_wake(&g_signal_arrived, INT_MAX, &count)); | |
| 97 /* | |
| 98 * |count| is always 0 since the thread waiting is now running the signal | |
| 99 * handler, so it did not actually counts as a wakeup. | |
| 100 */ | |
| 101 ASSERT_EQ(count, 0); | |
| 102 if (g_test_running) | |
| 103 g_signal_count++; | |
| 104 } | |
| 105 | |
| 106 void *futex_thread_func(void *arg) { | |
| 107 CHECK_OK(sem_post(&g_sem)); | |
| 108 struct timespec timeout; | |
| 109 /* | |
| 110 * Make the timeout be the current time plus 10 seconds. This timeout should | |
| 111 * never kick in, but if it does it means we deadlocked, so it's better to | |
| 112 * assert than letting the job itself time out. | |
| 113 */ | |
| 114 clock_gettime(CLOCK_REALTIME, &timeout); | |
| 115 timeout.tv_sec += 10; | |
| 116 while (g_test_running) { | |
| 117 int retval = __libnacl_irt_futex.futex_wait_abs(&g_signal_arrived, 0, | |
| 118 &timeout); | |
| 119 if (retval == EWOULDBLOCK) { | |
| 120 /* | |
| 121 * The signal handler executed before we could wait and changed the value | |
| 122 * of |g_signal_arrived|. | |
| 123 */ | |
| 124 } else { | |
| 125 ASSERT_EQ(retval, EINTR); | |
| 126 } | |
| 127 ASSERT_EQ(__sync_bool_compare_and_swap(&g_signal_arrived, 1, 0), 1); | |
| 128 /* | |
| 129 * Have to test again since we could have gone sleeping again after the last | |
| 130 * iteration. | |
| 131 */ | |
| 132 if (g_test_running) | |
| 133 CHECK_OK(sem_post(&g_sem)); | |
| 134 } | |
| 135 return NULL; | |
| 136 } | |
| 137 | |
| 138 void test_async_safe_futex() { | |
| 139 CHECK_OK(sem_init(&g_sem, 0, 0)); | |
| 140 nacl_signal_set_handler(futex_signal_handler); | |
| 141 | |
| 142 pthread_t tid; | |
| 143 g_signal_count = 0; | |
| 144 g_signal_arrived = 0; | |
| 145 g_test_running = true; | |
| 146 CHECK_OK(pthread_create(&tid, NULL, futex_thread_func, NULL)); | |
| 147 | |
| 148 CHECK_OK(sem_wait(&g_sem)); | |
| 149 const int kSignalCount = 1000; | |
| 150 for (int i = 0; i < kSignalCount; i++) { | |
| 151 CHECK_OK(nacl_signal_send_async_signal(g_child_tid)); | |
| 152 CHECK_OK(sem_wait(&g_sem)); | |
| 153 } | |
| 154 g_test_running = false; | |
| 155 /* Send a last signal to make sure any waiting syscalls get interrupted. */ | |
| 156 CHECK_OK(nacl_signal_send_async_signal(g_child_tid)); | |
| 157 CHECK_OK(pthread_join(tid, NULL)); | |
| 158 ASSERT_EQ(g_signal_count, kSignalCount); | |
| 159 CHECK_OK(sem_destroy(&g_sem)); | |
| 160 } | |
| 161 | |
| 162 /* | |
| 163 * Check that nacl_signal_send_async_signal() is async-signal-safe. | |
| 164 */ | |
| 165 void signal_signal_handler(NaClExceptionContext *exc) { | |
| 166 if (!g_test_running) | |
| 167 return; | |
| 168 if (++g_signal_count % 2 == 1) { | |
| 169 CHECK_OK(nacl_signal_send_async_signal(g_child_tid)); | |
| 170 g_signal_arrived = 1; | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 void *signal_thread_func(void *arg) { | |
| 175 CHECK_OK(sem_post(&g_sem)); | |
| 176 struct timespec req, rem; | |
| 177 /* | |
| 178 * In case we are unlucky and the signal arrives before the first sleep, limit | |
| 179 * the time sleeping to 10 msec. | |
| 180 */ | |
| 181 req.tv_sec = 0; | |
| 182 req.tv_nsec = 10000000; | |
| 183 while (g_test_running) { | |
| 184 while (g_test_running && !g_signal_arrived) { | |
| 185 int retval = nanosleep(&req, &rem); | |
| 186 if (retval != 0) | |
| 187 ASSERT_EQ(errno, EINTR); | |
| 188 } | |
| 189 /* | |
| 190 * Have to test again since we could have gone sleeping again after the last | |
| 191 * iteration. | |
| 192 */ | |
| 193 if (!g_test_running) | |
| 194 break; | |
| 195 g_signal_arrived = 0; | |
| 196 CHECK_OK(sem_post(&g_sem)); | |
| 197 } | |
| 198 return NULL; | |
| 199 } | |
| 200 | |
| 201 void test_async_safe_signal() { | |
| 202 CHECK_OK(sem_init(&g_sem, 0, 0)); | |
| 203 nacl_signal_set_handler(signal_signal_handler); | |
| 204 | |
| 205 pthread_t tid; | |
| 206 g_test_running = true; | |
| 207 g_signal_count = 0; | |
| 208 g_signal_arrived = 0; | |
| 209 CHECK_OK(pthread_create(&tid, NULL, signal_thread_func, NULL)); | |
| 210 | |
| 211 CHECK_OK(sem_wait(&g_sem)); | |
| 212 const int kSignalCount = 1000; | |
| 213 for (int i = 0; i < kSignalCount; i++) { | |
| 214 CHECK_OK(nacl_signal_send_async_signal(g_child_tid)); | |
| 215 CHECK_OK(sem_wait(&g_sem)); | |
| 216 } | |
| 217 g_test_running = false; | |
| 218 /* Send a last signal to make sure any waiting syscalls get interrupted. */ | |
| 219 CHECK_OK(nacl_signal_send_async_signal(g_child_tid)); | |
| 220 CHECK_OK(pthread_join(tid, NULL)); | |
| 221 ASSERT_EQ(g_signal_count, 2 * kSignalCount); | |
| 222 CHECK_OK(sem_destroy(&g_sem)); | |
| 223 } | |
| 224 | |
| 225 /* | |
| 226 * Check that passing 0 as |tid| to nacl_signal_send_async_signal() works and | |
| 227 * sends a signal to the main thread. | |
| 228 */ | |
| 229 void main_signal_handler(NaClExceptionContext *exc) { | |
| 230 g_signal_count = 1; | |
| 231 } | |
| 232 | |
| 233 void test_main_signal() { | |
| 234 nacl_signal_set_handler(main_signal_handler); | |
|
Mark Seaborn
2015/07/16 23:21:17
By calling this function, you're not testing that
Luis Héctor Chávez
2015/07/20 18:13:47
Done.
| |
| 235 | |
| 236 g_signal_count = 0; | |
| 237 CHECK_OK(nacl_signal_send_async_signal(0)); | |
| 238 ASSERT_EQ(g_signal_count, 1); | |
| 239 } | |
| 240 | |
| 241 void run_test(const char *test_name, void (*test_func)(void)) { | |
| 242 printf("Running %s...\n", test_name); | |
| 243 test_func(); | |
| 244 } | |
| 245 | |
| 246 #define RUN_TEST(test_func) (run_test(#test_func, test_func)) | |
| 247 | |
| 248 int main(void) { | |
| 249 /* | |
| 250 * In order to avoid modifying the libpthread implementation to save the | |
| 251 * native tid, mock that functionality so the tid is stored in a thread-local | |
|
Mark Seaborn
2015/07/16 23:21:17
Nit: "mock" -> "wrap"
Luis Héctor Chávez
2015/07/20 18:13:47
Done.
| |
| 252 * variable. | |
| 253 */ | |
| 254 __libnacl_irt_thread.thread_create = &thread_create_wrapper; | |
| 255 | |
| 256 RUN_TEST(test_send_signal_before_init); | |
| 257 | |
| 258 nonsfi_initialize_signal_handler(); | |
| 259 | |
| 260 RUN_TEST(test_async_safe_tls_get); | |
| 261 #if !defined(__arm__) | |
| 262 /* | |
| 263 * Signals are sometimes delivered after the futex_wait syscall returns (as | |
| 264 * opposed to interrupting it), which breaks this test. | |
| 265 */ | |
| 266 RUN_TEST(test_async_safe_futex); | |
| 267 #endif | |
| 268 RUN_TEST(test_async_safe_signal); | |
| 269 RUN_TEST(test_main_signal); | |
| 270 | |
| 271 printf("Done\n"); | |
| 272 | |
| 273 return 0; | |
| 274 } | |
| OLD | NEW |