18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

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.
This commit is contained in:
jerrrrycho
2026-11-25 05:24:12 +01:00
committed by GitHub
parent 236a25eacb
commit 7812784632
4 changed files with 146 additions and 93 deletions

View File

@@ -144,12 +144,11 @@ Besides the 200 million instructions of a call, a Lua screen is held to what run
- memory: a screen holds at most 128 MB: its Lua heap and the pixels of its images, fonts and sprites (4 bytes a pixel
of an image, 1 of a font or a sprite). An object that would take the screen over is refused (`memory limit
exceeded`), and a screen found holding more stops with that error. Garbage doesn't count: the check collects it
first. Where LuaJIT takes an allocator of the game's (2.1 in its GC64 mode), anything over twice the limit is refused
as it is asked for (`not enough memory`); the game's LuaJIT 2.0 on 64-bit Windows takes none, and there the heap is
checked every thousand instructions and before the calls below, and whatever a script makes between two checks is
made whole first: every concatenation (`a .. b`) within one interval of a thousand instructions, so a loop doubling a
string (`for i = 1, 11 do s = s .. s end` on a 1 MB `s`) gets to LuaJIT's own heap limit of about 2 GB before the
check sees it;
first, every thousand instructions and before the calls below. Between the checks the screen's heap goes through a
counter of the game's, in front of LuaJIT's own allocator, which refuses a block that would take the screen over
twice the limit as it is asked for (`not enough memory`, an error the script may catch). So what one instruction
makes is held as well: a loop doubling a string (`for i = 1, 11 do s = s .. s end` on a 1 MB `s`) stops at the
concatenation that would go over 256 MB, not at LuaJIT's own heap limit of about 2 GB;
- `string.rep` makes no string over 1 MB; `string.format`, `table.concat` and `string.gsub` (with a string or a table to
replace with) refuse a result that may be over the memory of a screen, counted from the lengths of what they are
given;

View File

@@ -32,7 +32,7 @@ http://mozilla.org/MPL/2.0/.
#include <algorithm>
#include <array>
#include <cstdlib>
#include <atomic>
#include <cstring>
#include <format>
#include <new>
@@ -57,22 +57,61 @@ constexpr char sprite_metatable[]{"eu07.sprite"};
// the address is the key of the program in the registry of its state
constexpr char program_key{0};
// the memory of a screen: what its Lua heap holds, when the allocator counts it, and the pixels of its images, fonts
// and sprites, which the objects keep outside the heap
// what set_lua_screen_memory_limit() set
std::atomic<std::size_t> &memory_limit_setting()
{
static std::atomic<std::size_t> setting{max_screen_memory};
return setting;
}
// what lua_screen_memory_peak() gives
std::atomic<std::size_t> &memory_peak()
{
static std::atomic<std::size_t> peak{0};
return peak;
}
// the memory of a screen: what its Lua heap holds and the pixels of its images, fonts and sprites, which the objects
// keep outside the heap
struct screen_memory
{
std::size_t limit{max_screen_memory}; // what the screen may hold, read when its state is made
std::size_t heap{0};
std::size_t objects{0};
bool counted{false}; // the state was made with limited_alloc
std::size_t peak{0}; // the most the screen held
bool enforced{false}; // a call into the script is in progress: the allocator refuses what goes over the limit
// what the screen holds when it is checked next (lua_program::over_memory) with a full collection
std::size_t collect_at{max_screen_memory};
// LuaJIT's own allocator of the state, which limited_alloc gives the blocks to
lua_Alloc base{nullptr};
void *base_data{nullptr};
};
// the allocator of a screen's state (lua_newstate), which refuses a block that would take the screen over twice
// max_screen_memory (what it holds, and its garbage between the collections) while a call into the script is in
// progress: LuaJIT raises "not enough memory" there, in the protected call. the game's own work between the calls isn't
// refused, as it isn't protected
// keeps the most the screen held
void note_peak(screen_memory &Memory)
{
auto const held{Memory.heap + Memory.objects};
if (held <= Memory.peak)
{
return;
}
Memory.peak = held;
auto &peak{memory_peak()};
auto seen{peak.load()};
while (held > seen && false == peak.compare_exchange_weak(seen, held))
{
// a failed exchange put the value another screen stored in seen; try again while held is still more
}
}
// the allocator of a screen's state, put in front of LuaJIT's own (lua_setallocf) once the state is made: LuaJIT 2.0
// on x64 (the game's on Windows) and 2.1 on x64 without GC64 make no state with an allocator of the game's
// (lua_newstate gives nullptr), as their heap has to stay in the low addresses only their own allocator keeps to. so
// LuaJIT's allocator still makes every block; this one counts them, and refuses a block that would take the screen over
// twice its limit (what it holds, and its garbage between the collections) while a call into the script is in
// progress: LuaJIT raises "not enough memory" there, in the protected call, before the block is made. that holds what
// one instruction makes, which the instruction hook sees only once it is made (a concatenation, a .. b). the game's own
// work between the calls isn't refused, as it isn't protected
void *limited_alloc(void *Data, void *Block, std::size_t Old, std::size_t New)
{
auto &memory{*static_cast<screen_memory *>(Data)};
@@ -80,20 +119,15 @@ void *limited_alloc(void *Data, void *Block, std::size_t Old, std::size_t New)
{
Old = 0;
}
if (New == 0)
{
std::free(Block);
memory.heap -= Old;
return nullptr;
}
if (New > Old && memory.enforced && memory.heap - Old + New + memory.objects > 2 * max_screen_memory)
if (New > Old && memory.enforced && memory.heap - std::min(Old, memory.heap) + New + memory.objects > 2 * memory.limit)
{
return nullptr;
}
auto *result{std::realloc(Block, New)};
if (result != nullptr)
auto *result{memory.base(memory.base_data, Block, Old, New)};
if (New == 0 || result != nullptr)
{
memory.heap = memory.heap - Old + New;
memory.heap = memory.heap - std::min(Old, memory.heap) + New;
note_peak(memory);
}
return result;
}
@@ -111,6 +145,12 @@ class lua_program final : public program
{
// the finalizers lua_close runs aren't counted: the budget of the last call may be spent
lua_sethook(m_state, nullptr, 0, 0);
// LuaJIT's own allocator back: lua_close frees the whole heap of its allocator only when the state has it,
// and otherwise leaves the heap's own bookkeeping behind
if (memory.base != nullptr)
{
lua_setallocf(m_state, memory.base, memory.base_data);
}
lua_close(m_state);
}
}
@@ -130,7 +170,7 @@ class lua_program final : public program
char const *stopped{nullptr}; // why the call in progress is being stopped: the hook raises it at every instruction
screen_memory memory;
// true when the screen would hold more than max_screen_memory with Adding bytes more. checked with a full collection
// true when the screen would hold more than its limit with Adding bytes more. checked with a full collection
// once it has made a quarter of the limit since the last one, so a screen near its limit isn't collected over and
// over
bool over_memory(lua_State *L, std::size_t Adding);
@@ -156,20 +196,15 @@ lua_program *program_of(lua_State *L)
bool lua_program::over_memory(lua_State *L, std::size_t const Adding)
{
auto const used{[this, L] {
auto const heap{memory.counted ? memory.heap
: static_cast<std::size_t>(lua_gc(L, LUA_GCCOUNT, 0)) * 1024 + static_cast<std::size_t>(lua_gc(L, LUA_GCCOUNTB, 0))};
return heap + memory.objects;
}};
if (used() + Adding <= memory.collect_at)
if (memory.heap + memory.objects + Adding <= memory.collect_at)
{
return false;
}
// what is garbage doesn't count
lua_gc(L, LUA_GCCOLLECT, 0);
auto const held{used()};
memory.collect_at = std::max(max_screen_memory, held + max_screen_memory / 4);
return held + Adding > max_screen_memory;
auto const held{memory.heap + memory.objects};
memory.collect_at = std::max(memory.limit, held + memory.limit / 4);
return held + Adding > memory.limit;
}
void count_hook(lua_State *L, lua_Debug * /*Debug*/)
@@ -188,7 +223,7 @@ void count_hook(lua_State *L, lua_Debug * /*Debug*/)
}
else if (self->over_memory(L, 0))
{
// what the allocator doesn't refuse (where the screen has none of its own) is caught here
// what the allocator lets through, up to twice the limit with the garbage, is caught here
self->stopped = "memory limit exceeded";
}
}
@@ -208,10 +243,17 @@ void reserve_memory(lua_State *L, std::size_t const Bytes)
{
if (auto *self{program_of(L)}; self != nullptr && self->over_memory(L, Bytes))
{
luaL_error(L, "memory limit exceeded (%d MB)", static_cast<int>(max_screen_memory / (1024 * 1024)));
luaL_error(L, "memory limit exceeded (%d MB)", static_cast<int>(self->memory.limit / (1024 * 1024)));
}
}
// the memory the screen of the state may hold
std::size_t memory_limit(lua_State *L)
{
auto const *self{program_of(L)};
return self != nullptr ? self->memory.limit : max_screen_memory;
}
// objects of the api -------------------------------------------------------------------------------------------------
// where the userdata of an object keeps the bytes it counts in the screen's memory, after the object
@@ -230,6 +272,7 @@ template <typename Type_> std::remove_cvref_t<Type_> *push_object(lua_State *L,
if (auto *self{program_of(L)}; self != nullptr)
{
self->memory.objects += Bytes;
note_peak(self->memory);
}
luaL_getmetatable(L, Metatable);
lua_setmetatable(L, -2);
@@ -855,7 +898,7 @@ int api_repeat(lua_State *L)
// the result may be over the memory of a screen. what they make otherwise is counted with the rest of the heap
int too_large(lua_State *L, char const *Function)
{
return luaL_error(L, "%s: the result may be over the memory of a screen (%d MB)", Function, static_cast<int>(max_screen_memory / (1024 * 1024)));
return luaL_error(L, "%s: the result may be over the memory of a screen (%d MB)", Function, static_cast<int>(memory_limit(L) / (1024 * 1024)));
}
// calls the library's own function, the upvalue, with the arguments, and returns what it returns
@@ -878,7 +921,7 @@ int api_format(lua_State *L)
{
total += lua_type(L, idx) == LUA_TSTRING ? 4.0 * static_cast<double>(lua_objlen(L, idx)) : 512.0;
}
if (total > static_cast<double>(max_screen_memory))
if (total > static_cast<double>(memory_limit(L)))
{
return too_large(L, "string.format");
}
@@ -894,6 +937,7 @@ int api_concat(lua_State *L)
luaL_optlstring(L, 2, "", &separator);
auto const first{luaL_optinteger(L, 3, 1)};
auto const last{lua_isnoneornil(L, 4) ? static_cast<lua_Integer>(lua_objlen(L, 1)) : luaL_checkinteger(L, 4)};
auto const limit{static_cast<double>(memory_limit(L))};
auto total{0.0};
for (auto idx = first; idx <= last; ++idx)
{
@@ -906,7 +950,7 @@ int api_concat(lua_State *L)
{
break;
}
if (total > static_cast<double>(max_screen_memory))
if (total > limit)
{
return too_large(L, "table.concat");
}
@@ -978,7 +1022,7 @@ int api_pattern(lua_State *L)
{
auto const size{static_cast<double>(subject)};
auto const matches{lua_isnumber(L, 4) ? std::clamp(static_cast<double>(lua_tonumber(L, 4)), 0.0, size + 1.0) : size + 1.0};
if (auto const replaced{replacement_length(L, 3, size, matches)}; replaced.has_value() && size + *replaced > static_cast<double>(max_screen_memory))
if (auto const replaced{replacement_length(L, 3, size, matches)}; replaced.has_value() && size + *replaced > static_cast<double>(memory_limit(L)))
{
return too_large(L, name);
}
@@ -1195,46 +1239,20 @@ int call_render(lua_State *L)
return 1;
}
// true when a screen's state can be made with an allocator of its own: LuaJIT 2.0 on x64, and 2.1 on x64 without GC64,
// refuse one (lua_newstate gives nullptr, and writes so to stderr: 2.0 isn't asked)
bool custom_allocator_works()
{
#if LUAJIT_VERSION_NUM < 20100
if constexpr (sizeof(void *) == 8)
{
return false;
}
#endif
static bool const works{[] {
screen_memory probe;
auto *state{lua_newstate(limited_alloc, &probe)};
if (state == nullptr)
{
return false;
}
lua_close(state);
return true;
}()};
return works;
}
bool lua_program::start()
{
// the screen's own allocator counts its memory, where LuaJIT takes one
if (custom_allocator_works())
{
m_state = lua_newstate(limited_alloc, &memory);
memory.counted = m_state != nullptr;
}
if (m_state == nullptr)
{
m_state = luaL_newstate();
}
m_state = luaL_newstate();
if (m_state == nullptr)
{
ErrorLog(std::format("Lua screen \"{}\": failed to create the state", script));
return false;
}
// the screen's allocator in front of LuaJIT's own, counting from what the state holds already
memory.limit = memory_limit_setting().load();
memory.collect_at = memory.limit;
memory.heap = static_cast<std::size_t>(lua_gc(m_state, LUA_GCCOUNT, 0)) * 1024 + static_cast<std::size_t>(lua_gc(m_state, LUA_GCCOUNTB, 0));
memory.base = lua_getallocf(m_state, &memory.base_data);
lua_setallocf(m_state, limited_alloc, &memory);
// no compiled code: LuaJIT calls no hooks in it, and the hook is what stops a script stuck in a loop
luaJIT_setmode(m_state, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF);
open_libraries();
@@ -1340,9 +1358,15 @@ class lua_runtime_host final : public runtime
} // namespace
bool lua_screen_memory_allocator()
void set_lua_screen_memory_limit(std::size_t const Bytes)
{
return custom_allocator_works();
memory_limit_setting().store(Bytes);
memory_peak().store(0);
}
std::size_t lua_screen_memory_peak()
{
return memory_peak().load();
}
std::unique_ptr<runtime> make_lua_runtime()

View File

@@ -39,16 +39,14 @@ constexpr std::size_t max_log_line{1024}; // bytes of the text, a longer one is
// 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
// 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};
// 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();
// 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
{

View File

@@ -1267,13 +1267,25 @@ for i = 1, 10000000 do kept[i] = ("x"):rep(1000) .. i end
return { render = function(state) return eu07.image(1, 1, { 255, 255, 255 }) end }
)"};
// a concatenation of 320 MB in one operation, from a string of 64 MB: only an allocator of the screen's own refuses
// it as it is made
// the memory limit of the Lua screens made while it lives, in place of max_screen_memory: a test that fills the memory
// of a screen stays small
class screen_memory_limit {
public:
explicit screen_memory_limit(std::size_t const Bytes) { script_screens::set_lua_screen_memory_limit(Bytes); }
~screen_memory_limit() { script_screens::set_lua_screen_memory_limit(script_screens::max_screen_memory); }
screen_memory_limit(screen_memory_limit const &) = delete;
screen_memory_limit &operator=(screen_memory_limit const &) = delete;
};
constexpr std::size_t small_screen_memory{32 * 1024 * 1024};
// with small_screen_memory: a concatenation of 80 MB in one operation, from a string of 8 MB, refused as it is made, in
// the pcall, as the instruction hook would see it only once it is made
constexpr std::string_view one_concatenation_script{R"(
local a = ("x"):rep(1024 * 1024)
for i = 1, 6 do a = a .. a end
local ok, message = pcall(function() return #(a .. a .. a .. a .. a) end)
local refused = #a == 64 * 1024 * 1024 and not ok and message:find("not enough memory", 1, true) ~= nil
for i = 1, 3 do a = a .. a end
local ok, message = pcall(function() return #(a .. a .. a .. a .. a .. a .. a .. a .. a .. a) end)
local refused = #a == 8 * 1024 * 1024 and not ok and message:find("not enough memory", 1, true) ~= nil
a = nil
return {
render = function(state)
@@ -1282,6 +1294,14 @@ return {
}
)"};
// doubles a string to 2 GB, 64 times small_screen_memory, in some fifty instructions: fewer than the thousand after
// which the instruction hook looks at the heap
constexpr std::string_view doubling_script{R"(
local s = ("x"):rep(1024 * 1024)
for i = 1, 11 do s = s .. s end
return { render = function(state) return eu07.image(1, 1, { 255, 255, 255 }) end }
)"};
// white when a coroutine yields across xpcall, and xpcall without a handler is an error
constexpr std::string_view xpcall_yield_script{R"(
local resume = coroutine.wrap(function()
@@ -1390,13 +1410,25 @@ TEST_CASE("lua screens: a screen holding more than its memory is switched off")
CHECK(log.lines_with("memory limit exceeded").size() + log.lines_with("not enough memory").size() > 0);
}
TEST_CASE("lua screens: with an allocator of the screen's own, a string made over the memory in one operation is refused") {
if (false == script_screens::lua_screen_memory_allocator()) {
MESSAGE("LuaJIT takes no allocator of the game's here (2.0, or 2.1 without GC64 on x64): not tested");
return;
}
TEST_CASE("lua screens: a string made over the memory in one operation is refused") {
screen_memory_limit const limit{small_screen_memory};
script_folder const folder{"lua_one_concatenation"};
CHECK(first_pixel(folder.write("concatenation.lua", one_concatenation_script), "concatenation") == 255);
CHECK(script_screens::lua_screen_memory_peak() <= 2 * small_screen_memory);
}
TEST_CASE("lua screens: a string doubled in a loop stops at the memory of the screen, not at LuaJIT's own limit") {
log_capture const log;
screen_memory_limit const limit{small_screen_memory};
script_folder const folder{"lua_doubling"};
check_switched_off(folder, "doubling.lua", doubling_script);
CHECK(log.lines_with("not enough memory").size() + log.lines_with("memory limit exceeded").size() > 0);
// past the limit between two checks of the hook, but refused before twice the limit (its garbage included). with
// LuaJIT 2.0 on x64 and no allocator in front of LuaJIT's own, this went on to its own limit, over 1 GB
auto const peak{script_screens::lua_screen_memory_peak()};
CAPTURE(peak);
CHECK(peak > small_screen_memory);
CHECK(peak <= 2 * small_screen_memory);
}
TEST_CASE("lua screens: a coroutine yields across xpcall") {