[AArch64,ELF] Restrict MOVZ/MOVK to non-PIC large code model (#70178)
[llvm-project.git] / lldb / unittests / Utility / DataExtractorTest.cpp
blob536e69755d17bb61c154e37baf86737627e8b3ca
1 //===-- DataExtractorTest.cpp ---------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include "lldb/Utility/DataExtractor.h"
13 using namespace lldb_private;
15 TEST(DataExtractorTest, GetBitfield) {
16 uint8_t buffer[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
17 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
18 sizeof(void *));
19 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig, sizeof(void *));
21 lldb::offset_t offset;
23 offset = 0;
24 ASSERT_EQ(buffer[1], LE.GetMaxU64Bitfield(&offset, sizeof(buffer), 8, 8));
25 offset = 0;
26 ASSERT_EQ(buffer[1], BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 8, 8));
27 offset = 0;
28 ASSERT_EQ(static_cast<uint64_t>(0xEFCDAB8967452301),
29 LE.GetMaxU64Bitfield(&offset, sizeof(buffer), 64, 0));
30 offset = 0;
31 ASSERT_EQ(static_cast<uint64_t>(0x0123456789ABCDEF),
32 BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 64, 0));
33 offset = 0;
34 ASSERT_EQ(static_cast<uint64_t>(0x01234567),
35 BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 32, 0));
36 offset = 0;
37 ASSERT_EQ(static_cast<uint64_t>(0x012345678),
38 BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 36, 0));
40 offset = 0;
41 ASSERT_EQ(int8_t(buffer[1]),
42 LE.GetMaxS64Bitfield(&offset, sizeof(buffer), 8, 8));
43 offset = 0;
44 ASSERT_EQ(int8_t(buffer[1]),
45 BE.GetMaxS64Bitfield(&offset, sizeof(buffer), 8, 8));
46 offset = 0;
47 ASSERT_EQ(static_cast<int64_t>(0xEFCDAB8967452301),
48 LE.GetMaxS64Bitfield(&offset, sizeof(buffer), 64, 0));
49 offset = 0;
50 ASSERT_EQ(static_cast<int64_t>(0x0123456789ABCDEF),
51 BE.GetMaxS64Bitfield(&offset, sizeof(buffer), 64, 0));
54 TEST(DataExtractorTest, PeekData) {
55 uint8_t buffer[] = {0x01, 0x02, 0x03, 0x04};
56 DataExtractor E(buffer, sizeof buffer, lldb::eByteOrderLittle, 4);
58 EXPECT_EQ(buffer + 0, E.PeekData(0, 0));
59 EXPECT_EQ(buffer + 0, E.PeekData(0, 4));
60 EXPECT_EQ(nullptr, E.PeekData(0, 5));
62 EXPECT_EQ(buffer + 2, E.PeekData(2, 0));
63 EXPECT_EQ(buffer + 2, E.PeekData(2, 2));
64 EXPECT_EQ(nullptr, E.PeekData(2, 3));
66 EXPECT_EQ(buffer + 4, E.PeekData(4, 0));
67 EXPECT_EQ(nullptr, E.PeekData(4, 1));
70 TEST(DataExtractorTest, GetCStr) {
71 uint8_t buffer[] = {'X', 'f', 'o', 'o', '\0'};
72 DataExtractor E(buffer, sizeof buffer, lldb::eByteOrderLittle, 4);
74 lldb::offset_t offset = 1;
75 EXPECT_STREQ("foo", E.GetCStr(&offset));
76 EXPECT_EQ(5U, offset);
79 TEST(DataExtractorTest, GetCStrEmpty) {
80 uint8_t buffer[] = {'X', '\0'};
81 DataExtractor E(buffer, sizeof buffer, lldb::eByteOrderLittle, 4);
83 lldb::offset_t offset = 1;
84 EXPECT_STREQ("", E.GetCStr(&offset));
85 EXPECT_EQ(2U, offset);
88 TEST(DataExtractorTest, GetCStrUnterminated) {
89 uint8_t buffer[] = {'X', 'f', 'o', 'o'};
90 DataExtractor E(buffer, sizeof buffer, lldb::eByteOrderLittle, 4);
92 lldb::offset_t offset = 1;
93 EXPECT_EQ(nullptr, E.GetCStr(&offset));
94 EXPECT_EQ(1U, offset);
97 TEST(DataExtractorTest, GetCStrAtEnd) {
98 uint8_t buffer[] = {'X'};
99 DataExtractor E(buffer, sizeof buffer, lldb::eByteOrderLittle, 4);
101 lldb::offset_t offset = 1;
102 EXPECT_EQ(nullptr, E.GetCStr(&offset));
103 EXPECT_EQ(1U, offset);
106 TEST(DataExtractorTest, GetCStrAtNullOffset) {
107 uint8_t buffer[] = {'f', 'o', 'o', '\0'};
108 DataExtractor E(buffer, sizeof buffer, lldb::eByteOrderLittle, 4);
110 lldb::offset_t offset = 0;
111 EXPECT_STREQ("foo", E.GetCStr(&offset));
112 EXPECT_EQ(4U, offset);
115 TEST(DataExtractorTest, UncommonAddressSize) {
116 uint8_t buffer[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
117 DataExtractor E2(buffer, sizeof buffer, lldb::eByteOrderLittle, 2);
118 DataExtractor E5(buffer, sizeof buffer, lldb::eByteOrderLittle, 5);
119 DataExtractor E7(buffer, sizeof buffer, lldb::eByteOrderLittle, 7);
121 lldb::offset_t offset;
123 // Test 2-byte addresses (for AVR).
124 offset = 0;
125 EXPECT_EQ(0x0201U, E2.GetMaxU64(&offset, 2));
126 EXPECT_EQ(2U, offset);
127 offset = 0;
128 EXPECT_EQ(0x0201U, E2.GetAddress(&offset));
129 EXPECT_EQ(2U, offset);
131 // Test 5-byte addresses.
132 offset = 0;
133 EXPECT_EQ(0x030201U, E5.GetMaxU64(&offset, 3));
134 EXPECT_EQ(3U, offset);
135 offset = 3;
136 EXPECT_EQ(0x0807060504U, E5.GetAddress(&offset));
137 EXPECT_EQ(8U, offset);
139 // Test 7-byte addresses.
140 offset = 0;
141 EXPECT_EQ(0x0504030201U, E7.GetMaxU64(&offset, 5));
142 EXPECT_EQ(5U, offset);
143 offset = 0;
144 EXPECT_EQ(0x07060504030201U, E7.GetAddress(&offset));
145 EXPECT_EQ(7U, offset);
148 TEST(DataExtractorTest, GetMaxU64) {
149 uint8_t buffer[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
150 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
151 sizeof(void *));
152 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig, sizeof(void *));
154 lldb::offset_t offset;
156 // Check with the minimum allowed byte size.
157 offset = 0;
158 EXPECT_EQ(0x01U, LE.GetMaxU64(&offset, 1));
159 EXPECT_EQ(1U, offset);
160 offset = 0;
161 EXPECT_EQ(0x01U, BE.GetMaxU64(&offset, 1));
162 EXPECT_EQ(1U, offset);
164 // Check with a non-zero offset.
165 offset = 1;
166 EXPECT_EQ(0x0302U, LE.GetMaxU64(&offset, 2));
167 EXPECT_EQ(3U, offset);
168 offset = 1;
169 EXPECT_EQ(0x0203U, BE.GetMaxU64(&offset, 2));
170 EXPECT_EQ(3U, offset);
172 // Check with the byte size not being a multiple of 2.
173 offset = 0;
174 EXPECT_EQ(0x07060504030201U, LE.GetMaxU64(&offset, 7));
175 EXPECT_EQ(7U, offset);
176 offset = 0;
177 EXPECT_EQ(0x01020304050607U, BE.GetMaxU64(&offset, 7));
178 EXPECT_EQ(7U, offset);
180 // Check with the maximum allowed byte size.
181 offset = 0;
182 EXPECT_EQ(0x0807060504030201U, LE.GetMaxU64(&offset, 8));
183 EXPECT_EQ(8U, offset);
184 offset = 0;
185 EXPECT_EQ(0x0102030405060708U, BE.GetMaxU64(&offset, 8));
186 EXPECT_EQ(8U, offset);
189 TEST(DataExtractorTest, GetMaxS64) {
190 uint8_t buffer[] = {0x01, 0x02, 0x83, 0x04, 0x05, 0x06, 0x07, 0x08};
191 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
192 sizeof(void *));
193 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig, sizeof(void *));
195 lldb::offset_t offset;
197 // Check with the minimum allowed byte size.
198 offset = 0;
199 EXPECT_EQ(0x01, LE.GetMaxS64(&offset, 1));
200 EXPECT_EQ(1U, offset);
201 offset = 0;
202 EXPECT_EQ(0x01, BE.GetMaxS64(&offset, 1));
203 EXPECT_EQ(1U, offset);
205 // Check that sign extension works correctly.
206 offset = 0;
207 int64_t value = LE.GetMaxS64(&offset, 3);
208 EXPECT_EQ(0xffffffffff830201U, *reinterpret_cast<uint64_t *>(&value));
209 EXPECT_EQ(3U, offset);
210 offset = 2;
211 value = BE.GetMaxS64(&offset, 3);
212 EXPECT_EQ(0xffffffffff830405U, *reinterpret_cast<uint64_t *>(&value));
213 EXPECT_EQ(5U, offset);
215 // Check with the maximum allowed byte size.
216 offset = 0;
217 EXPECT_EQ(0x0807060504830201, LE.GetMaxS64(&offset, 8));
218 EXPECT_EQ(8U, offset);
219 offset = 0;
220 EXPECT_EQ(0x0102830405060708, BE.GetMaxS64(&offset, 8));
221 EXPECT_EQ(8U, offset);
224 TEST(DataExtractorTest, GetMaxU64_unchecked) {
225 uint8_t buffer[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
226 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
227 sizeof(void *));
228 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig, sizeof(void *));
230 lldb::offset_t offset;
232 // Check with the minimum allowed byte size.
233 offset = 0;
234 EXPECT_EQ(0x01U, LE.GetMaxU64_unchecked(&offset, 1));
235 EXPECT_EQ(1U, offset);
236 offset = 0;
237 EXPECT_EQ(0x01U, BE.GetMaxU64_unchecked(&offset, 1));
238 EXPECT_EQ(1U, offset);
240 // Check with a non-zero offset.
241 offset = 1;
242 EXPECT_EQ(0x0302U, LE.GetMaxU64_unchecked(&offset, 2));
243 EXPECT_EQ(3U, offset);
244 offset = 1;
245 EXPECT_EQ(0x0203U, BE.GetMaxU64_unchecked(&offset, 2));
246 EXPECT_EQ(3U, offset);
248 // Check with the byte size not being a multiple of 2.
249 offset = 0;
250 EXPECT_EQ(0x07060504030201U, LE.GetMaxU64_unchecked(&offset, 7));
251 EXPECT_EQ(7U, offset);
252 offset = 0;
253 EXPECT_EQ(0x01020304050607U, BE.GetMaxU64_unchecked(&offset, 7));
254 EXPECT_EQ(7U, offset);
256 // Check with the maximum allowed byte size.
257 offset = 0;
258 EXPECT_EQ(0x0807060504030201U, LE.GetMaxU64_unchecked(&offset, 8));
259 EXPECT_EQ(8U, offset);
260 offset = 0;
261 EXPECT_EQ(0x0102030405060708U, BE.GetMaxU64_unchecked(&offset, 8));
262 EXPECT_EQ(8U, offset);
265 TEST(DataExtractorTest, GetSLEB128_bit63) {
266 uint8_t buffer[] = {0xff, 0x80, 0xff, 0x80, 0xff, 0x80, 0xff, 0x80, 0x7f};
268 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
269 sizeof(void *));
270 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig, sizeof(void *));
272 lldb::offset_t offset;
274 int64_t expected =
275 0b1111111100000001111111000000011111110000000111111100000001111111;
276 offset = 0;
277 EXPECT_EQ(expected, LE.GetSLEB128(&offset));
278 EXPECT_EQ(9U, offset);
279 offset = 0;
280 EXPECT_EQ(expected, BE.GetSLEB128(&offset));
281 EXPECT_EQ(9U, offset);
284 TEST(DataExtractorTest, GetULEB128_bit63) {
285 uint8_t buffer[] = {0xff, 0x80, 0xff, 0x80, 0xff, 0x80, 0xff, 0x80, 0x7f};
287 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
288 sizeof(void *));
289 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig, sizeof(void *));
291 lldb::offset_t offset;
293 uint64_t expected =
294 0b0111111100000001111111000000011111110000000111111100000001111111;
295 offset = 0;
296 EXPECT_EQ(expected, LE.GetULEB128(&offset));
297 EXPECT_EQ(9U, offset);
298 offset = 0;
299 EXPECT_EQ(expected, BE.GetULEB128(&offset));
300 EXPECT_EQ(9U, offset);
303 TEST(DataExtractorTest, GetFloat) {
304 float expected = 4.0f;
305 lldb::offset_t offset;
308 uint8_t buffer[] = {0x00, 0x00, 0x80, 0x40};
309 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
310 sizeof(void *));
312 offset = 0;
313 EXPECT_DOUBLE_EQ(expected, LE.GetFloat(&offset));
314 EXPECT_EQ(4U, offset);
318 uint8_t buffer[] = {0x40, 0x80, 0x00, 0x00};
319 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig,
320 sizeof(void *));
321 offset = 0;
322 EXPECT_DOUBLE_EQ(expected, BE.GetFloat(&offset));
323 EXPECT_EQ(4U, offset);
327 TEST(DataExtractorTest, GetFloatUnaligned) {
328 float expected = 4.0f;
329 lldb::offset_t offset;
332 uint8_t buffer[] = {0x00, 0x00, 0x00, 0x80, 0x40};
333 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
334 sizeof(void *));
336 offset = 1;
337 EXPECT_DOUBLE_EQ(expected, LE.GetFloat(&offset));
338 EXPECT_EQ(5U, offset);
342 uint8_t buffer[] = {0x00, 0x40, 0x80, 0x00, 0x00};
343 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig,
344 sizeof(void *));
345 offset = 1;
346 EXPECT_DOUBLE_EQ(expected, BE.GetFloat(&offset));
347 EXPECT_EQ(5U, offset);
351 TEST(DataExtractorTest, GetDouble) {
352 if (sizeof(double) != 8)
353 return;
355 double expected = 4.0f;
356 lldb::offset_t offset;
359 uint8_t buffer[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40};
360 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
361 sizeof(void *));
363 offset = 0;
364 EXPECT_DOUBLE_EQ(expected, LE.GetDouble(&offset));
365 EXPECT_EQ(8U, offset);
369 uint8_t buffer[] = {0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
370 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig,
371 sizeof(void *));
372 offset = 0;
373 EXPECT_DOUBLE_EQ(expected, BE.GetDouble(&offset));
374 EXPECT_EQ(8U, offset);
378 TEST(DataExtractorTest, GetDoubleUnaligned) {
379 if (sizeof(double) != 8)
380 return;
382 float expected = 4.0f;
383 lldb::offset_t offset;
386 uint8_t buffer[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40};
387 DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
388 sizeof(void *));
390 offset = 1;
391 EXPECT_DOUBLE_EQ(expected, LE.GetDouble(&offset));
392 EXPECT_EQ(9U, offset);
396 uint8_t buffer[] = {0x00, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
397 DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig,
398 sizeof(void *));
399 offset = 1;
400 EXPECT_DOUBLE_EQ(expected, BE.GetDouble(&offset));
401 EXPECT_EQ(9U, offset);