[llvm] 9d53db2 - [Support] Allow FileOutputBuffer::create to create an empty file
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue May 5 08:12:06 PDT 2020
Author: Fangrui Song
Date: 2020-05-05T08:11:58-07:00
New Revision: 9d53db2aa098ca5136c352c38710ea48bf078e7a
URL: https://github.com/llvm/llvm-project/commit/9d53db2aa098ca5136c352c38710ea48bf078e7a
DIFF: https://github.com/llvm/llvm-project/commit/9d53db2aa098ca5136c352c38710ea48bf078e7a.diff
LOG: [Support] Allow FileOutputBuffer::create to create an empty file
Size==0 triggers `assert(Size != 0)` in mapped_file_region::init.
I plan to use an empty file in D79339 (llvm-objcopy --dump-section).
According to POSIX, "If len is zero, mmap() shall fail and no mapping
shall be established." Just specialize case Size=0 to use
createInMemoryBuffer.
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D79338
Added:
Modified:
llvm/lib/Support/FileOutputBuffer.cpp
llvm/unittests/Support/FileOutputBufferTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/FileOutputBuffer.cpp b/llvm/lib/Support/FileOutputBuffer.cpp
index 0a5306f684d4..ec12820e9692 100644
--- a/llvm/lib/Support/FileOutputBuffer.cpp
+++ b/llvm/lib/Support/FileOutputBuffer.cpp
@@ -172,6 +172,10 @@ FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
if (Flags & F_executable)
Mode |= fs::all_exe;
+ // If Size is zero, don't use mmap which will fail with EINVAL.
+ if (Size == 0)
+ return createInMemoryBuffer(Path, Size, Mode);
+
fs::file_status Stat;
fs::status(Path, Stat);
diff --git a/llvm/unittests/Support/FileOutputBufferTest.cpp b/llvm/unittests/Support/FileOutputBufferTest.cpp
index 6b6196f97164..f7bb0833e5a0 100644
--- a/llvm/unittests/Support/FileOutputBufferTest.cpp
+++ b/llvm/unittests/Support/FileOutputBufferTest.cpp
@@ -140,6 +140,21 @@ TEST(FileOutputBuffer, Test) {
ASSERT_NO_ERROR(fs::file_size(Twine(File5), File5Size));
ASSERT_EQ(File5Size, 8000ULL);
ASSERT_NO_ERROR(fs::remove(File5.str()));
+
+ // TEST 6: Create an empty file.
+ SmallString<128> File6(TestDirectory);
+ File6.append("/file6");
+ {
+ Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+ FileOutputBuffer::create(File6, 0);
+ ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError()));
+ ASSERT_NO_ERROR(errorToErrorCode((*BufferOrErr)->commit()));
+ }
+ uint64_t File6Size;
+ ASSERT_NO_ERROR(fs::file_size(Twine(File6), File6Size));
+ ASSERT_EQ(File6Size, 0ULL);
+ ASSERT_NO_ERROR(fs::remove(File6.str()));
+
// Clean up.
ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
}
More information about the llvm-commits
mailing list