1 //===-- DataExtractor.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 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Utility/DataExtractor.h" 10 11 #include "lldb/lldb-defines.h" 12 #include "lldb/lldb-enumerations.h" 13 #include "lldb/lldb-forward.h" 14 #include "lldb/lldb-types.h" 15 16 #include "lldb/Utility/DataBuffer.h" 17 #include "lldb/Utility/DataBufferHeap.h" 18 #include "lldb/Utility/LLDBAssert.h" 19 #include "lldb/Utility/Log.h" 20 #include "lldb/Utility/Stream.h" 21 #include "lldb/Utility/StreamString.h" 22 #include "lldb/Utility/UUID.h" 23 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/Support/LEB128.h" 27 #include "llvm/Support/MD5.h" 28 #include "llvm/Support/MathExtras.h" 29 30 #include <algorithm> 31 #include <array> 32 #include <cassert> 33 #include <cstdint> 34 #include <string> 35 36 #include <cctype> 37 #include <cinttypes> 38 #include <cstring> 39 40 using namespace lldb; 41 using namespace lldb_private; 42 43 static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) { 44 uint16_t value; 45 memcpy(&value, ptr + offset, 2); 46 return value; 47 } 48 49 static inline uint32_t ReadInt32(const unsigned char *ptr, 50 offset_t offset = 0) { 51 uint32_t value; 52 memcpy(&value, ptr + offset, 4); 53 return value; 54 } 55 56 static inline uint64_t ReadInt64(const unsigned char *ptr, 57 offset_t offset = 0) { 58 uint64_t value; 59 memcpy(&value, ptr + offset, 8); 60 return value; 61 } 62 63 static inline uint16_t ReadInt16(const void *ptr) { 64 uint16_t value; 65 memcpy(&value, ptr, 2); 66 return value; 67 } 68 69 static inline uint16_t ReadSwapInt16(const unsigned char *ptr, 70 offset_t offset) { 71 uint16_t value; 72 memcpy(&value, ptr + offset, 2); 73 return llvm::ByteSwap_16(value); 74 } 75 76 static inline uint32_t ReadSwapInt32(const unsigned char *ptr, 77 offset_t offset) { 78 uint32_t value; 79 memcpy(&value, ptr + offset, 4); 80 return llvm::ByteSwap_32(value); 81 } 82 83 static inline uint64_t ReadSwapInt64(const unsigned char *ptr, 84 offset_t offset) { 85 uint64_t value; 86 memcpy(&value, ptr + offset, 8); 87 return llvm::ByteSwap_64(value); 88 } 89 90 static inline uint16_t ReadSwapInt16(const void *ptr) { 91 uint16_t value; 92 memcpy(&value, ptr, 2); 93 return llvm::ByteSwap_16(value); 94 } 95 96 static inline uint32_t ReadSwapInt32(const void *ptr) { 97 uint32_t value; 98 memcpy(&value, ptr, 4); 99 return llvm::ByteSwap_32(value); 100 } 101 102 static inline uint64_t ReadSwapInt64(const void *ptr) { 103 uint64_t value; 104 memcpy(&value, ptr, 8); 105 return llvm::ByteSwap_64(value); 106 } 107 108 static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size, 109 ByteOrder byte_order) { 110 uint64_t res = 0; 111 if (byte_order == eByteOrderBig) 112 for (size_t i = 0; i < byte_size; ++i) 113 res = (res << 8) | data[i]; 114 else { 115 assert(byte_order == eByteOrderLittle); 116 for (size_t i = 0; i < byte_size; ++i) 117 res = (res << 8) | data[byte_size - 1 - i]; 118 } 119 return res; 120 } 121 122 DataExtractor::DataExtractor() 123 : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)), 124 m_data_sp() {} 125 126 // This constructor allows us to use data that is owned by someone else. The 127 // data must stay around as long as this object is valid. 128 DataExtractor::DataExtractor(const void *data, offset_t length, 129 ByteOrder endian, uint32_t addr_size, 130 uint32_t target_byte_size /*=1*/) 131 : m_start(const_cast<uint8_t *>(static_cast<const uint8_t *>(data))), 132 m_end(const_cast<uint8_t *>(static_cast<const uint8_t *>(data)) + length), 133 m_byte_order(endian), m_addr_size(addr_size), m_data_sp(), 134 m_target_byte_size(target_byte_size) { 135 assert(addr_size >= 1 && addr_size <= 8); 136 } 137 138 // Make a shared pointer reference to the shared data in "data_sp" and set the 139 // endian swapping setting to "swap", and the address size to "addr_size". The 140 // shared data reference will ensure the data lives as long as any 141 // DataExtractor objects exist that have a reference to this data. 142 DataExtractor::DataExtractor(const DataBufferSP &data_sp, ByteOrder endian, 143 uint32_t addr_size, 144 uint32_t target_byte_size /*=1*/) 145 : m_start(nullptr), m_end(nullptr), m_byte_order(endian), 146 m_addr_size(addr_size), m_data_sp(), 147 m_target_byte_size(target_byte_size) { 148 assert(addr_size >= 1 && addr_size <= 8); 149 SetData(data_sp); 150 } 151 152 // Initialize this object with a subset of the data bytes in "data". If "data" 153 // contains shared data, then a reference to this shared data will added and 154 // the shared data will stay around as long as any object contains a reference 155 // to that data. The endian swap and address size settings are copied from 156 // "data". 157 DataExtractor::DataExtractor(const DataExtractor &data, offset_t offset, 158 offset_t length, uint32_t target_byte_size /*=1*/) 159 : m_start(nullptr), m_end(nullptr), m_byte_order(data.m_byte_order), 160 m_addr_size(data.m_addr_size), m_data_sp(), 161 m_target_byte_size(target_byte_size) { 162 assert(m_addr_size >= 1 && m_addr_size <= 8); 163 if (data.ValidOffset(offset)) { 164 offset_t bytes_available = data.GetByteSize() - offset; 165 if (length > bytes_available) 166 length = bytes_available; 167 SetData(data, offset, length); 168 } 169 } 170 171 DataExtractor::DataExtractor(const DataExtractor &rhs) 172 : m_start(rhs.m_start), m_end(rhs.m_end), m_byte_order(rhs.m_byte_order), 173 m_addr_size(rhs.m_addr_size), m_data_sp(rhs.m_data_sp), 174 m_target_byte_size(rhs.m_target_byte_size) { 175 assert(m_addr_size >= 1 && m_addr_size <= 8); 176 } 177 178 // Assignment operator 179 const DataExtractor &DataExtractor::operator=(const DataExtractor &rhs) { 180 if (this != &rhs) { 181 m_start = rhs.m_start; 182 m_end = rhs.m_end; 183 m_byte_order = rhs.m_byte_order; 184 m_addr_size = rhs.m_addr_size; 185 m_data_sp = rhs.m_data_sp; 186 } 187 return *this; 188 } 189 190 DataExtractor::~DataExtractor() = default; 191 192 // Clears the object contents back to a default invalid state, and release any 193 // references to shared data that this object may contain. 194 void DataExtractor::Clear() { 195 m_start = nullptr; 196 m_end = nullptr; 197 m_byte_order = endian::InlHostByteOrder(); 198 m_addr_size = sizeof(void *); 199 m_data_sp.reset(); 200 } 201 202 // If this object contains shared data, this function returns the offset into 203 // that shared data. Else zero is returned. 204 size_t DataExtractor::GetSharedDataOffset() const { 205 if (m_start != nullptr) { 206 const DataBuffer *data = m_data_sp.get(); 207 if (data != nullptr) { 208 const uint8_t *data_bytes = data->GetBytes(); 209 if (data_bytes != nullptr) { 210 assert(m_start >= data_bytes); 211 return m_start - data_bytes; 212 } 213 } 214 } 215 return 0; 216 } 217 218 // Set the data with which this object will extract from to data starting at 219 // BYTES and set the length of the data to LENGTH bytes long. The data is 220 // externally owned must be around at least as long as this object points to 221 // the data. No copy of the data is made, this object just refers to this data 222 // and can extract from it. If this object refers to any shared data upon 223 // entry, the reference to that data will be released. Is SWAP is set to true, 224 // any data extracted will be endian swapped. 225 lldb::offset_t DataExtractor::SetData(const void *bytes, offset_t length, 226 ByteOrder endian) { 227 m_byte_order = endian; 228 m_data_sp.reset(); 229 if (bytes == nullptr || length == 0) { 230 m_start = nullptr; 231 m_end = nullptr; 232 } else { 233 m_start = const_cast<uint8_t *>(static_cast<const uint8_t *>(bytes)); 234 m_end = m_start + length; 235 } 236 return GetByteSize(); 237 } 238 239 // Assign the data for this object to be a subrange in "data" starting 240 // "data_offset" bytes into "data" and ending "data_length" bytes later. If 241 // "data_offset" is not a valid offset into "data", then this object will 242 // contain no bytes. If "data_offset" is within "data" yet "data_length" is too 243 // large, the length will be capped at the number of bytes remaining in "data". 244 // If "data" contains a shared pointer to other data, then a ref counted 245 // pointer to that data will be made in this object. If "data" doesn't contain 246 // a shared pointer to data, then the bytes referred to in "data" will need to 247 // exist at least as long as this object refers to those bytes. The address 248 // size and endian swap settings are copied from the current values in "data". 249 lldb::offset_t DataExtractor::SetData(const DataExtractor &data, 250 offset_t data_offset, 251 offset_t data_length) { 252 m_addr_size = data.m_addr_size; 253 assert(m_addr_size >= 1 && m_addr_size <= 8); 254 // If "data" contains shared pointer to data, then we can use that 255 if (data.m_data_sp) { 256 m_byte_order = data.m_byte_order; 257 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, 258 data_length); 259 } 260 261 // We have a DataExtractor object that just has a pointer to bytes 262 if (data.ValidOffset(data_offset)) { 263 if (data_length > data.GetByteSize() - data_offset) 264 data_length = data.GetByteSize() - data_offset; 265 return SetData(data.GetDataStart() + data_offset, data_length, 266 data.GetByteOrder()); 267 } 268 return 0; 269 } 270 271 // Assign the data for this object to be a subrange of the shared data in 272 // "data_sp" starting "data_offset" bytes into "data_sp" and ending 273 // "data_length" bytes later. If "data_offset" is not a valid offset into 274 // "data_sp", then this object will contain no bytes. If "data_offset" is 275 // within "data_sp" yet "data_length" is too large, the length will be capped 276 // at the number of bytes remaining in "data_sp". A ref counted pointer to the 277 // data in "data_sp" will be made in this object IF the number of bytes this 278 // object refers to in greater than zero (if at least one byte was available 279 // starting at "data_offset") to ensure the data stays around as long as it is 280 // needed. The address size and endian swap settings will remain unchanged from 281 // their current settings. 282 lldb::offset_t DataExtractor::SetData(const DataBufferSP &data_sp, 283 offset_t data_offset, 284 offset_t data_length) { 285 m_start = m_end = nullptr; 286 287 if (data_length > 0) { 288 m_data_sp = data_sp; 289 if (data_sp) { 290 const size_t data_size = data_sp->GetByteSize(); 291 if (data_offset < data_size) { 292 m_start = data_sp->GetBytes() + data_offset; 293 const size_t bytes_left = data_size - data_offset; 294 // Cap the length of we asked for too many 295 if (data_length <= bytes_left) 296 m_end = m_start + data_length; // We got all the bytes we wanted 297 else 298 m_end = m_start + bytes_left; // Not all the bytes requested were 299 // available in the shared data 300 } 301 } 302 } 303 304 size_t new_size = GetByteSize(); 305 306 // Don't hold a shared pointer to the data buffer if we don't share any valid 307 // bytes in the shared buffer. 308 if (new_size == 0) 309 m_data_sp.reset(); 310 311 return new_size; 312 } 313 314 // Extract a single unsigned char from the binary data and update the offset 315 // pointed to by "offset_ptr". 316 // 317 // RETURNS the byte that was extracted, or zero on failure. 318 uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const { 319 const uint8_t *data = static_cast<const uint8_t *>(GetData(offset_ptr, 1)); 320 if (data) 321 return *data; 322 return 0; 323 } 324 325 // Extract "count" unsigned chars from the binary data and update the offset 326 // pointed to by "offset_ptr". The extracted data is copied into "dst". 327 // 328 // RETURNS the non-nullptr buffer pointer upon successful extraction of 329 // all the requested bytes, or nullptr when the data is not available in the 330 // buffer due to being out of bounds, or insufficient data. 331 void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst, 332 uint32_t count) const { 333 const uint8_t *data = 334 static_cast<const uint8_t *>(GetData(offset_ptr, count)); 335 if (data) { 336 // Copy the data into the buffer 337 memcpy(dst, data, count); 338 // Return a non-nullptr pointer to the converted data as an indicator of 339 // success 340 return dst; 341 } 342 return nullptr; 343 } 344 345 // Extract a single uint16_t from the data and update the offset pointed to by 346 // "offset_ptr". 347 // 348 // RETURNS the uint16_t that was extracted, or zero on failure. 349 uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const { 350 uint16_t val = 0; 351 const uint8_t *data = 352 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val))); 353 if (data) { 354 if (m_byte_order != endian::InlHostByteOrder()) 355 val = ReadSwapInt16(data); 356 else 357 val = ReadInt16(data); 358 } 359 return val; 360 } 361 362 uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const { 363 uint16_t val; 364 if (m_byte_order == endian::InlHostByteOrder()) 365 val = ReadInt16(m_start, *offset_ptr); 366 else 367 val = ReadSwapInt16(m_start, *offset_ptr); 368 *offset_ptr += sizeof(val); 369 return val; 370 } 371 372 uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const { 373 uint32_t val; 374 if (m_byte_order == endian::InlHostByteOrder()) 375 val = ReadInt32(m_start, *offset_ptr); 376 else 377 val = ReadSwapInt32(m_start, *offset_ptr); 378 *offset_ptr += sizeof(val); 379 return val; 380 } 381 382 uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const { 383 uint64_t val; 384 if (m_byte_order == endian::InlHostByteOrder()) 385 val = ReadInt64(m_start, *offset_ptr); 386 else 387 val = ReadSwapInt64(m_start, *offset_ptr); 388 *offset_ptr += sizeof(val); 389 return val; 390 } 391 392 // Extract "count" uint16_t values from the binary data and update the offset 393 // pointed to by "offset_ptr". The extracted data is copied into "dst". 394 // 395 // RETURNS the non-nullptr buffer pointer upon successful extraction of 396 // all the requested bytes, or nullptr when the data is not available in the 397 // buffer due to being out of bounds, or insufficient data. 398 void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst, 399 uint32_t count) const { 400 const size_t src_size = sizeof(uint16_t) * count; 401 const uint16_t *src = 402 static_cast<const uint16_t *>(GetData(offset_ptr, src_size)); 403 if (src) { 404 if (m_byte_order != endian::InlHostByteOrder()) { 405 uint16_t *dst_pos = static_cast<uint16_t *>(void_dst); 406 uint16_t *dst_end = dst_pos + count; 407 const uint16_t *src_pos = src; 408 while (dst_pos < dst_end) { 409 *dst_pos = ReadSwapInt16(src_pos); 410 ++dst_pos; 411 ++src_pos; 412 } 413 } else { 414 memcpy(void_dst, src, src_size); 415 } 416 // Return a non-nullptr pointer to the converted data as an indicator of 417 // success 418 return void_dst; 419 } 420 return nullptr; 421 } 422 423 // Extract a single uint32_t from the data and update the offset pointed to by 424 // "offset_ptr". 425 // 426 // RETURNS the uint32_t that was extracted, or zero on failure. 427 uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const { 428 uint32_t val = 0; 429 const uint8_t *data = 430 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val))); 431 if (data) { 432 if (m_byte_order != endian::InlHostByteOrder()) { 433 val = ReadSwapInt32(data); 434 } else { 435 memcpy(&val, data, 4); 436 } 437 } 438 return val; 439 } 440 441 // Extract "count" uint32_t values from the binary data and update the offset 442 // pointed to by "offset_ptr". The extracted data is copied into "dst". 443 // 444 // RETURNS the non-nullptr buffer pointer upon successful extraction of 445 // all the requested bytes, or nullptr when the data is not available in the 446 // buffer due to being out of bounds, or insufficient data. 447 void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst, 448 uint32_t count) const { 449 const size_t src_size = sizeof(uint32_t) * count; 450 const uint32_t *src = 451 static_cast<const uint32_t *>(GetData(offset_ptr, src_size)); 452 if (src) { 453 if (m_byte_order != endian::InlHostByteOrder()) { 454 uint32_t *dst_pos = static_cast<uint32_t *>(void_dst); 455 uint32_t *dst_end = dst_pos + count; 456 const uint32_t *src_pos = src; 457 while (dst_pos < dst_end) { 458 *dst_pos = ReadSwapInt32(src_pos); 459 ++dst_pos; 460 ++src_pos; 461 } 462 } else { 463 memcpy(void_dst, src, src_size); 464 } 465 // Return a non-nullptr pointer to the converted data as an indicator of 466 // success 467 return void_dst; 468 } 469 return nullptr; 470 } 471 472 // Extract a single uint64_t from the data and update the offset pointed to by 473 // "offset_ptr". 474 // 475 // RETURNS the uint64_t that was extracted, or zero on failure. 476 uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const { 477 uint64_t val = 0; 478 const uint8_t *data = 479 static_cast<const uint8_t *>(GetData(offset_ptr, sizeof(val))); 480 if (data) { 481 if (m_byte_order != endian::InlHostByteOrder()) { 482 val = ReadSwapInt64(data); 483 } else { 484 memcpy(&val, data, 8); 485 } 486 } 487 return val; 488 } 489 490 // GetU64 491 // 492 // Get multiple consecutive 64 bit values. Return true if the entire read 493 // succeeds and increment the offset pointed to by offset_ptr, else return 494 // false and leave the offset pointed to by offset_ptr unchanged. 495 void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst, 496 uint32_t count) const { 497 const size_t src_size = sizeof(uint64_t) * count; 498 const uint64_t *src = 499 static_cast<const uint64_t *>(GetData(offset_ptr, src_size)); 500 if (src) { 501 if (m_byte_order != endian::InlHostByteOrder()) { 502 uint64_t *dst_pos = static_cast<uint64_t *>(void_dst); 503 uint64_t *dst_end = dst_pos + count; 504 const uint64_t *src_pos = src; 505 while (dst_pos < dst_end) { 506 *dst_pos = ReadSwapInt64(src_pos); 507 ++dst_pos; 508 ++src_pos; 509 } 510 } else { 511 memcpy(void_dst, src, src_size); 512 } 513 // Return a non-nullptr pointer to the converted data as an indicator of 514 // success 515 return void_dst; 516 } 517 return nullptr; 518 } 519 520 uint32_t DataExtractor::GetMaxU32(offset_t *offset_ptr, 521 size_t byte_size) const { 522 lldbassert(byte_size > 0 && byte_size <= 4 && "GetMaxU32 invalid byte_size!"); 523 return GetMaxU64(offset_ptr, byte_size); 524 } 525 526 uint64_t DataExtractor::GetMaxU64(offset_t *offset_ptr, 527 size_t byte_size) const { 528 lldbassert(byte_size > 0 && byte_size <= 8 && "GetMaxU64 invalid byte_size!"); 529 switch (byte_size) { 530 case 1: 531 return GetU8(offset_ptr); 532 case 2: 533 return GetU16(offset_ptr); 534 case 4: 535 return GetU32(offset_ptr); 536 case 8: 537 return GetU64(offset_ptr); 538 default: { 539 // General case. 540 const uint8_t *data = 541 static_cast<const uint8_t *>(GetData(offset_ptr, byte_size)); 542 if (data == nullptr) 543 return 0; 544 return ReadMaxInt64(data, byte_size, m_byte_order); 545 } 546 } 547 return 0; 548 } 549 550 uint64_t DataExtractor::GetMaxU64_unchecked(offset_t *offset_ptr, 551 size_t byte_size) const { 552 switch (byte_size) { 553 case 1: 554 return GetU8_unchecked(offset_ptr); 555 case 2: 556 return GetU16_unchecked(offset_ptr); 557 case 4: 558 return GetU32_unchecked(offset_ptr); 559 case 8: 560 return GetU64_unchecked(offset_ptr); 561 default: { 562 uint64_t res = ReadMaxInt64(&m_start[*offset_ptr], byte_size, m_byte_order); 563 *offset_ptr += byte_size; 564 return res; 565 } 566 } 567 return 0; 568 } 569 570 int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const { 571 uint64_t u64 = GetMaxU64(offset_ptr, byte_size); 572 return llvm::SignExtend64(u64, 8 * byte_size); 573 } 574 575 uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size, 576 uint32_t bitfield_bit_size, 577 uint32_t bitfield_bit_offset) const { 578 assert(bitfield_bit_size <= 64); 579 uint64_t uval64 = GetMaxU64(offset_ptr, size); 580 581 if (bitfield_bit_size == 0) 582 return uval64; 583 584 int32_t lsbcount = bitfield_bit_offset; 585 if (m_byte_order == eByteOrderBig) 586 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size; 587 588 if (lsbcount > 0) 589 uval64 >>= lsbcount; 590 591 uint64_t bitfield_mask = 592 (bitfield_bit_size == 64 593 ? std::numeric_limits<uint64_t>::max() 594 : ((static_cast<uint64_t>(1) << bitfield_bit_size) - 1)); 595 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64) 596 return uval64; 597 598 uval64 &= bitfield_mask; 599 600 return uval64; 601 } 602 603 int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size, 604 uint32_t bitfield_bit_size, 605 uint32_t bitfield_bit_offset) const { 606 assert(size >= 1 && "GetMaxS64Bitfield size must be >= 1"); 607 assert(size <= 8 && "GetMaxS64Bitfield size must be <= 8"); 608 int64_t sval64 = GetMaxS64(offset_ptr, size); 609 if (bitfield_bit_size == 0) 610 return sval64; 611 int32_t lsbcount = bitfield_bit_offset; 612 if (m_byte_order == eByteOrderBig) 613 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size; 614 if (lsbcount > 0) 615 sval64 >>= lsbcount; 616 uint64_t bitfield_mask = llvm::maskTrailingOnes<uint64_t>(bitfield_bit_size); 617 sval64 &= bitfield_mask; 618 // sign extend if needed 619 if (sval64 & ((static_cast<uint64_t>(1)) << (bitfield_bit_size - 1))) 620 sval64 |= ~bitfield_mask; 621 return sval64; 622 } 623 624 float DataExtractor::GetFloat(offset_t *offset_ptr) const { 625 return Get<float>(offset_ptr, 0.0f); 626 } 627 628 double DataExtractor::GetDouble(offset_t *offset_ptr) const { 629 return Get<double>(offset_ptr, 0.0); 630 } 631 632 long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const { 633 long double val = 0.0; 634 #if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \ 635 defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64) 636 *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val), 637 endian::InlHostByteOrder()); 638 #else 639 *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val, 640 sizeof(val), endian::InlHostByteOrder()); 641 #endif 642 return val; 643 } 644 645 // Extract a single address from the data and update the offset pointed to by 646 // "offset_ptr". The size of the extracted address comes from the 647 // "this->m_addr_size" member variable and should be set correctly prior to 648 // extracting any address values. 649 // 650 // RETURNS the address that was extracted, or zero on failure. 651 uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const { 652 assert(m_addr_size >= 1 && m_addr_size <= 8); 653 return GetMaxU64(offset_ptr, m_addr_size); 654 } 655 656 uint64_t DataExtractor::GetAddress_unchecked(offset_t *offset_ptr) const { 657 assert(m_addr_size >= 1 && m_addr_size <= 8); 658 return GetMaxU64_unchecked(offset_ptr, m_addr_size); 659 } 660 661 size_t DataExtractor::ExtractBytes(offset_t offset, offset_t length, 662 ByteOrder dst_byte_order, void *dst) const { 663 const uint8_t *src = PeekData(offset, length); 664 if (src) { 665 if (dst_byte_order != GetByteOrder()) { 666 // Validate that only a word- or register-sized dst is byte swapped 667 assert(length == 1 || length == 2 || length == 4 || length == 8 || 668 length == 10 || length == 16 || length == 32); 669 670 for (uint32_t i = 0; i < length; ++i) 671 (static_cast<uint8_t *>(dst))[i] = src[length - i - 1]; 672 } else 673 ::memcpy(dst, src, length); 674 return length; 675 } 676 return 0; 677 } 678 679 // Extract data as it exists in target memory 680 lldb::offset_t DataExtractor::CopyData(offset_t offset, offset_t length, 681 void *dst) const { 682 const uint8_t *src = PeekData(offset, length); 683 if (src) { 684 ::memcpy(dst, src, length); 685 return length; 686 } 687 return 0; 688 } 689 690 // Extract data and swap if needed when doing the copy 691 lldb::offset_t 692 DataExtractor::CopyByteOrderedData(offset_t src_offset, offset_t src_len, 693 void *dst_void_ptr, offset_t dst_len, 694 ByteOrder dst_byte_order) const { 695 // Validate the source info 696 if (!ValidOffsetForDataOfSize(src_offset, src_len)) 697 assert(ValidOffsetForDataOfSize(src_offset, src_len)); 698 assert(src_len > 0); 699 assert(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle); 700 701 // Validate the destination info 702 assert(dst_void_ptr != nullptr); 703 assert(dst_len > 0); 704 assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle); 705 706 // Validate that only a word- or register-sized dst is byte swapped 707 assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 || 708 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 || 709 dst_len == 32); 710 711 // Must have valid byte orders set in this object and for destination 712 if (!(dst_byte_order == eByteOrderBig || 713 dst_byte_order == eByteOrderLittle) || 714 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle)) 715 return 0; 716 717 uint8_t *dst = static_cast<uint8_t *>(dst_void_ptr); 718 const uint8_t *src = PeekData(src_offset, src_len); 719 if (src) { 720 if (dst_len >= src_len) { 721 // We are copying the entire value from src into dst. Calculate how many, 722 // if any, zeroes we need for the most significant bytes if "dst_len" is 723 // greater than "src_len"... 724 const size_t num_zeroes = dst_len - src_len; 725 if (dst_byte_order == eByteOrderBig) { 726 // Big endian, so we lead with zeroes... 727 if (num_zeroes > 0) 728 ::memset(dst, 0, num_zeroes); 729 // Then either copy or swap the rest 730 if (m_byte_order == eByteOrderBig) { 731 ::memcpy(dst + num_zeroes, src, src_len); 732 } else { 733 for (uint32_t i = 0; i < src_len; ++i) 734 dst[i + num_zeroes] = src[src_len - 1 - i]; 735 } 736 } else { 737 // Little endian destination, so we lead the value bytes 738 if (m_byte_order == eByteOrderBig) { 739 for (uint32_t i = 0; i < src_len; ++i) 740 dst[i] = src[src_len - 1 - i]; 741 } else { 742 ::memcpy(dst, src, src_len); 743 } 744 // And zero the rest... 745 if (num_zeroes > 0) 746 ::memset(dst + src_len, 0, num_zeroes); 747 } 748 return src_len; 749 } else { 750 // We are only copying some of the value from src into dst.. 751 752 if (dst_byte_order == eByteOrderBig) { 753 // Big endian dst 754 if (m_byte_order == eByteOrderBig) { 755 // Big endian dst, with big endian src 756 ::memcpy(dst, src + (src_len - dst_len), dst_len); 757 } else { 758 // Big endian dst, with little endian src 759 for (uint32_t i = 0; i < dst_len; ++i) 760 dst[i] = src[dst_len - 1 - i]; 761 } 762 } else { 763 // Little endian dst 764 if (m_byte_order == eByteOrderBig) { 765 // Little endian dst, with big endian src 766 for (uint32_t i = 0; i < dst_len; ++i) 767 dst[i] = src[src_len - 1 - i]; 768 } else { 769 // Little endian dst, with big endian src 770 ::memcpy(dst, src, dst_len); 771 } 772 } 773 return dst_len; 774 } 775 } 776 return 0; 777 } 778 779 // Extracts a variable length NULL terminated C string from the data at the 780 // offset pointed to by "offset_ptr". The "offset_ptr" will be updated with 781 // the offset of the byte that follows the NULL terminator byte. 782 // 783 // If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is 784 // non-zero and there aren't enough available bytes, nullptr will be returned 785 // and "offset_ptr" will not be updated. 786 const char *DataExtractor::GetCStr(offset_t *offset_ptr) const { 787 const char *start = reinterpret_cast<const char *>(PeekData(*offset_ptr, 1)); 788 // Already at the end of the data. 789 if (!start) 790 return nullptr; 791 792 const char *end = reinterpret_cast<const char *>(m_end); 793 794 // Check all bytes for a null terminator that terminates a C string. 795 const char *terminator_or_end = std::find(start, end, '\0'); 796 797 // We didn't find a null terminator, so return nullptr to indicate that there 798 // is no valid C string at that offset. 799 if (terminator_or_end == end) 800 return nullptr; 801 802 // Update offset_ptr for the caller to point to the data behind the 803 // terminator (which is 1 byte long). 804 *offset_ptr += (terminator_or_end - start + 1UL); 805 return start; 806 } 807 808 // Extracts a NULL terminated C string from the fixed length field of length 809 // "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be 810 // updated with the offset of the byte that follows the fixed length field. 811 // 812 // If the offset pointed to by "offset_ptr" is out of bounds, or if the offset 813 // plus the length of the field is out of bounds, or if the field does not 814 // contain a NULL terminator byte, nullptr will be returned and "offset_ptr" 815 // will not be updated. 816 const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const { 817 const char *cstr = reinterpret_cast<const char *>(PeekData(*offset_ptr, len)); 818 if (cstr != nullptr) { 819 if (memchr(cstr, '\0', len) == nullptr) { 820 return nullptr; 821 } 822 *offset_ptr += len; 823 return cstr; 824 } 825 return nullptr; 826 } 827 828 // Peeks at a string in the contained data. No verification is done to make 829 // sure the entire string lies within the bounds of this object's data, only 830 // "offset" is verified to be a valid offset. 831 // 832 // Returns a valid C string pointer if "offset" is a valid offset in this 833 // object's data, else nullptr is returned. 834 const char *DataExtractor::PeekCStr(offset_t offset) const { 835 return reinterpret_cast<const char *>(PeekData(offset, 1)); 836 } 837 838 // Extracts an unsigned LEB128 number from this object's data starting at the 839 // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr" 840 // will be updated with the offset of the byte following the last extracted 841 // byte. 842 // 843 // Returned the extracted integer value. 844 uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const { 845 const uint8_t *src = PeekData(*offset_ptr, 1); 846 if (src == nullptr) 847 return 0; 848 849 unsigned byte_count = 0; 850 uint64_t result = llvm::decodeULEB128(src, &byte_count, m_end); 851 *offset_ptr += byte_count; 852 return result; 853 } 854 855 // Extracts an signed LEB128 number from this object's data starting at the 856 // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr" 857 // will be updated with the offset of the byte following the last extracted 858 // byte. 859 // 860 // Returned the extracted integer value. 861 int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const { 862 const uint8_t *src = PeekData(*offset_ptr, 1); 863 if (src == nullptr) 864 return 0; 865 866 unsigned byte_count = 0; 867 int64_t result = llvm::decodeSLEB128(src, &byte_count, m_end); 868 *offset_ptr += byte_count; 869 return result; 870 } 871 872 // Skips a ULEB128 number (signed or unsigned) from this object's data starting 873 // at the offset pointed to by "offset_ptr". The offset pointed to by 874 // "offset_ptr" will be updated with the offset of the byte following the last 875 // extracted byte. 876 // 877 // Returns the number of bytes consumed during the extraction. 878 uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const { 879 uint32_t bytes_consumed = 0; 880 const uint8_t *src = PeekData(*offset_ptr, 1); 881 if (src == nullptr) 882 return 0; 883 884 const uint8_t *end = m_end; 885 886 if (src < end) { 887 const uint8_t *src_pos = src; 888 while ((src_pos < end) && (*src_pos++ & 0x80)) 889 ++bytes_consumed; 890 *offset_ptr += src_pos - src; 891 } 892 return bytes_consumed; 893 } 894 895 // Dumps bytes from this object's data to the stream "s" starting 896 // "start_offset" bytes into this data, and ending with the byte before 897 // "end_offset". "base_addr" will be added to the offset into the dumped data 898 // when showing the offset into the data in the output information. 899 // "num_per_line" objects of type "type" will be dumped with the option to 900 // override the format for each object with "type_format". "type_format" is a 901 // printf style formatting string. If "type_format" is nullptr, then an 902 // appropriate format string will be used for the supplied "type". If the 903 // stream "s" is nullptr, then the output will be send to Log(). 904 lldb::offset_t DataExtractor::PutToLog(Log *log, offset_t start_offset, 905 offset_t length, uint64_t base_addr, 906 uint32_t num_per_line, 907 DataExtractor::Type type) const { 908 if (log == nullptr) 909 return start_offset; 910 911 offset_t offset; 912 offset_t end_offset; 913 uint32_t count; 914 StreamString sstr; 915 for (offset = start_offset, end_offset = offset + length, count = 0; 916 ValidOffset(offset) && offset < end_offset; ++count) { 917 if ((count % num_per_line) == 0) { 918 // Print out any previous string 919 if (sstr.GetSize() > 0) { 920 log->PutString(sstr.GetString()); 921 sstr.Clear(); 922 } 923 // Reset string offset and fill the current line string with address: 924 if (base_addr != LLDB_INVALID_ADDRESS) 925 sstr.Printf("0x%8.8" PRIx64 ":", 926 static_cast<uint64_t>(base_addr + (offset - start_offset))); 927 } 928 929 switch (type) { 930 case TypeUInt8: 931 sstr.Printf(" %2.2x", GetU8(&offset)); 932 break; 933 case TypeChar: { 934 char ch = GetU8(&offset); 935 sstr.Printf(" %c", llvm::isPrint(ch) ? ch : ' '); 936 } break; 937 case TypeUInt16: 938 sstr.Printf(" %4.4x", GetU16(&offset)); 939 break; 940 case TypeUInt32: 941 sstr.Printf(" %8.8x", GetU32(&offset)); 942 break; 943 case TypeUInt64: 944 sstr.Printf(" %16.16" PRIx64, GetU64(&offset)); 945 break; 946 case TypePointer: 947 sstr.Printf(" 0x%" PRIx64, GetAddress(&offset)); 948 break; 949 case TypeULEB128: 950 sstr.Printf(" 0x%" PRIx64, GetULEB128(&offset)); 951 break; 952 case TypeSLEB128: 953 sstr.Printf(" %" PRId64, GetSLEB128(&offset)); 954 break; 955 } 956 } 957 958 if (!sstr.Empty()) 959 log->PutString(sstr.GetString()); 960 961 return offset; // Return the offset at which we ended up 962 } 963 964 size_t DataExtractor::Copy(DataExtractor &dest_data) const { 965 if (m_data_sp) { 966 // we can pass along the SP to the data 967 dest_data.SetData(m_data_sp); 968 } else { 969 const uint8_t *base_ptr = m_start; 970 size_t data_size = GetByteSize(); 971 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size))); 972 } 973 return GetByteSize(); 974 } 975 976 bool DataExtractor::Append(DataExtractor &rhs) { 977 if (rhs.GetByteOrder() != GetByteOrder()) 978 return false; 979 980 if (rhs.GetByteSize() == 0) 981 return true; 982 983 if (GetByteSize() == 0) 984 return (rhs.Copy(*this) > 0); 985 986 size_t bytes = GetByteSize() + rhs.GetByteSize(); 987 988 DataBufferHeap *buffer_heap_ptr = nullptr; 989 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0)); 990 991 if (!buffer_sp || buffer_heap_ptr == nullptr) 992 return false; 993 994 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes(); 995 996 memcpy(bytes_ptr, GetDataStart(), GetByteSize()); 997 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize()); 998 999 SetData(buffer_sp); 1000 1001 return true; 1002 } 1003 1004 bool DataExtractor::Append(void *buf, offset_t length) { 1005 if (buf == nullptr) 1006 return false; 1007 1008 if (length == 0) 1009 return true; 1010 1011 size_t bytes = GetByteSize() + length; 1012 1013 DataBufferHeap *buffer_heap_ptr = nullptr; 1014 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0)); 1015 1016 if (!buffer_sp || buffer_heap_ptr == nullptr) 1017 return false; 1018 1019 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes(); 1020 1021 if (GetByteSize() > 0) 1022 memcpy(bytes_ptr, GetDataStart(), GetByteSize()); 1023 1024 memcpy(bytes_ptr + GetByteSize(), buf, length); 1025 1026 SetData(buffer_sp); 1027 1028 return true; 1029 } 1030 1031 void DataExtractor::Checksum(llvm::SmallVectorImpl<uint8_t> &dest, 1032 uint64_t max_data) { 1033 if (max_data == 0) 1034 max_data = GetByteSize(); 1035 else 1036 max_data = std::min(max_data, GetByteSize()); 1037 1038 llvm::MD5 md5; 1039 1040 const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data); 1041 md5.update(data); 1042 1043 llvm::MD5::MD5Result result; 1044 md5.final(result); 1045 1046 dest.clear(); 1047 dest.append(result.Bytes.begin(), result.Bytes.end()); 1048 } 1049