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