2012-10-05 04:29:44 +08:00
|
|
|
//===- llvm/unittest/Support/MemoryBufferTest.cpp - MemoryBuffer tests ----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2012-10-05 04:29:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements unit tests for the MemoryBuffer support class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2019-08-20 20:08:52 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2013-07-23 06:46:21 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-09-02 08:51:34 +08:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2020-04-16 06:12:08 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2013-07-23 06:46:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-12-19 20:15:50 +08:00
|
|
|
#include "llvm/Testing/Support/Error.h"
|
2012-10-05 04:29:44 +08:00
|
|
|
#include "gtest/gtest.h"
|
2019-08-22 16:13:30 +08:00
|
|
|
#if LLVM_ENABLE_THREADS
|
|
|
|
#include <thread>
|
|
|
|
#endif
|
|
|
|
#if LLVM_ON_UNIX
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2019-08-20 20:08:52 +08:00
|
|
|
#define ASSERT_NO_ERROR(x) \
|
|
|
|
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
|
|
|
|
SmallString<128> MessageStorage; \
|
|
|
|
raw_svector_ostream Message(MessageStorage); \
|
|
|
|
Message << #x ": did not return errc::success.\n" \
|
|
|
|
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
|
|
|
|
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
|
|
|
|
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
|
|
|
|
} else { \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ASSERT_ERROR(x) \
|
|
|
|
if (!x) { \
|
|
|
|
SmallString<128> MessageStorage; \
|
|
|
|
raw_svector_ostream Message(MessageStorage); \
|
|
|
|
Message << #x ": did not return a failure error code.\n"; \
|
|
|
|
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
|
|
|
|
}
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
namespace {
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
class MemoryBufferTest : public testing::Test {
|
|
|
|
protected:
|
|
|
|
MemoryBufferTest()
|
|
|
|
: data("this is some data")
|
|
|
|
{ }
|
|
|
|
|
2015-04-11 10:11:45 +08:00
|
|
|
void SetUp() override {}
|
2012-10-05 04:29:44 +08:00
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
/// Common testing for different modes of getOpenFileSlice.
|
|
|
|
/// Creates a temporary file with known contents, and uses
|
|
|
|
/// MemoryBuffer::getOpenFileSlice to map it.
|
|
|
|
/// If \p Reopen is true, the file is closed after creating and reopened
|
|
|
|
/// anew before using MemoryBuffer.
|
|
|
|
void testGetOpenFileSlice(bool Reopen);
|
|
|
|
|
2014-05-19 05:01:46 +08:00
|
|
|
typedef std::unique_ptr<MemoryBuffer> OwningBuffer;
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
std::string data;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, get) {
|
|
|
|
// Default name and null-terminator flag
|
|
|
|
OwningBuffer MB1(MemoryBuffer::getMemBuffer(data));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MB1.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// RequiresNullTerminator = false
|
|
|
|
OwningBuffer MB2(MemoryBuffer::getMemBuffer(data, "one", false));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MB2.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// RequiresNullTerminator = true
|
|
|
|
OwningBuffer MB3(MemoryBuffer::getMemBuffer(data, "two", true));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MB3.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// verify all 3 buffers point to the same address
|
|
|
|
EXPECT_EQ(MB1->getBufferStart(), MB2->getBufferStart());
|
|
|
|
EXPECT_EQ(MB2->getBufferStart(), MB3->getBufferStart());
|
|
|
|
|
|
|
|
// verify the original data is unmodified after deleting the buffers
|
|
|
|
MB1.reset();
|
|
|
|
MB2.reset();
|
|
|
|
MB3.reset();
|
|
|
|
EXPECT_EQ("this is some data", data);
|
|
|
|
}
|
|
|
|
|
2019-08-20 20:08:52 +08:00
|
|
|
TEST_F(MemoryBufferTest, getOpenFile) {
|
|
|
|
int FD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
ASSERT_EQ(sys::fs::createTemporaryFile("MemoryBufferTest_getOpenFile", "temp",
|
|
|
|
FD, TestPath),
|
|
|
|
std::error_code());
|
|
|
|
|
|
|
|
FileRemover Cleanup(TestPath);
|
|
|
|
raw_fd_ostream OF(FD, /*shouldClose*/ true);
|
|
|
|
OF << "12345678";
|
|
|
|
OF.close();
|
|
|
|
|
|
|
|
{
|
|
|
|
Expected<sys::fs::file_t> File = sys::fs::openNativeFileForRead(TestPath);
|
|
|
|
ASSERT_THAT_EXPECTED(File, Succeeded());
|
|
|
|
auto OnExit =
|
|
|
|
make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
|
|
|
|
ErrorOr<OwningBuffer> MB = MemoryBuffer::getOpenFile(*File, TestPath, 6);
|
|
|
|
ASSERT_NO_ERROR(MB.getError());
|
|
|
|
EXPECT_EQ("123456", MB.get()->getBuffer());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
Expected<sys::fs::file_t> File = sys::fs::openNativeFileForWrite(
|
|
|
|
TestPath, sys::fs::CD_OpenExisting, sys::fs::OF_None);
|
|
|
|
ASSERT_THAT_EXPECTED(File, Succeeded());
|
|
|
|
auto OnExit =
|
|
|
|
make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
|
|
|
|
ASSERT_ERROR(MemoryBuffer::getOpenFile(*File, TestPath, 6).getError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 02:02:13 +08:00
|
|
|
TEST_F(MemoryBufferTest, NullTerminator4K) {
|
|
|
|
// Test that a file with size that is a multiple of the page size can be null
|
|
|
|
// terminated correctly by MemoryBuffer.
|
|
|
|
int TestFD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
sys::fs::createTemporaryFile("MemoryBufferTest_NullTerminator4K", "temp",
|
|
|
|
TestFD, TestPath);
|
2016-09-02 08:51:34 +08:00
|
|
|
FileRemover Cleanup(TestPath);
|
2013-09-05 02:02:13 +08:00
|
|
|
raw_fd_ostream OF(TestFD, true, /*unbuffered=*/true);
|
|
|
|
for (unsigned i = 0; i < 4096 / 16; ++i) {
|
|
|
|
OF << "0123456789abcdef";
|
|
|
|
}
|
|
|
|
OF.close();
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<OwningBuffer> MB = MemoryBuffer::getFile(TestPath.c_str());
|
|
|
|
std::error_code EC = MB.getError();
|
2013-09-05 02:02:13 +08:00
|
|
|
ASSERT_FALSE(EC);
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
const char *BufData = MB.get()->getBufferStart();
|
2013-09-05 02:02:13 +08:00
|
|
|
EXPECT_EQ('f', BufData[4095]);
|
|
|
|
EXPECT_EQ('\0', BufData[4096]);
|
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
TEST_F(MemoryBufferTest, copy) {
|
|
|
|
// copy with no name
|
|
|
|
OwningBuffer MBC1(MemoryBuffer::getMemBufferCopy(data));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MBC1.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// copy with a name
|
|
|
|
OwningBuffer MBC2(MemoryBuffer::getMemBufferCopy(data, "copy"));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MBC2.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// verify the two copies do not point to the same place
|
|
|
|
EXPECT_NE(MBC1->getBufferStart(), MBC2->getBufferStart());
|
|
|
|
}
|
|
|
|
|
2019-08-22 16:13:30 +08:00
|
|
|
#if LLVM_ENABLE_THREADS
|
|
|
|
TEST_F(MemoryBufferTest, createFromPipe) {
|
|
|
|
sys::fs::file_t pipes[2];
|
|
|
|
#if LLVM_ON_UNIX
|
|
|
|
ASSERT_EQ(::pipe(pipes), 0) << strerror(errno);
|
|
|
|
#else
|
|
|
|
ASSERT_TRUE(::CreatePipe(&pipes[0], &pipes[1], nullptr, 0))
|
|
|
|
<< ::GetLastError();
|
|
|
|
#endif
|
|
|
|
auto ReadCloser = make_scope_exit([&] { sys::fs::closeFile(pipes[0]); });
|
|
|
|
std::thread Writer([&] {
|
|
|
|
auto WriteCloser = make_scope_exit([&] { sys::fs::closeFile(pipes[1]); });
|
|
|
|
for (unsigned i = 0; i < 5; ++i) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
#if LLVM_ON_UNIX
|
|
|
|
ASSERT_EQ(::write(pipes[1], "foo", 3), 3) << strerror(errno);
|
|
|
|
#else
|
|
|
|
DWORD Written;
|
|
|
|
ASSERT_TRUE(::WriteFile(pipes[1], "foo", 3, &Written, nullptr))
|
|
|
|
<< ::GetLastError();
|
|
|
|
ASSERT_EQ(Written, 3u);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ErrorOr<OwningBuffer> MB =
|
|
|
|
MemoryBuffer::getOpenFile(pipes[0], "pipe", /*FileSize*/ -1);
|
|
|
|
Writer.join();
|
|
|
|
ASSERT_NO_ERROR(MB.getError());
|
|
|
|
EXPECT_EQ(MB.get()->getBuffer(), "foofoofoofoofoo");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
TEST_F(MemoryBufferTest, make_new) {
|
|
|
|
// 0-sized buffer
|
2017-12-21 19:27:21 +08:00
|
|
|
OwningBuffer Zero(WritableMemoryBuffer::getNewUninitMemBuffer(0));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != Zero.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// uninitialized buffer with no name
|
2017-12-21 19:27:21 +08:00
|
|
|
OwningBuffer One(WritableMemoryBuffer::getNewUninitMemBuffer(321));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != One.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// uninitialized buffer with name
|
2017-12-21 19:27:21 +08:00
|
|
|
OwningBuffer Two(WritableMemoryBuffer::getNewUninitMemBuffer(123, "bla"));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != Two.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// 0-initialized buffer with no name
|
2018-01-10 01:26:06 +08:00
|
|
|
OwningBuffer Three(WritableMemoryBuffer::getNewMemBuffer(321, data));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != Three.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
for (size_t i = 0; i < 321; ++i)
|
|
|
|
EXPECT_EQ(0, Three->getBufferStart()[0]);
|
|
|
|
|
|
|
|
// 0-initialized buffer with name
|
2018-01-10 01:26:06 +08:00
|
|
|
OwningBuffer Four(WritableMemoryBuffer::getNewMemBuffer(123, "zeros"));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != Four.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
for (size_t i = 0; i < 123; ++i)
|
|
|
|
EXPECT_EQ(0, Four->getBufferStart()[0]);
|
|
|
|
}
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
void MemoryBufferTest::testGetOpenFileSlice(bool Reopen) {
|
2013-07-23 06:46:21 +08:00
|
|
|
// Test that MemoryBuffer::getOpenFile works properly when no null
|
|
|
|
// terminator is requested and the size is large enough to trigger
|
|
|
|
// the usage of memory mapping.
|
|
|
|
int TestFD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
// Create a temporary file and write data into it.
|
|
|
|
sys::fs::createTemporaryFile("prefix", "temp", TestFD, TestPath);
|
2016-09-02 08:51:34 +08:00
|
|
|
FileRemover Cleanup(TestPath);
|
2013-08-22 23:14:45 +08:00
|
|
|
// OF is responsible for closing the file; If the file is not
|
2013-07-24 04:58:51 +08:00
|
|
|
// reopened, it will be unbuffered so that the results are
|
|
|
|
// immediately visible through the fd.
|
|
|
|
raw_fd_ostream OF(TestFD, true, !Reopen);
|
2013-07-23 06:46:21 +08:00
|
|
|
for (int i = 0; i < 60000; ++i) {
|
|
|
|
OF << "0123456789";
|
|
|
|
}
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
if (Reopen) {
|
|
|
|
OF.close();
|
|
|
|
EXPECT_FALSE(sys::fs::openFileForRead(TestPath.c_str(), TestFD));
|
|
|
|
}
|
|
|
|
|
[Support] Move llvm::MemoryBuffer to sys::fs::file_t
Summary:
On Windows, Posix integer file descriptors are a compatibility layer
over native file handles provided by the C runtime. There is a hard
limit on the maximum number of file descriptors that a process can open,
and the limit is 8192. LLD typically doesn't run into this limit because
it opens input files, maps them into memory, and then immediately closes
the file descriptor. This prevents it from running out of FDs.
For various reasons, I'd like to open handles to every input file and
keep them open during linking. That requires migrating MemoryBuffer over
to taking open native file handles instead of integer FDs.
Reviewers: aganea, Bigcheese
Reviewed By: aganea
Subscribers: smeenai, silvas, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, llvm-commits, zturner
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63453
llvm-svn: 365588
2019-07-10 08:34:13 +08:00
|
|
|
ErrorOr<OwningBuffer> Buf = MemoryBuffer::getOpenFileSlice(
|
|
|
|
sys::fs::convertFDToNativeFile(TestFD), TestPath.c_str(),
|
|
|
|
40000, // Size
|
|
|
|
80000 // Offset
|
|
|
|
);
|
2014-07-07 01:43:13 +08:00
|
|
|
|
|
|
|
std::error_code EC = Buf.getError();
|
2013-07-23 06:46:21 +08:00
|
|
|
EXPECT_FALSE(EC);
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
StringRef BufData = Buf.get()->getBuffer();
|
2013-07-23 06:46:21 +08:00
|
|
|
EXPECT_EQ(BufData.size(), 40000U);
|
|
|
|
EXPECT_EQ(BufData[0], '0');
|
|
|
|
EXPECT_EQ(BufData[9], '9');
|
|
|
|
}
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
TEST_F(MemoryBufferTest, getOpenFileNoReopen) {
|
|
|
|
testGetOpenFileSlice(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, getOpenFileReopened) {
|
|
|
|
testGetOpenFileSlice(true);
|
|
|
|
}
|
|
|
|
|
2014-10-08 08:22:18 +08:00
|
|
|
TEST_F(MemoryBufferTest, slice) {
|
|
|
|
// Create a file that is six pages long with different data on each page.
|
|
|
|
int FD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
sys::fs::createTemporaryFile("MemoryBufferTest_Slice", "temp", FD, TestPath);
|
2016-09-02 08:51:34 +08:00
|
|
|
FileRemover Cleanup(TestPath);
|
2014-10-08 08:22:18 +08:00
|
|
|
raw_fd_ostream OF(FD, true, /*unbuffered=*/true);
|
|
|
|
for (unsigned i = 0; i < 0x2000 / 8; ++i) {
|
|
|
|
OF << "12345678";
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < 0x2000 / 8; ++i) {
|
|
|
|
OF << "abcdefgh";
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < 0x2000 / 8; ++i) {
|
|
|
|
OF << "ABCDEFGH";
|
|
|
|
}
|
|
|
|
OF.close();
|
|
|
|
|
|
|
|
// Try offset of one page.
|
|
|
|
ErrorOr<OwningBuffer> MB = MemoryBuffer::getFileSlice(TestPath.str(),
|
|
|
|
0x4000, 0x1000);
|
|
|
|
std::error_code EC = MB.getError();
|
|
|
|
ASSERT_FALSE(EC);
|
|
|
|
EXPECT_EQ(0x4000UL, MB.get()->getBufferSize());
|
|
|
|
|
|
|
|
StringRef BufData = MB.get()->getBuffer();
|
|
|
|
EXPECT_TRUE(BufData.substr(0x0000,8).equals("12345678"));
|
|
|
|
EXPECT_TRUE(BufData.substr(0x0FF8,8).equals("12345678"));
|
|
|
|
EXPECT_TRUE(BufData.substr(0x1000,8).equals("abcdefgh"));
|
|
|
|
EXPECT_TRUE(BufData.substr(0x2FF8,8).equals("abcdefgh"));
|
|
|
|
EXPECT_TRUE(BufData.substr(0x3000,8).equals("ABCDEFGH"));
|
|
|
|
EXPECT_TRUE(BufData.substr(0x3FF8,8).equals("ABCDEFGH"));
|
|
|
|
|
|
|
|
// Try non-page aligned.
|
|
|
|
ErrorOr<OwningBuffer> MB2 = MemoryBuffer::getFileSlice(TestPath.str(),
|
|
|
|
0x3000, 0x0800);
|
|
|
|
EC = MB2.getError();
|
|
|
|
ASSERT_FALSE(EC);
|
|
|
|
EXPECT_EQ(0x3000UL, MB2.get()->getBufferSize());
|
|
|
|
|
|
|
|
StringRef BufData2 = MB2.get()->getBuffer();
|
|
|
|
EXPECT_TRUE(BufData2.substr(0x0000,8).equals("12345678"));
|
|
|
|
EXPECT_TRUE(BufData2.substr(0x17F8,8).equals("12345678"));
|
|
|
|
EXPECT_TRUE(BufData2.substr(0x1800,8).equals("abcdefgh"));
|
|
|
|
EXPECT_TRUE(BufData2.substr(0x2FF8,8).equals("abcdefgh"));
|
|
|
|
}
|
2017-12-19 20:15:50 +08:00
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, writableSlice) {
|
|
|
|
// Create a file initialized with some data
|
|
|
|
int FD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
sys::fs::createTemporaryFile("MemoryBufferTest_WritableSlice", "temp", FD,
|
|
|
|
TestPath);
|
|
|
|
FileRemover Cleanup(TestPath);
|
|
|
|
raw_fd_ostream OF(FD, true);
|
|
|
|
for (unsigned i = 0; i < 0x1000; ++i)
|
|
|
|
OF << "0123456789abcdef";
|
|
|
|
OF.close();
|
|
|
|
|
|
|
|
{
|
|
|
|
auto MBOrError =
|
|
|
|
WritableMemoryBuffer::getFileSlice(TestPath.str(), 0x6000, 0x2000);
|
|
|
|
ASSERT_FALSE(MBOrError.getError());
|
|
|
|
// Write some data. It should be mapped private, so that upon completion
|
|
|
|
// the original file contents are not modified.
|
|
|
|
WritableMemoryBuffer &MB = **MBOrError;
|
|
|
|
ASSERT_EQ(0x6000u, MB.getBufferSize());
|
|
|
|
char *Start = MB.getBufferStart();
|
|
|
|
ASSERT_EQ(MB.getBufferEnd(), MB.getBufferStart() + MB.getBufferSize());
|
|
|
|
::memset(Start, 'x', MB.getBufferSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto MBOrError = MemoryBuffer::getFile(TestPath);
|
|
|
|
ASSERT_FALSE(MBOrError.getError());
|
|
|
|
auto &MB = **MBOrError;
|
|
|
|
ASSERT_EQ(0x10000u, MB.getBufferSize());
|
|
|
|
for (size_t i = 0; i < MB.getBufferSize(); i += 0x10)
|
|
|
|
EXPECT_EQ("0123456789abcdef", MB.getBuffer().substr(i, 0x10)) << "i: " << i;
|
|
|
|
}
|
2018-03-09 04:34:47 +08:00
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, writeThroughFile) {
|
|
|
|
// Create a file initialized with some data
|
|
|
|
int FD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
sys::fs::createTemporaryFile("MemoryBufferTest_WriteThrough", "temp", FD,
|
|
|
|
TestPath);
|
|
|
|
FileRemover Cleanup(TestPath);
|
|
|
|
raw_fd_ostream OF(FD, true);
|
|
|
|
OF << "0123456789abcdef";
|
|
|
|
OF.close();
|
|
|
|
{
|
|
|
|
auto MBOrError = WriteThroughMemoryBuffer::getFile(TestPath);
|
|
|
|
ASSERT_FALSE(MBOrError.getError());
|
|
|
|
// Write some data. It should be mapped readwrite, so that upon completion
|
|
|
|
// the original file contents are modified.
|
|
|
|
WriteThroughMemoryBuffer &MB = **MBOrError;
|
2018-03-09 05:54:30 +08:00
|
|
|
ASSERT_EQ(16u, MB.getBufferSize());
|
2018-03-09 04:34:47 +08:00
|
|
|
char *Start = MB.getBufferStart();
|
|
|
|
ASSERT_EQ(MB.getBufferEnd(), MB.getBufferStart() + MB.getBufferSize());
|
|
|
|
::memset(Start, 'x', MB.getBufferSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto MBOrError = MemoryBuffer::getFile(TestPath);
|
|
|
|
ASSERT_FALSE(MBOrError.getError());
|
|
|
|
auto &MB = **MBOrError;
|
2018-03-09 05:54:30 +08:00
|
|
|
ASSERT_EQ(16u, MB.getBufferSize());
|
2018-03-09 04:34:47 +08:00
|
|
|
EXPECT_EQ("xxxxxxxxxxxxxxxx", MB.getBuffer());
|
|
|
|
}
|
2020-04-09 11:29:39 +08:00
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, mmapVolatileNoNull) {
|
|
|
|
// Verify that `MemoryBuffer::getOpenFile` will use mmap when
|
|
|
|
// `RequiresNullTerminator = false`, `IsVolatile = true`, and the file is
|
|
|
|
// large enough to use mmap.
|
|
|
|
//
|
|
|
|
// This is done because Clang should use this mode to open module files, and
|
|
|
|
// falling back to malloc for them causes a huge memory usage increase.
|
|
|
|
|
|
|
|
int FD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
ASSERT_NO_ERROR(sys::fs::createTemporaryFile(
|
|
|
|
"MemoryBufferTest_mmapVolatileNoNull", "temp", FD, TestPath));
|
|
|
|
FileRemover Cleanup(TestPath);
|
|
|
|
raw_fd_ostream OF(FD, true);
|
2020-04-16 06:12:08 +08:00
|
|
|
// Create a file large enough to mmap. 4 pages should be enough.
|
|
|
|
unsigned PageSize = sys::Process::getPageSizeEstimate();
|
|
|
|
unsigned FileWrites = (PageSize * 4) / 8;
|
|
|
|
for (unsigned i = 0; i < FileWrites; ++i)
|
2020-04-09 11:29:39 +08:00
|
|
|
OF << "01234567";
|
2020-04-16 05:46:29 +08:00
|
|
|
OF.close();
|
|
|
|
|
|
|
|
Expected<sys::fs::file_t> File = sys::fs::openNativeFileForRead(TestPath);
|
|
|
|
ASSERT_THAT_EXPECTED(File, Succeeded());
|
|
|
|
auto OnExit =
|
|
|
|
make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
|
2020-04-09 11:29:39 +08:00
|
|
|
|
2020-04-16 05:46:29 +08:00
|
|
|
auto MBOrError = MemoryBuffer::getOpenFile(*File, TestPath,
|
2020-04-09 11:29:39 +08:00
|
|
|
/*FileSize=*/-1, /*RequiresNullTerminator=*/false, /*IsVolatile=*/true);
|
|
|
|
ASSERT_NO_ERROR(MBOrError.getError())
|
|
|
|
OwningBuffer MB = std::move(*MBOrError);
|
|
|
|
EXPECT_EQ(MB->getBufferKind(), MemoryBuffer::MemoryBuffer_MMap);
|
2020-04-16 06:12:08 +08:00
|
|
|
EXPECT_EQ(MB->getBufferSize(), std::size_t(FileWrites * 8));
|
2020-04-09 11:29:39 +08:00
|
|
|
EXPECT_TRUE(MB->getBuffer().startswith("01234567"));
|
|
|
|
}
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|