1 //===- llvm/unittest/Support/DataExtractorTest.cpp - DataExtractor 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 #include "llvm/Support/DataExtractor.h"
10 #include "llvm/Testing/Support/Error.h"
11 #include "gtest/gtest.h"
16 const char numberData
[] = "\x80\x90\xFF\xFF\x80\x00\x00\x00";
17 const char leb128data
[] = "\xA6\x49";
18 const char bigleb128data
[] = "\xAA\xA9\xFF\xAA\xFF\xAA\xFF\x4A";
20 TEST(DataExtractorTest
, OffsetOverflow
) {
21 DataExtractor
DE(StringRef(numberData
, sizeof(numberData
)-1), false, 8);
22 EXPECT_FALSE(DE
.isValidOffsetForDataOfSize(-2U, 5));
25 TEST(DataExtractorTest
, UnsignedNumbers
) {
26 DataExtractor
DE(StringRef(numberData
, sizeof(numberData
)-1), false, 8);
29 EXPECT_EQ(0x80U
, DE
.getU8(&offset
));
30 EXPECT_EQ(1U, offset
);
32 EXPECT_EQ(0x8090U
, DE
.getU16(&offset
));
33 EXPECT_EQ(2U, offset
);
35 EXPECT_EQ(0x8090FFFFU
, DE
.getU32(&offset
));
36 EXPECT_EQ(4U, offset
);
38 EXPECT_EQ(0x8090FFFF80000000ULL
, DE
.getU64(&offset
));
39 EXPECT_EQ(8U, offset
);
41 EXPECT_EQ(0x8090FFFF80000000ULL
, DE
.getAddress(&offset
));
42 EXPECT_EQ(8U, offset
);
46 EXPECT_EQ(data
, DE
.getU32(&offset
, data
, 2));
47 EXPECT_EQ(0x8090FFFFU
, data
[0]);
48 EXPECT_EQ(0x80000000U
, data
[1]);
49 EXPECT_EQ(8U, offset
);
52 // Now for little endian.
53 DE
= DataExtractor(StringRef(numberData
, sizeof(numberData
)-1), true, 4);
54 EXPECT_EQ(0x9080U
, DE
.getU16(&offset
));
55 EXPECT_EQ(2U, offset
);
57 EXPECT_EQ(0xFFFF9080U
, DE
.getU32(&offset
));
58 EXPECT_EQ(4U, offset
);
60 EXPECT_EQ(0x80FFFF9080ULL
, DE
.getU64(&offset
));
61 EXPECT_EQ(8U, offset
);
63 EXPECT_EQ(0xFFFF9080U
, DE
.getAddress(&offset
));
64 EXPECT_EQ(4U, offset
);
67 EXPECT_EQ(data
, DE
.getU32(&offset
, data
, 2));
68 EXPECT_EQ(0xFFFF9080U
, data
[0]);
69 EXPECT_EQ(0x80U
, data
[1]);
70 EXPECT_EQ(8U, offset
);
73 TEST(DataExtractorTest
, SignedNumbers
) {
74 DataExtractor
DE(StringRef(numberData
, sizeof(numberData
)-1), false, 8);
77 EXPECT_EQ(-128, DE
.getSigned(&offset
, 1));
78 EXPECT_EQ(1U, offset
);
80 EXPECT_EQ(-32624, DE
.getSigned(&offset
, 2));
81 EXPECT_EQ(2U, offset
);
83 EXPECT_EQ(-2137980929, DE
.getSigned(&offset
, 4));
84 EXPECT_EQ(4U, offset
);
86 EXPECT_EQ(-9182558167379214336LL, DE
.getSigned(&offset
, 8));
87 EXPECT_EQ(8U, offset
);
90 TEST(DataExtractorTest
, Strings
) {
91 const char stringData
[] = "hellohello\0hello";
92 DataExtractor
DE(StringRef(stringData
, sizeof(stringData
)-1), false, 8);
95 EXPECT_EQ(stringData
, DE
.getCStr(&offset
));
96 EXPECT_EQ(11U, offset
);
97 EXPECT_EQ(nullptr, DE
.getCStr(&offset
));
98 EXPECT_EQ(11U, offset
);
100 DataExtractor::Cursor
C(0);
101 EXPECT_EQ(stringData
, DE
.getCStr(C
));
102 EXPECT_EQ(11U, C
.tell());
103 EXPECT_EQ(nullptr, DE
.getCStr(C
));
104 EXPECT_EQ(11U, C
.tell());
107 FailedWithMessage("no null terminated string at offset 0xb"));
110 TEST(DataExtractorTest
, LEB128
) {
111 DataExtractor
DE(StringRef(leb128data
, sizeof(leb128data
)-1), false, 8);
114 EXPECT_EQ(9382ULL, DE
.getULEB128(&offset
));
115 EXPECT_EQ(2U, offset
);
117 EXPECT_EQ(-7002LL, DE
.getSLEB128(&offset
));
118 EXPECT_EQ(2U, offset
);
120 DataExtractor
BDE(StringRef(bigleb128data
, sizeof(bigleb128data
)-1), false,8);
122 EXPECT_EQ(42218325750568106ULL, BDE
.getULEB128(&offset
));
123 EXPECT_EQ(8U, offset
);
125 EXPECT_EQ(-29839268287359830LL, BDE
.getSLEB128(&offset
));
126 EXPECT_EQ(8U, offset
);
129 TEST(DataExtractorTest
, LEB128_error
) {
130 DataExtractor
DE(StringRef("\x81"), false, 8);
132 EXPECT_EQ(0U, DE
.getULEB128(&Offset
));
133 EXPECT_EQ(0U, Offset
);
136 EXPECT_EQ(0U, DE
.getSLEB128(&Offset
));
137 EXPECT_EQ(0U, Offset
);
139 DataExtractor::Cursor
C(0);
140 EXPECT_EQ(0U, DE
.getULEB128(C
));
143 FailedWithMessage("unable to decode LEB128 at offset 0x00000000: "
144 "malformed uleb128, extends past end"));
146 C
= DataExtractor::Cursor(0);
147 EXPECT_EQ(0U, DE
.getSLEB128(C
));
150 FailedWithMessage("unable to decode LEB128 at offset 0x00000000: "
151 "malformed sleb128, extends past end"));
153 // Show non-zero offsets are reported appropriately.
154 C
= DataExtractor::Cursor(1);
155 EXPECT_EQ(0U, DE
.getULEB128(C
));
158 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
159 "malformed uleb128, extends past end"));
162 TEST(DataExtractorTest
, Cursor_tell
) {
163 DataExtractor
DE(StringRef("AB"), false, 8);
164 DataExtractor::Cursor
C(0);
165 // A successful read operation advances the cursor
166 EXPECT_EQ('A', DE
.getU8(C
));
167 EXPECT_EQ(1u, C
.tell());
169 // An unsuccessful one doesn't.
170 EXPECT_EQ(0u, DE
.getU16(C
));
171 EXPECT_EQ(1u, C
.tell());
173 // And neither do any subsequent operations.
174 EXPECT_EQ(0, DE
.getU8(C
));
175 EXPECT_EQ(1u, C
.tell());
177 consumeError(C
.takeError());
180 TEST(DataExtractorTest
, Cursor_seek
) {
181 DataExtractor::Cursor
C(5);
184 EXPECT_EQ(3u, C
.tell());
187 EXPECT_EQ(8u, C
.tell());
189 EXPECT_THAT_ERROR(C
.takeError(), Succeeded());
192 TEST(DataExtractorTest
, Cursor_takeError
) {
193 DataExtractor
DE(StringRef("AB"), false, 8);
194 DataExtractor::Cursor
C(0);
195 // Initially, the cursor is in the "success" state.
196 EXPECT_THAT_ERROR(C
.takeError(), Succeeded());
198 // It remains "success" after a successful read.
199 EXPECT_EQ('A', DE
.getU8(C
));
200 EXPECT_THAT_ERROR(C
.takeError(), Succeeded());
202 // An unsuccessful read sets the error state.
203 EXPECT_EQ(0u, DE
.getU32(C
));
204 EXPECT_THAT_ERROR(C
.takeError(), Failed());
206 // Once set the error sticks until explicitly cleared.
207 EXPECT_EQ(0u, DE
.getU32(C
));
208 EXPECT_EQ(0, DE
.getU8(C
));
209 EXPECT_THAT_ERROR(C
.takeError(), Failed());
211 // At which point reads can be succeed again.
212 EXPECT_EQ('B', DE
.getU8(C
));
213 EXPECT_THAT_ERROR(C
.takeError(), Succeeded());
216 TEST(DataExtractorTest
, Cursor_chaining
) {
217 DataExtractor
DE(StringRef("ABCD"), false, 8);
218 DataExtractor::Cursor
C(0);
220 // Multiple reads can be chained without trigerring any assertions.
221 EXPECT_EQ('A', DE
.getU8(C
));
222 EXPECT_EQ('B', DE
.getU8(C
));
223 EXPECT_EQ('C', DE
.getU8(C
));
224 EXPECT_EQ('D', DE
.getU8(C
));
225 // And the error checked at the end.
226 EXPECT_THAT_ERROR(C
.takeError(), Succeeded());
229 #if defined(GTEST_HAS_DEATH_TEST) && defined(_DEBUG) && \
230 LLVM_ENABLE_ABI_BREAKING_CHECKS
231 TEST(DataExtractorDeathTest
, Cursor
) {
232 DataExtractor
DE(StringRef("AB"), false, 8);
234 // Even an unused cursor must be checked for errors:
235 EXPECT_DEATH(DataExtractor::Cursor(0),
236 "Success values must still be checked prior to being destroyed");
239 auto C
= std::make_unique
<DataExtractor::Cursor
>(0);
240 EXPECT_EQ(0u, DE
.getU32(*C
));
241 // It must also be checked after an unsuccessful operation.
243 EXPECT_DEATH(C
.reset(), "unexpected end of data");
244 EXPECT_THAT_ERROR(C
->takeError(), Failed());
247 auto C
= std::make_unique
<DataExtractor::Cursor
>(0);
248 EXPECT_EQ('A', DE
.getU8(*C
));
249 // Same goes for a successful one.
252 "Success values must still be checked prior to being destroyed");
253 EXPECT_THAT_ERROR(C
->takeError(), Succeeded());
256 auto C
= std::make_unique
<DataExtractor::Cursor
>(0);
257 EXPECT_EQ('A', DE
.getU8(*C
));
258 EXPECT_EQ(0u, DE
.getU32(*C
));
259 // Even if a successful operation is followed by an unsuccessful one.
260 EXPECT_DEATH(C
.reset(), "unexpected end of data");
261 EXPECT_THAT_ERROR(C
->takeError(), Failed());
264 auto C
= std::make_unique
<DataExtractor::Cursor
>(0);
265 EXPECT_EQ(0u, DE
.getU32(*C
));
266 EXPECT_EQ(0, DE
.getU8(*C
));
267 // Even if an unsuccessful operation is followed by one that would normally
269 EXPECT_DEATH(C
.reset(), "unexpected end of data");
270 EXPECT_THAT_ERROR(C
->takeError(), Failed());
275 TEST(DataExtractorTest
, getU8_vector
) {
276 DataExtractor
DE(StringRef("AB"), false, 8);
277 DataExtractor::Cursor
C(0);
278 SmallVector
<uint8_t, 2> S
;
281 EXPECT_THAT_ERROR(C
.takeError(), Failed());
282 EXPECT_EQ("", toStringRef(S
));
285 EXPECT_THAT_ERROR(C
.takeError(), Succeeded());
286 EXPECT_EQ("AB", toStringRef(S
));
288 C
= DataExtractor::Cursor(0x47);
292 FailedWithMessage("offset 0x47 is beyond the end of data at 0x2"));
295 TEST(DataExtractorTest
, getU24
) {
296 DataExtractor
DE(StringRef("ABCD"), false, 8);
297 DataExtractor::Cursor
C(0);
299 EXPECT_EQ(0x414243u
, DE
.getU24(C
));
300 EXPECT_EQ(0u, DE
.getU24(C
));
301 EXPECT_EQ(3u, C
.tell());
302 EXPECT_THAT_ERROR(C
.takeError(), Failed());
305 TEST(DataExtractorTest
, skip
) {
306 DataExtractor
DE(StringRef("AB"), false, 8);
307 DataExtractor::Cursor
C(0);
310 EXPECT_THAT_ERROR(C
.takeError(), Failed());
311 EXPECT_EQ(0u, C
.tell());
314 EXPECT_THAT_ERROR(C
.takeError(), Succeeded());
315 EXPECT_EQ(2u, C
.tell());
318 TEST(DataExtractorTest
, eof
) {
319 DataExtractor
DE(StringRef("A"), false, 8);
320 DataExtractor::Cursor
C(0);
322 EXPECT_FALSE(DE
.eof(C
));
324 EXPECT_EQ(0, DE
.getU16(C
));
325 EXPECT_FALSE(DE
.eof(C
));
326 EXPECT_THAT_ERROR(C
.takeError(), Failed());
328 EXPECT_EQ('A', DE
.getU8(C
));
329 EXPECT_TRUE(DE
.eof(C
));
330 EXPECT_THAT_ERROR(C
.takeError(), Succeeded());
333 TEST(DataExtractorTest
, size
) {
334 uint8_t Data
[] = {'A', 'B', 'C', 'D'};
335 DataExtractor
DE1(StringRef(reinterpret_cast<char *>(Data
), sizeof(Data
)),
337 EXPECT_EQ(DE1
.size(), sizeof(Data
));
338 DataExtractor
DE2(ArrayRef
<uint8_t>(Data
), false, 8);
339 EXPECT_EQ(DE2
.size(), sizeof(Data
));
342 TEST(DataExtractorTest
, FixedLengthString
) {
343 const char Data
[] = "hello\x00\x00\x00world \thola\x00";
344 DataExtractor
DE(StringRef(Data
, sizeof(Data
)-1), false, 8);
347 // Test extracting too many bytes doesn't modify Offset and returns None.
348 Str
= DE
.getFixedLengthString(&Offset
, sizeof(Data
));
349 EXPECT_TRUE(Str
.empty());
350 EXPECT_EQ(Offset
, 0u);
352 // Test extracting a fixed width C string with trailing NULL characters.
353 Str
= DE
.getFixedLengthString(&Offset
, 8);
354 EXPECT_EQ(Offset
, 8u);
355 EXPECT_EQ(Str
.size(), 5u);
356 EXPECT_EQ(Str
, "hello");
357 // Test extracting a fixed width C string with trailing space and tab
359 Str
= DE
.getFixedLengthString(&Offset
, 8, " \t");
360 EXPECT_EQ(Offset
, 16u);
361 EXPECT_EQ(Str
.size(), 5u);
362 EXPECT_EQ(Str
, "world");
363 // Now extract a normal C string.
364 Str
= DE
.getCStrRef(&Offset
);
365 EXPECT_EQ(Str
.size(), 4u);
366 EXPECT_EQ(Str
, "hola");
370 TEST(DataExtractorTest
, GetBytes
) {
371 // Use data with an embedded NULL character for good measure.
372 const char Data
[] = "\x01\x02\x00\x04";
373 StringRef
Bytes(Data
, sizeof(Data
)-1);
374 DataExtractor
DE(Bytes
, false, 8);
377 // Test extracting too many bytes doesn't modify Offset and returns None.
378 Str
= DE
.getBytes(&Offset
, sizeof(Data
));
379 EXPECT_TRUE(Str
.empty());
380 EXPECT_EQ(Offset
, 0u);
381 // Test extracting 4 bytes from the stream.
382 Str
= DE
.getBytes(&Offset
, 4);
383 EXPECT_EQ(Offset
, 4u);
384 EXPECT_EQ(Str
.size(), 4u);
385 EXPECT_EQ(Str
, Bytes
);
387 DataExtractor::Cursor
C(0);
388 EXPECT_EQ(StringRef("\x01\x02"), DE
.getBytes(C
, 2));
389 EXPECT_EQ(StringRef("\x00\x04", 2), DE
.getBytes(C
, 2));
390 EXPECT_EQ(StringRef(), DE
.getBytes(C
, 2));
391 EXPECT_EQ(StringRef(), DE
.getBytes(C
, 2));
392 EXPECT_EQ(4u, C
.tell());
393 EXPECT_THAT_ERROR(C
.takeError(), Failed());