Skip to content

Commit 4a8161f

Browse files
[llvm-jitlink] Fix use of getaddrinfo(3) when connecting remote executor via TCP socket
Since llvm-jitlink moved from gethostbyname to getaddrinfo in D95477, it seems to no longer connect to llvm-jitlink-executor via TCP. I can reproduce this behavior on both, Debian 10 and macOS 10.15.7: ``` > llvm-jitlink-executor listen=localhost:10819 -- > llvm-jitlink --oop-executor-connect=localhost:10819 /path/to/obj.o Failed to resolve localhost:10819 ``` Reviewed By: rzurob Differential Revision: https://reviews.llvm.org/D98579
1 parent 20d9326 commit 4a8161f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -704,28 +704,28 @@ LLVMJITLinkRemoteTargetProcessControl::ConnectToExecutor() {
704704
addrinfo Hints{};
705705
Hints.ai_family = AF_INET;
706706
Hints.ai_socktype = SOCK_STREAM;
707-
Hints.ai_protocol = PF_INET;
708707
Hints.ai_flags = AI_NUMERICSERV;
709-
if (getaddrinfo(HostName.c_str(), PortStr.str().c_str(), &Hints, &AI) != 0)
710-
return make_error<StringError>("Failed to resolve " + HostName + ":" +
711-
Twine(Port),
708+
if (int EC =
709+
getaddrinfo(HostName.c_str(), PortStr.str().c_str(), &Hints, &AI))
710+
return make_error<StringError>(formatv("Failed to resolve {0}:{1} ({2})",
711+
HostName, Port, gai_strerror(EC)),
712712
inconvertibleErrorCode());
713713

714-
int SockFD = socket(PF_INET, SOCK_STREAM, 0);
715-
sockaddr_in ServAddr;
716-
memset(&ServAddr, 0, sizeof(ServAddr));
717-
ServAddr.sin_family = PF_INET;
718-
ServAddr.sin_port = htons(Port);
719-
720714
// getaddrinfo returns a list of address structures. Go through the list
721715
// to find one we can connect to.
716+
int SockFD;
722717
int ConnectRC = -1;
723718
for (addrinfo *Server = AI; Server; Server = Server->ai_next) {
724-
memmove(&Server->ai_addr, &ServAddr.sin_addr.s_addr, Server->ai_addrlen);
725-
ConnectRC = connect(SockFD, reinterpret_cast<sockaddr *>(&ServAddr),
726-
sizeof(ServAddr));
719+
// If socket fails, maybe it's because the address family is not supported.
720+
// Skip to the next addrinfo structure.
721+
if ((SockFD = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0)
722+
continue;
723+
724+
ConnectRC = connect(SockFD, Server->ai_addr, Server->ai_addrlen);
727725
if (ConnectRC == 0)
728726
break;
727+
728+
close(SockFD);
729729
}
730730
freeaddrinfo(AI);
731731
if (ConnectRC == -1)

0 commit comments

Comments
 (0)