1// RUN: %clang_tsan -O1 %s -o %t && %deflake %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_uc;
10void *fiber, *orig_fiber;
11
12int var;
13
14void func() {
15 var = 1;
16 __tsan_switch_to_fiber(fiber: orig_fiber, flags: __tsan_switch_to_fiber_no_sync);
17 swapcontext(&uc, &orig_uc);
18}
19
20int main() {
21 orig_fiber = __tsan_get_current_fiber();
22 fiber = __tsan_create_fiber(flags: 0);
23 getcontext(&uc);
24 uc.uc_stack.ss_sp = stack;
25 uc.uc_stack.ss_size = sizeof(stack);
26 uc.uc_link = 0;
27 makecontext(&uc, func, 0);
28 var = 2;
29 __tsan_switch_to_fiber(fiber, flags: __tsan_switch_to_fiber_no_sync);
30 swapcontext(&orig_uc, &uc);
31 __tsan_destroy_fiber(fiber);
32 fprintf(stderr, format: "PASS\n");
33 return 0;
34}
35
36// CHECK: WARNING: ThreadSanitizer: data race
37// CHECK: PASS
38

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