Skip to content

[lldb] Fix the SocketTest failure on unsupported hosts #118673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2024

Conversation

ashgti
Copy link
Contributor

@ashgti ashgti commented Dec 4, 2024

The test SocketTest::TCPListen0MultiListenerGetListeningConnectionURI is failing on hosts that do not map localhost to both an ipv4 and ipv6 address. For example this build https://lab.llvm.org/buildbot/#/builders/195/builds/1909.

To fix this, I added a helper to validate if the host has an /etc/hosts entry for both ipv4 and ipv6, otherwise we skip the test.

…ionURI failure on unsupported hosts.

This failure https://lab.llvm.org/buildbot/#/builders/195/builds/1909 happened due to the host not having a `localhost` /etc/hosts entry for an ipv6 address.

To fix this, I added a helper to validate if the host has an /etc/hosts entry for both ipv4 and ipv6, otherwise we skip the test.
@ashgti ashgti requested a review from labath December 4, 2024 17:43
@ashgti ashgti requested a review from JDevlieghere as a code owner December 4, 2024 17:43
@llvmbot llvmbot added the lldb label Dec 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 4, 2024

@llvm/pr-subscribers-lldb

Author: John Harrison (ashgti)

Changes

The test SocketTest::TCPListen0MultiListenerGetListeningConnectionURI is failing on hosts that do not map localhost to both an ipv4 and ipv6 address. For example this build https://lab.llvm.org/buildbot/#/builders/195/builds/1909.

To fix this, I added a helper to validate if the host has an /etc/hosts entry for both ipv4 and ipv6, otherwise we skip the test.


Full diff: https://github.com/llvm/llvm-project/pull/118673.diff

3 Files Affected:

  • (modified) lldb/unittests/Host/SocketTest.cpp (+1-1)
  • (modified) lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp (+18)
  • (modified) lldb/unittests/TestingSupport/Host/SocketTestUtilities.h (+5)
diff --git a/lldb/unittests/Host/SocketTest.cpp b/lldb/unittests/Host/SocketTest.cpp
index 689ef4019c618c..9cbf3e26b0883f 100644
--- a/lldb/unittests/Host/SocketTest.cpp
+++ b/lldb/unittests/Host/SocketTest.cpp
@@ -271,7 +271,7 @@ TEST_P(SocketTest, TCPListen0GetListeningConnectionURI) {
 }
 
 TEST_F(SocketTest, TCPListen0MultiListenerGetListeningConnectionURI) {
-  if (!HostSupportsIPv6() || !HostSupportsIPv4())
+  if (!HostSupportsLocalhostToIPv4() || !HostSupportsLocalhostToIPv6())
     return;
 
   llvm::Expected<std::unique_ptr<TCPSocket>> sock =
diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
index eb5bda0fabc770..86349351b0c3a4 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
@@ -116,6 +116,24 @@ bool lldb_private::HostSupportsIPv6() {
   return CheckIPSupport("IPv6", "[::1]:0");
 }
 
+bool lldb_private::HostSupportsLocalhostToIPv4() {
+  if (!HostSupportsIPv4())
+    return false;
+
+  auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET,
+                                                 SOCK_STREAM, IPPROTO_TCP);
+  return !addresses.empty();
+}
+
+bool lldb_private::HostSupportsLocalhostToIPv6() {
+  if (!HostSupportsIPv6())
+    return false;
+
+  auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET6,
+                                                 SOCK_STREAM, IPPROTO_TCP);
+  return !addresses.empty();
+}
+
 llvm::Expected<std::string> lldb_private::GetLocalhostIP() {
   if (HostSupportsIPv4())
     return "127.0.0.1";
diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
index efd17428ceefb6..e5bab163cf82eb 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
@@ -43,6 +43,11 @@ void CreateDomainConnectedSockets(llvm::StringRef path,
 bool HostSupportsIPv6();
 bool HostSupportsIPv4();
 
+/// Returns true if the name `localhost` maps to a loopback IPv4 address.
+bool HostSupportsLocalhostToIPv4();
+/// Returns true if the name `localhost` maps to a loopback IPv6 address.
+bool HostSupportsLocalhostToIPv6();
+
 /// Return an IP for localhost based on host support.
 ///
 /// This will return either "127.0.0.1" if IPv4 is detected, or "[::1]" if IPv6

@dzhidzhoev
Copy link
Member

Hmm, on my machine it fails with the same error after the commit is applied

Script:
--
/home/parallels/llvm-build-release/tools/lldb/unittests/Host/./HostTests --gtest_filter=SocketTest.TCPListen0MultiListenerGetListeningConnectionURI
--
/home/parallels/llvm-project/lldb/unittests/Host/SocketTest.cpp:289: Failure
Value of: sock.get()->GetListeningConnectionURI()
Expected: has 2 elements and there exists some permutation of elements such that:
 - element #0 is equal to "connection://[::1]:35725", and
 - element #1 is equal to "connection://[127.0.0.1]:35725"
  Actual: { "connection://[127.0.0.1]:35725" }, which has 1 element


/home/parallels/llvm-project/lldb/unittests/Host/SocketTest.cpp:289
Value of: sock.get()->GetListeningConnectionURI()
Expected: has 2 elements and there exists some permutation of elements such that:
 - element #0 is equal to "connection://[::1]:35725", and
 - element #1 is equal to "connection://[127.0.0.1]:35725"
  Actual: { "connection://[127.0.0.1]:35725" }, which has 1 element

return false;

auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET6,
SOCK_STREAM, IPPROTO_TCP);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got

llvm-project git:(becab1c1b0a3) cat /etc/hosts     
127.0.0.1 localhost
127.0.1.1 ubuntu-linux-22-04-desktop

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

For some reason, getaddrinfo("localhost", AF_UNSPEC, ...) returns only one address (IPv4 127.0.0.1), but getaddrinfo("localhost", AF_INET6, ...) returns an address as well (IPv6 ::1).

I think it's probably an oddity in getaddrinfo implementation (my gai.conf is empty). This check seems to work the way expected:

auto addresses = SocketAddress::GetAddressInfo(
    "localhost", nullptr, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
return std::find_if(addresses.begin(), addresses.end(),
                    [](SocketAddress &addr) {
                      return addr.GetFamily() == AF_INET6;
                    }) != addresses.end();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion!

I tried this on my mac by commenting out the ::1 localhost line in my hosts file, but I wonder if I had some caching or something happening.

Applied this suggestion.

@ashgti ashgti merged commit 0964328 into llvm:main Dec 5, 2024
5 of 6 checks passed
Comment on lines +125 to +128
return std::find_if(addresses.begin(), addresses.end(),
[](SocketAddress &addr) {
return addr.GetFamily() == AF_INET;
}) != addresses.end();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small llvm-ization:

return llvm::any_of(addresses, [](const SocketAddress &addr) { return addr.GetFamily() == AF_INET; });

ashgti added a commit that referenced this pull request Dec 9, 2024
This was suggested in a previous patch after the commit was already
submitted.

Original suggestion
#118673 (comment)
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Dec 23, 2024
Local branch amd-gfx abbd942 Merged main:caf8942cd9e5 into amd-gfx:5f484c18d2a2
Remote branch main 0964328 [lldb] Fix the SocketTest failure on unsupported hosts (llvm#118673)

Change-Id: I8af78caad0bfeb0fe14eedd005ce56f62b49e81b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants