From: Reid Kleckner Date: Fri, 2 Sep 2016 00:51:34 +0000 (+0000) Subject: Try to fix some temp file leaks in SupportTests, PR18335 X-Git-Tag: llvmorg-4.0.0-rc1~10815 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=75e557f1fd9392a20989e57d31eaa1878f7ca9c1;p=platform%2Fupstream%2Fllvm.git Try to fix some temp file leaks in SupportTests, PR18335 llvm-svn: 280443 --- diff --git a/llvm/unittests/Support/MemoryBufferTest.cpp b/llvm/unittests/Support/MemoryBufferTest.cpp index 963dcd9..0efa22c 100644 --- a/llvm/unittests/Support/MemoryBufferTest.cpp +++ b/llvm/unittests/Support/MemoryBufferTest.cpp @@ -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, NullTerminator4K) { 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::testGetOpenFileSlice(bool Reopen) { 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"; diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index a1e1365..0c61229 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -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))); diff --git a/llvm/unittests/Support/SpecialCaseListTest.cpp b/llvm/unittests/Support/SpecialCaseListTest.cpp index 0657f80..28c21da 100644 --- a/llvm/unittests/Support/SpecialCaseListTest.cpp +++ b/llvm/unittests/Support/SpecialCaseListTest.cpp @@ -130,6 +130,8 @@ TEST_F(SpecialCaseListTest, MultipleBlacklists) { 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); } } diff --git a/llvm/unittests/Support/raw_pwrite_stream_test.cpp b/llvm/unittests/Support/raw_pwrite_stream_test.cpp index a62f6ba..8885567 100644 --- a/llvm/unittests/Support/raw_pwrite_stream_test.cpp +++ b/llvm/unittests/Support/raw_pwrite_stream_test.cpp @@ -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";