From b492426a79eefeaf01c26a1f9067ac8aff15e43b Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Thu, 12 Mar 2026 20:01:31 -0400 Subject: [PATCH] Revert "Make `Address` a `int64` if `` is included (#2407)" This reverts commit 2b5554a21de7a3c1ae06644ca8bdceb03b2018c4. --- .gitmodules | 3 + bridge/include/LogicProvider.h | 2 + core/logic/AMBuilder | 11 ++- core/logic/PseudoAddrManager.cpp | 123 ++++++++++++++++++++++++++++ core/logic/PseudoAddrManager.h | 48 +++++++++++ core/logic/common_logic.cpp | 15 ++++ core/logic/common_logic.h | 5 ++ core/logic/libaddrz | 1 + core/logic/smn_core.cpp | 63 ++++---------- core/logic/smn_gameconfigs.cpp | 42 +++------- core/smn_entities.cpp | 33 ++------ core/sourcemod.cpp | 10 +++ core/sourcemod.h | 2 + extensions/dhooks/AMBuilder | 2 +- extensions/dhooks/natives.cpp | 47 +++-------- extensions/dhooks/vhook.cpp | 4 +- extensions/sdktools/vcaller.cpp | 42 +++------- extensions/sdktools/vdecoder.cpp | 19 ++--- plugins/include/sdktools.inc | 3 +- plugins/include/sourcemod.inc | 19 ++--- plugins/include/virtual_address.inc | 16 +--- public/ISourceMod.h | 10 +-- public/amtl | 2 +- sourcepawn | 2 +- 24 files changed, 300 insertions(+), 224 deletions(-) create mode 100644 core/logic/PseudoAddrManager.cpp create mode 100644 core/logic/PseudoAddrManager.h create mode 160000 core/logic/libaddrz diff --git a/.gitmodules b/.gitmodules index dce1debb..1a0be190 100644 --- a/.gitmodules +++ b/.gitmodules @@ -12,3 +12,6 @@ [submodule "public/safetyhook"] path = public/safetyhook url = https://github.com/alliedmodders/safetyhook +[submodule "core/logic/libaddrz"] + path = core/logic/libaddrz + url = https://github.com/dvander/libaddrz.git diff --git a/bridge/include/LogicProvider.h b/bridge/include/LogicProvider.h index 351eb7cf..7c01a0f2 100644 --- a/bridge/include/LogicProvider.h +++ b/bridge/include/LogicProvider.h @@ -71,6 +71,8 @@ struct sm_logic_t void (*RegisterProfiler)(IProfilingTool *tool); ICellArray * (*CreateCellArray)(size_t blocksize); void (*FreeCellArray)(ICellArray *arr); + void * (*FromPseudoAddress)(uint32_t pseudoAddr); + uint32_t (*ToPseudoAddress)(void *addr); void (*SetEntityLumpWritable)(bool writable); bool (*ParseEntityLumpString)(const char *entityString, int &status, size_t &position); const char * (*GetEntityLumpString)(); diff --git a/core/logic/AMBuilder b/core/logic/AMBuilder index a858f185..581f0c91 100644 --- a/core/logic/AMBuilder +++ b/core/logic/AMBuilder @@ -85,7 +85,16 @@ for cxx in builder.targets: 'FrameIterator.cpp', 'DatabaseConfBuilder.cpp', 'LumpManager.cpp', - 'smn_entitylump.cpp' + 'smn_entitylump.cpp', + 'libaddrz/addrz.cpp', + 'libaddrz/mapping.cpp', + 'libaddrz/platform.cpp', + 'libaddrz/proc_maps.cpp', + 'PseudoAddrManager.cpp', ] + if binary.compiler.target.platform == 'linux': + binary.sources += ['libaddrz/platform_linux.cpp'] + elif binary.compiler.target.platform == 'windows': + binary.sources += ['libaddrz/platform_windows.cpp'] SM.binaries += [builder.Add(binary)] diff --git a/core/logic/PseudoAddrManager.cpp b/core/logic/PseudoAddrManager.cpp new file mode 100644 index 00000000..c784545c --- /dev/null +++ b/core/logic/PseudoAddrManager.cpp @@ -0,0 +1,123 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod + * Copyright (C) 2004-2017 AlliedModders LLC. All rights reserved. + * ============================================================================= + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, version 3.0, as published by the + * Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + * + * As a special exception, AlliedModders LLC gives you permission to link the + * code of this program (as well as its derivative works) to "Half-Life 2," the + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software + * by the Valve Corporation. You must obey the GNU General Public License in + * all respects for all other code used. Additionally, AlliedModders LLC grants + * this exception to all derivative works. AlliedModders LLC defines further + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), + * or . + */ + +#include "PseudoAddrManager.h" +#include +#ifdef PLATFORM_APPLE +#include +#include +#endif +#ifdef PLATFORM_LINUX +#include +#endif +#ifdef PLATFORM_WINDOWS +#include +#endif + +PseudoAddressManager::PseudoAddressManager() : m_dictionary(am::IPlatform::GetDefault()) +{ +} + +void PseudoAddressManager::Initialize() { +#ifdef PLATFORM_WINDOWS + auto process = GetCurrentProcess(); + auto get_module_details = [process](const char* name, void*& baseAddress, size_t& moduleSize) { + if (process == NULL) { + return false; + } + auto hndl = GetModuleHandle(name); + if (hndl == NULL) { + return false; + } + MODULEINFO info; + if (!GetModuleInformation(process, hndl, &info, sizeof(info))) { + return false; + } + moduleSize = info.SizeOfImage; + baseAddress = info.lpBaseOfDll; + return true; + }; +#endif +#ifdef PLATFORM_LINUX + auto get_module_details = [](const char* name, void* baseAddress, size_t& moduleSize) { + auto hndl = dlopen(name, RTLD_NOLOAD); + if (hndl == NULL) { + return false; + } + void* addr = dlsym(hndl, "CreateInterface"); + dlclose(hndl); + + if (!addr) { + return false; + } + + Dl_info info; + if (dladdr(addr, &info) == 0) { + return false; + } + + baseAddress = info.dli_fbase; + // It doesn't matter much if we figure out the module size + // libaddrz coalesce maps on linux + moduleSize = 0; + return true; + }; +#endif + + // Early map commonly used modules, it's okay if not all of them are here + // Everything else will be caught by "ToPseudoAddress" but you risk running out of ranges by then + const char* libs[] = { "engine", "server", "tier0", "vstdlib" }; + + char formattedName[64]; + for (int i = 0; i < sizeof(libs) / sizeof(const char*); i++) { + bridge->FormatSourceBinaryName(libs[i], formattedName, sizeof(formattedName)); + void* base_addr = nullptr; + size_t module_size = 0; + if (get_module_details(formattedName, base_addr, module_size)) { + // Create the mapping (hopefully) + m_dictionary.Make32bitAddress(base_addr, module_size); + } + } +} + +void *PseudoAddressManager::FromPseudoAddress(uint32_t paddr) +{ + if (paddr == 0) { + return nullptr; + } + return m_dictionary.RecoverAddress(paddr).value_or(nullptr); +} + +uint32_t PseudoAddressManager::ToPseudoAddress(void *addr) +{ + if (addr == nullptr) { + return 0; + } + return m_dictionary.Make32bitAddress(addr).value_or(0); +} diff --git a/core/logic/PseudoAddrManager.h b/core/logic/PseudoAddrManager.h new file mode 100644 index 00000000..4c6ca59d --- /dev/null +++ b/core/logic/PseudoAddrManager.h @@ -0,0 +1,48 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod + * Copyright (C) 2004-2017 AlliedModders LLC. All rights reserved. + * ============================================================================= + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, version 3.0, as published by the + * Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + * + * As a special exception, AlliedModders LLC gives you permission to link the + * code of this program (as well as its derivative works) to "Half-Life 2," the + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software + * by the Valve Corporation. You must obey the GNU General Public License in + * all respects for all other code used. Additionally, AlliedModders LLC grants + * this exception to all derivative works. AlliedModders LLC defines further + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), + * or . + */ + +#ifndef _INCLUDE_SOURCEMOD_PSEUDOADDRESSMANAGER_H_ +#define _INCLUDE_SOURCEMOD_PSEUDOADDRESSMANAGER_H_ + +#include "common_logic.h" +#include "libaddrz/addrz.h" + +class PseudoAddressManager +{ +public: + PseudoAddressManager(); +public: + void *FromPseudoAddress(uint32_t paddr); + uint32_t ToPseudoAddress(void *addr); + void Initialize(); +private: + am::AddressDict m_dictionary; +}; + +#endif // _INCLUDE_SOURCEMOD_PSEUDOADDRESSMANAGER_H_ diff --git a/core/logic/common_logic.cpp b/core/logic/common_logic.cpp index d5c0456c..cc9e1de6 100644 --- a/core/logic/common_logic.cpp +++ b/core/logic/common_logic.cpp @@ -57,6 +57,7 @@ #include "RootConsoleMenu.h" #include "CellArray.h" #include "smn_entitylump.h" +#include "PseudoAddrManager.h" #include #include @@ -86,6 +87,7 @@ IScriptManager *scripts = &g_PluginSys; IExtensionSys *extsys = &g_Extensions; ILogger *logger = &g_Logger; CNativeOwner g_CoreNatives; +PseudoAddressManager pseudoAddr; EntityLumpParseResult lastParseResult; @@ -117,6 +119,16 @@ static void RegisterProfiler(IProfilingTool *tool) g_ProfileToolManager.RegisterTool(tool); } +static void *FromPseudoAddress(uint32_t paddr) +{ + return pseudoAddr.FromPseudoAddress(paddr); +} + +static uint32_t ToPseudoAddress(void *addr) +{ + return pseudoAddr.ToPseudoAddress(addr); +} + static void SetEntityLumpWritable(bool writable) { g_bLumpAvailableForWriting = writable; @@ -179,6 +191,8 @@ static sm_logic_t logic = RegisterProfiler, CellArray::New, CellArray::Free, + FromPseudoAddress, + ToPseudoAddress, SetEntityLumpWritable, ParseEntityLumpString, GetEntityLumpString, @@ -213,6 +227,7 @@ static void logic_init(CoreProvider* core, sm_logic_t* _logic) g_pSourcePawn2 = *core->spe2; SMGlobalClass::head = core->listeners; + pseudoAddr.Initialize(); g_ShareSys.Initialize(); g_pCoreIdent = g_ShareSys.CreateCoreIdentity(); diff --git a/core/logic/common_logic.h b/core/logic/common_logic.h index f2510212..faed421a 100644 --- a/core/logic/common_logic.h +++ b/core/logic/common_logic.h @@ -33,6 +33,9 @@ #define _INCLUDE_SOURCEMOD_COMMON_LOGIC_H_ #include "../sm_globals.h" +#ifdef KE_ARCH_X64 +#include "PseudoAddrManager.h" +#endif namespace SourceMod { class CoreProvider; @@ -57,6 +60,7 @@ class IVEngineServerBridge; #endif } // namespace SourceMod struct ServerGlobals; +class PseudoAddressManager; extern SourceMod::CoreProvider *bridge; extern SourceMod::IHandleSys *handlesys; @@ -76,6 +80,7 @@ extern SourceMod::IScriptManager *scripts; extern SourceMod::IExtensionSys *extsys; extern SourceMod::ILogger *logger; extern SourceMod::IMenuManager *menus; +extern PseudoAddressManager pseudoAddr; #if defined SM_LOGIC extern SourceMod::IVEngineServerBridge *engine; diff --git a/core/logic/libaddrz b/core/logic/libaddrz new file mode 160000 index 00000000..003d1cd0 --- /dev/null +++ b/core/logic/libaddrz @@ -0,0 +1 @@ +Subproject commit 003d1cd0a3e87ea0949811b305d9921b4debed74 diff --git a/core/logic/smn_core.cpp b/core/logic/smn_core.cpp index 6db6e766..0c98e481 100644 --- a/core/logic/smn_core.cpp +++ b/core/logic/smn_core.cpp @@ -59,6 +59,7 @@ #include #include #include +#include "PseudoAddrManager.h" #include using namespace SourceMod; @@ -864,13 +865,8 @@ enum NumberType static cell_t LoadFromAddress(IPluginContext *pContext, const cell_t *params) { void *addr = reinterpret_cast(params[1]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - addr = (void*)value; + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + addr = pseudoAddr.FromPseudoAddress(params[1]); } if (addr == NULL) @@ -899,13 +895,8 @@ static cell_t LoadFromAddress(IPluginContext *pContext, const cell_t *params) static cell_t StoreToAddress(IPluginContext *pContext, const cell_t *params) { void *addr = reinterpret_cast(params[1]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - addr = (void*)value; + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + addr = pseudoAddr.FromPseudoAddress(params[1]); } if (addr == NULL) @@ -959,19 +950,9 @@ static cell_t StoreToAddress(IPluginContext *pContext, const cell_t *params) static cell_t LoadAddressFromAddress(IPluginContext *pContext, const cell_t *params) { - cell_t shift_param = 0; - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - shift_param = 1; - } - - void *addr = reinterpret_cast(params[shift_param + 1]); - if (shift_param != 0) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[shift_param + 1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - addr = (void*)value; + void *addr = reinterpret_cast(params[1]); + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + addr = pseudoAddr.FromPseudoAddress(params[1]); } if (addr == NULL) @@ -982,14 +963,10 @@ static cell_t LoadAddressFromAddress(IPluginContext *pContext, const cell_t *par { return pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr); } - void* data = *reinterpret_cast(addr); - if (shift_param != 0) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - *reinterpret_cast(sp_addr) = reinterpret_cast(data); + void* data = *reinterpret_cast(addr); + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + return pseudoAddr.ToPseudoAddress(data); } return reinterpret_cast(data); } @@ -997,13 +974,8 @@ static cell_t LoadAddressFromAddress(IPluginContext *pContext, const cell_t *par static cell_t StoreAddressToAddress(IPluginContext *pContext, const cell_t *params) { void *addr = reinterpret_cast(params[1]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - addr = (void*)value; + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + addr = pseudoAddr.FromPseudoAddress(params[1]); } if (addr == NULL) @@ -1016,13 +988,8 @@ static cell_t StoreAddressToAddress(IPluginContext *pContext, const cell_t *para } void *data = reinterpret_cast(params[2]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[2], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - data = (void*)value; + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + data = pseudoAddr.FromPseudoAddress(params[2]); } bool updateMemAccess = params[3]; diff --git a/core/logic/smn_gameconfigs.cpp b/core/logic/smn_gameconfigs.cpp index efeacb47..f8c6cbc8 100644 --- a/core/logic/smn_gameconfigs.cpp +++ b/core/logic/smn_gameconfigs.cpp @@ -32,6 +32,7 @@ #include "common_logic.h" #include #include "GameConfigs.h" +#include "PseudoAddrManager.h" HandleType_t g_GameConfigsType; @@ -134,12 +135,7 @@ static cell_t smn_GameConfGetKeyValue(IPluginContext *pCtx, const cell_t *params static cell_t smn_GameConfGetAddress(IPluginContext *pCtx, const cell_t *params) { - cell_t shift_param = 0; - if (pCtx->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - shift_param = 1; - } - - Handle_t hndl = static_cast(params[shift_param + 1]); + Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; IGameConfig *gc; @@ -155,30 +151,20 @@ static cell_t smn_GameConfGetAddress(IPluginContext *pCtx, const cell_t *params) char *key; void* val; - pCtx->LocalToString(params[shift_param + 2], &key); + pCtx->LocalToString(params[2], &key); - if (!gc->GetAddress(key, &val)) { - val = nullptr; - } + if (!gc->GetAddress(key, &val)) + return 0; - if (shift_param != 0) { - cell_t* sp_addr; - if (int err = pCtx->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pCtx->ThrowNativeErrorEx(err, "Could not read argument"); - } - *reinterpret_cast(sp_addr) = reinterpret_cast(val); + if (pCtx->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + return pseudoAddr.ToPseudoAddress(val); } return reinterpret_cast(val); } static cell_t smn_GameConfGetMemSig(IPluginContext *pCtx, const cell_t *params) { - cell_t shift_param = 0; - if (pCtx->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - shift_param = 1; - } - - Handle_t hndl = static_cast(params[shift_param + 1]); + Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; IGameConfig *gc; @@ -194,19 +180,15 @@ static cell_t smn_GameConfGetMemSig(IPluginContext *pCtx, const cell_t *params) char *key; void *val; - pCtx->LocalToString(params[shift_param + 2], &key); + pCtx->LocalToString(params[2], &key); if (!gc->GetMemSig(key, &val)) { - val = nullptr; + return 0; } - if (shift_param != 0) { - cell_t* sp_addr; - if (int err = pCtx->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pCtx->ThrowNativeErrorEx(err, "Could not read argument"); - } - *reinterpret_cast(sp_addr) = reinterpret_cast(val); + if (pCtx->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + return pseudoAddr.ToPseudoAddress(val); } return reinterpret_cast(val); } diff --git a/core/smn_entities.cpp b/core/smn_entities.cpp index 1c0e2197..62a3cd74 100644 --- a/core/smn_entities.cpp +++ b/core/smn_entities.cpp @@ -725,13 +725,8 @@ static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params) static cell_t LoadEntityFromHandleAddress(IPluginContext *pContext, const cell_t *params) { void *addr = reinterpret_cast(params[1]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - addr = (void*)value; + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + addr = g_SourceMod.FromPseudoAddress(params[1]); } if (addr == NULL) @@ -840,13 +835,8 @@ static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params) static cell_t StoreEntityToHandleAddress(IPluginContext *pContext, const cell_t *params) { void *addr = reinterpret_cast(params[1]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - addr = (void*)value; + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + addr = g_SourceMod.FromPseudoAddress(params[1]); } if (addr == NULL) @@ -2774,23 +2764,14 @@ static cell_t SetEntityFlags(IPluginContext *pContext, const cell_t *params) static cell_t GetEntityAddress(IPluginContext *pContext, const cell_t *params) { - cell_t shift_param = 0; - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - shift_param = 1; - } - - CBaseEntity * pEntity = GetEntity(params[shift_param + 1]); + CBaseEntity * pEntity = GetEntity(params[1]); if (!pEntity) { return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); } - if (shift_param != 0) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - *reinterpret_cast(sp_addr) = reinterpret_cast(pEntity); + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + return g_SourceMod.ToPseudoAddress(pEntity); } return reinterpret_cast(pEntity); } diff --git a/core/sourcemod.cpp b/core/sourcemod.cpp index 5f2dde5f..be595032 100644 --- a/core/sourcemod.cpp +++ b/core/sourcemod.cpp @@ -832,6 +832,16 @@ bool SourceModBase::IsMapRunning() return g_OnMapStarted; } +void *SourceModBase::FromPseudoAddress(uint32_t pseudoAddr) +{ + return logicore.FromPseudoAddress(pseudoAddr); +} + +uint32_t SourceModBase::ToPseudoAddress(void *addr) +{ + return logicore.ToPseudoAddress(addr); +} + class ConVarRegistrar : public IConCommandBaseAccessor, public SMGlobalClass diff --git a/core/sourcemod.h b/core/sourcemod.h index 3667f227..0b4eb23e 100644 --- a/core/sourcemod.h +++ b/core/sourcemod.h @@ -135,6 +135,8 @@ public: // ISourceMod int GetPluginId(); int GetShApiVersion(); bool IsMapRunning(); + void *FromPseudoAddress(uint32_t pseudoAddr); + uint32_t ToPseudoAddress(void *addr); private: void ShutdownServices(); private: diff --git a/extensions/dhooks/AMBuilder b/extensions/dhooks/AMBuilder index 3ea79871..fb31852f 100644 --- a/extensions/dhooks/AMBuilder +++ b/extensions/dhooks/AMBuilder @@ -50,7 +50,7 @@ for cxx in builder.targets: SM.AddCDetour(binary) if binary.compiler.target.arch == 'x86': - #binary.sources += ['../../sourcepawn/vm/x86/assembler-x86.cpp'] + binary.sources += ['../../sourcepawn/vm/x86/assembler-x86.cpp'] binary.compiler.cxxincludes += [ os.path.join(builder.sourcePath, 'public', 'jit', 'x86'), diff --git a/extensions/dhooks/natives.cpp b/extensions/dhooks/natives.cpp index 8127ae3e..1310b530 100644 --- a/extensions/dhooks/natives.cpp +++ b/extensions/dhooks/natives.cpp @@ -174,14 +174,9 @@ cell_t Native_CreateHook(IPluginContext *pContext, const cell_t *params) //native Handle:DHookCreateDetour(Address:funcaddr, CallingConvention:callConv, ReturnType:returntype, ThisPointerType:thistype); cell_t Native_CreateDetour(IPluginContext *pContext, const cell_t *params) { - void *addr = reinterpret_cast(params[1]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - addr = (void*)value; + void* addr = reinterpret_cast(params[1]); + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + addr = g_pSM->FromPseudoAddress(params[1]); } HookSetup *setup = new HookSetup((ReturnType)params[3], PASSFLAG_BYVAL, (CallingConvention)params[2], (ThisPointerType)params[4], addr); @@ -651,14 +646,9 @@ cell_t HookRawImpl(IPluginContext *pContext, const cell_t *params, int callbackI if (removalcbIndex > 0) removalcb = pContext->GetFunctionById(params[removalcbIndex]); - void *iface = reinterpret_cast(params[3]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[3], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - iface = (void*)value; + void* iface = reinterpret_cast(params[3]); + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + iface = g_pSM->FromPseudoAddress(params[3]); } for(int i = g_pHooks.size() -1; i >= 0; i--) @@ -1479,24 +1469,19 @@ cell_t Native_IsNullParam(IPluginContext *pContext, const cell_t *params) //native Address:DHookGetParamAddress(Handle:hParams, num); cell_t Native_GetParamAddress(IPluginContext *pContext, const cell_t *params) { - cell_t shift_param = 0; - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - shift_param = 1; - } - HookParamsStruct *paramStruct; - if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)¶mStruct, pContext, params[shift_param + 1])) + if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)¶mStruct, pContext, params[1])) { return 0; } - if(params[shift_param + 2] <= 0 || params[shift_param + 2] > (int)paramStruct->dg->params.size()) + if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size()) { - return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[shift_param + 2], paramStruct->dg->params.size()); + return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size()); } - int index = params[shift_param + 2] - 1; + int index = params[2] - 1; HookParamType type = paramStruct->dg->params.at(index).type; if(type != HookParamType_StringPtr && type != HookParamType_CharPtr && type != HookParamType_VectorPtr && type != HookParamType_CBaseEntity && type != HookParamType_ObjectPtr && type != HookParamType_Edict && type != HookParamType_Unknown) @@ -1506,16 +1491,10 @@ cell_t Native_GetParamAddress(IPluginContext *pContext, const cell_t *params) size_t offset = GetParamOffset(paramStruct, index); - auto return_ptr = *(void**)((intptr_t)paramStruct->orgParams + offset); - - if (shift_param != 0) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - *reinterpret_cast(sp_addr) = reinterpret_cast(return_ptr); + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + return g_pSM->ToPseudoAddress(*(void**)((intptr_t)paramStruct->orgParams + offset)); } - return reinterpret_cast(return_ptr); + return *(cell_t *)((intptr_t)paramStruct->orgParams + offset); } sp_nativeinfo_t g_Natives[] = diff --git a/extensions/dhooks/vhook.cpp b/extensions/dhooks/vhook.cpp index 0dae16fe..673c0b80 100644 --- a/extensions/dhooks/vhook.cpp +++ b/extensions/dhooks/vhook.cpp @@ -473,8 +473,8 @@ cell_t GetThisPtr(IPluginContext* pContext, void *iface, ThisPointerType type) return -1; return gamehelpers->EntityToBCompatRef((CBaseEntity *)iface); } - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - return 0; + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + return g_pSM->ToPseudoAddress(iface); } return (cell_t)iface; } diff --git a/extensions/sdktools/vcaller.cpp b/extensions/sdktools/vcaller.cpp index 1dce3967..3ce3e438 100644 --- a/extensions/sdktools/vcaller.cpp +++ b/extensions/sdktools/vcaller.cpp @@ -88,11 +88,11 @@ inline void DecodePassMethod(ValveType vtype, SDKPassMethod method, PassType &ty static cell_t StartPrepSDKCall(IPluginContext *pContext, const cell_t *params) { auto call_type = (ValveCallType)params[1]; - if (call_type == ValveCall_Raw && pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - return pContext->ThrowNativeError("SDKCall_Raw is unavailable for plugins that have enabled int64 address."); + if (call_type == ValveCall_Raw && pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + return pContext->ThrowNativeError("SDKCall_Raw is unavailable for plugins that have enabled virtual address."); } - if (call_type == ValveCall_VirtualAddress && pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) != SP_ERROR_NONE) { - return pContext->ThrowNativeError("SDKCall_VirtualAddress is unavailable for plugins that haven't enabled int64 address."); + if (call_type == ValveCall_VirtualAddress && pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) != SP_ERROR_NONE) { + return pContext->ThrowNativeError("SDKCall_VirtualAddress is unavailable for plugins that haven't enabled virtual address."); } s_numparams = 0; s_vtbl_index = -1; @@ -181,13 +181,8 @@ static cell_t PrepSDKCall_SetSignature(IPluginContext *pContext, const cell_t *p static cell_t PrepSDKCall_SetAddress(IPluginContext *pContext, const cell_t *params) { s_call_addr = reinterpret_cast(params[1]); - if (pContext->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) { - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - s_call_addr = (void*)value; + if (pContext->GetRuntime()->FindPubvarByName("__Virtual_Address__", nullptr) == SP_ERROR_NONE) { + s_call_addr = g_pSM->FromPseudoAddress(params[1]); } return (s_call_addr != NULL) ? 1 : 0; @@ -436,12 +431,9 @@ static cell_t SDKCall(IPluginContext *pContext, const cell_t *params) } //note: varargs pawn args are passed by-ref - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[startparam], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - auto value = *reinterpret_cast(sp_addr); - void* thisptr = (void*)value; + cell_t *cell; + pContext->LocalToPhysAddr(params[startparam], &cell); + void* thisptr = reinterpret_cast(g_pSM->FromPseudoAddress(*cell)); if (thisptr == nullptr) { @@ -475,9 +467,6 @@ static cell_t SDKCall(IPluginContext *pContext, const cell_t *params) startparam += 2; } else if (vc->retinfo->vtype == Valve_Vector || vc->retinfo->vtype == Valve_QAngle) - { - startparam += 1; - } else if (vc->retinfo->vtype == Valve_VirtualAddress) { startparam += 1; } @@ -585,21 +574,12 @@ static cell_t SDKCall(IPluginContext *pContext, const cell_t *params) } return *addr ? 1 : 0; } else if (vc->retinfo->vtype == Valve_VirtualAddress) { - if (numparams < 3) - { - return pContext->ThrowNativeError("Expected argument (3) for VirtualAddress storage"); - } - void *addr = *(void **)vc->retbuf; + void *addr = *(void **)vc->retbuf; if (vc->retinfo->flags & PASSFLAG_ASPOINTER) { addr = *(void **)addr; } - cell_t* sp_addr; - if (int err = pContext->LocalToPhysAddr(params[retparam], &sp_addr); err != SP_ERROR_NONE) { - return pContext->ThrowNativeErrorEx(err, "Could not read argument"); - } - *reinterpret_cast(sp_addr) = reinterpret_cast(addr); - return 0; + return g_pSM->ToPseudoAddress(addr); } else { cell_t *addr = (cell_t *)vc->retbuf; if (vc->retinfo->flags & PASSFLAG_ASPOINTER) diff --git a/extensions/sdktools/vdecoder.cpp b/extensions/sdktools/vdecoder.cpp index 0b99498f..b2d52a8d 100644 --- a/extensions/sdktools/vdecoder.cpp +++ b/extensions/sdktools/vdecoder.cpp @@ -295,16 +295,14 @@ DataStatus EncodeValveParam(IPluginContext *pContext, } case Valve_VirtualAddress: { + cell_t *addr; + pContext->LocalToPhysAddr(param, &addr); + if (data->flags & PASSFLAG_ASPOINTER) { buffer = *(void **)buffer; } - - cell_t* sp_addr; - if (pContext->LocalToPhysAddr(param, &sp_addr) != SP_ERROR_NONE) { - return Data_Fail; - } - *reinterpret_cast(sp_addr) = reinterpret_cast(*(void**)buffer); + *addr = g_pSM->ToPseudoAddress(*(void**)buffer); return Data_Okay; } @@ -628,14 +626,7 @@ DataStatus DecodeValveParam(IPluginContext *pContext, *(void **)buffer = (unsigned char *)_buffer + pCall->stackEnd + data->obj_offset; buffer = *(void **)buffer; } - - cell_t* sp_addr; - if (pContext->LocalToPhysAddr(param, &sp_addr) != SP_ERROR_NONE) { - return Data_Fail; - } - void* addr = (void*)(*reinterpret_cast(sp_addr)); - - + void* addr = g_pSM->FromPseudoAddress(param); if (addr == nullptr && (data->decflags & VDECODE_FLAG_ALLOWNULL) == 0) { pContext->ThrowNativeError("NULL Address not allowed"); diff --git a/plugins/include/sdktools.inc b/plugins/include/sdktools.inc index d7737514..15d1dfda 100644 --- a/plugins/include/sdktools.inc +++ b/plugins/include/sdktools.inc @@ -90,7 +90,7 @@ enum SDKType SDKType_Edict, /**< edict_t (always as pointer) */ SDKType_String, /**< NULL-terminated string (always as pointer) */ SDKType_Bool, /**< Boolean (any) */ - SDKType_VirtualAddress, /**< SM Virtual Address (unavailable if plugin doesn't include ) */ + SDKType_VirtualAddress, /**< SM Virtual Address */ }; enum SDKPassMethod @@ -192,7 +192,6 @@ native Handle EndPrepSDKCall(); * If the return value is a Vector or QAngles, the SECOND parameter must be a Float[3]. * If the return value is a string, the THIRD parameter must be a String buffer, and the * FOURTH parameter must be the maximum length. - * If the return value is a Virtual Address, the SECOND parameter must be an Address variable. * All parameters must be passed after the above is followed. Failure to follow these * rules will result in crashes or wildly unexpected behavior! * diff --git a/plugins/include/sourcemod.inc b/plugins/include/sourcemod.inc index 1430d3a6..1e6743b3 100644 --- a/plugins/include/sourcemod.inc +++ b/plugins/include/sourcemod.inc @@ -78,20 +78,6 @@ struct Plugin #include #include -#if defined _virtual_address_included -typedef Address = int64; -public const int64 __Int64_Address__ = 0; - -/**< Equivallent to nullptr */ -public const int64 Address_Null = 0; -/**< Size of a pointer in bytes (same as `sizeof(void*)`) */ -public const int64 Address_PointerSize = 0; -#else -enum Address { - Address_Null = 0 -}; -#endif - enum APLRes { APLRes_Success = 0, /**< Plugin should load */ @@ -739,6 +725,11 @@ enum NumberType NumberType_Int32 }; +enum Address +{ + Address_Null = 0 // a typical invalid result when an address lookup fails +}; + /** * Load up to 4 bytes from a memory address. * diff --git a/plugins/include/virtual_address.inc b/plugins/include/virtual_address.inc index 1afb7078..542b4244 100644 --- a/plugins/include/virtual_address.inc +++ b/plugins/include/virtual_address.inc @@ -1,7 +1,7 @@ /** * vim: set ts=4 : * ============================================================================= - * SourceMod (C)2026 AlliedModders LLC. All rights reserved. + * SourceMod (C)2004-2025 AlliedModders LLC. All rights reserved. * ============================================================================= * * This file is part of the SourceMod/SourcePawn SDK. @@ -35,15 +35,5 @@ #endif #define _virtual_address_included -#if defined _sourcemod_included -#error must be included before ! -#endif - -// Including this file in your plugin will turn on SM's Virtual Address -// -// For pointer-sized reading/storage, we recommend the usage of -// - LoadAddressFromAddress over LoadFromAddress -// - StoreAddressToAddress over StoreToAddress -// -// For SDKCalls involving raw pointers, we discourage the usage of -// - SDKType_PlainOldData (32 bits wide) - Use SDKType_VirtualAddress instead \ No newline at end of file +public const Address __Virtual_Address__ = view_as
(0); +public const Address PointerSize = view_as
(0); /**< Size of a pointer in bytes (same as `sizeof(void*)`) */ diff --git a/public/ISourceMod.h b/public/ISourceMod.h index 802cfa43..78c327bc 100644 --- a/public/ISourceMod.h +++ b/public/ISourceMod.h @@ -319,20 +319,18 @@ namespace SourceMod virtual bool IsMapRunning() = 0; /** - * @brief DEPRECATED DO NOT USE - * Converts 32-bit pseudo address to memory address on 64-bit platforms. + * @brief Converts 32-bit pseudo address to memory address on 64-bit platforms. * * @return Memory address, or nullptr if pseudo address could not be converted. */ - virtual void *FromPseudoAddress(uint32_t pseudoAddr) { return nullptr; } + virtual void *FromPseudoAddress(uint32_t pseudoAddr) = 0; /** - * @brief DEPRECATED DO NOT USE - * Converts memory address to 32-bit pseudo address on 64-bit platforms. + * @brief Converts memory address to 32-bit pseudo address on 64-bit platforms. * * @return Pseudo address, or 0 if memory address could not be converted. */ - virtual uint32_t ToPseudoAddress(void *addr) { return 0; } + virtual uint32_t ToPseudoAddress(void *addr) = 0; }; } diff --git a/public/amtl b/public/amtl index 77552c9c..2d3b1a33 160000 --- a/public/amtl +++ b/public/amtl @@ -1 +1 @@ -Subproject commit 77552c9c0dc91edf8c4fd8ce801c51e026f7be75 +Subproject commit 2d3b1a3378a3728637f26660c9ffc2df3189cf62 diff --git a/sourcepawn b/sourcepawn index d04cd924..11b22edb 160000 --- a/sourcepawn +++ b/sourcepawn @@ -1 +1 @@ -Subproject commit d04cd924fc4b8fad3ff6fc3d7ad910abeccb5d71 +Subproject commit 11b22edb634b9764d19fd28699e03289cfd18520