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