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

Conversation

JGRennison
Copy link
Contributor

Motivation / Problem

Script functions called by ScriptList::Valuate are never suspended.
A single script function called by ScriptList::Valuate could therefore consume an unlimited amount of CPU time, blocking the actual game.

Description

Generate an error, killing the script, once a threshold number of ops is consumed by a (top level) valuate operation, even in the case where the function would never otherwise return.

Limitations

Using the suspend parameter of sq_call complicates the accounting of the remaining ops and the handling of the stack in the case where the limit has been reached. An attempt was made using this, but was abandoned in favour of the approach in this PR.

The existing squirrel code is mostly very ugly. In order to maintain a consistent code style care has been taken to ensure that this PR is also aesthetically displeasing.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, game_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@@ -12,9 +12,13 @@
#include "script_controller.hpp"
#include "../../debug.h"
#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/.

@glx22
Copy link
Contributor

glx22 commented Jan 2, 2024

ScriptVehicleList constructor will need the same kind of limitation for the filter.

@@ -906,6 +918,7 @@ SQInteger ScriptList::Valuate(HSQUIRRELVM vm)
/* See below for explanation. The extra pop is the return value. */
sq_pop(vm, nparam + 4);

vm->_ops_till_suspend_error_threshold = backup_ops_error_threshold;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should make a small Backup wrapper here, so it is automatically restored for us when leaving scope?

Dunno if that is actually an improvement tbh .. but missing a return is also an easy mistake made :(

Copy link
Member

Choose a reason for hiding this comment

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

Make, or use AutoRestoreBackup?

@glx22 glx22 merged commit 48b6b18 into OpenTTD:master Jan 2, 2024
20 checks passed
@JGRennison JGRennison deleted the fix-limit-list-valuate-ops branch January 9, 2024 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants