1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2// UNSUPPORTED: tvos, watchos
3// XFAIL: ios && !iossim
4#include "sanitizer_common/sanitizer_ucontext.h"
5#include "test.h"
6#include <setjmp.h>
7
8char stack[64 * 1024] __attribute__((aligned(16)));
9
10sigjmp_buf jmpbuf, orig_jmpbuf[2];
11void *fiber, *orig_fiber[2];
12
13const unsigned N = 1000;
14
15__attribute__((noinline))
16void switch0() {
17 if (!sigsetjmp(jmpbuf, 0)) {
18 __tsan_switch_to_fiber(fiber: orig_fiber[0], flags: 0);
19 siglongjmp(env: orig_jmpbuf[0], val: 1);
20 }
21}
22
23void func() {
24 if (!sigsetjmp(jmpbuf, 0)) {
25 __tsan_switch_to_fiber(fiber: orig_fiber[0], flags: 0);
26 siglongjmp(env: orig_jmpbuf[0], val: 1);
27 }
28 for (;;) {
29 switch0();
30 if (!sigsetjmp(jmpbuf, 0)) {
31 __tsan_switch_to_fiber(fiber: orig_fiber[1], flags: 0);
32 siglongjmp(env: orig_jmpbuf[1], val: 1);
33 }
34 }
35}
36
37void *Thread(void *x) {
38 orig_fiber[1] = __tsan_get_current_fiber();
39 for (unsigned i = 0; i < N; i++) {
40 barrier_wait(barrier: &barrier);
41 if (!sigsetjmp(orig_jmpbuf[1], 0)) {
42 __tsan_switch_to_fiber(fiber, flags: 0);
43 siglongjmp(env: jmpbuf, val: 1);
44 }
45 barrier_wait(barrier: &barrier);
46 }
47 return 0;
48}
49
50int main() {
51 fiber = __tsan_create_fiber(flags: 0);
52 barrier_init(barrier: &barrier, count: 2);
53 pthread_t t;
54 pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0);
55 orig_fiber[0] = __tsan_get_current_fiber();
56 ucontext_t uc, orig_uc;
57 getcontext(&uc);
58 uc.uc_stack.ss_sp = stack;
59 uc.uc_stack.ss_size = sizeof(stack);
60 uc.uc_link = 0;
61 makecontext(&uc, func, 0);
62 if (!sigsetjmp(orig_jmpbuf[0], 0)) {
63 __tsan_switch_to_fiber(fiber, flags: 0);
64 swapcontext(&orig_uc, &uc);
65 }
66 for (unsigned i = 0; i < N; i++) {
67 if (!sigsetjmp(orig_jmpbuf[0], 0)) {
68 __tsan_switch_to_fiber(fiber, flags: 0);
69 siglongjmp(env: jmpbuf, val: 1);
70 }
71 barrier_wait(barrier: &barrier);
72 barrier_wait(barrier: &barrier);
73 }
74 pthread_join(th: t, thread_return: 0);
75 __tsan_destroy_fiber(fiber);
76 fprintf(stderr, format: "PASS\n");
77 return 0;
78}
79
80// CHECK-NOT: WARNING: ThreadSanitizer:
81// CHECK: PASS
82

source code of compiler-rt/test/tsan/fiber_longjmp.cpp