18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/scripting/scriptscreenprogram.h
jerrrrycho 8201ccb3b0 fix(scripting): hold what runs without the hook in the Lua screens
- no newproxy: LuaJIT runs a __gc without the hook, at any allocation and
  in lua_close, so a looping finalizer hung the worker. a table's __gc
  isn't run, ffi isn't opened: the screens have no finalizer of their own
- render and init of the script's table are read inside the protected,
  counted call, as is the state table of a refresh: an __index that fails
  or loops switches the screen off instead of ending the program, and one
  that gives render works
- string.find, match, gmatch and gsub are refused when the steps they may
  take at worst (script_screens::lua_pattern_cost) are over 1e8, about a
  tenth of a second of LuaJIT's backtracking
- a screen holds at most 128 MB, its heap and the pixels of its images,
  fonts and sprites: the hook checks it with a full collection, the api
  reserves pixels before making them, and string.format, table.concat and
  string.gsub refuse a result over it from the lengths of their arguments.
  where LuaJIT takes an allocator of the game's (2.1 GC64) the heap gets
  one that refuses past twice the limit, which also stops a single large
  concatenation; LuaJIT 2.0 on x64 takes none
- xpcall is made in Lua on top of pcall, so a coroutine yields across it
- the limits go into limit_libraries(), apart from opening the libraries
2026-11-16 08:54:29 +01:00

141 lines
5.5 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
// the inside of the script screens: what the queue (scripting/scriptscreens.cpp) asks of a language runtime
// (scripting/luaprogram.cpp, scripting/wasmprogram.cpp), and the helpers they share
#include "scripting/scriptscreens.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace script_screens
{
// the largest side of a screen image, in pixels
constexpr int max_image_side{4096};
// the most instructions one call into a screen may run, so that a script stuck in a loop can't hang the worker
constexpr std::int64_t instruction_limit{200'000'000};
// what one call into a screen may send and log; the rest is dropped, which the log tells once per screen. the longest
// name of a command the engine knows is under 40 characters
constexpr std::size_t max_commands_per_call{64};
constexpr std::size_t max_command_name{64};
constexpr int max_log_lines_per_call{32};
constexpr std::size_t max_log_line{1024}; // bytes of the text, a longer one is cut
// the largest file a screen reads: its script or module, a file below its folder
constexpr std::uintmax_t max_screen_file{16 * 1024 * 1024};
// the memory a Lua screen may hold: its Lua heap, and the pixels of its images, fonts and sprites. checked with a full
// collection, so its garbage doesn't count; between the collections it may take the screen up to twice this
constexpr std::size_t max_screen_memory{128 * 1024 * 1024};
// true when the Lua screens count their memory in an allocator of their own, which refuses what goes over
// max_screen_memory as it is asked for: LuaJIT 2.1 in its GC64 mode, a 32-bit build. LuaJIT 2.0 on x64 (the game's on
// Windows) and 2.1 on x64 without GC64 take no allocator; there the memory is checked by the instruction hook every
// so many instructions, and before what makes images and large strings in one call (string.rep, string.format,
// table.concat, string.gsub), but not at the concatenation operator: one concatenation of large strings is made
// whole, as far as LuaJIT's own heap goes (2 GB on x64), and the screen is stopped at the next check
bool lua_screen_memory_allocator();
struct pending_command
{
std::string name;
std::int32_t param1{0};
std::int32_t param2{0};
std::uint32_t vehicle{0};
};
// one refresh of a screen: what it gets and what it gives back
struct render_call
{
dictionary_source const &input;
std::uint32_t vehicle{0}; // the target of the commands
std::vector<pending_command> commands; // sent during the call
// the image drawn, RGBA, from the top row down; empty when the screen didn't change
std::vector<std::uint8_t> pixels;
int width{0};
int height{0};
// the post-processing still to run on the image (image:led_post, image:resize of the Lua screens)
std::vector<post_step> post;
};
// a running script: a Lua state, a WebAssembly instance. made, called and destroyed on the worker thread, or by the
// queue once the worker stopped
class program
{
public:
program() = default;
virtual ~program() = default;
program(program const &) = delete;
program &operator=(program const &) = delete;
// runs a refresh. false when the screen can't run (any more); the reason is in the log
virtual bool render(render_call &Call) = 0;
};
// a language: started and stopped with the queue
class runtime
{
public:
runtime() = default;
virtual ~runtime() = default;
runtime(runtime const &) = delete;
runtime &operator=(runtime const &) = delete;
// loads the script of a screen and runs its setup. nullptr when it fails; the reason is in the log
virtual std::unique_ptr<program> load(std::string const &Script, script_screen_kind Kind) = 0;
// called on the worker thread when it starts and before it ends
virtual void attach_thread() {}
virtual void detach_thread() {}
};
std::unique_ptr<runtime> make_lua_runtime();
std::unique_ptr<runtime> make_wasm_runtime();
// the text as UTF-8: names of sceneries and vehicles may come in windows-1250
std::string utf8_text(std::string const &Text);
// a file of the screen's folder or below; nothing for a path leading outside, or no such file
std::optional<std::string> read_screen_file(std::string const &Folder, std::string_view Path);
// the file, when it is a regular file of at most max_screen_file bytes: not a folder, a device or anything larger
std::optional<std::string> read_regular_file(std::string const &Path);
// what a screen sends and logs, held to the limits of one call. Name: how the log names the screen
class output_limits
{
public:
explicit output_limits(std::string Name) : m_name{std::move(Name)} {}
// a new call into the screen
void start_call()
{
m_log_lines = 0;
}
// true when the command may go with the call; a command over the limits is dropped
bool take_command(render_call const &Call, std::string_view Name);
// the line to log, cut to max_log_line; nothing when the call logged enough
std::optional<std::string> take_log(std::string_view Text);
private:
std::string m_name;
int m_log_lines{0};
bool m_commands_dropped{false}; // logged once
bool m_logs_dropped{false}; // logged once
};
// seconds of a steady clock, the same for every screen, for animations that don't depend on how often the screen is
// refreshed
double screen_time();
bool valid_image_side(int Side);
} // namespace script_screens