Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: tests/nonsfi/user_async_signal_test.cc

Issue 1300883003: Revert of Non-SFI mode: Add Linux asynchronous signal support (Closed) Base URL: https://chromium.googlesource.com/native_client/src/native_client.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/nonsfi/nacl.scons ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_assert.h"
11 #include "native_client/src/untrusted/irt/irt.h"
12 #include "native_client/src/untrusted/nacl/nacl_irt.h"
13 #include "native_client/src/untrusted/nacl/nacl_thread.h"
14
15 #define CHECK_OK(expr) ASSERT_EQ(expr, 0)
16
17 namespace {
18
19 struct nacl_irt_thread_v0_2 libnacl_irt_thread_v0_2;
20 struct nacl_irt_async_signal_handling libnacl_irt_async_signal_handling;
21
22 volatile int g_signal_count;
23 volatile int g_signal_arrived;
24 volatile int g_test_running;
25 nacl_irt_tid_t g_child_tid;
26 void *g_expected_tls;
27 sem_t g_sem;
28
29 int thread_create_wrapper(void (*start_func)(void), void *stack,
30 void *thread_ptr) {
31 return libnacl_irt_thread_v0_2.thread_create(start_func, stack, thread_ptr,
32 &g_child_tid);
33 }
34
35 int set_async_signal_handler(NaClIrtAsyncSignalHandler handler) {
36 return libnacl_irt_async_signal_handling.set_async_signal_handler(handler);
37 }
38
39 int send_async_signal(nacl_irt_tid_t tid) {
40 return libnacl_irt_async_signal_handling.send_async_signal(tid);
41 }
42
43 /*
44 * Check that sending a signal before initializing signal support will result in
45 * an error.
46 */
47 void test_send_signal_before_set_handler() {
48 int retval = send_async_signal(0);
49 ASSERT_EQ(retval, ESRCH);
50 }
51
52 /*
53 * Check that nacl_tls_get() is async-signal-safe.
54 */
55 void tls_get_signal_handler(NaClExceptionContext *exc) {
56 if (!g_test_running)
57 return;
58 ASSERT_EQ(nacl_tls_get(), g_expected_tls);
59 g_signal_count++;
60 g_signal_arrived = 1;
61 }
62
63 void *tls_get_thread_func(void *arg) {
64 g_expected_tls = nacl_tls_get();
65 CHECK_OK(sem_post(&g_sem));
66 while (g_test_running) {
67 ASSERT_EQ(nacl_tls_get(), g_expected_tls);
68 if (__sync_bool_compare_and_swap(&g_signal_arrived, 1, 0)) {
69 CHECK_OK(sem_post(&g_sem));
70 }
71 }
72 return NULL;
73 }
74
75 void test_async_safe_tls_get() {
76 CHECK_OK(sem_init(&g_sem, 0, 0));
77 CHECK_OK(set_async_signal_handler(tls_get_signal_handler));
78
79 pthread_t tid;
80 g_signal_count = 0;
81 g_signal_arrived = 0;
82 g_test_running = true;
83 CHECK_OK(pthread_create(&tid, NULL, tls_get_thread_func, NULL));
84
85 CHECK_OK(sem_wait(&g_sem));
86 const int kSignalCount = 1000;
87 for (int i = 0; i < kSignalCount; i++) {
88 CHECK_OK(send_async_signal(g_child_tid));
89 CHECK_OK(sem_wait(&g_sem));
90 }
91 g_test_running = false;
92 /* Send a last signal to make sure any waiting syscalls get interrupted. */
93 CHECK_OK(send_async_signal(g_child_tid));
94 CHECK_OK(pthread_join(tid, NULL));
95 ASSERT_EQ(g_signal_count, kSignalCount);
96 CHECK_OK(sem_destroy(&g_sem));
97 }
98
99 #if !defined(__arm__)
100 /* This test is broken on QEMU. */
101
102 /*
103 * Check that both futex_wake() and futex_wait_abs() are signal-async-safe.
104 */
105 void futex_signal_handler(NaClExceptionContext *exc) {
106 int count = 0;
107 ASSERT_EQ(__sync_bool_compare_and_swap(&g_signal_arrived, 0, 1), 1);
108 CHECK_OK(__libnacl_irt_futex.futex_wake(&g_signal_arrived, INT_MAX, &count));
109 /*
110 * |count| is always 0 since the thread waiting is now running the signal
111 * handler, so it did not actually count as a wakeup.
112 */
113 ASSERT_EQ(count, 0);
114 if (g_test_running)
115 g_signal_count++;
116 }
117
118 void *futex_thread_func(void *arg) {
119 CHECK_OK(sem_post(&g_sem));
120 struct timespec timeout;
121 /*
122 * Make the timeout be the current time plus 10 seconds. This timeout should
123 * never kick in, but if it does it means we deadlocked, so it's better to
124 * assert than letting the job itself time out.
125 */
126 clock_gettime(CLOCK_REALTIME, &timeout);
127 timeout.tv_sec += 10;
128 while (g_test_running) {
129 int retval = __libnacl_irt_futex.futex_wait_abs(&g_signal_arrived, 0,
130 &timeout);
131 if (retval == EWOULDBLOCK) {
132 /*
133 * The signal handler executed before we could wait and changed the value
134 * of |g_signal_arrived|.
135 */
136 } else {
137 /*
138 * futex_wait_abs, when provided with a non-NULL timeout argument, can be
139 * interrupted and will set errno to EINTR. This can happen even if the
140 * SA_RESTART flag was used.
141 */
142 ASSERT_EQ(retval, EINTR);
143 }
144 ASSERT_EQ(__sync_bool_compare_and_swap(&g_signal_arrived, 1, 0), 1);
145 /*
146 * Have to test again since we could have gone sleeping again after the last
147 * iteration.
148 */
149 if (g_test_running)
150 CHECK_OK(sem_post(&g_sem));
151 }
152 return NULL;
153 }
154
155 void test_async_safe_futex() {
156 CHECK_OK(sem_init(&g_sem, 0, 0));
157 CHECK_OK(set_async_signal_handler(futex_signal_handler));
158
159 pthread_t tid;
160 g_signal_count = 0;
161 g_signal_arrived = 0;
162 g_test_running = true;
163 CHECK_OK(pthread_create(&tid, NULL, futex_thread_func, NULL));
164
165 CHECK_OK(sem_wait(&g_sem));
166 const int kSignalCount = 1000;
167 for (int i = 0; i < kSignalCount; i++) {
168 CHECK_OK(send_async_signal(g_child_tid));
169 CHECK_OK(sem_wait(&g_sem));
170 }
171 g_test_running = false;
172 /* Send a last signal to make sure any waiting syscalls get interrupted. */
173 CHECK_OK(send_async_signal(g_child_tid));
174 CHECK_OK(pthread_join(tid, NULL));
175 ASSERT_EQ(g_signal_count, kSignalCount);
176 CHECK_OK(sem_destroy(&g_sem));
177 }
178
179 #endif
180
181 /*
182 * Check that futex_wait_abs() with no timeout is restarted.
183 * As opposed to the above test with futex, the signal handler does not try to
184 * wake the thread up, since it will sometimes be called _after_ the
185 * futex_wait_abs() returns.
186 */
187 void futex_wait_signal_handler(NaClExceptionContext *exc) {
188 ASSERT_EQ(__sync_bool_compare_and_swap(&g_signal_arrived, 0, 1), 1);
189 }
190
191 void *futex_wait_thread_func(void *arg) {
192 volatile int *futex = (volatile int *)arg;
193 CHECK_OK(sem_post(&g_sem));
194 while (g_test_running) {
195 /*
196 * Unfortunately, Linux sometimes can return 0 (instead of EINTR) on
197 * futex_wait_abs() when it is spuriously woken up.
198 */
199 while (*futex == 0) {
200 int retval = __libnacl_irt_futex.futex_wait_abs(futex, 0, NULL);
201 if (retval != EWOULDBLOCK)
202 ASSERT_EQ(retval, 0);
203 }
204 ASSERT_EQ(__sync_bool_compare_and_swap(futex, 1, 0), 1);
205
206 /*
207 * Have to test again since we could have gone sleeping again after the last
208 * iteration.
209 */
210 if (g_test_running) {
211 ASSERT_EQ(__sync_bool_compare_and_swap(&g_signal_arrived, 1, 0), 1);
212 g_signal_count++;
213 CHECK_OK(sem_post(&g_sem));
214 }
215 }
216 return NULL;
217 }
218
219 void test_futex_wait_restart() {
220 CHECK_OK(sem_init(&g_sem, 0, 0));
221 CHECK_OK(set_async_signal_handler(futex_wait_signal_handler));
222
223 pthread_t tid;
224 g_signal_count = 0;
225 g_signal_arrived = 0;
226 volatile int futex = 0;
227 g_test_running = true;
228 CHECK_OK(pthread_create(&tid, NULL, futex_wait_thread_func, (void *)&futex));
229
230 CHECK_OK(sem_wait(&g_sem));
231 const int kSignalCount = 1000;
232 int count = 0;
233 for (int i = 0; i < kSignalCount; i++) {
234 /* Yield to the other process to try and get it in the desired state. */
235 sched_yield();
236 CHECK_OK(send_async_signal(g_child_tid));
237 sched_yield();
238
239 /* Wake it up using futex. This time, |count| may be 1. */
240 ASSERT_EQ(__sync_bool_compare_and_swap(&futex, 0, 1), 1);
241 CHECK_OK(__libnacl_irt_futex.futex_wake(&futex, INT_MAX, &count));
242 ASSERT_LE(count, 1);
243
244 CHECK_OK(sem_wait(&g_sem));
245 }
246 g_test_running = false;
247 /*
248 * Wake the thread up again in case it waited again.
249 */
250 __sync_bool_compare_and_swap(&futex, 0, 1);
251 CHECK_OK(__libnacl_irt_futex.futex_wake(&futex, INT_MAX, &count));
252 CHECK_OK(pthread_join(tid, NULL));
253 ASSERT_EQ(g_signal_count, kSignalCount);
254 CHECK_OK(sem_destroy(&g_sem));
255 }
256
257 /*
258 * Check that send_async_signal() is async-signal-safe.
259 */
260 void signal_signal_handler(NaClExceptionContext *exc) {
261 if (!g_test_running)
262 return;
263 if (++g_signal_count % 2 == 1) {
264 CHECK_OK(send_async_signal(g_child_tid));
265 g_signal_arrived = 1;
266 }
267 }
268
269 void *signal_thread_func(void *arg) {
270 CHECK_OK(sem_post(&g_sem));
271 struct timespec req, rem;
272 /*
273 * In case we are unlucky and the signal arrives before the first sleep, limit
274 * the time sleeping to 10 msec.
275 */
276 req.tv_sec = 0;
277 req.tv_nsec = 10000000;
278 while (g_test_running) {
279 while (g_test_running && !g_signal_arrived) {
280 int retval = nanosleep(&req, &rem);
281 if (retval != 0)
282 ASSERT_EQ(errno, EINTR);
283 }
284 /*
285 * Have to test again since we could have gone sleeping again after the last
286 * iteration.
287 */
288 if (!g_test_running)
289 break;
290 g_signal_arrived = 0;
291 CHECK_OK(sem_post(&g_sem));
292 }
293 return NULL;
294 }
295
296 void test_async_safe_signal() {
297 CHECK_OK(sem_init(&g_sem, 0, 0));
298 CHECK_OK(set_async_signal_handler(signal_signal_handler));
299
300 pthread_t tid;
301 g_test_running = true;
302 g_signal_count = 0;
303 g_signal_arrived = 0;
304 CHECK_OK(pthread_create(&tid, NULL, signal_thread_func, NULL));
305
306 CHECK_OK(sem_wait(&g_sem));
307 const int kSignalCount = 1000;
308 for (int i = 0; i < kSignalCount; i++) {
309 CHECK_OK(send_async_signal(g_child_tid));
310 CHECK_OK(sem_wait(&g_sem));
311 }
312 g_test_running = false;
313 /* Send a last signal to make sure any waiting syscalls get interrupted. */
314 CHECK_OK(send_async_signal(g_child_tid));
315 CHECK_OK(pthread_join(tid, NULL));
316 ASSERT_EQ(g_signal_count, 2 * kSignalCount);
317 CHECK_OK(sem_destroy(&g_sem));
318 }
319
320 /*
321 * Check that passing 0 as |tid| to send_async_signal() works and
322 * sends a signal to the main thread.
323 */
324 void main_signal_handler(NaClExceptionContext *exc) {
325 g_signal_count = 1;
326 }
327
328 void test_main_signal() {
329 CHECK_OK(set_async_signal_handler(main_signal_handler));
330
331 g_signal_count = 0;
332 CHECK_OK(send_async_signal(NACL_IRT_MAIN_THREAD_TID));
333 ASSERT_EQ(g_signal_count, 1);
334 }
335
336 void run_test(const char *test_name, void (*test_func)(void)) {
337 printf("Running %s...\n", test_name);
338 test_func();
339 }
340
341 } // namespace
342
343 #define RUN_TEST(test_func) (run_test(#test_func, test_func))
344
345 int main(void) {
346 size_t bytes;
347 bytes = nacl_interface_query(NACL_IRT_THREAD_v0_2, &libnacl_irt_thread_v0_2,
348 sizeof(libnacl_irt_thread_v0_2));
349 ASSERT_EQ(bytes, sizeof(libnacl_irt_thread_v0_2));
350
351 bytes = nacl_interface_query(NACL_IRT_ASYNC_SIGNAL_HANDLING_v0_1,
352 &libnacl_irt_async_signal_handling,
353 sizeof(libnacl_irt_async_signal_handling));
354 ASSERT_EQ(bytes, sizeof(libnacl_irt_async_signal_handling));
355
356 /*
357 * In order to avoid modifying the libpthread implementation to save the
358 * native tid, wrap that functionality so the tid is stored in a global
359 * variable.
360 */
361 __libnacl_irt_thread.thread_create = &thread_create_wrapper;
362
363 RUN_TEST(test_send_signal_before_set_handler);
364
365 RUN_TEST(test_async_safe_tls_get);
366 #if !defined(__arm__)
367 /*
368 * Signals are sometimes delivered after the futex_wait syscall returns (as
369 * opposed to interrupting it), which breaks this test.
370 *
371 * This problem only seems to happen in QEMU.
372 */
373 RUN_TEST(test_async_safe_futex);
374 #endif
375 RUN_TEST(test_futex_wait_restart);
376 RUN_TEST(test_async_safe_signal);
377 RUN_TEST(test_main_signal);
378
379 printf("Done\n");
380
381 return 0;
382 }
OLDNEW
« no previous file with comments | « tests/nonsfi/nacl.scons ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698