1// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
2
3#include <pthread.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <setjmp.h>
7
8void bar(jmp_buf env) {
9 volatile int x = 42;
10 longjmp(env: env, val: 42);
11 x++;
12}
13
14void foo(jmp_buf env) {
15 volatile int x = 42;
16 bar(env);
17 x++;
18}
19
20__attribute__((noinline)) void badguy() {
21 pthread_mutex_t mtx;
22 pthread_mutex_init(mutex: &mtx, mutexattr: 0);
23 pthread_mutex_lock(mutex: &mtx);
24 pthread_mutex_destroy(mutex: &mtx);
25}
26
27__attribute__((noinline)) void mymain() {
28 jmp_buf env;
29 if (setjmp(env) == 42) {
30 badguy();
31 return;
32 }
33 foo(env);
34 fprintf(stderr, format: "FAILED\n");
35}
36
37int main() {
38 volatile int x = 42;
39 mymain();
40 return x;
41}
42
43// CHECK-NOT: FAILED
44// CHECK: WARNING: ThreadSanitizer: destroy of a locked mutex
45// CHECK: #0 pthread_mutex_destroy
46// CHECK: #1 badguy
47// CHECK: #2 mymain
48// CHECK: #3 main
49
50

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