1 //===- llvm/unittest/Support/MemoryBufferTest.cpp - MemoryBuffer tests ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements unit tests for the MemoryBuffer support class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/FileUtilities.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/Testing/Support/Error.h"
18 #include "gtest/gtest.h"
24 class MemoryBufferTest
: public testing::Test
{
27 : data("this is some data")
30 void SetUp() override
{}
32 /// Common testing for different modes of getOpenFileSlice.
33 /// Creates a temporary file with known contents, and uses
34 /// MemoryBuffer::getOpenFileSlice to map it.
35 /// If \p Reopen is true, the file is closed after creating and reopened
36 /// anew before using MemoryBuffer.
37 void testGetOpenFileSlice(bool Reopen
);
39 typedef std::unique_ptr
<MemoryBuffer
> OwningBuffer
;
44 TEST_F(MemoryBufferTest
, get
) {
45 // Default name and null-terminator flag
46 OwningBuffer
MB1(MemoryBuffer::getMemBuffer(data
));
47 EXPECT_TRUE(nullptr != MB1
.get());
49 // RequiresNullTerminator = false
50 OwningBuffer
MB2(MemoryBuffer::getMemBuffer(data
, "one", false));
51 EXPECT_TRUE(nullptr != MB2
.get());
53 // RequiresNullTerminator = true
54 OwningBuffer
MB3(MemoryBuffer::getMemBuffer(data
, "two", true));
55 EXPECT_TRUE(nullptr != MB3
.get());
57 // verify all 3 buffers point to the same address
58 EXPECT_EQ(MB1
->getBufferStart(), MB2
->getBufferStart());
59 EXPECT_EQ(MB2
->getBufferStart(), MB3
->getBufferStart());
61 // verify the original data is unmodified after deleting the buffers
65 EXPECT_EQ("this is some data", data
);
68 TEST_F(MemoryBufferTest
, NullTerminator4K
) {
69 // Test that a file with size that is a multiple of the page size can be null
70 // terminated correctly by MemoryBuffer.
72 SmallString
<64> TestPath
;
73 sys::fs::createTemporaryFile("MemoryBufferTest_NullTerminator4K", "temp",
75 FileRemover
Cleanup(TestPath
);
76 raw_fd_ostream
OF(TestFD
, true, /*unbuffered=*/true);
77 for (unsigned i
= 0; i
< 4096 / 16; ++i
) {
78 OF
<< "0123456789abcdef";
82 ErrorOr
<OwningBuffer
> MB
= MemoryBuffer::getFile(TestPath
.c_str());
83 std::error_code EC
= MB
.getError();
86 const char *BufData
= MB
.get()->getBufferStart();
87 EXPECT_EQ('f', BufData
[4095]);
88 EXPECT_EQ('\0', BufData
[4096]);
91 TEST_F(MemoryBufferTest
, copy
) {
93 OwningBuffer
MBC1(MemoryBuffer::getMemBufferCopy(data
));
94 EXPECT_TRUE(nullptr != MBC1
.get());
97 OwningBuffer
MBC2(MemoryBuffer::getMemBufferCopy(data
, "copy"));
98 EXPECT_TRUE(nullptr != MBC2
.get());
100 // verify the two copies do not point to the same place
101 EXPECT_NE(MBC1
->getBufferStart(), MBC2
->getBufferStart());
104 TEST_F(MemoryBufferTest
, make_new
) {
106 OwningBuffer
Zero(WritableMemoryBuffer::getNewUninitMemBuffer(0));
107 EXPECT_TRUE(nullptr != Zero
.get());
109 // uninitialized buffer with no name
110 OwningBuffer
One(WritableMemoryBuffer::getNewUninitMemBuffer(321));
111 EXPECT_TRUE(nullptr != One
.get());
113 // uninitialized buffer with name
114 OwningBuffer
Two(WritableMemoryBuffer::getNewUninitMemBuffer(123, "bla"));
115 EXPECT_TRUE(nullptr != Two
.get());
117 // 0-initialized buffer with no name
118 OwningBuffer
Three(WritableMemoryBuffer::getNewMemBuffer(321, data
));
119 EXPECT_TRUE(nullptr != Three
.get());
120 for (size_t i
= 0; i
< 321; ++i
)
121 EXPECT_EQ(0, Three
->getBufferStart()[0]);
123 // 0-initialized buffer with name
124 OwningBuffer
Four(WritableMemoryBuffer::getNewMemBuffer(123, "zeros"));
125 EXPECT_TRUE(nullptr != Four
.get());
126 for (size_t i
= 0; i
< 123; ++i
)
127 EXPECT_EQ(0, Four
->getBufferStart()[0]);
130 void MemoryBufferTest::testGetOpenFileSlice(bool Reopen
) {
131 // Test that MemoryBuffer::getOpenFile works properly when no null
132 // terminator is requested and the size is large enough to trigger
133 // the usage of memory mapping.
135 SmallString
<64> TestPath
;
136 // Create a temporary file and write data into it.
137 sys::fs::createTemporaryFile("prefix", "temp", TestFD
, TestPath
);
138 FileRemover
Cleanup(TestPath
);
139 // OF is responsible for closing the file; If the file is not
140 // reopened, it will be unbuffered so that the results are
141 // immediately visible through the fd.
142 raw_fd_ostream
OF(TestFD
, true, !Reopen
);
143 for (int i
= 0; i
< 60000; ++i
) {
149 EXPECT_FALSE(sys::fs::openFileForRead(TestPath
.c_str(), TestFD
));
152 ErrorOr
<OwningBuffer
> Buf
=
153 MemoryBuffer::getOpenFileSlice(TestFD
, TestPath
.c_str(),
158 std::error_code EC
= Buf
.getError();
161 StringRef BufData
= Buf
.get()->getBuffer();
162 EXPECT_EQ(BufData
.size(), 40000U);
163 EXPECT_EQ(BufData
[0], '0');
164 EXPECT_EQ(BufData
[9], '9');
167 TEST_F(MemoryBufferTest
, getOpenFileNoReopen
) {
168 testGetOpenFileSlice(false);
171 TEST_F(MemoryBufferTest
, getOpenFileReopened
) {
172 testGetOpenFileSlice(true);
175 TEST_F(MemoryBufferTest
, reference
) {
176 OwningBuffer
MB(MemoryBuffer::getMemBuffer(data
));
177 MemoryBufferRef
MBR(*MB
);
179 EXPECT_EQ(MB
->getBufferStart(), MBR
.getBufferStart());
180 EXPECT_EQ(MB
->getBufferIdentifier(), MBR
.getBufferIdentifier());
183 TEST_F(MemoryBufferTest
, slice
) {
184 // Create a file that is six pages long with different data on each page.
186 SmallString
<64> TestPath
;
187 sys::fs::createTemporaryFile("MemoryBufferTest_Slice", "temp", FD
, TestPath
);
188 FileRemover
Cleanup(TestPath
);
189 raw_fd_ostream
OF(FD
, true, /*unbuffered=*/true);
190 for (unsigned i
= 0; i
< 0x2000 / 8; ++i
) {
193 for (unsigned i
= 0; i
< 0x2000 / 8; ++i
) {
196 for (unsigned i
= 0; i
< 0x2000 / 8; ++i
) {
201 // Try offset of one page.
202 ErrorOr
<OwningBuffer
> MB
= MemoryBuffer::getFileSlice(TestPath
.str(),
204 std::error_code EC
= MB
.getError();
206 EXPECT_EQ(0x4000UL
, MB
.get()->getBufferSize());
208 StringRef BufData
= MB
.get()->getBuffer();
209 EXPECT_TRUE(BufData
.substr(0x0000,8).equals("12345678"));
210 EXPECT_TRUE(BufData
.substr(0x0FF8,8).equals("12345678"));
211 EXPECT_TRUE(BufData
.substr(0x1000,8).equals("abcdefgh"));
212 EXPECT_TRUE(BufData
.substr(0x2FF8,8).equals("abcdefgh"));
213 EXPECT_TRUE(BufData
.substr(0x3000,8).equals("ABCDEFGH"));
214 EXPECT_TRUE(BufData
.substr(0x3FF8,8).equals("ABCDEFGH"));
216 // Try non-page aligned.
217 ErrorOr
<OwningBuffer
> MB2
= MemoryBuffer::getFileSlice(TestPath
.str(),
221 EXPECT_EQ(0x3000UL
, MB2
.get()->getBufferSize());
223 StringRef BufData2
= MB2
.get()->getBuffer();
224 EXPECT_TRUE(BufData2
.substr(0x0000,8).equals("12345678"));
225 EXPECT_TRUE(BufData2
.substr(0x17F8,8).equals("12345678"));
226 EXPECT_TRUE(BufData2
.substr(0x1800,8).equals("abcdefgh"));
227 EXPECT_TRUE(BufData2
.substr(0x2FF8,8).equals("abcdefgh"));
230 TEST_F(MemoryBufferTest
, writableSlice
) {
231 // Create a file initialized with some data
233 SmallString
<64> TestPath
;
234 sys::fs::createTemporaryFile("MemoryBufferTest_WritableSlice", "temp", FD
,
236 FileRemover
Cleanup(TestPath
);
237 raw_fd_ostream
OF(FD
, true);
238 for (unsigned i
= 0; i
< 0x1000; ++i
)
239 OF
<< "0123456789abcdef";
244 WritableMemoryBuffer::getFileSlice(TestPath
.str(), 0x6000, 0x2000);
245 ASSERT_FALSE(MBOrError
.getError());
246 // Write some data. It should be mapped private, so that upon completion
247 // the original file contents are not modified.
248 WritableMemoryBuffer
&MB
= **MBOrError
;
249 ASSERT_EQ(0x6000u
, MB
.getBufferSize());
250 char *Start
= MB
.getBufferStart();
251 ASSERT_EQ(MB
.getBufferEnd(), MB
.getBufferStart() + MB
.getBufferSize());
252 ::memset(Start
, 'x', MB
.getBufferSize());
255 auto MBOrError
= MemoryBuffer::getFile(TestPath
);
256 ASSERT_FALSE(MBOrError
.getError());
257 auto &MB
= **MBOrError
;
258 ASSERT_EQ(0x10000u
, MB
.getBufferSize());
259 for (size_t i
= 0; i
< MB
.getBufferSize(); i
+= 0x10)
260 EXPECT_EQ("0123456789abcdef", MB
.getBuffer().substr(i
, 0x10)) << "i: " << i
;
263 TEST_F(MemoryBufferTest
, writeThroughFile
) {
264 // Create a file initialized with some data
266 SmallString
<64> TestPath
;
267 sys::fs::createTemporaryFile("MemoryBufferTest_WriteThrough", "temp", FD
,
269 FileRemover
Cleanup(TestPath
);
270 raw_fd_ostream
OF(FD
, true);
271 OF
<< "0123456789abcdef";
274 auto MBOrError
= WriteThroughMemoryBuffer::getFile(TestPath
);
275 ASSERT_FALSE(MBOrError
.getError());
276 // Write some data. It should be mapped readwrite, so that upon completion
277 // the original file contents are modified.
278 WriteThroughMemoryBuffer
&MB
= **MBOrError
;
279 ASSERT_EQ(16u, MB
.getBufferSize());
280 char *Start
= MB
.getBufferStart();
281 ASSERT_EQ(MB
.getBufferEnd(), MB
.getBufferStart() + MB
.getBufferSize());
282 ::memset(Start
, 'x', MB
.getBufferSize());
285 auto MBOrError
= MemoryBuffer::getFile(TestPath
);
286 ASSERT_FALSE(MBOrError
.getError());
287 auto &MB
= **MBOrError
;
288 ASSERT_EQ(16u, MB
.getBufferSize());
289 EXPECT_EQ("xxxxxxxxxxxxxxxx", MB
.getBuffer());