File: DumpDataExtractorTest.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (390 lines) | stat: -rw-r--r-- 18,019 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//===-- DataDumpExtractorTest.cpp -----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "lldb/Core/DumpDataExtractor.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Endian.h"
#include "lldb/Utility/StreamString.h"
#include "gtest/gtest.h"
#include <complex>
#include <limits>

using namespace lldb;
using namespace lldb_private;

static void TestDumpWithAddress(uint64_t base_addr, size_t item_count,
                                llvm::StringRef expected) {
  std::vector<uint8_t> data{0x11, 0x22};
  StreamString result;
  DataBufferHeap dumpbuffer(&data[0], data.size());
  DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(),
                          endian::InlHostByteOrder(), /*addr_size=*/4);

  DumpDataExtractor(extractor, &result, 0, lldb::Format::eFormatHex,
                    /*item_byte_size=*/1, item_count,
                    /*num_per_line=*/1, base_addr, 0, 0);
  ASSERT_EQ(expected, result.GetString());
}

TEST(DumpDataExtractorTest, BaseAddress) {
  TestDumpWithAddress(0x12341234, 1, "0x12341234: 0x11");
  TestDumpWithAddress(LLDB_INVALID_ADDRESS, 1, "0x11");
  TestDumpWithAddress(0x12341234, 2, "0x12341234: 0x11\n0x12341235: 0x22");
  TestDumpWithAddress(LLDB_INVALID_ADDRESS, 2, "0x11\n0x22");
}

static void TestDumpWithOffset(offset_t start_offset,
                               llvm::StringRef expected) {
  std::vector<uint8_t> data{0x11, 0x22, 0x33};
  StreamString result;
  DataBufferHeap dumpbuffer(&data[0], data.size());
  DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(),
                          endian::InlHostByteOrder(), /*addr_size=*/4);

  DumpDataExtractor(extractor, &result, start_offset, lldb::Format::eFormatHex,
                    /*item_byte_size=*/1, /*item_count=*/data.size(),
                    /*num_per_line=*/data.size(), /*base_addr=*/0, 0, 0);
  ASSERT_EQ(expected, result.GetString());
}

TEST(DumpDataExtractorTest, StartOffset) {
  TestDumpWithOffset(0, "0x00000000: 0x11 0x22 0x33");
  // The offset applies to the DataExtractor, not the address used when
  // formatting.
  TestDumpWithOffset(1, "0x00000000: 0x22 0x33");
  // If the offset is outside the DataExtractor's range we do nothing.
  TestDumpWithOffset(3, "");
}

TEST(DumpDataExtractorTest, NullStream) {
  // We don't do any work if there is no output stream.
  uint8_t c = 0x11;
  StreamString result;
  DataBufferHeap dumpbuffer(&c, 0);
  DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(),
                          endian::InlHostByteOrder(), /*addr_size=*/4);

  DumpDataExtractor(extractor, nullptr, 0, lldb::Format::eFormatHex,
                    /*item_byte_size=*/1, /*item_count=*/1,
                    /*num_per_line=*/1, /*base_addr=*/0, 0, 0);
  ASSERT_EQ("", result.GetString());
}

static void TestDumpImpl(const void *data, size_t data_size,
                         size_t item_byte_size, size_t item_count,
                         size_t num_per_line, uint64_t base_addr,
                         lldb::Format format, llvm::StringRef expected) {
  StreamString result;
  DataBufferHeap dumpbuffer(data, data_size);
  DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(),
                          endian::InlHostByteOrder(),
                          /*addr_size=*/4);
  DumpDataExtractor(extractor, &result, 0, format, item_byte_size, item_count,
                    num_per_line, base_addr, 0, 0);
  ASSERT_EQ(expected, result.GetString());
}

template <typename T>
static void TestDump(T data, lldb::Format format, llvm::StringRef expected) {
  TestDumpImpl(&data, sizeof(T), sizeof(T), 1, 1, LLDB_INVALID_ADDRESS, format,
               expected);
}

static void TestDump(llvm::StringRef str, lldb::Format format,
                     llvm::StringRef expected) {
  TestDumpImpl(str.bytes_begin(),
               // +1 to include the NULL char as the last byte
               str.size() + 1, str.size() + 1, 1, 1, LLDB_INVALID_ADDRESS,
               format, expected);
}

template <typename T>
static void TestDump(const std::vector<T> data, lldb::Format format,
                     llvm::StringRef expected) {
  size_t sz_bytes = data.size() * sizeof(T);
  TestDumpImpl(&data[0], sz_bytes, sz_bytes, data.size(), 1,
               LLDB_INVALID_ADDRESS, format, expected);
}

TEST(DumpDataExtractorTest, Formats) {
  TestDump<uint8_t>(1, lldb::eFormatDefault, "0x01");
  TestDump<uint8_t>(1, lldb::eFormatBoolean, "true");
  TestDump<uint8_t>(0xAA, lldb::eFormatBinary, "0b10101010");
  TestDump<uint8_t>(1, lldb::eFormatBytes, "01");
  TestDump<uint8_t>(1, lldb::eFormatBytesWithASCII, "01  .");
  TestDump('?', lldb::eFormatChar, "'?'");
  TestDump('\x1A', lldb::eFormatCharPrintable, ".");
  TestDump('#', lldb::eFormatCharPrintable, "#");
  TestDump(std::complex<float>(1.2, 3.4), lldb::eFormatComplex, "1.2 + 3.4i");
  TestDump(std::complex<double>(4.5, 6.7), lldb::eFormatComplex, "4.5 + 6.7i");

  // long double is not tested here because for some platforms we treat it as 10
  // bytes when the compiler allocates 16 bytes of space for it. (see
  // DataExtractor::GetLongDouble) Meaning that when we extract the second one,
  // it gets the wrong value (it's 6 bytes off). You could manually construct a
  // set of bytes to match the 10 byte format but then if the test runs on a
  // machine where we don't use 10 it'll break.

  // Test printable characters.
  TestDump(llvm::StringRef("aardvark"), lldb::Format::eFormatCString,
           "\"aardvark\"");
  // Test unprintable characters.
  TestDump(llvm::StringRef("\xcf\xfa\xed\xfe\f"), lldb::Format::eFormatCString,
           "\"\\xcf\\xfa\\xed\\xfe\\f\"");
  // Test a mix of printable and unprintable characters.
  TestDump(llvm::StringRef("\xcf\xfa\ffoo"), lldb::Format::eFormatCString,
           "\"\\xcf\\xfa\\ffoo\"");

  TestDump<uint16_t>(99, lldb::Format::eFormatDecimal, "99");
  // Just prints as a signed integer.
  TestDump(-1, lldb::Format::eFormatEnum, "-1");
  TestDump(0xcafef00d, lldb::Format::eFormatHex, "0xcafef00d");
  TestDump(0xcafef00d, lldb::Format::eFormatHexUppercase, "0xCAFEF00D");
  TestDump(0.456, lldb::Format::eFormatFloat, "0.45600000000000002");
  TestDump(9, lldb::Format::eFormatOctal, "011");
  // Chars packed into an integer.
  TestDump<uint32_t>(0x4C4C4442, lldb::Format::eFormatOSType, "'LLDB'");
  // Unicode8 doesn't have a specific formatter.
  TestDump<uint8_t>(0x34, lldb::Format::eFormatUnicode8, "0x34");
  TestDump<uint16_t>(0x1122, lldb::Format::eFormatUnicode16, "U+1122");
  TestDump<uint32_t>(0x12345678, lldb::Format::eFormatUnicode32,
                     "U+0x12345678");
  TestDump<unsigned int>(654321, lldb::Format::eFormatUnsigned, "654321");
  // This pointer is printed based on the size of uint64_t, so the test is the
  // same for 32/64 bit host.
  TestDump<uint64_t>(0x4444555566667777, lldb::Format::eFormatPointer,
                     "0x4444555566667777");

  TestDump(std::vector<char>{'A', '\x01', 'C'},
           lldb::Format::eFormatVectorOfChar, "{A\\x01C}");
  TestDump(std::vector<int8_t>{0, -1, std::numeric_limits<int8_t>::max()},
           lldb::Format::eFormatVectorOfSInt8, "{0 -1 127}");
  TestDump(std::vector<uint8_t>{12, 0xFF, 34},
           lldb::Format::eFormatVectorOfUInt8, "{0x0c 0xff 0x22}");
  TestDump(std::vector<int16_t>{-1, 1234, std::numeric_limits<int16_t>::max()},
           lldb::Format::eFormatVectorOfSInt16, "{-1 1234 32767}");
  TestDump(std::vector<uint16_t>{0xffff, 0xabcd, 0x1234},
           lldb::Format::eFormatVectorOfUInt16, "{0xffff 0xabcd 0x1234}");
  TestDump(std::vector<int32_t>{0, -1, std::numeric_limits<int32_t>::max()},
           lldb::Format::eFormatVectorOfSInt32, "{0 -1 2147483647}");
  TestDump(std::vector<uint32_t>{0, 0xffffffff, 0x1234abcd},
           lldb::Format::eFormatVectorOfUInt32,
           "{0x00000000 0xffffffff 0x1234abcd}");
  TestDump(std::vector<int64_t>{0, -1, std::numeric_limits<int64_t>::max()},
           lldb::Format::eFormatVectorOfSInt64, "{0 -1 9223372036854775807}");
  TestDump(std::vector<uint64_t>{0, 0xaaaabbbbccccdddd},
           lldb::Format::eFormatVectorOfUInt64,
           "{0x0000000000000000 0xaaaabbbbccccdddd}");

  // See half2float for format details.
  // Test zeroes.
  TestDump(std::vector<uint16_t>{0x0000, 0x8000},
           lldb::Format::eFormatVectorOfFloat16, "{0 -0}");
  // Some subnormal numbers.
  TestDump(std::vector<uint16_t>{0x0001, 0x8001},
           lldb::Format::eFormatVectorOfFloat16, "{5.9605E-8 -5.9605E-8}");
  // A full mantisse and empty expontent.
  TestDump(std::vector<uint16_t>{0x83ff, 0x03ff},
           lldb::Format::eFormatVectorOfFloat16, "{-6.0976E-5 6.0976E-5}");
  // Some normal numbers.
  TestDump(std::vector<uint16_t>{0b0100001001001000},
           lldb::Format::eFormatVectorOfFloat16, "{3.1406}");
  // Largest and smallest normal number.
  TestDump(std::vector<uint16_t>{0x0400, 0x7bff},
           lldb::Format::eFormatVectorOfFloat16, "{6.1035E-5 65504}");
  TestDump(std::vector<uint16_t>{0xabcd, 0x1234},
           lldb::Format::eFormatVectorOfFloat16, "{-0.060944 7.5722E-4}");

  // quiet/signaling NaNs.
  TestDump(std::vector<uint16_t>{0xffff, 0xffc0, 0x7fff, 0x7fc0},
           lldb::Format::eFormatVectorOfFloat16, "{NaN NaN NaN NaN}");
  // +/-Inf.
  TestDump(std::vector<uint16_t>{0xfc00, 0x7c00},
           lldb::Format::eFormatVectorOfFloat16, "{-Inf +Inf}");

  TestDump(std::vector<float>{std::numeric_limits<float>::min(),
                              std::numeric_limits<float>::max()},
           lldb::Format::eFormatVectorOfFloat32,
           "{1.17549435E-38 3.40282347E+38}");
  TestDump(std::vector<float>{std::numeric_limits<float>::quiet_NaN(),
                              std::numeric_limits<float>::signaling_NaN(),
                              -std::numeric_limits<float>::quiet_NaN(),
                              -std::numeric_limits<float>::signaling_NaN()},
           lldb::Format::eFormatVectorOfFloat32, "{NaN NaN NaN NaN}");
  TestDump(std::vector<double>{std::numeric_limits<double>::min(),
                               std::numeric_limits<double>::max()},
           lldb::Format::eFormatVectorOfFloat64,
           "{2.2250738585072014E-308 1.7976931348623157E+308}");
  TestDump(
      std::vector<double>{
          std::numeric_limits<double>::quiet_NaN(),
          std::numeric_limits<double>::signaling_NaN(),
          -std::numeric_limits<double>::quiet_NaN(),
          -std::numeric_limits<double>::signaling_NaN(),
      },
      lldb::Format::eFormatVectorOfFloat64, "{NaN NaN NaN NaN}");

  // Not sure we can rely on having uint128_t everywhere so emulate with
  // uint64_t.
  TestDump(
      std::vector<uint64_t>{0x1, 0x1111222233334444, 0xaaaabbbbccccdddd, 0x0},
      lldb::Format::eFormatVectorOfUInt128,
      "{0x11112222333344440000000000000001 "
      "0x0000000000000000aaaabbbbccccdddd}");

  TestDump(std::vector<int>{2, 4}, lldb::Format::eFormatComplexInteger,
           "2 + 4i");

  // Without an execution context this just prints the pointer on its own.
  TestDump<uint32_t>(0x11223344, lldb::Format::eFormatAddressInfo,
                     "0x11223344");

  // Input not written in hex form because that requires C++17.
  TestDump<float>(10, lldb::Format::eFormatHexFloat, "0x1.4p3");
  TestDump<double>(10, lldb::Format::eFormatHexFloat, "0x1.4p3");
  // long double not supported, see ItemByteSizeErrors.

  // Can't disassemble without an execution context.
  TestDump<uint32_t>(0xcafef00d, lldb::Format::eFormatInstruction,
                     "invalid target");

  // Has no special handling, intended for use elsewhere.
  TestDump<int>(99, lldb::Format::eFormatVoid, "0x00000063");
}

TEST(DumpDataExtractorTest, FormatCharArray) {
  // Unlike the other formats, charArray isn't 1 array of N chars.
  // It must be passed as N chars of 1 byte each.
  // (eFormatVectorOfChar does this swap for you)
  std::vector<char> data{'A', '\x01', '#'};
  StreamString result;
  DataBufferHeap dumpbuffer(&data[0], data.size());
  DataExtractor extractor(dumpbuffer.GetBytes(), dumpbuffer.GetByteSize(),
                          endian::InlHostByteOrder(), /*addr_size=*/4);

  DumpDataExtractor(extractor, &result, 0, lldb::Format::eFormatCharArray,
                    /*item_byte_size=*/1,
                    /*item_count=*/data.size(),
                    /*num_per_line=*/data.size(), 0, 0, 0);
  ASSERT_EQ("0x00000000: A\\x01#", result.GetString());

  result.Clear();
  DumpDataExtractor(extractor, &result, 0, lldb::Format::eFormatCharArray, 1,
                    data.size(), 1, 0, 0, 0);
  // ASSERT macro thinks the split strings are multiple arguments so make a var.
  const char *expected = "0x00000000: A\n"
                         "0x00000001: \\x01\n"
                         "0x00000002: #";
  ASSERT_EQ(expected, result.GetString());
}

template <typename T>
void TestDumpMultiLine(std::vector<T> data, lldb::Format format,
                       size_t num_per_line, llvm::StringRef expected) {
  size_t sz_bytes = data.size() * sizeof(T);
  TestDumpImpl(&data[0], sz_bytes, data.size(), sz_bytes, num_per_line,
               0x80000000, format, expected);
}

template <typename T>
void TestDumpMultiLine(const T *data, size_t num_items, lldb::Format format,
                       size_t num_per_line, llvm::StringRef expected) {
  TestDumpImpl(data, sizeof(T) * num_items, sizeof(T), num_items, num_per_line,
               0x80000000, format, expected);
}

TEST(DumpDataExtractorTest, MultiLine) {
  // A vector counts as 1 item regardless of size.
  TestDumpMultiLine(std::vector<uint8_t>{0x11},
                    lldb::Format::eFormatVectorOfUInt8, 1,
                    "0x80000000: {0x11}");
  TestDumpMultiLine(std::vector<uint8_t>{0x11, 0x22},
                    lldb::Format::eFormatVectorOfUInt8, 1,
                    "0x80000000: {0x11 0x22}");

  // If you have multiple vectors then that's multiple items.
  // Here we say that these 2 bytes are actually 2 1 byte vectors.
  const std::vector<uint8_t> vector_data{0x11, 0x22};
  TestDumpMultiLine(vector_data.data(), 2, lldb::Format::eFormatVectorOfUInt8,
                    1, "0x80000000: {0x11}\n0x80000001: {0x22}");

  // Single value formats can span multiple lines.
  const std::vector<uint8_t> bytes{0x11, 0x22, 0x33};
  const char *expected_bytes_3_line = "0x80000000: 0x11\n"
                                      "0x80000001: 0x22\n"
                                      "0x80000002: 0x33";
  TestDumpMultiLine(bytes.data(), bytes.size(), lldb::Format::eFormatHex, 1,
                    expected_bytes_3_line);

  // Lines may not have the full number of items.
  TestDumpMultiLine(bytes.data(), bytes.size(), lldb::Format::eFormatHex, 4,
                    "0x80000000: 0x11 0x22 0x33");
  const char *expected_bytes_2_line = "0x80000000: 0x11 0x22\n"
                                      "0x80000002: 0x33";
  TestDumpMultiLine(bytes.data(), bytes.size(), lldb::Format::eFormatHex, 2,
                    expected_bytes_2_line);

  // The line address accounts for item sizes other than 1 byte.
  const std::vector<uint16_t> shorts{0x1111, 0x2222, 0x3333};
  const char *expected_shorts_2_line = "0x80000000: 0x1111 0x2222\n"
                                       "0x80000004: 0x3333";
  TestDumpMultiLine(shorts.data(), shorts.size(), lldb::Format::eFormatHex, 2,
                    expected_shorts_2_line);

  // The ascii column is positioned using the maximum line length.
  const std::vector<char> chars{'L', 'L', 'D', 'B'};
  const char *expected_chars_2_lines = "0x80000000: 4c 4c 44  LLD\n"
                                       "0x80000003: 42        B";
  TestDumpMultiLine(chars.data(), chars.size(),
                    lldb::Format::eFormatBytesWithASCII, 3,
                    expected_chars_2_lines);
}

void TestDumpWithItemByteSize(size_t item_byte_size, lldb::Format format,
                              llvm::StringRef expected) {
  // We won't be reading this data so anything will do.
  uint8_t dummy = 0;
  TestDumpImpl(&dummy, 1, item_byte_size, 1, 1, LLDB_INVALID_ADDRESS, format,
               expected);
}

TEST(DumpDataExtractorTest, ItemByteSizeErrors) {
  TestDumpWithItemByteSize(
      16, lldb::Format::eFormatBoolean,
      "error: unsupported byte size (16) for boolean format");
  TestDumpWithItemByteSize(21, lldb::Format::eFormatChar,
                           "error: unsupported byte size (21) for char format");
  TestDumpWithItemByteSize(
      18, lldb::Format::eFormatComplexInteger,
      "error: unsupported byte size (18) for complex integer format");

  // The code uses sizeof(long double) for these checks. This changes by host
  // but we know it won't be >16.
  TestDumpWithItemByteSize(
      34, lldb::Format::eFormatComplex,
      "error: unsupported byte size (34) for complex float format");
  TestDumpWithItemByteSize(
      18, lldb::Format::eFormatFloat,
      "error: unsupported byte size (18) for float format");

  // We want sizes to exactly match one of float/double.
  TestDumpWithItemByteSize(
      14, lldb::Format::eFormatComplex,
      "error: unsupported byte size (14) for complex float format");
  TestDumpWithItemByteSize(3, lldb::Format::eFormatFloat,
                           "error: unsupported byte size (3) for float format");

  // We only allow float and double size.
  TestDumpWithItemByteSize(
      1, lldb::Format::eFormatHexFloat,
      "error: unsupported byte size (1) for hex float format");
  TestDumpWithItemByteSize(
      17, lldb::Format::eFormatHexFloat,
      "error: unsupported byte size (17) for hex float format");
}