1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include <arpa/inet.h>
3#include <assert.h>
4#include <netinet/in.h>
5#include <pthread.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <sys/socket.h>
9#include <sys/types.h>
10#include <unistd.h>
11
12struct sockaddr_in addr4;
13struct sockaddr_in6 addr6;
14struct sockaddr *addr;
15socklen_t addrlen;
16int X;
17
18void *ClientThread(void *x) {
19 X = 42;
20 int c = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
21 if (connect(c, addr, addrlen)) {
22 perror("connect");
23 exit(1);
24 }
25 if (send(c, "a", 1, 0) != 1) {
26 perror("send");
27 exit(1);
28 }
29 close(c);
30 return NULL;
31}
32
33int main() {
34 addr4.sin_family = AF_INET;
35 addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
36 addr4.sin_port = INADDR_ANY;
37 addr = (struct sockaddr *)&addr4;
38 addrlen = sizeof(addr4);
39
40 int s = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
41 if (s <= 0) {
42 // Try to fall-back to IPv6
43 addr6.sin6_family = AF_INET6;
44 addr6.sin6_addr = in6addr_loopback;
45 addr6.sin6_port = INADDR_ANY;
46 addr = (struct sockaddr *)&addr6;
47 addrlen = sizeof(addr6);
48 s = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
49 }
50 assert(s > 0);
51
52 bind(s, addr, addrlen);
53 getsockname(s, addr, &addrlen);
54 listen(s, 10);
55 pthread_t t;
56 pthread_create(&t, 0, ClientThread, 0);
57 int c = accept(s, 0, 0);
58 char buf;
59 while (read(c, &buf, 1) != 1) {
60 }
61 X = 43;
62 close(c);
63 close(s);
64 pthread_join(t, 0);
65 fprintf(stderr, "OK\n");
66}
67
68// CHECK-NOT: WARNING: ThreadSanitizer: data race
69
70