20 if (moduleName.empty() || className.empty())
43 std::memcpy(&word,
reinterpret_cast<const void*
>(address),
sizeof(word));
50 std::memcpy(&value,
reinterpret_cast<const void*
>(address),
sizeof(value));
54static std::vector<uintptr_t>
FindWords(std::span<const ScanRange> ranges, uintptr_t value)
56 std::vector<uintptr_t> found;
59 const auto begin =
reinterpret_cast<uintptr_t
>(range.Base);
60 const uintptr_t first = ((begin +
sizeof(
void*) - 1) & ~uintptr_t{
sizeof(
void*) - 1}) +
sizeof(
void*);
61 for (uintptr_t at = first; at + 2 *
sizeof(
void*) <= begin + range.Size; at +=
sizeof(
void*))
74 return std::to_string(className.size()) + std::string(className);
81 const std::string_view needle(typeName.c_str(), typeName.size() + 1);
84 const std::string_view memory(
reinterpret_cast<const char*
>(range.Base), range.Size);
85 for (
size_t at = memory.find(needle); at != std::string_view::npos; at = memory.find(needle, at + 1))
88 if (at > 0 && (std::isalnum(
static_cast<unsigned char>(memory[at - 1])) || memory[at - 1] ==
'_'))
93 for (uintptr_t name :
FindWords(ranges,
reinterpret_cast<uintptr_t
>(range.Base) + at))
95 for (uintptr_t typeInfo :
FindWords(ranges, name -
sizeof(
void*)))
98 if (
ReadWord(typeInfo -
sizeof(
void*)) == 0 &&
101 return reinterpret_cast<void*
>(typeInfo +
sizeof(
void*));
113 ReadWord(
reinterpret_cast<uintptr_t
>(
object)) ==
reinterpret_cast<uintptr_t
>(table);
129 if (!address || address %
alignof(
void*) != 0 ||
135 const auto* name =
reinterpret_cast<const char*
>(
ReadWord(address +
sizeof(
void*)));
140 return std::isdigit(
static_cast<unsigned char>(*name)) || *name ==
'N' || *name ==
'*';
143static bool NameIs(uintptr_t address, std::string_view wanted)
145 const auto* name =
reinterpret_cast<const char*
>(
ReadWord(address +
sizeof(
void*)));
146 return IsReadableAddress(name, wanted.size() + 1) && std::string_view(name, wanted.size()) == wanted &&
147 name[wanted.size()] ==
'\0';
172 if (count == 0 || count >
MaxBases ||
194 int depth, std::vector<FoundBase>& found)
201 const auto visit = [&](uintptr_t base, intptr_t offset,
bool isVirtual) {
210 found.push_back(here);
214 CollectBases(base, wanted, here, kinds, depth + 1, found);
224 const uint32_t count =
BaseCount(typeInfo, kinds);
225 for (uint32_t i = 0; i < count; ++i)
228 const auto offsetFlags =
static_cast<intptr_t
>(
ReadWord(entry +
sizeof(
void*)));
235 const auto address =
reinterpret_cast<uintptr_t
>(typeInfo);
238 return std::unexpected(
Error::Invalid(
"the class has no readable typeinfo"));
242 std::vector<FoundBase> found;
247 return std::unexpected(
Error::NotFound(std::format(
"'{}' is not a base", baseName)));
249 if (found.size() > 1)
251 return std::unexpected(
Error::Invalid(std::format(
"'{}' is a base {} times", baseName, found.size())));
253 if (found.front().Virtual)
255 return std::unexpected(
Error::Unsupported(std::format(
"'{}' is a virtual base", baseName)));
257 return static_cast<int>(found.front().
Offset);
262 void* found =
nullptr;
263 for (uintptr_t at :
FindWords(ranges,
reinterpret_cast<uintptr_t
>(typeInfo)))
265 const uintptr_t table = at +
sizeof(
void*);
266 if (
static_cast<intptr_t
>(
ReadWord(at -
sizeof(
void*))) != offsetToTop ||
276 found =
reinterpret_cast<void*
>(table);
void Warn(std::format_string< Args... > fmt, Args &&... args)
void * FindVirtualTableByTypeInfo(std::span< const ScanRange > ranges, const void *typeInfo, intptr_t offsetToTop)
bool FindImage(std::string_view moduleName, Image &module)
static constexpr int MaxDepth
static std::vector< uintptr_t > FindWords(std::span< const ScanRange > ranges, uintptr_t value)
static uint32_t ReadU32(uintptr_t address)
bool IsExecutableAddress(const void *address)
bool IsReadableAddress(const void *address, size_t bytes)
static uintptr_t ReadWord(uintptr_t address)
static void CollectBases(uintptr_t typeInfo, std::string_view wanted, FoundBase at, const TypeInfoKinds &kinds, int depth, std::vector< FoundBase > &found)
static bool HasSingleBase(uintptr_t typeInfo, const TypeInfoKinds &kinds)
static bool NameIs(uintptr_t address, std::string_view wanted)
std::expected< T, Error > Result
static bool LooksLikeTypeInfo(uintptr_t address)
Result< int > FindBaseOffsetByTypeInfo(const void *typeInfo, std::string_view baseName, const TypeInfoKinds &kinds)
static uint32_t BaseCount(uintptr_t typeInfo, const TypeInfoKinds &kinds)
static constexpr size_t TypeInfoFlags
static constexpr uintptr_t VirtualBaseFlag
void * FindVirtualTableByTypeName(std::span< const ScanRange > ranges, std::string_view className)
static constexpr size_t TypeInfoCount
std::string LengthPrefixedName(std::string_view className)
static constexpr size_t BaseEntrySize
static constexpr uint32_t MaxBases
static constexpr size_t TypeInfoBaseList
void * FindVirtualTable(std::string_view moduleName, std::string_view className)
static constexpr size_t TypeInfoBase
std::string PlatformModuleName(std::string_view moduleName)
bool HasVtable(const void *object, const void *table)
void * FindVirtualTableIn(const Image &module, std::string_view className)
static Error Invalid(std::string detail)
static Error Unsupported(std::string detail)
static Error NotFound(std::string detail)