xref: /aosp_15_r20/external/compiler-rt/test/tsan/longjmp4.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
2*7c3d14c8STreehugger Robot 
3*7c3d14c8STreehugger Robot // Longjmp assembly has not been implemented for mips64 yet
4*7c3d14c8STreehugger Robot // XFAIL: mips64
5*7c3d14c8STreehugger Robot 
6*7c3d14c8STreehugger Robot #include <pthread.h>
7*7c3d14c8STreehugger Robot #include <stdio.h>
8*7c3d14c8STreehugger Robot #include <stdlib.h>
9*7c3d14c8STreehugger Robot #include <setjmp.h>
10*7c3d14c8STreehugger Robot #include <string.h>
11*7c3d14c8STreehugger Robot 
bar(jmp_buf env)12*7c3d14c8STreehugger Robot void bar(jmp_buf env) {
13*7c3d14c8STreehugger Robot   volatile int x = 42;
14*7c3d14c8STreehugger Robot   jmp_buf env2;
15*7c3d14c8STreehugger Robot   memcpy(env2, env, sizeof(jmp_buf));
16*7c3d14c8STreehugger Robot   longjmp(env2, 42);
17*7c3d14c8STreehugger Robot   x++;
18*7c3d14c8STreehugger Robot }
19*7c3d14c8STreehugger Robot 
foo(jmp_buf env)20*7c3d14c8STreehugger Robot void foo(jmp_buf env) {
21*7c3d14c8STreehugger Robot   volatile int x = 42;
22*7c3d14c8STreehugger Robot   bar(env);
23*7c3d14c8STreehugger Robot   x++;
24*7c3d14c8STreehugger Robot }
25*7c3d14c8STreehugger Robot 
badguy()26*7c3d14c8STreehugger Robot void badguy() {
27*7c3d14c8STreehugger Robot   pthread_mutex_t mtx;
28*7c3d14c8STreehugger Robot   pthread_mutex_init(&mtx, 0);
29*7c3d14c8STreehugger Robot   pthread_mutex_lock(&mtx);
30*7c3d14c8STreehugger Robot   pthread_mutex_destroy(&mtx);
31*7c3d14c8STreehugger Robot }
32*7c3d14c8STreehugger Robot 
mymain()33*7c3d14c8STreehugger Robot void mymain() {
34*7c3d14c8STreehugger Robot   jmp_buf env;
35*7c3d14c8STreehugger Robot   if (setjmp(env) == 42) {
36*7c3d14c8STreehugger Robot     badguy();
37*7c3d14c8STreehugger Robot     return;
38*7c3d14c8STreehugger Robot   }
39*7c3d14c8STreehugger Robot   foo(env);
40*7c3d14c8STreehugger Robot   fprintf(stderr, "FAILED\n");
41*7c3d14c8STreehugger Robot }
42*7c3d14c8STreehugger Robot 
main()43*7c3d14c8STreehugger Robot int main() {
44*7c3d14c8STreehugger Robot   volatile int x = 42;
45*7c3d14c8STreehugger Robot   mymain();
46*7c3d14c8STreehugger Robot   return x;
47*7c3d14c8STreehugger Robot }
48*7c3d14c8STreehugger Robot 
49*7c3d14c8STreehugger Robot // CHECK-NOT: FAILED
50*7c3d14c8STreehugger Robot // CHECK: WARNING: ThreadSanitizer: destroy of a locked mutex
51*7c3d14c8STreehugger Robot // CHECK:   #0 pthread_mutex_destroy
52*7c3d14c8STreehugger Robot // CHECK:   #1 badguy
53*7c3d14c8STreehugger Robot // CHECK:   #2 mymain
54*7c3d14c8STreehugger Robot // CHECK:   #3 main
55*7c3d14c8STreehugger Robot 
56