mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-28 06:57:50 +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:
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user