Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lldb/include/lldb/Utility/DataExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ class DataExtractor {
/// \return
/// A pointer to the bytes in this object's data if the offset
/// and length are valid, or nullptr otherwise.
const void *GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const {
virtual const void *GetData(lldb::offset_t *offset_ptr,
lldb::offset_t length) const {
const uint8_t *ptr = PeekData(*offset_ptr, length);
if (ptr)
*offset_ptr += length;
Expand Down Expand Up @@ -609,17 +610,17 @@ class DataExtractor {
/// The extracted uint8_t value.
uint8_t GetU8(lldb::offset_t *offset_ptr) const;

uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const {
virtual uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const {
uint8_t val = m_start[*offset_ptr];
*offset_ptr += 1;
return val;
}

uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const;
virtual uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const;

uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const;
virtual uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const;

uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const;
virtual uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const;
/// Extract \a count uint8_t values from \a *offset_ptr.
///
/// Extract \a count uint8_t values from the binary data at the offset
Expand Down Expand Up @@ -829,7 +830,8 @@ class DataExtractor {
/// A non-nullptr data pointer if \a offset is a valid offset and
/// there are \a length bytes available at that offset, nullptr
/// otherwise.
const uint8_t *PeekData(lldb::offset_t offset, lldb::offset_t length) const {
virtual const uint8_t *PeekData(lldb::offset_t offset,
lldb::offset_t length) const {
if (ValidOffsetForDataOfSize(offset, length))
return m_start + offset;
return nullptr;
Expand Down
4 changes: 4 additions & 0 deletions lldb/include/lldb/Utility/RangeMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ class RangeDataVector {

RangeDataVector(Compare compare = Compare()) : m_compare(compare) {}

RangeDataVector(std::initializer_list<AugmentedEntry> entries,
Compare compare = Compare())
: m_entries(entries), m_compare(compare) {}

~RangeDataVector() = default;

void Append(const Entry &entry) { m_entries.emplace_back(entry); }
Expand Down
75 changes: 75 additions & 0 deletions lldb/include/lldb/Utility/VirtualDataExtractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_UTILITY_VIRTUALDATAEXTRACTOR_H
#define LLDB_UTILITY_VIRTUALDATAEXTRACTOR_H

#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/RangeMap.h"
#include "lldb/lldb-types.h"

namespace lldb_private {

/// A DataExtractor subclass that allows reading data at virtual addresses
/// using a lookup table that maps virtual address ranges to physical offsets.
///
/// This class maintains a lookup table where each entry contains:
/// - base: starting virtual address for this entry
/// - size: size of this entry in bytes
/// - data: physical offset in the underlying data buffer
///
/// Reads are translated from virtual addresses to physical offsets using
/// this lookup table. Reads cannot cross entry boundaries and this is
/// enforced with assertions.
class VirtualDataExtractor : public DataExtractor {
public:
/// Type alias for the range map used internally.
/// Maps virtual addresses (base) to physical offsets (data).
using LookupTable =
RangeDataVector<lldb::offset_t, lldb::offset_t, lldb::offset_t>;

VirtualDataExtractor() = default;

VirtualDataExtractor(const void *data, lldb::offset_t data_length,
lldb::ByteOrder byte_order, uint32_t addr_size,
LookupTable lookup_table);

VirtualDataExtractor(const lldb::DataBufferSP &data_sp,
lldb::ByteOrder byte_order, uint32_t addr_size,
LookupTable lookup_table);

const void *GetData(lldb::offset_t *offset_ptr,
lldb::offset_t length) const override;

const uint8_t *PeekData(lldb::offset_t offset,
lldb::offset_t length) const override;

/// Unchecked overrides
/// @{
uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const override;
uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const override;
uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const override;
uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const override;
/// @}

protected:
/// Find the lookup entry that contains the given virtual address.
const LookupTable::Entry *FindEntry(lldb::offset_t virtual_addr) const;

/// Validate that a read at a virtual address is within bounds and
/// does not cross entry boundaries.
bool ValidateVirtualRead(lldb::offset_t virtual_addr,
lldb::offset_t length) const;

private:
LookupTable m_lookup_table;
};

} // namespace lldb_private

#endif // LLDB_UTILITY_VIRTUALDATAEXTRACTOR_H
1 change: 1 addition & 0 deletions lldb/source/Utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ add_lldb_library(lldbUtility NO_INTERNAL_DEPENDENCIES
UserIDResolver.cpp
VASprintf.cpp
VMRange.cpp
VirtualDataExtractor.cpp
XcodeSDK.cpp
ZipFile.cpp

Expand Down
139 changes: 139 additions & 0 deletions lldb/source/Utility/VirtualDataExtractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Utility/VirtualDataExtractor.h"
#include <cassert>

using namespace lldb;
using namespace lldb_private;

VirtualDataExtractor::VirtualDataExtractor(const void *data,
offset_t data_length,
ByteOrder byte_order,
uint32_t addr_size,
LookupTable lookup_table)
: DataExtractor(data, data_length, byte_order, addr_size),
m_lookup_table(std::move(lookup_table)) {
m_lookup_table.Sort();
}

VirtualDataExtractor::VirtualDataExtractor(const DataBufferSP &data_sp,
ByteOrder byte_order,
uint32_t addr_size,
LookupTable lookup_table)
: DataExtractor(data_sp, byte_order, addr_size),
m_lookup_table(std::move(lookup_table)) {
m_lookup_table.Sort();
}

const VirtualDataExtractor::LookupTable::Entry *
VirtualDataExtractor::FindEntry(offset_t virtual_addr) const {
// Use RangeDataVector's binary search instead of linear search.
return m_lookup_table.FindEntryThatContains(virtual_addr);
}

bool VirtualDataExtractor::ValidateVirtualRead(offset_t virtual_addr,
offset_t length) const {
const LookupTable::Entry *entry = FindEntry(virtual_addr);
if (!entry)
return false;

// Assert that the read does not cross entry boundaries.
// RangeData.Contains() checks if a range is fully contained.
assert(entry->Contains(LookupTable::Range(virtual_addr, length)) &&
"Read crosses lookup table entry boundary");

// Also validate that the physical offset is within the data buffer.
// RangeData.data contains the physical offset.
offset_t physical_offset = entry->data + (virtual_addr - entry->base);
return ValidOffsetForDataOfSize(physical_offset, length);
}

const void *VirtualDataExtractor::GetData(offset_t *offset_ptr,
offset_t length) const {
// Override to treat offset as virtual address.
if (!offset_ptr)
return nullptr;

offset_t virtual_addr = *offset_ptr;

if (!ValidateVirtualRead(virtual_addr, length))
return nullptr;

const LookupTable::Entry *entry = FindEntry(virtual_addr);
assert(entry && "ValidateVirtualRead should have found an entry");

offset_t physical_offset = entry->data + (virtual_addr - entry->base);
// Use base class PeekData directly to avoid recursion.
const void *result = DataExtractor::PeekData(physical_offset, length);

if (result) {
// Advance the virtual offset pointer.
*offset_ptr += length;
}

return result;
}

const uint8_t *VirtualDataExtractor::PeekData(offset_t offset,
offset_t length) const {
// Override to treat offset as virtual address.
if (!ValidateVirtualRead(offset, length))
return nullptr;

const LookupTable::Entry *entry = FindEntry(offset);
assert(entry && "ValidateVirtualRead should have found an entry");

offset_t physical_offset = entry->data + (offset - entry->base);
// Use the base class PeekData with the physical offset.
return DataExtractor::PeekData(physical_offset, length);
}

uint8_t VirtualDataExtractor::GetU8_unchecked(offset_t *offset_ptr) const {
offset_t virtual_addr = *offset_ptr;
const LookupTable::Entry *entry = FindEntry(virtual_addr);
assert(entry && "Unchecked methods require valid virtual address");

offset_t physical_offset = entry->data + (virtual_addr - entry->base);
uint8_t result = DataExtractor::GetU8_unchecked(&physical_offset);
*offset_ptr += 1;
return result;
}

uint16_t VirtualDataExtractor::GetU16_unchecked(offset_t *offset_ptr) const {
offset_t virtual_addr = *offset_ptr;
const LookupTable::Entry *entry = FindEntry(virtual_addr);
assert(entry && "Unchecked methods require valid virtual address");

offset_t physical_offset = entry->data + (virtual_addr - entry->base);
uint16_t result = DataExtractor::GetU16_unchecked(&physical_offset);
*offset_ptr += 2;
return result;
}

uint32_t VirtualDataExtractor::GetU32_unchecked(offset_t *offset_ptr) const {
offset_t virtual_addr = *offset_ptr;
const LookupTable::Entry *entry = FindEntry(virtual_addr);
assert(entry && "Unchecked methods require valid virtual address");

offset_t physical_offset = entry->data + (virtual_addr - entry->base);
uint32_t result = DataExtractor::GetU32_unchecked(&physical_offset);
*offset_ptr += 4;
return result;
}

uint64_t VirtualDataExtractor::GetU64_unchecked(offset_t *offset_ptr) const {
offset_t virtual_addr = *offset_ptr;
const LookupTable::Entry *entry = FindEntry(virtual_addr);
assert(entry && "Unchecked methods require valid virtual address");

offset_t physical_offset = entry->data + (virtual_addr - entry->base);
uint64_t result = DataExtractor::GetU64_unchecked(&physical_offset);
*offset_ptr += 8;
return result;
}
1 change: 1 addition & 0 deletions lldb/unittests/Utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ add_lldb_unittest(UtilityTests
UserIDResolverTest.cpp
UUIDTest.cpp
VASprintfTest.cpp
VirtualDataExtractorTest.cpp
VMRangeTest.cpp
XcodeSDKTest.cpp

Expand Down
Loading