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
7char stack[64 * 1024] __attribute__((aligned(16)));
8
9ucontext_t uc, orig_uc1, orig_uc2;
10void *fiber, *orig_fiber1, *orig_fiber2;
11
12int var;
13
14void *Thread(void *x) {
15 orig_fiber2 = __tsan_get_current_fiber();
16 swapcontext(&orig_uc2, &orig_uc1);
17 return 0;
18}
19
20void func() {
21 pthread_t t;
22 pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0);
23 pthread_join(th: t, thread_return: 0);
24 __tsan_switch_to_fiber(fiber: orig_fiber1, flags: 0);
25 swapcontext(&uc, &orig_uc1);
26}
27
28int main() {
29 orig_fiber1 = __tsan_get_current_fiber();
30 fiber = __tsan_create_fiber(flags: 0);
31 getcontext(&uc);
32 uc.uc_stack.ss_sp = stack;
33 uc.uc_stack.ss_size = sizeof(stack);
34 uc.uc_link = 0;
35 makecontext(&uc, func, 0);
36 var = 1;
37 __tsan_switch_to_fiber(fiber, flags: 0);
38 swapcontext(&orig_uc1, &uc);
39 var = 2;
40 __tsan_switch_to_fiber(fiber: orig_fiber2, flags: 0);
41 swapcontext(&orig_uc1, &orig_uc2);
42 var = 3;
43 __tsan_destroy_fiber(fiber);
44 fprintf(stderr, format: "PASS\n");
45 return 0;
46}
47
48// CHECK-NOT: WARNING: ThreadSanitizer:
49// CHECK: PASS
50

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