Skip to content

Commit

Permalink
Make code more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Nov 2, 2024
1 parent 6628dc7 commit 9d73f61
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
3 changes: 1 addition & 2 deletions RemoteInput/JVM.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ JVM::JVM() noexcept : vm(nullptr), createdVM(false), loadedJNI(false), module(nu
}
#endif

this->createdVM = false;
jint num_vms = 0;
const jint max_vms = 1;
JavaVM* vms[max_vms] = {0};
if (this->JNI_GetCreatedJavaVMs(vms, max_vms, &num_vms) == JNI_OK)
{
for (int i = 0; i < num_vms; ++i)
{
if (vms[i] != NULL)
if (vms[i] != nullptr)
{
this->vm = vms[i];
break;
Expand Down
74 changes: 44 additions & 30 deletions RemoteInput/Platform/Platform_Darwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@
#include <libproc.h>
#include <sys/syscall.h>
#include <mach-o/dyld.h>
#include <filesystem>
#endif // defined

#if defined(__APPLE__)
std::string StripPath(const std::string& path_to_strip)
{
return std::filesystem::path(path_to_strip).filename().string();
}

void GetDesktopResolution(int &width, int &height) noexcept
{
auto get_screen_resolution = [&]{
Expand Down Expand Up @@ -163,9 +169,13 @@ bool IsThreadAlive(std::int32_t tid) noexcept
for (std::uint32_t i = 0; i < count; ++i)
{
const char* name = _dyld_get_image_name(i);
if (!strcasestr(name, partial_module_name))
if (name)
{
result.push_back(name);
std::string module_name = StripPath(name);
if (!strcasestr(module_name.c_str(), partial_module_name))
{
result.push_back(name);
}
}
}

Expand All @@ -178,43 +188,47 @@ bool IsThreadAlive(std::int32_t tid) noexcept
for (std::uint32_t i = 0; i < count; ++i)
{
const char* name = _dyld_get_image_name(i);
if (strcasestr(name, module_name))
if (name)
{
return dlopen(name, RTLD_NOLOAD);

/*const struct mach_header* header = _dyld_get_image_header(i);
//std::intptr_t offset = _dyld_get_image_vmaddr_slide(i);
const struct load_command* cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char *>(header) + sizeof(struct mach_header));
if (header->magic == MH_MAGIC_64)
std::string stripped_name = StripPath(name);
if (strcasestr(stripped_name.c_str(), module_name))
{
cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char *>(header) + sizeof(struct mach_header_64));
}
return dlopen(name, RTLD_NOLOAD);

for (std::uint32_t j = 0; j < header->ncmds; ++j)
{
if (cmd->cmd == LC_SEGMENT)
{
const struct segment_command* seg = reinterpret_cast<const struct segment_command*>(cmd);
void* base_addr = reinterpret_cast<void*>(seg->vmaddr); //seg->vmaddr + offset;
/*const struct mach_header* header = _dyld_get_image_header(i);
//std::intptr_t offset = _dyld_get_image_vmaddr_slide(i);
Dl_info info = {0};
dladdr(base_addr, &info);
return dlopen(info.dli_fname, RTLD_NOLOAD);
const struct load_command* cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char *>(header) + sizeof(struct mach_header));
if (header->magic == MH_MAGIC_64)
{
cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char *>(header) + sizeof(struct mach_header_64));
}
if (cmd->cmd == LC_SEGMENT_64)
for (std::uint32_t j = 0; j < header->ncmds; ++j)
{
const struct segment_command_64* seg = reinterpret_cast<const struct segment_command_64*>(cmd);
void* base_addr = reinterpret_cast<void*>(seg->vmaddr); //seg->vmaddr + offset;
if (cmd->cmd == LC_SEGMENT)
{
const struct segment_command* seg = reinterpret_cast<const struct segment_command*>(cmd);
void* base_addr = reinterpret_cast<void*>(seg->vmaddr); //seg->vmaddr + offset;
Dl_info info = {0};
dladdr(base_addr, &info);
return dlopen(info.dli_fname, RTLD_NOLOAD);
}
Dl_info info = {0};
dladdr(base_addr, &info);
return dlopen(info.dli_fname, RTLD_NOLOAD);
}
cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char*>(cmd) + cmd->cmdsize);
}*/
if (cmd->cmd == LC_SEGMENT_64)
{
const struct segment_command_64* seg = reinterpret_cast<const struct segment_command_64*>(cmd);
void* base_addr = reinterpret_cast<void*>(seg->vmaddr); //seg->vmaddr + offset;
Dl_info info = {0};
dladdr(base_addr, &info);
return dlopen(info.dli_fname, RTLD_NOLOAD);
}
cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char*>(cmd) + cmd->cmdsize);
}*/
}
}
}
return dlopen(module_name, RTLD_NOLOAD);
Expand Down
15 changes: 11 additions & 4 deletions RemoteInput/Platform/Platform_Linux.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
#include <cstring>
#include <vector>
#include <algorithm>
#include <filesystem>
#endif // defined

#if defined(__linux__)
std::string StripPath(const std::string& path_to_strip)
{
return std::filesystem::path(path_to_strip).filename().string();
}

void GetDesktopResolution(int &width, int &height) noexcept
{
Display* display = XOpenDisplay(nullptr);
Expand Down Expand Up @@ -419,10 +425,10 @@ std::vector<std::string> GetLoadedModuleNames(const char* partial_module_name) n
if (info && info->dlpi_name)
{
Module* module_info = static_cast<Module*>(data);
if (strcasestr(info->dlpi_name, module_info->module_name))
std::string module_name = StripPath(info->dlpi_name);
if (strcasestr(module_name.c_str(), module_info->module_name))
{
module_info->result.push_back(info->dlpi_name);
return 1;
}
}
return 0;
Expand All @@ -441,10 +447,11 @@ void* GetModuleHandle(const char* module_name) noexcept
if (info && info->dlpi_name)
{
Module* module_info = static_cast<Module*>(data);
if (strcasestr(info->dlpi_name, module_info->module_name))
std::string module_name = StripPath(info->dlpi_name);
if (strcasestr(module_name.c_str(), module_info->module_name))
{
module_info->result = dlopen(module_info->module_name, RTLD_NOLOAD);
return 1;
return module_info->result ? 1 : 0;
}
}
return 0;
Expand Down

0 comments on commit 9d73f61

Please sign in to comment.