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

(cherry picked from commit 1d0b40b2b2f2998d96221b18ae65a4d1524c61a7)
This commit is contained in:
Jonathan G Rennison
2024-01-01 22:51:54 +00:00
parent 42c8f50551
commit be4f8b91c6
5 changed files with 36 additions and 0 deletions

View File

@@ -115,6 +115,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;
@@ -743,6 +745,10 @@ exception_restore:
{
DecreaseOps(1);
if (ShouldSuspend()) { _suspended = SQTrue; _suspended_traps = traps; return true; }
if (IsOpsTillSuspendError()) {
Raise_Error("excessive CPU usage in %s", _ops_till_suspend_error_label);
SQ_THROW();
}
const SQInstruction &_i_ = *ci->_ip++;
#ifdef _DEBUG_DUMP

View File

@@ -168,6 +168,8 @@ public:
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()
@@ -175,6 +177,11 @@ public:
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;