* 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.
- 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
- Lua: once the instruction budget of a call is spent, the count hook
runs at every instruction and raises again, so pcall, nested pcall and
coroutine.resume can't swallow the limit; xpcall is rebuilt on pcall
so its handler runs after the error left f, while the hook works
- Lua: the metatables of eu07.image/font/sprite are locked
(__metatable = false); __gc destroys only an object of its own type,
once
- Lua: read_png checks the size with stbi_info before decoding
- Lua: string.rep makes no string over 1 MiB
- Lua and WASM: at most 64 commands per call, names up to 64 bytes,
32 log lines per call cut at 1024 bytes; the drops are logged once
- fonts: a glyph wider than 4096 is skipped; more than 4096 glyphs,
8192 columns or an image side over 4096 refuse the font; cut_glyph
reads only the columns inside the image
- the script path from the .mmd (cab screens, destination signs,
make: textures) must stay inside the game folder; the files a screen
reads, its script and its module must be regular files of 16 MiB at
most
Tests for each in test_script_screen_host.cpp, test_script_screen_drawing.cpp
and test_script_screens.cpp; the test stubs can capture the log.