2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2025-01-13 23:28:23 +03:00

Fix crash and reworked find_memloc

This commit is contained in:
s1lent 2017-11-15 22:13:36 +07:00
parent 1be6696e6b
commit 5b7fe14723
No known key found for this signature in database
GPG Key ID: 0FE401DC73916B5C
5 changed files with 40 additions and 11 deletions

View File

@ -86,9 +86,9 @@ MPlugin* MPluginList::find(const char* findpath)
// Find a plugin that uses the given memory location.
MPlugin* MPluginList::find_memloc(void* memptr)
{
for (auto p : m_plugins) {
if (p->m_sys_module.load(memptr))
return p;
module_handle_t ptr = CSysModule::find(memptr);
if (ptr != CSysModule::INVALID_HANDLE) {
return find(ptr);
}
return nullptr;

View File

@ -1,6 +1,6 @@
#include "precompiled.h"
MRegCmd::MRegCmd(const char* cmd_name, REG_CMD_FN cmd_handler, MPlugin* cmd_plugin) : m_pfunction(cmd_handler), m_plugid(cmd_plugin->index()), m_status(RG_VALID)
MRegCmd::MRegCmd(const char* cmd_name, REG_CMD_FN cmd_handler, MPlugin* cmd_plugin) : m_pfunction(cmd_handler), m_plugid(cmd_plugin ? cmd_plugin->index() : 0), m_status(RG_VALID)
{
m_name = Q_strdup(cmd_name);
}

View File

@ -31,8 +31,7 @@ void EXT_FUNC meta_AddServerCommand(const char* cmd_name, void (*function)())
META_DEBUG(4, "called: meta_AddServerCommand; cmd_name=%s, function=%d, plugin=%s", cmd_name, function, plug ? plug->file() : "unknown");
if (!plug) {
META_ERROR("Failed to find memloc for regcmd '%s'", cmd_name);
return;
META_WARNING("Failed to find memloc for regcmd '%s'", cmd_name);
}
// See if this command was previously registered, ie a "reloaded" plugin.
@ -64,13 +63,11 @@ void EXT_FUNC meta_CVarRegister(cvar_t* pCvar)
// try to find which plugin is registering this cvar
if (!plug) {
META_ERROR("Failed to find memloc for regcvar '%s'", pCvar->name);
return;
META_WARNING("Failed to find memloc for regcvar '%s'", pCvar->name);
}
// See if this cvar was previously registered, ie a "reloaded" plugin.
auto reg = g_regCvars->find(pCvar->name);
if (!reg) {
reg = g_regCvars->add(pCvar, plug);
CVAR_REGISTER(reg->getcvar());
@ -89,7 +86,6 @@ void EXT_FUNC meta_CVarRegister(cvar_t* pCvar)
// commands) nor any useful actions to perform upon plugin unload (like the
// commands and cvars). This merely provides differently located storage
// for the string.
int EXT_FUNC meta_RegUserMsg(const char* pszName, int iSize)
{
char* cp = Q_strdup(pszName);

View File

@ -31,7 +31,14 @@ char *CSysModule::find_pattern(char *pos, int range, const char *pattern, size_t
module_handle_t CSysModule::load(void *addr)
{
MEMORY_BASIC_INFORMATION mem;
VirtualQuery(addr, &mem, sizeof(mem));
if (!VirtualQuery(addr, &mem, sizeof(mem)))
return INVALID_HANDLE;
if (mem.State != MEM_COMMIT)
return INVALID_HANDLE;
if (!mem.AllocationBase)
return INVALID_HANDLE;
IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)mem.AllocationBase;
IMAGE_NT_HEADERS *pe = (IMAGE_NT_HEADERS *)((uintptr_t)dos + (uintptr_t)dos->e_lfanew);
@ -47,6 +54,16 @@ module_handle_t CSysModule::load(void *addr)
return m_handle;
}
module_handle_t CSysModule::find(void *addr)
{
module_handle_t hHandle = INVALID_HANDLE;
if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCTSTR>(addr), &hHandle)) {
return INVALID_HANDLE;
}
return hHandle;
}
module_handle_t CSysModule::load(const char *filepath)
{
if (!m_handle) {
@ -121,6 +138,21 @@ module_handle_t CSysModule::load(void *addr)
return m_handle;
}
module_handle_t CSysModule::find(void *addr)
{
Dl_info dlinfo;
if ((!dladdr(addr, &dlinfo) && !dlinfo.dli_fbase) || !dlinfo.dli_fname) {
return false;
}
module_handle_t hHandle = INVALID_HANDLE;
if (!(hHandle = dlopen(dlinfo.dli_fname, RTLD_NOW | RTLD_NOLOAD))) {
return INVALID_HANDLE;
}
return hHandle;
}
module_handle_t CSysModule::load(const char *filepath)
{
if (!m_handle) {

View File

@ -24,6 +24,7 @@ public:
char *find_string_push(const char *string);
char *find_pattern(char *pos, int range, const char *pattern, size_t len);
static module_handle_t find(void *addr);
static const char *getloaderror();
static const module_handle_t INVALID_HANDLE;