1 // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 // REQUIRES: x86_64-target-arch
3 // UNSUPPORTED: tvos, watchos
12 void ucontext_do_switch(void **save
, void **load
);
13 void ucontext_trampoline();
16 __asm__(".global " ASM_SYMBOL(ucontext_do_switch
) "\n"
17 ASM_SYMBOL(ucontext_do_switch
) ":\n\t"
24 "movq %rsp, (%rdi)\n\t"
25 "movq (%rsi), %rsp\n\t"
34 __asm__(".global " ASM_SYMBOL(ucontext_trampoline
) "\n"
35 ASM_SYMBOL(ucontext_trampoline
) ":\n\t"
37 ".cfi_undefined rip\n\t"
42 void ucontext_init(ucontext
*context
, void *stack
, unsigned stack_sz
,
43 void (*func
)(void*), void *arg
) {
44 void **sp
= reinterpret_cast<void **>(static_cast<char *>(stack
) + stack_sz
);
46 *(--sp
) = reinterpret_cast<void *>(ucontext_trampoline
);
52 *(--sp
) = reinterpret_cast<void *>(func
); // rbx
54 context
->fiber
= __tsan_create_fiber(0);
57 void ucontext_free(ucontext
*context
) {
58 __tsan_destroy_fiber(context
->fiber
);
61 __attribute__((no_sanitize_thread
))
62 void ucontext_switch(ucontext
*save
, ucontext
*load
) {
63 save
->fiber
= __tsan_get_current_fiber();
64 __tsan_switch_to_fiber(load
->fiber
, 0);
65 ucontext_do_switch(&save
->sp
, &load
->sp
);
68 char stack
[64 * 1024] __attribute__((aligned(16)));
72 void func(void *arg
) {
73 __asm__
__volatile__(".cfi_undefined rip");
74 ucontext_switch(&uc
, &orig_uc
);
78 ucontext_init(&uc
, stack
, sizeof(stack
), func
, 0);
79 ucontext_switch(&orig_uc
, &uc
);
81 fprintf(stderr
, "PASS\n");
85 // CHECK-NOT: WARNING: ThreadSanitizer: