Files
sourcemod/core/logic/ForwardSys.cpp
T
Adrian-Stefan MaresandClaude Opus 5.5 e7ac9b013d Add int64 support to forwards (#2594)
* Fix forwards accepting arguments of the wrong type

CForward::ProcessArguments returns whether the arguments are valid, but
its error paths return !!SetError(...), which is true for any error
code. Mismatched arguments were therefore accepted and the forward ran
anyway, while m_errstate stayed set and made the next Execute() on that
forward fail with a stale error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU

* Add Call_PushInt64 and Param_Int64

Since Address became an int64, plugins can no longer pass one through a
forward or Call_StartFunction: an int64 parameter is passed as a
reference to its value, so Call_PushCell hands the callee an invalid
reference and crashes the server, and Call_PushArray hands it the heap
array instead of the value.

Add Call_PushInt64, which pushes the value with CallArgs::PushInt64 the
way DHooks does, and Param_Int64, so forwards can declare and check
64-bit integer parameters.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU

* Move the Call_PushInt64 test to the mock testsuite

Run it in CI with the other hl2sdk-mock tests, on x86 and x86_64,
instead of as a manual test plugin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU

* Pass int64 arguments to legacy plugins as cells

Plugins compiled before Address became an int64, or with oldspcomp, take
an Address as a single cell and crash when handed an int64 argument.
Like DHooks, check the callee's plugin for __Int64_Address__ and pass
those plugins the low 32 bits instead, so a forward can have both kinds
of listeners.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU

* Document how legacy plugins receive Call_PushInt64 values

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU

* Don't downgrade int64 arguments for legacy plugins

The forward system shouldn't know about the Address convention. Code
that declares a forward parameter as an Address can handle plugins that
take it as a cell.

This reverts commits 345b0dcb4 and 78b7bc14b.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU

* Add Param_SMAddress and Call_PushAddress

Plugins compiled before Address became an int64 take an Address as one
cell. Arguments declared as Param_SMAddress in a forward, or pushed with
Call_PushAddress to a function, are passed to those plugins as the low 32
bits, like DHooks does. Param_Int64 arguments are always passed as int64.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU

* Refuse non-Address int64 arguments for plugins compiled before 1.13

Plugins compiled before SourceMod 1.13 have no int64 type, so an int64
argument can only reach them in a parameter they declared as a cell.
Only an Address has a sane value there, its low 32 bits; any other int64
does not. Report an error naming the plugin and skip it instead of
passing it a reference it reads as a cell.

oldspcomp with the 1.13 includes also declares Address as an int64 and
__Int64_Address__, so describe these plugins as compiled before 1.13.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-27 00:44:33 +00:00

580 lines
14 KiB
C++

// vim: set ts=4 sw=4 tw=99 noet :
// =============================================================================
// SourceMod
// Copyright (C) 2004-2015 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 <http://www.gnu.org/licenses/>.
//
// 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 <http://www.sourcemod.net/license.php>.
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include "ForwardSys.h"
#include "DebugReporter.h"
#include "common_logic.h"
#include <bridge/include/IScriptManager.h>
#include <amtl/am-string.h>
#include <ReentrantList.h>
using namespace ke;
using sp::CallArgs;
CForwardManager g_Forwards;
// Genesis turns to its source, reduction occurs stepwise although the essence
// is all one. End of line. FTL system check.
void CForwardManager::OnSourceModAllInitialized()
{
scripts->AddPluginsListener(this);
sharesys->AddInterface(NULL, this);
}
IForward *CForwardManager::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, ...)
{
va_list ap;
va_start(ap, types);
CForward *fwd = CForward::CreateForward(name, et, num_params, types, ap);
va_end(ap);
if (fwd)
{
scripts->AddFunctionsToForward(name, fwd);
m_managed.push_back(fwd);
}
return fwd;
}
IChangeableForward *CForwardManager::CreateForwardEx(const char *name, ExecType et, int num_params, const ParamType *types, ...)
{
va_list ap;
va_start(ap, types);
CForward *fwd = CForward::CreateForward(name, et, num_params, types, ap);
va_end(ap);
if (fwd)
{
m_unmanaged.push_back(fwd);
}
return fwd;
}
void CForwardManager::OnPluginLoaded(IPlugin *plugin)
{
/* Attach any globally managed forwards */
for (ForwardIter iter(m_managed); !iter.done(); iter.next()) {
CForward *fwd = (*iter);
IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
if (pFunc)
fwd->AddFunction(pFunc);
}
}
void CForwardManager::OnPluginUnloaded(IPlugin *plugin)
{
for (ForwardIter iter(m_managed); !iter.done(); iter.next()) {
CForward *fwd = (*iter);
fwd->RemoveFunctionsOfPlugin(plugin);
}
for (ForwardIter iter(m_unmanaged); !iter.done(); iter.next()) {
CForward *fwd = (*iter);
fwd->RemoveFunctionsOfPlugin(plugin);
}
}
IForward *CForwardManager::FindForward(const char *name, IChangeableForward **ifchng)
{
for (ForwardIter iter(m_managed); !iter.done(); iter.next()) {
CForward *fwd = (*iter);
if (strcmp(fwd->GetForwardName(), name) == 0) {
if (ifchng)
*ifchng = NULL;
return fwd;
}
}
for (ForwardIter iter(m_unmanaged); !iter.done(); iter.next()) {
CForward *fwd = (*iter);
if (strcmp(fwd->GetForwardName(), name) == 0) {
if (ifchng)
*ifchng = fwd;
return fwd;
}
}
if (ifchng)
*ifchng = NULL;
return NULL;
}
void CForwardManager::ReleaseForward(IForward *aForward)
{
CForward *fwd = static_cast<CForward *>(aForward);
if (fwd->m_ExecDepth > 0)
{
/* Still executing (possibly nested); defer the real deletion until
* the outermost Execute() call unwinds. */
fwd->m_deleted = true;
return;
}
m_managed.remove(fwd);
m_unmanaged.remove(fwd);
delete fwd;
}
void CForwardManager::OnPluginPauseChange(IPlugin *plugin, bool paused)
{
if (paused)
return;
/* Attach any globally managed forwards */
for (ForwardIter iter(m_managed); !iter.done(); iter.next()) {
CForward *fwd = (*iter);
IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
// Only add functions, if they aren't registered yet!
if (pFunc && !fwd->IsFunctionRegistered(pFunc))
{
fwd->AddFunction(pFunc);
}
}
}
/*************************************
* ACTUAL FORWARD API IMPLEMENTATION *
*************************************/
CForward::CForward(ExecType et, const char *name, const ParamType *types, unsigned num_params)
: m_addresses(0),
m_numparams(0),
m_ExecType(et),
m_errstate(SP_ERROR_NONE),
m_ExecDepth(0),
m_deleted(false)
{
ke::SafeStrcpy(m_name, sizeof(m_name), name ? name : "");
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;
}
CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, va_list ap)
{
ParamType _types[SP_MAX_EXEC_PARAMS];
if (num_params > SP_MAX_EXEC_PARAMS)
{
return NULL;
}
if (types == NULL && num_params)
{
for (unsigned int i=0; i<num_params; i++)
_types[i] = (ParamType)va_arg(ap, int);
types = _types;
} else {
for (unsigned int i=0; i<num_params; i++)
_types[i] = types[i];
}
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() : "<unknown>", i + 1);
return false;
}
arg.u.value = static_cast<cell_t>(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);
for (uint32_t i = 0; i < args.argc; i++) {
if (i >= m_numparams)
return !SetError(SP_ERROR_PARAM);
ParamType pt = m_types[i];
if (pt == Param_Any)
continue;
auto& arg = args.argv[i];
switch (arg.type) {
case CallArgs::ARG_CELL:
if (arg.flags & CallArgs::FLAG_CELL_TYPE_FLOAT) {
if (pt != Param_Float)
return !SetError(SP_ERROR_PARAM);
} else {
if (pt != Param_Cell)
return !SetError(SP_ERROR_PARAM);
}
break;
case CallArgs::ARG_CELL_BY_REF:
if (arg.flags & CallArgs::FLAG_CELL_TYPE_FLOAT) {
if (pt != Param_FloatByRef)
return !SetError(SP_ERROR_PARAM);
} else {
if (pt != Param_CellByRef)
return !SetError(SP_ERROR_PARAM);
}
break;
case CallArgs::ARG_ARRAY:
if (pt != Param_Array)
return !SetError(SP_ERROR_PARAM);
break;
case CallArgs::ARG_CHAR_ARRAY:
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;
}
}
return true;
}
int CForward::Execute(cell_t *result, IForwardFilter *filter)
{
auto args = std::move(default_args_);
default_args_.Reset();
return Execute(args, result, filter);
}
int CForward::Execute(const sp::CallArgs& in_args, cell_t *result, IForwardFilter *filter)
{
if (m_errstate) {
int err = m_errstate;
Cancel();
return err;
}
auto args = in_args;
unsigned int success = 0;
cell_t high_result = 0;
cell_t low_result = 0;
cell_t cur_result = 0;
if (!ProcessArguments(args)) {
int err = m_errstate;
Cancel();
return err;
}
m_ExecDepth++;
for (FuncIter iter(m_functions); !iter.done(); iter.next())
{
IPluginFunction *func = (*iter);
if (filter)
filter->Preprocess(func, args);
if (func->GetParentRuntime()->IsPaused())
continue;
ExceptionHandler eh(func->GetParentRuntime());
if (!InvokePluginFunction(func, args, m_addresses, &cur_result))
continue;
success++;
switch (m_ExecType)
{
case ET_Event:
if (cur_result > high_result)
high_result = cur_result;
break;
case ET_Hook:
if (cur_result > high_result)
high_result = cur_result;
break;
case ET_LowEvent:
/* Check if the current result is the lowest so far (or if it's the first result) */
if (cur_result < low_result || success == 1)
low_result = cur_result;
break;
default:
break;
}
if (m_ExecType == ET_Hook && (ResultType)high_result == Pl_Stop)
break;
}
if (success)
{
switch (m_ExecType)
{
case ET_Ignore:
cur_result = 0;
break;
case ET_Event:
case ET_Hook:
cur_result = high_result;
break;
case ET_LowEvent:
cur_result = low_result;
break;
default:
break;
}
if (result)
*result = cur_result;
}
/* If a callback we just invoked freed this forward's handle (e.g. the
* plugin deletes its own private forward from within one of its own
* callbacks), the actual delete is deferred to here so we don't touch
* freed memory by continuing to use "this" above. */
if (--m_ExecDepth == 0 && m_deleted)
{
g_Forwards.ReleaseForward(this);
return SP_ERROR_NONE;
}
return SP_ERROR_NONE;
}
int CForward::PushCell(cell_t cell)
{
default_args_.PushCell(cell);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushInt64(int64_t value)
{
default_args_.PushInt64(value);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushFloat(float number)
{
default_args_.PushFloat(number);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushCellByRef(cell_t *cell, int flags)
{
default_args_.PushCellByRef(cell, flags);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushFloatByRef(float *num, int flags)
{
default_args_.PushFloatByRef(num, flags);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushArray(cell_t *inarray, unsigned int cells, int flags)
{
if (!inarray) {
return SetError(SP_ERROR_PARAM);
}
default_args_.PushArray(inarray, cells, flags);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushString(const char *string)
{
if (!string) {
return SetError(SP_ERROR_PARAM);
}
default_args_.PushString(string);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
{
default_args_.PushString(buffer, length, sz_flags | cp_flags);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
void CForward::Cancel()
{
default_args_.Reset();
m_errstate = SP_ERROR_NONE;
}
bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
{
IPluginFunction *pFunc = pContext->GetFunctionById(index);
if (!pFunc)
{
return false;
}
return AddFunction(pFunc);
}
bool CForward::RemoveFunction(IPluginContext *pContext, funcid_t index)
{
IPluginFunction *pFunc = pContext->GetFunctionById(index);
if (!pFunc)
{
return false;
}
return RemoveFunction(pFunc);
}
bool CForward::RemoveFunction(IPluginFunction *func)
{
ReentrantList<IPluginFunction *> *lst;
if (func->IsRunnable())
lst = &m_functions;
else
lst = &m_paused;
bool found = false;
for (FuncIter iter(lst); !iter.done(); iter.next()) {
if (*iter == func) {
iter.remove();
found = true;
break;
}
}
/* Cancel a call, if any */
if (found || default_args_.argc)
default_args_.Reset();
return found;
}
unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
{
unsigned int removed = 0;
for (FuncIter iter(m_functions); !iter.done(); iter.next()) {
IPluginFunction *func = *iter;
if (func->GetParentContext() == plugin->GetBaseContext()) {
iter.remove();
removed++;
}
}
return removed;
}
bool CForward::AddFunction(IPluginFunction *func)
{
if (default_args_.argc)
return false;
if (func->IsRunnable())
m_functions.push_back(func);
else
m_paused.push_back(func);
return true;
}
bool CForward::IsFunctionRegistered(IPluginFunction *func)
{
ReentrantList<IPluginFunction *> *lst;
if (func->IsRunnable())
lst = &m_functions;
else
lst = &m_paused;
for (FuncIter iter(lst); !iter.done(); iter.next()) {
if ((*iter) == func)
return true;
}
return false;
}
const char *CForward::GetForwardName()
{
return m_name;
}
unsigned int CForward::GetFunctionCount()
{
return m_functions.size();
}
ExecType CForward::GetExecType()
{
return m_ExecType;
}