[PATCH] D98579: [llvm-jitlink] Fix use of getaddrinfo(3) when connecting remote executor via TCP socket

Stefan Gränitz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 13 05:35:13 PST 2021


sgraenitz created this revision.
sgraenitz added reviewers: lhames, rzurob, saghir.
sgraenitz requested review of this revision.
Herald added a project: LLVM.

Since llvm-jitlink moved from gethostbyname to getaddrinfo in D95477 <https://reviews.llvm.org/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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98579

Files:
  llvm/tools/llvm-jitlink/llvm-jitlink.cpp


Index: llvm/tools/llvm-jitlink/llvm-jitlink.cpp
===================================================================
--- llvm/tools/llvm-jitlink/llvm-jitlink.cpp
+++ llvm/tools/llvm-jitlink/llvm-jitlink.cpp
@@ -704,28 +704,28 @@
   addrinfo Hints{};
   Hints.ai_family = AF_INET;
   Hints.ai_socktype = SOCK_STREAM;
-  Hints.ai_protocol = PF_INET;
   Hints.ai_flags = AI_NUMERICSERV;
-  if (getaddrinfo(HostName.c_str(), PortStr.str().c_str(), &Hints, &AI) != 0)
-    return make_error<StringError>("Failed to resolve " + HostName + ":" +
-                                       Twine(Port),
+  if (int EC =
+          getaddrinfo(HostName.c_str(), PortStr.str().c_str(), &Hints, &AI))
+    return make_error<StringError>(formatv("Failed to resolve {0}:{1} ({2})",
+                                           HostName, Port, gai_strerror(EC)),
                                    inconvertibleErrorCode());
 
-  int SockFD = socket(PF_INET, SOCK_STREAM, 0);
-  sockaddr_in ServAddr;
-  memset(&ServAddr, 0, sizeof(ServAddr));
-  ServAddr.sin_family = PF_INET;
-  ServAddr.sin_port = htons(Port);
-
   // getaddrinfo returns a list of address structures.  Go through the list
   // to find one we can connect to.
+  int SockFD;
   int ConnectRC = -1;
   for (addrinfo *Server = AI; Server; Server = Server->ai_next) {
-    memmove(&Server->ai_addr, &ServAddr.sin_addr.s_addr, Server->ai_addrlen);
-    ConnectRC = connect(SockFD, reinterpret_cast<sockaddr *>(&ServAddr),
-                        sizeof(ServAddr));
+    // If socket fails, maybe it's because the address family is not supported.
+    // Skip to the next addrinfo structure.
+    if ((SockFD = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0)
+      continue;
+
+    ConnectRC = connect(SockFD, Server->ai_addr, Server->ai_addrlen);
     if (ConnectRC == 0)
       break;
+
+    close(SockFD);
   }
   freeaddrinfo(AI);
   if (ConnectRC == -1)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98579.330446.patch
Type: text/x-patch
Size: 1921 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210313/c6917467/attachment.bin>


More information about the llvm-commits mailing list