Skip to content
Open
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
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ add_subdirectory(third_party)
# regenny
#
file(GLOB_RECURSE regenny_sources "src/*")

if(MSVC)
list(FILTER regenny_sources EXCLUDE REGEX "src/arch/Linux.cpp$")
else()
list(FILTER regenny_sources EXCLUDE REGEX "src/arch/Windows.cpp$")
endif()

add_executable(regenny ${regenny_sources})
target_include_directories(regenny PRIVATE "src")
target_link_libraries(regenny PRIVATE
Expand All @@ -27,4 +34,4 @@ target_link_libraries(regenny PRIVATE
lua
sol2::sol2
luagenny
)
)
3 changes: 2 additions & 1 deletion src/Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

class Helpers {
public:
virtual ~Helpers() = default;
virtual std::map<uint32_t, std::string> processes() = 0;
};
};
2 changes: 2 additions & 0 deletions src/Process.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Process.hpp"

#include <cstring>

bool Process::read(uintptr_t address, void* buffer, size_t size) {
// If we're reading from read-only memory we can just use the cached version since it hasn't changed.
for (auto&& ro_allocation : m_read_only_allocations) {
Expand Down
4 changes: 3 additions & 1 deletion src/Process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Process {
public:
virtual ~Process() = default;

class Module {
public:
std::string name{};
Expand Down Expand Up @@ -74,4 +76,4 @@ class Process {
virtual std::optional<uintptr_t> handle_allocate(uintptr_t address, size_t size, uint64_t flags) {
return std::nullopt;
}
};
};
34 changes: 30 additions & 4 deletions src/ReGenny.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#include <cstdlib>
#include <type_traits>

#if __has_include(<ppl.h>)
#include <ppl.h>
#define REGENNY_HAS_PPL
#endif

#include <LuaGenny.h>
#include <fmt/format.h>
Expand Down Expand Up @@ -1021,8 +1024,7 @@ void ReGenny::rtti_sweep_ui() {
std::vector<uint8_t> base_data(m_type->size());
m_process->read(m_address, base_data.data(), base_data.size());

// for (size_t i = 0; i < base_data.size(); i += sizeof(void*)) {
concurrency::parallel_for(size_t{0}, base_data.size(), size_t{sizeof(void*)}, [&](size_t i) {
static const auto process_data = [&](std::size_t i) {
if (i + sizeof(void*) >= base_data.size()) {
return;
}
Expand All @@ -1043,7 +1045,19 @@ void ReGenny::rtti_sweep_ui() {
std::scoped_lock _{m_ui.rtti_lock};
m_ui.rtti_sweep_text += fmt::format("struct {:s}* @ 0x{:x}\n", *tname, (uintptr_t)i);
}
};

#ifdef REGENNY_HAS_PPL
concurrency::parallel_for(size_t{0}, base_data.size(), size_t{sizeof(void*)}, [&](size_t i) {
#else
for (size_t i = 0; i < base_data.size(); i += sizeof(void*)) {
#endif
process_data(i);
#ifdef REGENNY_HAS_PPL
});
#else
}
#endif

struct Chain {
uintptr_t base;
Expand Down Expand Up @@ -1073,8 +1087,7 @@ void ReGenny::rtti_sweep_ui() {

std::recursive_mutex local_mutex{};

// for (size_t i = 0; i < data.size(); i += sizeof(void*)) {
concurrency::parallel_for(size_t{0}, data.size(), size_t{sizeof(void*)}, [&](size_t i) {
static const auto process_data = [&](std::size_t i) {
if (i + sizeof(void*) >= data.size()) {
return;
}
Expand Down Expand Up @@ -1106,7 +1119,19 @@ void ReGenny::rtti_sweep_ui() {
std::scoped_lock _{local_mutex};
result.insert(result.end(), new_results.begin(), new_results.end());
}
};

#ifdef REGENNY_HAS_PPL
concurrency::parallel_for(size_t{0}, data.size(), size_t{sizeof(void*)}, [&](size_t i) {
#else
for (size_t i = 0; i < data.size(); i += sizeof(void*)) {
#endif
process_data(i);
#ifdef REGENNY_HAS_PPL
});
#else
}
#endif

// std::sort(result.begin(), result.end(), [](auto&& a, auto&& b) { return a.offset < b.offset; });

Expand Down Expand Up @@ -1311,6 +1336,7 @@ void ReGenny::memory_ui() {
void ReGenny::set_address() {
for (auto address : query_address_resolvers(m_ui.address)) {
auto addr_str = fmt::format("0x{:x}", address);
std::cout << addr_str << std::endl;
if (auto addr = parse_address(addr_str)) {
m_parsed_address = *addr;
}
Expand Down
6 changes: 6 additions & 0 deletions src/arch/Arch.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#ifdef _WIN32
#include "Windows.hpp"
#else
#include "Linux.hpp"
#endif

#include "Arch.hpp"

std::unique_ptr<Helpers> arch::make_helpers() {
#ifdef _WIN32
return std::make_unique<arch::WindowsHelpers>();
#else
return std::make_unique<arch::LinuxHelpers>();
#endif
}

std::unique_ptr<Process> arch::open_process(uint32_t process_id) {
#ifdef _WIN32
return std::make_unique<arch::WindowsProcess>(process_id);
#else
return std::make_unique<arch::LinuxProcess>(process_id);
#endif
}
164 changes: 164 additions & 0 deletions src/arch/Linux.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include "Linux.hpp"

#include <algorithm>
#include <array>
#include <cassert>
#include <charconv>
#include <csignal>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <fcntl.h>
#include <filesystem>
#include <fstream>
#include <map>
#include <optional>
#include <signal.h>
#include <string>
#include <string_view>
#include <sys/types.h>
#include <system_error>
#include <unistd.h>
#include <utility>
#include <vector>

namespace arch {

LinuxProcess::LinuxProcess(pid_t process_id) : m_process(process_id) {
std::string path = "/proc/" + std::to_string(process_id) + "/mem";
rw_handle = open(path.c_str(), O_RDWR);
assert(rw_handle >= 0); // TODO better error handling

std::fstream maps{"/proc/" + std::to_string(process_id) + "/maps", std::fstream::in};
assert(maps);

for (std::string line; std::getline(maps, line);) {
if (line.empty())
continue; // ?

std::uintptr_t begin = 0;
std::uintptr_t end = 0;
std::array<char, 3> perms{};
char shared = 0;
std::string name;

int offset = -1;

sscanf(line.c_str(), "%zx-%zx %c%c%c%c %*x %*x:%*x %*x%n", &begin, &end, &perms[0], &perms[1], &perms[2],
&shared, &offset);

while (offset < line.length() && line[offset] == ' ')
offset++;
if (offset < line.length())
name = line.c_str() + offset;

if (!name.empty()) {
constexpr static const char* DELETED_TAG = " (deleted)";
constexpr static std::size_t DELETED_TAG_LEN = std::char_traits<char>::length(DELETED_TAG);
if (name.ends_with(DELETED_TAG)) {
name = name.substr(0, name.length() - DELETED_TAG_LEN);
}

if (name[0] == '[') {
perms[0] = '-';
}
}

if (!name.empty()) {
auto it = std::ranges::find_if(m_modules, [&name](Module& module) { return module.name == name; });

if (it == m_modules.end()) {
m_modules.emplace_back(name, begin, end, end - begin);
} else {
it->start = std::min(it->start, begin);
it->end = std::min(it->end, end);
it->size = it->end - it->start;
}
}

m_allocations.emplace_back(begin, end, end - begin, perms[0] == 'r', perms[1] == 'w', perms[2] == 'x');

if (perms[0] == 'r') {
ReadOnlyAllocation ro_alloc{
Allocation(begin, end, end - begin, perms[0] == 'r', perms[1] == 'w', perms[2] == 'x'), {}};

ro_alloc.mem.reserve(end - begin);

if (read(begin, ro_alloc.mem.data(), end - begin)) {
m_read_only_allocations.emplace_back(std::move(ro_alloc));
}
}
}

maps.close();
}

uint32_t LinuxProcess::process_id() {
return m_process;
}

bool LinuxProcess::ok() {
return kill(m_process, 0) == 0;
}

std::optional<std::string> LinuxProcess::get_typename(uintptr_t ptr) {
// TODO
return {};
}
std::optional<std::string> LinuxProcess::get_typename_from_vtable(uintptr_t ptr) {
// TODO
return {};
}

bool LinuxProcess::handle_write(uintptr_t address, const void* buffer, size_t size) {
return pwrite(rw_handle, buffer, size, address) == static_cast<int>(size);
}
bool LinuxProcess::handle_read(uintptr_t address, void* buffer, size_t size) {
return pread(rw_handle, buffer, size, address) == static_cast<int>(size);
}
std::optional<uint64_t> LinuxProcess::handle_protect(uintptr_t address, size_t size, uint64_t flags) {
// TODO
return {};
}
std::optional<uintptr_t> LinuxProcess::handle_allocate(uintptr_t address, size_t size, uint64_t flags) {
// TODO
return {};
}

std::map<uint32_t, std::string> LinuxHelpers::processes() {
std::map<std::uint32_t, std::string> processes;
for (const std::filesystem::path& it : std::filesystem::directory_iterator{"/proc"}) {
if (!std::filesystem::is_directory(it))
continue;

const std::string& str = it.filename();

pid_t pid;
auto result = std::from_chars(str.data(), str.data() + str.size(), pid);

if (result.ec != std::errc{})
continue;

std::ifstream f{"/proc/" + str + "/status"};

std::string name;

for (std::string line; std::getline(f, line);) {
static constexpr std::string_view PATTERN = "Name:";

if (line.starts_with(PATTERN)) {
name = line.substr(line.find_first_not_of(' ', PATTERN.length() + 1));
break;
}
}

f.close();
if (name.empty())
continue;

processes.insert({pid, name});
}
return processes;
}

} // namespace arch
38 changes: 38 additions & 0 deletions src/arch/Linux.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <array>
#include <sys/types.h>

#include "Helpers.hpp"
#include "Process.hpp"

namespace arch {
class LinuxProcess : public Process {
public:
LinuxProcess(pid_t process_id);
~LinuxProcess() override = default;

uint32_t process_id() override;
bool ok() override;

std::optional<std::string> get_typename(uintptr_t ptr) override;
std::optional<std::string> get_typename_from_vtable(uintptr_t ptr) override;

protected:
bool handle_write(uintptr_t address, const void* buffer, size_t size) override;
bool handle_read(uintptr_t address, void* buffer, size_t size) override;
std::optional<uint64_t> handle_protect(uintptr_t address, size_t size, uint64_t flags) override;
std::optional<uintptr_t> handle_allocate(uintptr_t address, size_t size, uint64_t flags) override;

private:
pid_t m_process{};
int rw_handle;
};

class LinuxHelpers : public Helpers {
public:
~LinuxHelpers() override = default;

std::map<uint32_t, std::string> processes() override;
};
} // namespace arch
Loading