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 int c = socket(domain: addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
20 X = 42;
21 if (connect(fd: c, addr: addr, len: addrlen)) {
22 perror(s: "connect");
23 exit(status: 1);
24 }
25 close(fd: c);
26 return NULL;
27}
28
29int main() {
30 addr4.sin_family = AF_INET;
31 addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
32 addr4.sin_port = INADDR_ANY;
33 addr = (struct sockaddr *)&addr4;
34 addrlen = sizeof(addr4);
35
36 int s = socket(domain: addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
37 if (s <= 0) {
38 // Try to fall-back to IPv6
39 addr6.sin6_family = AF_INET6;
40 addr6.sin6_addr = in6addr_loopback;
41 addr6.sin6_port = INADDR_ANY;
42 addr = (struct sockaddr *)&addr6;
43 addrlen = sizeof(addr6);
44 s = socket(domain: addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
45 }
46 assert(s > 0);
47
48 bind(fd: s, addr: addr, len: addrlen);
49 getsockname(fd: s, addr: addr, len: &addrlen);
50 listen(fd: s, n: 10);
51 pthread_t t;
52 pthread_create(newthread: &t, attr: 0, start_routine: ClientThread, arg: 0);
53 int c = accept(fd: s, addr: 0, addr_len: 0);
54 X = 42;
55 pthread_join(th: t, thread_return: 0);
56 close(fd: c);
57 close(fd: s);
58 fprintf(stderr, format: "OK\n");
59}
60
61// CHECK-NOT: WARNING: ThreadSanitizer: data race
62
63

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