Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: Limit total script ops that can be consumed by a list valuate #11670

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/3rdparty/squirrel/squirrel/sqvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ SQVM::SQVM(SQSharedState *ss)
_can_suspend = false;
_in_stackoverflow = false;
_ops_till_suspend = 0;
_ops_till_suspend_error_threshold = INT64_MIN;
_ops_till_suspend_error_label = nullptr;
_callsstack = nullptr;
_callsstacksize = 0;
_alloccallsstacksize = 0;
Expand Down Expand Up @@ -744,6 +746,10 @@ bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQIn
{
DecreaseOps(1);
if (ShouldSuspend()) { _suspended = SQTrue; _suspended_traps = traps; return true; }
if (IsOpsTillSuspendError()) {
Raise_Error(fmt::format("excessive CPU usage in {}", _ops_till_suspend_error_label));
SQ_THROW();
}

const SQInstruction &_i_ = *ci->_ip++;
#ifdef _DEBUG_DUMP
Expand Down
7 changes: 7 additions & 0 deletions src/3rdparty/squirrel/squirrel/sqvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,20 @@ typedef sqvector<CallInfo> CallInfoVec;

SQBool _can_suspend;
SQInteger _ops_till_suspend;
SQInteger _ops_till_suspend_error_threshold;
const char *_ops_till_suspend_error_label;
SQBool _in_stackoverflow;

bool ShouldSuspend()
{
return _can_suspend && _ops_till_suspend <= 0;
}

bool IsOpsTillSuspendError()
{
return _ops_till_suspend < _ops_till_suspend_error_threshold;
}

void DecreaseOps(SQInteger amount)
{
if (_ops_till_suspend - amount < _ops_till_suspend) _ops_till_suspend -= amount;
Expand Down
10 changes: 10 additions & 0 deletions src/script/api/script_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include "script_list.hpp"
#include "script_controller.hpp"
#include "../../debug.h"
#include "../../core/backup_type.hpp"
#include "../../script/squirrel.hpp"
#include <../squirrel/sqvm.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using ../ with #include <> looks wrong to me.

Copy link
Contributor Author

@JGRennison JGRennison Jan 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how both of the existing squirrel includes in /src/script/ are.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely by error. A while ago we fixed a bunch of those "" vs <> for squirrel. Guess those were missed :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't compile with the ../ removed.
In CMakeLists.txt there is include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/squirrel/include), but
src/script/squirrel.cpp wants includes in src/3rdparty/squirrel/squirrel/.


#include "../../safeguards.h"

Expand Down Expand Up @@ -866,6 +868,14 @@ SQInteger ScriptList::Valuate(HSQUIRRELVM vm)
bool backup_allow = ScriptObject::GetAllowDoCommand();
ScriptObject::SetAllowDoCommand(false);

/* Limit the total number of ops that can be consumed by a valuate operation */
SQInteger new_ops_error_threshold = vm->_ops_till_suspend_error_threshold;
if (vm->_ops_till_suspend_error_threshold == INT64_MIN) {
new_ops_error_threshold = vm->_ops_till_suspend - MAX_VALUATE_OPS;
vm->_ops_till_suspend_error_label = "valuator function";
}
AutoRestoreBackup ops_error_threshold_backup(vm->_ops_till_suspend_error_threshold, new_ops_error_threshold);

/* Push the function to call */
sq_push(vm, 2);

Expand Down
3 changes: 3 additions & 0 deletions src/script/api/script_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#include "script_object.hpp"

/** Maximum number of operations allowed for valuating a list. */
static const int MAX_VALUATE_OPS = 500000;

class ScriptListSorter;

/**
Expand Down
10 changes: 10 additions & 0 deletions src/script/api/script_vehiclelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "../../depot_map.h"
#include "../../vehicle_base.h"
#include "../../train.h"
#include "../../core/backup_type.hpp"
#include <../squirrel/sqvm.h>

#include "../../safeguards.h"

Expand All @@ -41,6 +43,14 @@ ScriptVehicleList::ScriptVehicleList(HSQUIRRELVM vm)
bool backup_allow = ScriptObject::GetAllowDoCommand();
ScriptObject::SetAllowDoCommand(false);

/* Limit the total number of ops that can be consumed by a filter operation, if a filter function is present */
SQInteger new_ops_error_threshold = vm->_ops_till_suspend_error_threshold;
if (nparam >= 1 && vm->_ops_till_suspend_error_threshold == INT64_MIN) {
new_ops_error_threshold = vm->_ops_till_suspend - MAX_VALUATE_OPS;
vm->_ops_till_suspend_error_label = "vehicle filter function";
}
AutoRestoreBackup ops_error_threshold_backup(vm->_ops_till_suspend_error_threshold, new_ops_error_threshold);

for (const Vehicle *v : Vehicle::Iterate()) {
if (v->owner != ScriptObject::GetCompany() && !ScriptCompanyMode::IsDeity()) continue;
if (!v->IsPrimaryVehicle() && !(v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon())) continue;
Expand Down