[libc-commits] [libunwind] [flang] [lldb] [libcxx] [clang] [libc] [compiler-rt] [llvm] [clang-tools-extra] [lld] [libc++][test] try to directly create socket file in /tmp when filepath is too long (PR #77058)

Wu Yingcong via libc-commits libc-commits at lists.llvm.org
Sun Jan 7 23:12:46 PST 2024


https://github.com/yingcong-wu updated https://github.com/llvm/llvm-project/pull/77058

>From 202fb858344d102bd5199cd749bb15195dbce558 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Fri, 5 Jan 2024 00:48:34 -0800
Subject: [PATCH 1/7] try to directly create file in /tmp when filepath is too
 long

---
 libcxx/test/support/filesystem_test_helper.h | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index a049efe03d844e..271b2bb5cafe23 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -18,6 +18,7 @@
 #include <chrono>
 #include <cstdint>
 #include <cstdio> // for printf
+#include <ctime>
 #include <string>
 #include <system_error>
 #include <type_traits>
@@ -324,10 +325,22 @@ struct scoped_test_env
 
         ::sockaddr_un address;
         address.sun_family = AF_UNIX;
+
+        // If file.size() is too big, try to create a file directly inside
+        // /tmp to make sure file path is short enough.
+        if (file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
+            fs::path const tmp = "/tmp";
+            std::size_t i      = std::hash<std::string>()(std::to_string(std::time(nullptr)));
+            fs::path p         = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
+            while (utils::exists(p.string())) {
+              p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
+            }
+            file = p.string();
+        }
         assert(file.size() <= sizeof(address.sun_path));
         ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
         int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
-        ::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address));
+        assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
         return file;
     }
 #endif

>From 5d64d75bf7ad8422eb54594fa8cc8dfda7f14e4e Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Fri, 5 Jan 2024 01:13:24 -0800
Subject: [PATCH 2/7] format

---
 libcxx/test/support/filesystem_test_helper.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index 271b2bb5cafe23..d33fcf5497f084 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -329,13 +329,13 @@ struct scoped_test_env
         // If file.size() is too big, try to create a file directly inside
         // /tmp to make sure file path is short enough.
         if (file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
-            fs::path const tmp = "/tmp";
-            std::size_t i      = std::hash<std::string>()(std::to_string(std::time(nullptr)));
-            fs::path p         = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
-            while (utils::exists(p.string())) {
-              p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
-            }
-            file = p.string();
+          fs::path const tmp = "/tmp";
+          std::size_t i      = std::hash<std::string>()(std::to_string(std::time(nullptr)));
+          fs::path p         = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
+          while (utils::exists(p.string())) {
+            p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
+          }
+          file = p.string();
         }
         assert(file.size() <= sizeof(address.sun_path));
         ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));

>From ec6ff976d4e338fb1cd219409ee47b75b3b6a324 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Fri, 5 Jan 2024 01:59:22 -0800
Subject: [PATCH 3/7] fix error

---
 libcxx/test/support/filesystem_test_helper.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index d33fcf5497f084..001625d97775f8 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -321,27 +321,27 @@ struct scoped_test_env
   // allow tests to call this unguarded.
 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
     std::string create_socket(std::string file) {
-        file = sanitize_path(std::move(file));
+        std::string socket_file = sanitize_path(file);
 
         ::sockaddr_un address;
         address.sun_family = AF_UNIX;
 
         // If file.size() is too big, try to create a file directly inside
         // /tmp to make sure file path is short enough.
-        if (file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
+        if (socket_file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
           fs::path const tmp = "/tmp";
           std::size_t i      = std::hash<std::string>()(std::to_string(std::time(nullptr)));
           fs::path p         = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
           while (utils::exists(p.string())) {
             p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
           }
-          file = p.string();
+          socket_file = p.string();
         }
-        assert(file.size() <= sizeof(address.sun_path));
-        ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
+        assert(socket_file.size() <= sizeof(address.sun_path));
+        ::strncpy(address.sun_path, socket_file.c_str(), sizeof(address.sun_path));
         int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
         assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
-        return file;
+        return socket_file;
     }
 #endif
 

>From ee20687568528256bfc7afa4e0bde3a1b0fd426d Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Fri, 5 Jan 2024 02:48:23 -0800
Subject: [PATCH 4/7] format

---
 libcxx/test/support/filesystem_test_helper.h | 40 ++++++++++----------
 1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index 001625d97775f8..e472eced8217a1 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -321,27 +321,27 @@ struct scoped_test_env
   // allow tests to call this unguarded.
 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
     std::string create_socket(std::string file) {
-        std::string socket_file = sanitize_path(file);
-
-        ::sockaddr_un address;
-        address.sun_family = AF_UNIX;
-
-        // If file.size() is too big, try to create a file directly inside
-        // /tmp to make sure file path is short enough.
-        if (socket_file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
-          fs::path const tmp = "/tmp";
-          std::size_t i      = std::hash<std::string>()(std::to_string(std::time(nullptr)));
-          fs::path p         = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
-          while (utils::exists(p.string())) {
-            p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
-          }
-          socket_file = p.string();
+      std::string socket_file = sanitize_path(file);
+
+      ::sockaddr_un address;
+      address.sun_family = AF_UNIX;
+
+      // If file.size() is too big, try to create a file directly inside
+      // /tmp to make sure file path is short enough.
+      if (socket_file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
+        fs::path const tmp = "/tmp";
+        std::size_t i      = std::hash<std::string>()(std::to_string(std::time(nullptr)));
+        fs::path p         = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
+        while (utils::exists(p.string())) {
+          p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
         }
-        assert(socket_file.size() <= sizeof(address.sun_path));
-        ::strncpy(address.sun_path, socket_file.c_str(), sizeof(address.sun_path));
-        int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
-        assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
-        return socket_file;
+        socket_file = p.string();
+      }
+      assert(socket_file.size() <= sizeof(address.sun_path));
+      ::strncpy(address.sun_path, socket_file.c_str(), sizeof(address.sun_path));
+      int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
+      assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
+      return socket_file;
     }
 #endif
 

>From ae4bac19e69c6aaff897a1178cdbf32be26aa80b Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Sun, 7 Jan 2024 19:43:00 -0800
Subject: [PATCH 5/7] change to use tmpnam

---
 libcxx/test/support/filesystem_test_helper.h | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index e472eced8217a1..b85e72d2071a8b 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -18,7 +18,6 @@
 #include <chrono>
 #include <cstdint>
 #include <cstdio> // for printf
-#include <ctime>
 #include <string>
 #include <system_error>
 #include <type_traits>
@@ -321,27 +320,21 @@ struct scoped_test_env
   // allow tests to call this unguarded.
 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
     std::string create_socket(std::string file) {
-      std::string socket_file = sanitize_path(file);
+      file = sanitize_path(std::move(file));
 
       ::sockaddr_un address;
       address.sun_family = AF_UNIX;
 
       // If file.size() is too big, try to create a file directly inside
       // /tmp to make sure file path is short enough.
-      if (socket_file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
-        fs::path const tmp = "/tmp";
-        std::size_t i      = std::hash<std::string>()(std::to_string(std::time(nullptr)));
-        fs::path p         = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
-        while (utils::exists(p.string())) {
-          p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
-        }
-        socket_file = p.string();
+      if (file.size() <= sizeof(address.sun_path)) {
+        file = std::tmpnam(nullptr);
       }
-      assert(socket_file.size() <= sizeof(address.sun_path));
-      ::strncpy(address.sun_path, socket_file.c_str(), sizeof(address.sun_path));
+      assert(file.size() <= sizeof(address.sun_path));
+      ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
       int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
       assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
-      return socket_file;
+      return file;
     }
 #endif
 

>From 64de052166058a9caf560f1366ba3bb67de90337 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Sun, 7 Jan 2024 21:57:07 -0800
Subject: [PATCH 6/7] not apply for android

---
 libcxx/test/support/filesystem_test_helper.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index b85e72d2071a8b..69f914bc8e3c05 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -325,11 +325,15 @@ struct scoped_test_env
       ::sockaddr_un address;
       address.sun_family = AF_UNIX;
 
-      // If file.size() is too big, try to create a file directly inside
-      // /tmp to make sure file path is short enough.
+// If file.size() is too big, try to create a file directly inside
+// /tmp to make sure file path is short enough.
+// Android platform warns about tmpnam, since the problem does not appear
+// on Android, let's not apply it for Android.
+#  if !defined(__ANDROID__)
       if (file.size() <= sizeof(address.sun_path)) {
         file = std::tmpnam(nullptr);
       }
+#  endif
       assert(file.size() <= sizeof(address.sun_path));
       ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
       int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);

>From 9c6e13079a0bda2debee5aaaab403860906446c3 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Sun, 7 Jan 2024 22:02:26 -0800
Subject: [PATCH 7/7] format

---
 libcxx/test/support/filesystem_test_helper.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index 69f914bc8e3c05..2ff237a4b6220c 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -330,16 +330,16 @@ struct scoped_test_env
 // Android platform warns about tmpnam, since the problem does not appear
 // on Android, let's not apply it for Android.
 #  if !defined(__ANDROID__)
-      if (file.size() <= sizeof(address.sun_path)) {
-        file = std::tmpnam(nullptr);
-      }
-#  endif
-      assert(file.size() <= sizeof(address.sun_path));
-      ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
-      int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
-      assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
-      return file;
+    if (file.size() <= sizeof(address.sun_path)) {
+      file = std::tmpnam(nullptr);
     }
+#  endif
+    assert(file.size() <= sizeof(address.sun_path));
+    ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
+    int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
+    assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
+    return file;
+  }
 #endif
 
     fs::path test_root;



More information about the libc-commits mailing list