Index: test/asan/TestCases/Linux/recvfrom.cc =================================================================== --- test/asan/TestCases/Linux/recvfrom.cc +++ test/asan/TestCases/Linux/recvfrom.cc @@ -2,7 +2,6 @@ // // RUN: %clangxx_asan %s -o %t && not %run %t 2>&1 | FileCheck %s // -// REQUIRES: broken // UNSUPPORTED: android #include @@ -14,8 +13,9 @@ #include #include -const int kPortNum = 1234; +int kPortNum = 1234; const int kBufSize = 10; +pthread_mutex_t server_ready_mu = PTHREAD_MUTEX_INITIALIZER; static void *server_thread_udp(void *data) { char buf[kBufSize / 2]; @@ -29,9 +29,15 @@ serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons(kPortNum); - if (bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) - fprintf(stderr, "ERROR on binding\n"); - + // In parallel test run we may already have acquired port 1234. Increase + // kPortNum until we found free port. + while (bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) { + ++kPortNum; + serveraddr.sin_port = htons(kPortNum); + } + // Release the mutex since the server is ready now and kPortNum has proper + // value. + pthread_mutex_unlock(&server_ready_mu); recvfrom(sockfd, buf, kBufSize, 0, NULL, NULL); // BOOM // CHECK: {{WRITE of size 9 at 0x.* thread T1}} // CHECK: {{ #1 0x.* in server_thread_udp.*recvfrom.cc:}}[[@LINE-2]] @@ -66,6 +72,10 @@ serveraddr.sin_family = AF_INET; memcpy((char *)&serveraddr.sin_addr.s_addr, (char *)server->h_addr, server->h_length); + + // Wait until the server found free port and initialized kPortNum with proper + // value. + pthread_mutex_lock(&server_ready_mu); serveraddr.sin_port = htons(kPortNum); sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *) &serveraddr, sizeof(serveraddr));