18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 22:55:16 +01:00
Commit Graph

8 Commits

Author SHA1 Message Date
jerrrrycho
ff638fb5b2 Merge remote-tracking branch 'origin/staging' into fix/script-screen-sandbox-limits
scripting/luaprogram.cpp: the libraries are opened by lua_sandbox::open_safe_libraries() of staging (#358), which removes newproxy and string.dump as this branch did; the limits of this branch (limit_libraries) are applied after it.
2026-11-19 04:54:36 +01:00
jerrrrycho
178f5b63fe fix(scripting): gsub with a metatable'd table, the trim idiom, the memory doc
- replacement_length() popped nothing after lua_getmetatable() pushed a metatable, so gsub got it
  as its 4th argument ("bad argument #4 to 'gsub'"); it is popped now.
- lua_pattern_cost(): a repetition followed by what always matches (as the first %s* of
  "^%s*(.-)%s*$") is tried once and doesn't multiply; a repetition of a character or a %class
  right before the final '$' counts the longest run of it in the subject, not the subject. The
  trim of a 10 KB line of words is allowed, a long run of spaces is still refused (it takes the
  square of the run). The doc names a trim that is linear on any string.
- the doc says every concatenation within one hook interval is made whole before the memory check.
2026-11-17 09:10:08 +01:00
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
jerrrrycho
acda6562cd fix(scripting): sandbox the scenery Lua scripts like the Lua screens
The scenery scripts (lua <file> in the .scn) ran in a state with all of
LuaJIT's libraries (io, os, ffi, debug, require, load*), loaded with
luaL_dofile from any path, '..' and absolute ones included, and their
errors in event handlers went through an unprotected lua_call.

- scripting/luasandbox.{h,cpp}: what the Lua states share: the safe
  library set (moved from lua_program::open_libraries), loading a text-only
  chunk (moved from load_chunk), reading a script from a folder through the
  screens' path check (regular file, at most 16 MB), running it under pcall,
  and a clock-only os table (time, clock, difftime, date with the C89
  conversions; MSVC's strftime ends the program on an unknown one).
- lua.cpp: the scenery state gets the safe set and the clock; interpret()
  takes the scenery folder and the file and logs a refused or failing script
  with its file name; event handlers run under pcall and are logged with the
  event name; the scriptapi functions check their string argument
  (luaL_checkstring) instead of making a std::string from NULL; a memcell
  table without str updates the text to "" instead of from NULL.
- luaprogram.cpp: calls the shared helpers, no change for the screens.
- eu07.ini lua.fulllibraries (default no) gives trusted sceneries all
  libraries back. Documented in script_screens_doc.md.
- tests/unit/test_lua_sandbox.cpp: 8 cases for the shared sandbox.
2026-11-13 15:23:01 +01:00
jerrrrycho
1bd7187db7 fix(scripting): close the small holes in the script screen sandbox
- 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.
2026-11-13 14:25:18 +01:00
jerrrrycho
5bac4e1cf1 Fix the four issues failing the Sonar quality gate on master (#323)
* fix(scripting): forward the object in push_object of the Lua screens (cpp:S5417)

push_object takes a forwarding reference and called std::move on it, which
SonarCloud reports as a blocker bug. It only worked because every caller
passes an rvalue: with an lvalue Type_ is deduced as a reference, and
new Type_(std::move(Object)) doesn't even compile.

The object is now forwarded, and the userdata is sized and built for
std::remove_cvref_t<Type_>, the type collect<> and check_object<> are used
with: an rvalue is moved in, an lvalue would be copied. Every caller passes
an rvalue of lua_image, bitmap_font or glyph, so they move the object in
exactly as before.

* fix(simulation): turbidity from the overcast without branches of the same value (cpp:S3923)

The chain of conditionals setting Global.fTurbidity had pairs of branches
with the same result (<= 0.10 and <= 0.20 both 4, and so on, the last two
both 9), which SonarCloud reports as a bug. The thresholds that changed
nothing are gone and the mapping is a small function: up to 0.20 gives 4,
up to 0.40 5, up to 0.60 6, up to 0.70 7, up to 0.80 8, anything else 9.
The comparisons are the same ones, in the same order, so every overcast
value gives the turbidity it gave before.

* test: script screen tests keep their files in the build folder, not in the temporary one (cpp:S5443)

The folders of the script screen host tests were made in the system's
temporary directory, and the sandbox test put its "outside.txt" straight
into that directory. SonarCloud reports both uses as vulnerabilities:
the directory is writable by everyone.

The folders are now made in the build folder of the unit tests, passed to
them as EU07_UNIT_TESTS_SCRATCH, the way EU07_UNIT_TESTS_DATA is. The
scripts are one level down in the folder of a test, so the file the sandbox
test puts outside the folder of the scripts is still inside the folder of
the test and goes with it. Nothing depends on the working directory, and
nothing is left behind.
2026-11-11 06:04:31 +01:00
e433b43fef Script screens: led_post and resize of the Lua screens on the gpu 2026-11-10 16:22:35 +01:00
c4924f756d Lua script screens beside the WebAssembly ones 2026-11-10 15:29:25 +01:00