diff --git a/core/logic/ForwardSys.cpp b/core/logic/ForwardSys.cpp index 5cb5f986..01047892 100644 --- a/core/logic/ForwardSys.cpp +++ b/core/logic/ForwardSys.cpp @@ -174,7 +174,8 @@ void CForwardManager::OnPluginPauseChange(IPlugin *plugin, bool paused) *************************************/ CForward::CForward(ExecType et, const char *name, const ParamType *types, unsigned num_params) - : m_numparams(0), + : m_addresses(0), + m_numparams(0), m_ExecType(et), m_errstate(SP_ERROR_NONE), m_ExecDepth(0), @@ -182,8 +183,11 @@ CForward::CForward(ExecType et, const char *name, const ParamType *types, unsign { ke::SafeStrcpy(m_name, sizeof(m_name), name ? name : ""); - for (unsigned i = 0; i < num_params; i++) + for (unsigned i = 0; i < num_params; i++) { m_types[i] = types[i]; + if (types[i] == Param_SMAddress) + m_addresses |= 1u << i; + } m_numparams = num_params; } @@ -210,6 +214,36 @@ CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int nu return new CForward(et, name, _types, num_params); } +bool InvokePluginFunction(IPluginFunction *func, const CallArgs& args, AddressMask addresses, cell_t *result) +{ + // Plugins compiled before SourceMod 1.13 take an Address as one cell and cannot read an int64 argument. Like DHooks, + // pass them the low 32 bits of an Address instead. Any other int64 has no single cell equivalent, so they cannot be + // called with one. + bool has_int64 = false; + for (uint32_t i = 0; i < args.argc; i++) + has_int64 |= args.argv[i].type == CallArgs::ARG_INT64; + if (!has_int64 || func->GetParentRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) + return func->Invoke(args, result); + + CallArgs legacy_args = args; + for (uint32_t i = 0; i < legacy_args.argc; i++) { + auto& arg = legacy_args.argv[i]; + if (arg.type != CallArgs::ARG_INT64) + continue; + if (!(addresses & (1u << i))) { + IPluginContext *ctx = func->GetParentRuntime()->GetDefaultContext(); + IPlugin *plugin = pluginsys->FindPluginByContext(ctx); + ctx->ReportError("Plugin \"%s\" was compiled before SourceMod 1.13 and cannot receive int64 argument %u", + plugin ? plugin->GetFilename() : "", i + 1); + return false; + } + arg.u.value = static_cast(arg.u.i64); + arg.type = CallArgs::ARG_CELL; + arg.flags = 0; + } + return func->Invoke(legacy_args, result); +} + bool CForward::ProcessArguments(const CallArgs& args) { if (args.error) return !SetError(SP_ERROR_PARAMS_MAX); @@ -250,6 +284,10 @@ bool CForward::ProcessArguments(const CallArgs& args) { if (pt != Param_String) return !SetError(SP_ERROR_PARAM); break; + case CallArgs::ARG_INT64: + if (pt != Param_Int64 && pt != Param_SMAddress) + return !SetError(SP_ERROR_PARAM); + break; } } @@ -298,7 +336,7 @@ int CForward::Execute(const sp::CallArgs& in_args, cell_t *result, IForwardFilte ExceptionHandler eh(func->GetParentRuntime()); - if (!func->Invoke(args, &cur_result)) + if (!InvokePluginFunction(func, args, m_addresses, &cur_result)) continue; success++; diff --git a/core/logic/ForwardSys.h b/core/logic/ForwardSys.h index f7bbe6af..1d1a9a16 100644 --- a/core/logic/ForwardSys.h +++ b/core/logic/ForwardSys.h @@ -39,6 +39,14 @@ typedef ReentrantList::iterator FuncIter; /* :TODO: a global name max define for sourcepawn, should mirror compiler's sNAMEMAX */ #define FORWARDS_NAME_MAX 64 +// The argument positions that hold an Address, one bit per position. +using AddressMask = uint32_t; +static_assert(SP_MAX_EXEC_PARAMS <= sizeof(AddressMask) * 8, "AddressMask must have a bit for every parameter"); + +// Invokes a plugin function, passing the given Address arguments as one cell to plugins compiled before SourceMod 1.13, +// and refusing to pass those plugins any other int64 argument. +bool InvokePluginFunction(IPluginFunction *func, const sp::CallArgs& args, AddressMask addresses, cell_t *result); + class CForward : public IChangeableForward { friend class CForwardManager; @@ -88,6 +96,7 @@ protected: /* Type and name information */ sp::CallArgs default_args_; ParamType m_types[SP_MAX_EXEC_PARAMS]; + AddressMask m_addresses; char m_name[FORWARDS_NAME_MAX+1]; unsigned int m_numparams; ExecType m_ExecType; diff --git a/core/logic/smn_functions.cpp b/core/logic/smn_functions.cpp index 4ca3565d..aea1f08f 100644 --- a/core/logic/smn_functions.cpp +++ b/core/logic/smn_functions.cpp @@ -32,6 +32,7 @@ #include #include "common_logic.h" +#include "ForwardSys.h" #include #include #include @@ -43,6 +44,7 @@ HandleType_t g_PrivateFwdType = 0; static bool s_CallStarted = false; static sp::CallArgs sArgs; +static AddressMask s_Addresses = 0; static IPluginFunction *s_pFunction = NULL; static IForward *s_pForward = NULL; @@ -98,6 +100,7 @@ inline void ResetCall() s_pFunction = NULL; s_pForward = NULL; sArgs.Reset(); + s_Addresses = 0; } static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params) @@ -392,6 +395,38 @@ static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params) return 1; } +static cell_t sm_CallPushInt64(IPluginContext *pContext, const cell_t *params) +{ + cell_t *addr; + + if (!s_CallStarted) + { + return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress"); + } + + // 64-bit integer arguments are passed by reference. + if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE) + return pContext->ThrowNativeErrorEx(err, nullptr); + + int64_t value; + memcpy(&value, addr, sizeof(value)); + sArgs.PushInt64(value); + + if (sArgs.error) + return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr); + + return 1; +} + +static cell_t sm_CallPushAddress(IPluginContext *pContext, const cell_t *params) +{ + uint32_t index = sArgs.argc; + cell_t ret = sm_CallPushInt64(pContext, params); + if (s_CallStarted && !sArgs.error) + s_Addresses |= 1u << index; + return ret; +} + static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params) { int err; @@ -556,6 +591,7 @@ static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeErrorEx(err, nullptr); auto local_args = sArgs; + auto local_addresses = s_Addresses; // Note: ExceptionHandler and Execute() swallow exceptions, so this is okay. if (s_pFunction) @@ -563,7 +599,7 @@ static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params) IPluginFunction *pFunction = s_pFunction; ResetCall(); ExceptionHandler eh(pContext); - if (!pFunction->Invoke(local_args, result)) + if (!InvokePluginFunction(pFunction, local_args, local_addresses, result)) return eh.Code(); } else { IForward *pForward = s_pForward; @@ -656,6 +692,8 @@ REGISTER_NATIVES(functionNatives) {"Call_StartForward", sm_CallStartForward}, {"Call_PushCell", sm_CallPushCell}, {"Call_PushCellRef", sm_CallPushCellRef}, + {"Call_PushInt64", sm_CallPushInt64}, + {"Call_PushAddress", sm_CallPushAddress}, {"Call_PushFloat", sm_CallPushFloat}, {"Call_PushFloatRef", sm_CallPushFloatRef}, {"Call_PushArray", sm_CallPushArray}, diff --git a/plugins/include/functions.inc b/plugins/include/functions.inc index 2f8e0181..2c948587 100644 --- a/plugins/include/functions.inc +++ b/plugins/include/functions.inc @@ -48,7 +48,9 @@ enum ParamType Param_String = (3<<1)|SP_PARAMFLAG_BYREF, /**< Only strings can be pushed */ Param_Array = (4<<1)|SP_PARAMFLAG_BYREF, /**< Only arrays can be pushed */ Param_CellByRef = (1<<1)|SP_PARAMFLAG_BYREF, /**< Only a cell by reference can be pushed */ - Param_FloatByRef = (2<<1)|SP_PARAMFLAG_BYREF /**< Only a float by reference can be pushed */ + Param_FloatByRef = (2<<1)|SP_PARAMFLAG_BYREF, /**< Only a float by reference can be pushed */ + Param_Int64 = (5<<1), /**< Only 64-bit integers can be pushed */ + Param_SMAddress = (6<<1) /**< Only Addresses can be pushed */ }; /** @@ -302,6 +304,31 @@ native void Call_StartFunction(Handle plugin, Function func); */ native void Call_PushCell(any value); +/** + * Pushes a 64-bit integer onto the current call. + * + * @note Plugins compiled before SourceMod 1.13 cannot receive a 64-bit integer. + * Calling one of them fails with an error. Use Call_PushAddress for an + * Address. + * @note Cannot be used before a call has been started. + * + * @param value 64-bit integer value to push. + * @error Called before a call has been started. + */ +native void Call_PushInt64(int64 value); + +/** + * Pushes an Address onto the current call. + * + * @note Unlike Call_PushInt64, plugins compiled before SourceMod 1.13 + * receive the low 32 bits of the address. + * @note Cannot be used before a call has been started. + * + * @param value Address to push. + * @error Called before a call has been started. + */ +native void Call_PushAddress(Address value); + /** * Pushes a cell by reference onto the current call. * diff --git a/plugins/testsuite/mock/test_int64_forward.sp b/plugins/testsuite/mock/test_int64_forward.sp new file mode 100644 index 00000000..5b10d06f --- /dev/null +++ b/plugins/testsuite/mock/test_int64_forward.sp @@ -0,0 +1,133 @@ +#pragma semicolon 1 +#pragma newdecls required + +#include +#include + +// Has both halves set, so a value truncated to one cell fails the check. +int64 g_Value; +int g_Calls; + + +public void OnPluginStart() +{ + g_Value = (view_as(0x12345678) << 32) | view_as(0x0badf00d); + + Test_PushInt64ToFunction(); + Test_PushInt64ToGlobalForward(); + Test_PushInt64ToAnyParam(); + Test_PushInt64ToCellParam(); + Test_PushAddressToFunction(); + Test_PushAddressToGlobalForward(); +} + + +void Test_PushInt64ToFunction() +{ + SetTestContext("Test Call_PushInt64 to a function"); + + g_Calls = 0; + Call_StartFunction(INVALID_HANDLE, __OnInt64); + Call_PushInt64(g_Value); + Call_PushCell(2); + AssertEq("Call_Finish", Call_Finish(), SP_ERROR_NONE); + AssertEq("Calls", g_Calls, 1); +} + + +void Test_PushInt64ToGlobalForward() +{ + SetTestContext("Test Call_PushInt64 to a Param_Int64 forward"); + + GlobalForward fwd = new GlobalForward("__OnInt64", ET_Ignore, Param_Int64, Param_Cell); + + g_Calls = 0; + Call_StartForward(fwd); + Call_PushInt64(g_Value); + Call_PushCell(2); + AssertEq("Call_Finish", Call_Finish(), SP_ERROR_NONE); + AssertEq("Calls", g_Calls, 1); + + delete fwd; +} + + +void Test_PushInt64ToAnyParam() +{ + SetTestContext("Test Call_PushInt64 to a Param_Any forward"); + + PrivateForward fwd = new PrivateForward(ET_Ignore, Param_Any, Param_Cell); + fwd.AddFunction(INVALID_HANDLE, __OnInt64); + + g_Calls = 0; + Call_StartForward(fwd); + Call_PushInt64(g_Value); + Call_PushCell(2); + AssertEq("Call_Finish", Call_Finish(), SP_ERROR_NONE); + AssertEq("Calls", g_Calls, 1); + + delete fwd; +} + + +void Test_PushInt64ToCellParam() +{ + SetTestContext("Test Call_PushInt64 to a Param_Cell forward"); + + PrivateForward fwd = new PrivateForward(ET_Ignore, Param_Cell, Param_Cell); + fwd.AddFunction(INVALID_HANDLE, __OnInt64); + + g_Calls = 0; + Call_StartForward(fwd); + Call_PushInt64(g_Value); + Call_PushCell(2); + AssertEq("Call_Finish", Call_Finish(), SP_ERROR_PARAM); + AssertEq("Calls", g_Calls, 0); + + delete fwd; +} + + +void Test_PushAddressToFunction() +{ + SetTestContext("Test Call_PushAddress to a function"); + + g_Calls = 0; + Call_StartFunction(INVALID_HANDLE, __OnAddress); + Call_PushAddress(view_as
(g_Value)); + Call_PushCell(2); + AssertEq("Call_Finish", Call_Finish(), SP_ERROR_NONE); + AssertEq("Calls", g_Calls, 1); +} + + +void Test_PushAddressToGlobalForward() +{ + SetTestContext("Test Call_PushAddress to a Param_SMAddress forward"); + + GlobalForward fwd = new GlobalForward("__OnAddress", ET_Ignore, Param_SMAddress, Param_Cell); + + g_Calls = 0; + Call_StartForward(fwd); + Call_PushAddress(view_as
(g_Value)); + Call_PushCell(2); + AssertEq("Call_Finish", Call_Finish(), SP_ERROR_NONE); + AssertEq("Calls", g_Calls, 1); + + delete fwd; +} + +public void __OnInt64(int64 value, int marker) +{ + g_Calls++; + AssertInt64Eq("__OnInt64 param value", value, g_Value); + AssertEq("__OnInt64 param marker", marker, 2); +} + + +public void __OnAddress(Address value, int marker) +{ + g_Calls++; + AssertInt64Eq("__OnAddress param value", view_as(value), g_Value); + AssertEq("__OnAddress param marker", marker, 2); +} diff --git a/public/IForwardSys.h b/public/IForwardSys.h index 5ce29364..d4495dac 100644 --- a/public/IForwardSys.h +++ b/public/IForwardSys.h @@ -93,6 +93,8 @@ namespace SourceMod #define SP_PARAMTYPE_FLOAT (2<<1) #define SP_PARAMTYPE_STRING (3<<1)|SP_PARAMFLAG_BYREF #define SP_PARAMTYPE_ARRAY (4<<1)|SP_PARAMFLAG_BYREF + #define SP_PARAMTYPE_INT64 (5<<1) + #define SP_PARAMTYPE_SMADDRESS (6<<1) /** * @brief Describes the various ways to pass parameters to plugins. @@ -106,6 +108,8 @@ namespace SourceMod Param_Array = SP_PARAMTYPE_ARRAY, /**< Only arrays can be pushed */ Param_CellByRef = SP_PARAMTYPE_CELL|SP_PARAMFLAG_BYREF, /**< Only a cell by reference can be pushed */ Param_FloatByRef = SP_PARAMTYPE_FLOAT|SP_PARAMFLAG_BYREF, /**< Only a float by reference can be pushed */ + Param_Int64 = SP_PARAMTYPE_INT64, /**< Only 64-bit integers can be pushed */ + Param_SMAddress = SP_PARAMTYPE_SMADDRESS, /**< Only Addresses can be pushed; plugins compiled before SourceMod 1.13 get the low 32 bits */ }; struct ByrefInfo