[lldb] Fix some bugs in the Pipe class and add tests
authorPavel Labath <pavel@labath.sk>
Fri, 8 Jan 2021 19:37:09 +0000 (20:37 +0100)
committerPavel Labath <pavel@labath.sk>
Sun, 10 Jan 2021 20:59:16 +0000 (21:59 +0100)
- s/createUniqueFile/createUniquePath -- we don't want to create the file,
  just the file name
- s/data()/str().c_str()/ -- paths given to system apis must be
  null-terminated

lldb/source/Host/posix/PipePosix.cpp
lldb/unittests/Host/CMakeLists.txt
lldb/unittests/Host/PipeTest.cpp [new file with mode: 0644]

index 780222d..7cd05a1 100644 (file)
@@ -117,7 +117,7 @@ Status PipePosix::CreateNew(llvm::StringRef name, bool child_process_inherit) {
     return Status("Pipe is already opened");
 
   Status error;
-  if (::mkfifo(name.data(), 0660) != 0)
+  if (::mkfifo(name.str().c_str(), 0660) != 0)
     error.SetErrorToErrno();
 
   return error;
@@ -138,8 +138,8 @@ Status PipePosix::CreateWithUniqueName(llvm::StringRef prefix,
   // try again.
   Status error;
   do {
-    llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath(),
-                                    named_pipe_path);
+    llvm::sys::fs::createUniquePath(tmpdir_file_spec.GetPath(), named_pipe_path,
+                                    /*MakeAbsolute=*/false);
     error = CreateNew(named_pipe_path, child_process_inherit);
   } while (error.GetError() == EEXIST);
 
@@ -158,7 +158,7 @@ Status PipePosix::OpenAsReader(llvm::StringRef name,
     flags |= O_CLOEXEC;
 
   Status error;
-  int fd = llvm::sys::RetryAfterSignal(-1, ::open, name.data(), flags);
+  int fd = llvm::sys::RetryAfterSignal(-1, ::open, name.str().c_str(), flags);
   if (fd != -1)
     m_fds[READ] = fd;
   else
@@ -189,7 +189,7 @@ PipePosix::OpenAsWriterWithTimeout(llvm::StringRef name,
     }
 
     errno = 0;
-    int fd = ::open(name.data(), flags);
+    int fd = ::open(name.str().c_str(), flags);
     if (fd == -1) {
       const auto errno_copy = errno;
       // We may get ENXIO if a reader side of the pipe hasn't opened yet.
index 663645c..1cc0cb0 100644 (file)
@@ -7,6 +7,7 @@ set (FILES
   HostTest.cpp
   MainLoopTest.cpp
   NativeProcessProtocolTest.cpp
+  PipeTest.cpp
   ProcessLaunchInfoTest.cpp
   SocketAddressTest.cpp
   SocketTest.cpp
diff --git a/lldb/unittests/Host/PipeTest.cpp b/lldb/unittests/Host/PipeTest.cpp
new file mode 100644 (file)
index 0000000..e8d2c49
--- /dev/null
@@ -0,0 +1,48 @@
+//===-- PipeTest.cpp ------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/Pipe.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostInfo.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+
+class PipeTest : public testing::Test {
+public:
+  SubsystemRAII<FileSystem, HostInfo> subsystems;
+};
+
+TEST_F(PipeTest, CreateWithUniqueName) {
+  Pipe pipe;
+  llvm::SmallString<0> name;
+  ASSERT_THAT_ERROR(pipe.CreateWithUniqueName("PipeTest-CreateWithUniqueName",
+                                              /*child_process_inherit=*/false,
+                                              name)
+                        .ToError(),
+                    llvm::Succeeded());
+}
+
+TEST_F(PipeTest, OpenAsReader) {
+  Pipe pipe;
+  llvm::SmallString<0> name;
+  ASSERT_THAT_ERROR(pipe.CreateWithUniqueName("PipeTest-OpenAsReader",
+                                              /*child_process_inherit=*/false,
+                                              name)
+                        .ToError(),
+                    llvm::Succeeded());
+
+  // Ensure name is not null-terminated
+  size_t name_len = name.size();
+  name += "foobar";
+  llvm::StringRef name_ref(name.data(), name_len);
+  ASSERT_THAT_ERROR(
+      pipe.OpenAsReader(name_ref, /*child_process_inherit=*/false).ToError(),
+      llvm::Succeeded());
+}