[Lldb-commits] [lldb] [lldb] Gracefully handle sockets being unavailable (PR #194712)

via lldb-commits lldb-commits at lists.llvm.org
Tue Apr 28 12:54:16 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

<details>
<summary>Changes</summary>

Gracefully handle sockets being unavailable, for example because the test suite is running in a sandboxed environment where you're not allowed to call bind.

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


9 Files Affected:

- (modified) lldb/unittests/Host/MainLoopTest.cpp (+4) 
- (modified) lldb/unittests/Host/SocketTest.cpp (+23-4) 
- (modified) lldb/unittests/Platform/Android/AdbClientTest.cpp (+4) 
- (modified) lldb/unittests/Platform/Android/CMakeLists.txt (+1) 
- (modified) lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp (+16-1) 
- (modified) lldb/unittests/TestingSupport/Host/SocketTestUtilities.h (+3) 
- (modified) lldb/unittests/debugserver/CMakeLists.txt (+1) 
- (modified) lldb/unittests/debugserver/RNBSocketTest.cpp (+7) 
- (modified) lldb/unittests/tools/lldb-server/tests/TestBase.h (+6) 


``````````diff
diff --git a/lldb/unittests/Host/MainLoopTest.cpp b/lldb/unittests/Host/MainLoopTest.cpp
index 8a248100c936a..9fb8032f48d3b 100644
--- a/lldb/unittests/Host/MainLoopTest.cpp
+++ b/lldb/unittests/Host/MainLoopTest.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Host/MainLoop.h"
+#include "TestingSupport/Host/SocketTestUtilities.h"
 #include "TestingSupport/SubsystemRAII.h"
 #include "lldb/Host/ConnectionFileDescriptor.h"
 #include "lldb/Host/FileSystem.h"
@@ -28,6 +29,9 @@ class MainLoopTest : public testing::Test {
   SubsystemRAII<FileSystem, Socket> subsystems;
 
   void SetUp() override {
+    if (!HostSupportsIPv4() && !HostSupportsIPv6())
+      GTEST_SKIP() << "TCP sockets unavailable";
+
     Status error;
     auto listen_socket_up = std::make_unique<TCPSocket>(true);
     ASSERT_TRUE(error.Success());
diff --git a/lldb/unittests/Host/SocketTest.cpp b/lldb/unittests/Host/SocketTest.cpp
index 46be02e2de470..3a95708283a84 100644
--- a/lldb/unittests/Host/SocketTest.cpp
+++ b/lldb/unittests/Host/SocketTest.cpp
@@ -80,12 +80,16 @@ TEST_F(SocketTest, DecodeHostAndPort) {
 TEST_F(SocketTest, CreatePair) {
   std::vector<std::optional<Socket::SocketProtocol>> functional_protocols = {
       std::nullopt,
-      Socket::ProtocolTcp,
+  };
+  if (HostSupportsIPv4() || HostSupportsIPv6())
+    functional_protocols.push_back(Socket::ProtocolTcp);
 #if LLDB_ENABLE_POSIX
-      Socket::ProtocolUnixDomain,
-      Socket::ProtocolUnixAbstract,
+  if (HostSupportsDomainSockets()) {
+    functional_protocols.push_back(Socket::ProtocolUnixDomain);
+    functional_protocols.push_back(Socket::ProtocolUnixAbstract);
+  }
 #endif
-  };
+
   for (auto p : functional_protocols) {
     auto expected_socket_pair = Socket::CreatePair(p);
     ASSERT_THAT_EXPECTED(expected_socket_pair, llvm::Succeeded());
@@ -114,6 +118,9 @@ TEST_F(SocketTest, CreatePair) {
 
 #if LLDB_ENABLE_POSIX
 TEST_F(SocketTest, DomainListenConnectAccept) {
+  if (!HostSupportsDomainSockets())
+    GTEST_SKIP() << "Domain sockets unavailable";
+
   llvm::SmallString<64> Path;
   std::error_code EC =
       llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path);
@@ -130,6 +137,9 @@ TEST_F(SocketTest, DomainListenConnectAccept) {
 }
 
 TEST_F(SocketTest, DomainListenGetListeningConnectionURI) {
+  if (!HostSupportsDomainSockets())
+    GTEST_SKIP() << "Domain sockets unavailable";
+
   llvm::SmallString<64> Path;
   std::error_code EC =
       llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path);
@@ -152,6 +162,9 @@ TEST_F(SocketTest, DomainListenGetListeningConnectionURI) {
 }
 
 TEST_F(SocketTest, DomainMainLoopAccept) {
+  if (!HostSupportsDomainSockets())
+    GTEST_SKIP() << "Domain sockets unavailable";
+
   llvm::SmallString<64> Path;
   std::error_code EC =
       llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path);
@@ -356,6 +369,9 @@ TEST_P(SocketTest, UDPGetConnectURI) {
 
 #if LLDB_ENABLE_POSIX
 TEST_F(SocketTest, DomainGetConnectURI) {
+  if (!HostSupportsDomainSockets())
+    GTEST_SKIP() << "Domain sockets unavailable";
+
   llvm::SmallString<64> domain_path;
   std::error_code EC = llvm::sys::fs::createUniqueDirectory(
       "DomainListenConnectAccept", domain_path);
@@ -378,6 +394,9 @@ TEST_F(SocketTest, DomainGetConnectURI) {
 }
 
 TEST_F(SocketTest, DomainSocketFromBoundNativeSocket) {
+  if (!HostSupportsDomainSockets())
+    GTEST_SKIP() << "Domain sockets unavailable";
+
   // Generate a name for the domain socket.
   llvm::SmallString<64> name;
   std::error_code EC = llvm::sys::fs::createUniqueDirectory(
diff --git a/lldb/unittests/Platform/Android/AdbClientTest.cpp b/lldb/unittests/Platform/Android/AdbClientTest.cpp
index 97aa2173c2333..69d866b54d20e 100644
--- a/lldb/unittests/Platform/Android/AdbClientTest.cpp
+++ b/lldb/unittests/Platform/Android/AdbClientTest.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "Plugins/Platform/Android/AdbClient.h"
+#include "TestingSupport/Host/SocketTestUtilities.h"
 #include "lldb/Host/Socket.h"
 #include "lldb/Host/common/TCPSocket.h"
 #include "gtest/gtest.h"
@@ -112,6 +113,9 @@ static uint16_t FindUnusedPort() {
 // This test is disabled on Windows due to platform-specific socket behavior
 // that causes assertion failures in TCPSocket::Listen()
 TEST_F(AdbClientTest, RealTcpConnection) {
+  if (!HostSupportsIPv4() && !HostSupportsIPv6())
+    GTEST_SKIP() << "TCP sockets unavailable";
+
   uint16_t unused_port = FindUnusedPort();
   ASSERT_NE(unused_port, 0) << "Failed to find an unused port";
 
diff --git a/lldb/unittests/Platform/Android/CMakeLists.txt b/lldb/unittests/Platform/Android/CMakeLists.txt
index df6a95576f562..05adff5f5e7de 100644
--- a/lldb/unittests/Platform/Android/CMakeLists.txt
+++ b/lldb/unittests/Platform/Android/CMakeLists.txt
@@ -5,5 +5,6 @@ add_lldb_unittest(AdbClientTests
   PlatformAndroidTest.cpp
 
   LINK_LIBS
+    lldbHostHelpers
     lldbPluginPlatformAndroid
   )
diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
index c950a0d82d784..2c5e310ea9a9b 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
@@ -94,7 +94,8 @@ static bool CheckIPSupport(llvm::StringRef Proto, llvm::StringRef Addr) {
   handleAllErrors(std::move(Err), [&](std::unique_ptr<llvm::ECError> ECErr) {
     std::error_code ec = ECErr->convertToErrorCode();
     if (ec == std::make_error_code(std::errc::address_family_not_supported) ||
-        ec == std::make_error_code(std::errc::address_not_available))
+        ec == std::make_error_code(std::errc::address_not_available) ||
+        ec == std::make_error_code(std::errc::operation_not_permitted))
       HasProtocolError = true;
   });
   if (HasProtocolError) {
@@ -146,3 +147,17 @@ llvm::Expected<std::string> lldb_private::GetLocalhostIP() {
   return llvm::createStringError(
       "Neither IPv4 nor IPv6 appear to be supported");
 }
+
+#if LLDB_ENABLE_POSIX
+bool lldb_private::HostSupportsDomainSockets() {
+  llvm::SmallString<64> Path;
+  if (llvm::sys::fs::createUniqueDirectory("SocketTestCanary", Path))
+    return false;
+  llvm::sys::path::append(Path, "test");
+  DomainSocket sock(true);
+  Status status = sock.Listen(Path, 1);
+  llvm::sys::fs::remove(Path);
+  llvm::sys::fs::remove(Path.str().rsplit('/').first);
+  return status.Success();
+}
+#endif
diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
index e5bab163cf82e..a7c9aee162e65 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
@@ -42,6 +42,9 @@ void CreateDomainConnectedSockets(llvm::StringRef path,
 
 bool HostSupportsIPv6();
 bool HostSupportsIPv4();
+#if LLDB_ENABLE_POSIX
+bool HostSupportsDomainSockets();
+#endif
 
 /// Returns true if the name `localhost` maps to a loopback IPv4 address.
 bool HostSupportsLocalhostToIPv4();
diff --git a/lldb/unittests/debugserver/CMakeLists.txt b/lldb/unittests/debugserver/CMakeLists.txt
index c2646eb113be9..a4829f68254c0 100644
--- a/lldb/unittests/debugserver/CMakeLists.txt
+++ b/lldb/unittests/debugserver/CMakeLists.txt
@@ -14,6 +14,7 @@ add_lldb_unittest(debugserverTests
   LINK_LIBS
     lldbDebugserverCommon
     lldbHost
+    lldbHostHelpers
     LLVMTestingSupport
   )
 
diff --git a/lldb/unittests/debugserver/RNBSocketTest.cpp b/lldb/unittests/debugserver/RNBSocketTest.cpp
index 48f747b09587f..3b0e2e30e7dc4 100644
--- a/lldb/unittests/debugserver/RNBSocketTest.cpp
+++ b/lldb/unittests/debugserver/RNBSocketTest.cpp
@@ -14,6 +14,7 @@
 
 #include "RNBDefs.h"
 #include "RNBSocket.h"
+#include "TestingSupport/Host/SocketTestUtilities.h"
 #include "lldb/Host/Socket.h"
 #include "lldb/Host/common/TCPSocket.h"
 #include "llvm/Testing/Support/Error.h"
@@ -53,6 +54,9 @@ static void ServerCallbackv4(const void *baton, in_port_t port) {
 }
 
 void TestSocketListen(const char *addr) {
+  if (!lldb_private::HostSupportsIPv4() && !lldb_private::HostSupportsIPv6())
+    GTEST_SKIP() << "TCP sockets unavailable";
+
   // Skip IPv6 tests if there isn't a valid interafce
   auto addresses = lldb_private::SocketAddress::GetAddressInfo(
       addr, NULL, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
@@ -85,6 +89,9 @@ TEST(RNBSocket, LoopBackListenIPv6) { TestSocketListen("::1"); }
 TEST(RNBSocket, AnyListen) { TestSocketListen("*"); }
 
 void TestSocketConnect(const char *addr) {
+  if (!lldb_private::HostSupportsIPv4() && !lldb_private::HostSupportsIPv6())
+    GTEST_SKIP() << "TCP sockets unavailable";
+
   // Skip IPv6 tests if there isn't a valid interafce
   auto addresses = lldb_private::SocketAddress::GetAddressInfo(
       addr, NULL, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
diff --git a/lldb/unittests/tools/lldb-server/tests/TestBase.h b/lldb/unittests/tools/lldb-server/tests/TestBase.h
index 6b0a9cdfd7c6f..5ae303626c728 100644
--- a/lldb/unittests/tools/lldb-server/tests/TestBase.h
+++ b/lldb/unittests/tools/lldb-server/tests/TestBase.h
@@ -10,6 +10,7 @@
 #define LLDB_UNITTESTS_TOOLS_LLDB_SERVER_TESTS_TESTBASE_H
 
 #include "TestClient.h"
+#include "TestingSupport/Host/SocketTestUtilities.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Host/Socket.h"
@@ -33,6 +34,11 @@ class TestBase: public ::testing::Test {
     lldb_private::FileSystem::Terminate();
   }
 
+  void SetUp() override {
+    if (!lldb_private::HostSupportsIPv4() && !lldb_private::HostSupportsIPv6())
+      GTEST_SKIP() << "TCP sockets unavailable";
+  }
+
   static std::string getInferiorPath(llvm::StringRef Name) {
     llvm::SmallString<64> Path(LLDB_TEST_INFERIOR_PATH);
     llvm::sys::path::append(Path, Name + LLDB_TEST_INFERIOR_SUFFIX);

``````````

</details>


https://github.com/llvm/llvm-project/pull/194712


More information about the lldb-commits mailing list