[llvm] r280443 - Try to fix some temp file leaks in SupportTests, PR18335

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 1 17:51:34 PDT 2016


Author: rnk
Date: Thu Sep  1 19:51:34 2016
New Revision: 280443

URL: http://llvm.org/viewvc/llvm-project?rev=280443&view=rev
Log:
Try to fix some temp file leaks in SupportTests, PR18335

Modified:
    llvm/trunk/unittests/Support/MemoryBufferTest.cpp
    llvm/trunk/unittests/Support/Path.cpp
    llvm/trunk/unittests/Support/SpecialCaseListTest.cpp
    llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp

Modified: llvm/trunk/unittests/Support/MemoryBufferTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/MemoryBufferTest.cpp?rev=280443&r1=280442&r2=280443&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/MemoryBufferTest.cpp (original)
+++ llvm/trunk/unittests/Support/MemoryBufferTest.cpp Thu Sep  1 19:51:34 2016
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/raw_ostream.h"
 #include "gtest/gtest.h"
@@ -71,6 +72,7 @@ TEST_F(MemoryBufferTest, NullTerminator4
   SmallString<64> TestPath;
   sys::fs::createTemporaryFile("MemoryBufferTest_NullTerminator4K", "temp",
                                TestFD, TestPath);
+  FileRemover Cleanup(TestPath);
   raw_fd_ostream OF(TestFD, true, /*unbuffered=*/true);
   for (unsigned i = 0; i < 4096 / 16; ++i) {
     OF << "0123456789abcdef";
@@ -133,6 +135,7 @@ void MemoryBufferTest::testGetOpenFileSl
   SmallString<64> TestPath;
   // Create a temporary file and write data into it.
   sys::fs::createTemporaryFile("prefix", "temp", TestFD, TestPath);
+  FileRemover Cleanup(TestPath);
   // OF is responsible for closing the file; If the file is not
   // reopened, it will be unbuffered so that the results are
   // immediately visible through the fd.
@@ -182,6 +185,7 @@ TEST_F(MemoryBufferTest, slice) {
   int FD;
   SmallString<64> TestPath;
   sys::fs::createTemporaryFile("MemoryBufferTest_Slice", "temp", FD, TestPath);
+  FileRemover Cleanup(TestPath);
   raw_fd_ostream OF(FD, true, /*unbuffered=*/true);
   for (unsigned i = 0; i < 0x2000 / 8; ++i) {
     OF << "12345678";

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=280443&r1=280442&r2=280443&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Thu Sep  1 19:51:34 2016
@@ -13,6 +13,7 @@
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/raw_ostream.h"
 #include "gtest/gtest.h"
@@ -487,6 +488,8 @@ TEST_F(FileSystemTest, Unique) {
      fs::createUniqueDirectory("dir2", Dir2));
   ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
   ASSERT_NE(F1, F2);
+  ASSERT_NO_ERROR(fs::remove(TempPath2));
+  ASSERT_NO_ERROR(fs::remove(TempPath));
 }
 
 TEST_F(FileSystemTest, TempFiles) {
@@ -530,6 +533,7 @@ TEST_F(FileSystemTest, TempFiles) {
   SmallString<64> TempPath3;
   ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
   ASSERT_FALSE(TempPath3.endswith("."));
+  FileRemover Cleanup3(TempPath3);
 
   // Create a hard link to Temp1.
   ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
@@ -851,6 +855,8 @@ TEST_F(FileSystemTest, Resize) {
   fs::file_status Status;
   ASSERT_NO_ERROR(fs::status(FD, Status));
   ASSERT_EQ(Status.getSize(), 123U);
+  ::close(FD);
+  ASSERT_NO_ERROR(fs::remove(TempPath));
 }
 
 TEST_F(FileSystemTest, FileMapping) {
@@ -874,21 +880,25 @@ TEST_F(FileSystemTest, FileMapping) {
     mfr.data()[Val.size()] = 0;
     // Unmap temp file
   }
+  ASSERT_EQ(close(FileDescriptor), 0);
 
   // Map it back in read-only
-  int FD;
-  EC = fs::openFileForRead(Twine(TempPath), FD);
-  ASSERT_NO_ERROR(EC);
-  fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
-  ASSERT_NO_ERROR(EC);
-
-  // Verify content
-  EXPECT_EQ(StringRef(mfr.const_data()), Val);
-
-  // Unmap temp file
-  fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
-  ASSERT_NO_ERROR(EC);
-  ASSERT_EQ(close(FD), 0);
+  {
+    int FD;
+    EC = fs::openFileForRead(Twine(TempPath), FD);
+    ASSERT_NO_ERROR(EC);
+    fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
+    ASSERT_NO_ERROR(EC);
+
+    // Verify content
+    EXPECT_EQ(StringRef(mfr.const_data()), Val);
+
+    // Unmap temp file
+    fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
+    ASSERT_NO_ERROR(EC);
+    ASSERT_EQ(close(FD), 0);
+  }
+  ASSERT_NO_ERROR(fs::remove(TempPath));
 }
 
 TEST(Support, NormalizePath) {
@@ -1002,6 +1012,7 @@ TEST_F(FileSystemTest, PathFromFD) {
   SmallString<64> TempPath;
   ASSERT_NO_ERROR(
       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+  FileRemover Cleanup(TempPath);
 
   // Make sure it exists.
   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
@@ -1030,6 +1041,7 @@ TEST_F(FileSystemTest, PathFromFDWin32)
   SmallString<64> TempPath;
   ASSERT_NO_ERROR(
     fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+  FileRemover Cleanup(TempPath);
 
   // Make sure it exists.
   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
@@ -1066,6 +1078,7 @@ TEST_F(FileSystemTest, PathFromFDUnicode
   ASSERT_NO_ERROR(
     fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
                             "\xE2\x84\xB5.0", FileDescriptor, TempPath));
+  FileRemover Cleanup(TempPath);
 
   // Make sure it exists.
   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
@@ -1089,6 +1102,7 @@ TEST_F(FileSystemTest, OpenFileForRead)
   SmallString<64> TempPath;
   ASSERT_NO_ERROR(
       fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+  FileRemover Cleanup(TempPath);
 
   // Make sure it exists.
   ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));

Modified: llvm/trunk/unittests/Support/SpecialCaseListTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/SpecialCaseListTest.cpp?rev=280443&r1=280442&r2=280443&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/SpecialCaseListTest.cpp (original)
+++ llvm/trunk/unittests/Support/SpecialCaseListTest.cpp Thu Sep  1 19:51:34 2016
@@ -130,6 +130,8 @@ TEST_F(SpecialCaseListTest, MultipleBlac
   EXPECT_TRUE(SCL->inSection("src", "ban", "init"));
   EXPECT_TRUE(SCL->inSection("src", "tomfoolery"));
   EXPECT_TRUE(SCL->inSection("src", "tomfoglery"));
+  for (auto &Path : Files)
+    sys::fs::remove(Path);
 }
 
 }

Modified: llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp?rev=280443&r1=280442&r2=280443&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp (original)
+++ llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp Thu Sep  1 19:51:34 2016
@@ -10,10 +10,20 @@
 #include "gtest/gtest.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
 #include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
 
+#define ASSERT_NO_ERROR(x)                                                     \
+  if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
+    errs() << #x ": did not return errc::success.\n"                           \
+           << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"           \
+           << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n";       \
+  } else {                                                                     \
+  }
+
+
 namespace {
 
 TEST(raw_pwrite_ostreamTest, TestSVector) {
@@ -32,10 +42,28 @@ TEST(raw_pwrite_ostreamTest, TestSVector
 #endif
 }
 
+#ifdef _MSC_VER
+#define setenv(name, var, ignore) _putenv_s(name, var)
+#endif
+
 TEST(raw_pwrite_ostreamTest, TestFD) {
   SmallString<64> Path;
   int FD;
-  sys::fs::createTemporaryFile("foo", "bar", FD, Path);
+
+  // If we want to clean up from a death test, we have to remove the file from
+  // the parent process. Have the parent create the file, pass it via
+  // environment variable to the child, let the child crash, and then remove it
+  // in the parent.
+  const char *ParentPath = getenv("RAW_PWRITE_TEST_FILE");
+  if (ParentPath) {
+    Path = ParentPath;
+    ASSERT_NO_ERROR(sys::fs::openFileForRead(Path, FD));
+  } else {
+    ASSERT_NO_ERROR(sys::fs::createTemporaryFile("foo", "bar", FD, Path));
+    setenv("RAW_PWRITE_TEST_FILE", Path.c_str(), true);
+  }
+  FileRemover Cleanup(Path);
+
   raw_fd_ostream OS(FD, true);
   OS << "abcd";
   StringRef Test = "test";




More information about the llvm-commits mailing list