Skip to content

Commit

Permalink
feat: add memory size API
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Oct 7, 2023
1 parent 3695c44 commit c276690
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
45 changes: 33 additions & 12 deletions src/liteloader/api/memory/MemoryUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,47 +52,68 @@ inline void memcpy_t(void* dst, const void* src) {
* @param symbol Symbol
* @return function pointer
*/
LLAPI FuncPtr resolveSymbol(const char* symbol);
LLNDAPI FuncPtr resolveSymbol(const char* symbol);

/**
* @brief resolve signature to function pointer
* @param t Signature
* @return function pointer
*/
LLAPI FuncPtr resolveSignature(const char* signature);
LLNDAPI FuncPtr resolveSignature(const char* signature);

/**
* @brief lookup symbol name of a function address
* @param func Function address
* @return symbols
*/
LLAPI std::vector<std::string> lookupSymbol(FuncPtr func);
LLNDAPI std::vector<std::string> lookupSymbol(FuncPtr func);

template <uintptr_t off, typename RTN = void, typename... Args>
auto constexpr virtualCall(void const* _this, Args&&... args) -> RTN {
return (*(RTN(**)(void const*, Args&&...))(*(uintptr_t*)_this + off))(_this, std::forward<Args>(args)...);
}

template <typename T, uintptr_t off>
constexpr T& dAccess(void* ptr) {
return *(T*)(reinterpret_cast<uintptr_t>(ptr) + off);
template <typename T>
[[nodiscard]] constexpr T& dAccess(void* ptr, uintptr_t off) {
return *(T*)(((uintptr_t)ptr) + off);
}

template <typename T, uintptr_t off>
constexpr T const& dAccess(void const* ptr) {
template <typename T>
[[nodiscard]] constexpr T const& dAccess(void const* ptr, uintptr_t off) {
return *(T*)(((uintptr_t)ptr) + off);
}

template <typename T>
constexpr T& dAccess(void* ptr, uintptr_t off) {
return *(T*)(((uintptr_t)ptr) + off);
[[nodiscard]] inline size_t getMemSizeFromPtr(std::shared_ptr<T>& ptr) {
// clang-format off
return _msize(dAccess<void*>(std::addressof(ptr), 8) // ptr* 8, rep* 8
) - ( // rep:
8 + // vtable
4 * 2 // uses & weaks
/**/ // storage
);
// clang-format on
}

template <typename T>
constexpr T const& dAccess(void const* ptr, uintptr_t off) {
return *(T*)(((uintptr_t)ptr) + off);
[[nodiscard]] inline size_t getMemSizeFromPtr(std::weak_ptr<T>& ptr) {
// clang-format off
return _msize(dAccess<void*>(std::addressof(ptr), 8) // ptr* 8, rep* 8
) - ( // rep:
8 + // vtable
4 * 2 // uses & weaks
/**/ // storage
);
// clang-format on
}

template <typename T, typename D>
[[nodiscard]] inline size_t getMemSizeFromPtr(std::unique_ptr<T, D>& ptr) {
return _msize(ptr.get());
}

[[nodiscard]] inline size_t getMemSizeFromPtr(void* ptr) { return _msize(ptr); }

template <FixedString symbol>
inline FuncPtr symbolCache = resolveSymbol(symbol);

Expand Down
21 changes: 11 additions & 10 deletions src/liteloader/api/utils/PatchHelper.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "liteloader/api/base/StdInt.h"
#include "memoryapi.h"
#include <exception>
#include <string>
Expand Down Expand Up @@ -42,16 +43,16 @@
// }
//}

template <int len>
template <size_t len>
struct PatchHelper {
uchar data[len];
using ref_t = uchar (&)[len];
constexpr bool operator==(ref_t ref) const noexcept { return memcmp(data, ref, sizeof data) == 0; }
constexpr bool operator!=(ref_t ref) const noexcept { return memcmp(data, ref, sizeof data) != 0; }
constexpr bool operator==(PatchHelper ref) const noexcept { return memcmp(data, ref.data, sizeof data) == 0; }
constexpr bool operator!=(PatchHelper ref) const noexcept { return memcmp(data, ref.data, sizeof data) != 0; }
inline void operator=(ref_t ref) { memcpy(data, ref, sizeof data); }
inline bool DoPatch(PatchHelper expected, PatchHelper patched) {
constexpr bool operator==(ref_t ref) const noexcept { return memcmp(data, ref, sizeof data) == 0; }
constexpr bool operator!=(ref_t ref) const noexcept { return memcmp(data, ref, sizeof data) != 0; }
constexpr bool operator==(PatchHelper ref) const noexcept { return memcmp(data, ref.data, sizeof data) == 0; }
constexpr bool operator!=(PatchHelper ref) const noexcept { return memcmp(data, ref.data, sizeof data) != 0; }
inline PatchHelper& operator=(ref_t ref) { memcpy(data, ref, sizeof data); }
inline bool DoPatch(PatchHelper expected, PatchHelper patched) {
if (*this == expected) {
*this = patched;
return true;
Expand All @@ -66,7 +67,7 @@ struct PatchHelper {
return result;
}

inline std::string Dump() const noexcept {
[[nodiscard]] inline std::string Dump() const noexcept {
char buffer[2 * len + 1] = {};
char* ptr = buffer;
for (auto& ch : data) ptr += sprintf(ptr, "%02X", (uint)ch);
Expand All @@ -75,8 +76,8 @@ struct PatchHelper {
};

struct NopFiller {
template <int len>
inline operator PatchHelper<len>() {
template <size_t len>
inline explicit operator PatchHelper<len>() {
PatchHelper<len> ret;
memset(ret.data, 0x90, len);
return ret;
Expand Down
10 changes: 5 additions & 5 deletions src/liteloader/test/Packet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct magic_enum::customize::enum_range<MinecraftPacketIds> {

#ifdef GENERATE_PACKET

std::string getVTableName(void** vtable) {
auto res = ll::memory::lookupSymbol(*vtable);
std::string getVTableName(void* vtable) {
auto res = ll::memory::lookupSymbol(*((void**)vtable));

if (res.size() == 1) {
return res[0].substr(4, res[0].size() - 9);
Expand All @@ -35,9 +35,9 @@ inline void forEachPacket(std::function<void(Packet const& packet, std::string c
while (packetId < 500) {
auto packet = MinecraftPackets::createPacket((MinecraftPacketIds)packetId);
if (packet) {
auto size = _msize((void**)packet.get() - 2);
auto size = ll::memory::getMemSizeFromPtr(packet);

auto className = getVTableName((void**)packet.get());
auto className = getVTableName(packet.get());

ll::logger.warn(
"Packet: enum: {}, getName: {}, vtable: {}, id: {},size: {}",
Expand All @@ -52,7 +52,7 @@ inline void forEachPacket(std::function<void(Packet const& packet, std::string c
ll::logger.error("intresting, different name, get: {}, typeid: {}", packet->getName(), className);
}

callback(*packet, className, size - 16);
callback(*packet, className, size);
}
packetId++;
}
Expand Down
4 changes: 2 additions & 2 deletions src/liteloader/test/TestCommandRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class TestCommand : public Command {
.addOptions(CommandParameterOption::EnumAutocompleteExpansion),
makeOptional<CommandParameterDataType::SoftEnum>(
&TestCommand::target,
"softEnumName",
"SoftEnumName",
"SoftEnumName",
&TestCommand::target_isSet
)
Expand Down Expand Up @@ -165,7 +165,7 @@ LL_AUTO_STATIC_HOOK(
origin(server, networkCommands, networkTestCommands, permissionsFile);
// Test CommandRegistry
try {
// TestCommand::setup(server.getCommands().getRegistry());
TestCommand::setup(server.getCommands().getRegistry());
} catch (std::exception const& e) {
ll::logger.error("Exception occurred in TestCommand::setup");
ll::logger.error("Error message: {} , type: {}", TextEncoding::toUTF8(e.what()), typeid(e).name());
Expand Down

0 comments on commit c276690

Please sign in to comment.