18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 23:57:09 +01:00
Files
maszyna/scripting/scriptscreenprogram.h
jerrrrycho 7812784632 fix(scripting): hold the Lua screen heap at each allocation on 64-bit Windows too (#490)
* fix(scripting): hold the Lua screen heap at each allocation on 64-bit Windows too

LuaJIT 2.0 on x64, the game's runtime on Windows, makes no state with an
allocator of the game's, so a Lua screen's memory was checked only by the
instruction hook every 1000 instructions. A loop doubling a string
(s = s .. s, about 50 instructions) went on to LuaJIT's own heap limit:
the unit test binary peaked at 1271 MB.

The state is now made with luaL_newstate() and the screen's counting
allocator is put in front of LuaJIT's own with lua_setallocf, which
LuaJIT 2.0 has. LuaJIT's allocator still makes every block, so its heap
stays where 2.0 on x64 needs it; the screen's allocator refuses a block
that would take the screen over twice its limit during a script call,
and LuaJIT raises "not enough memory" before the string is made. The
same path now runs with every LuaJIT, so the lua_newstate probe is gone.
LuaJIT's allocator is put back before lua_close, which frees its arena
only then.

The same loop now peaks at 310 MB with the game's 128 MB limit. The
limit is read per screen, so the unit tests use 32 MB: a new test runs
the doubling loop, and the one-concatenation test, skipped on Windows
until now, runs everywhere. Follows up remark 4 of the review of #356.

* fix(lua): the two memory globals behind functions, and the default order

SonarCloud: the limit setting and the peak were mutable globals (S5421),
the peak loop used relaxed order (S8417) and had an empty body (S108).
They are function-local statics now, the loop uses the default order
and says why it loops.
2026-11-25 05:24:12 +01:00

139 lines
5.2 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, which
// the screen's allocator refuses to go over as each block is asked for
constexpr std::size_t max_screen_memory{128 * 1024 * 1024};
// for the unit tests, which fill the memory of a screen and stay small: the memory a Lua screen made from now on may
// hold in place of max_screen_memory, and the most any Lua screen held since it was set (its heap and its objects)
void set_lua_screen_memory_limit(std::size_t Bytes);
std::size_t lua_screen_memory_peak();
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