18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-28 22:03:20 +01:00

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.
This commit is contained in:
jerrrrycho
2026-11-13 14:25:18 +01:00
parent 39e8bcc59d
commit 1bd7187db7
14 changed files with 750 additions and 54 deletions

View File

@@ -5,6 +5,7 @@
#include <algorithm>
#include <array>
#include <string>
#include <vector>
// the drawing the Lua screens get from the game: fonts read as scripts/led_util.py reads them, text and images
@@ -85,6 +86,44 @@ TEST_CASE("script screen drawing: a description without glyphs, or one wider tha
CHECK_FALSE(font->find(U'w').lit(4, 0));
}
TEST_CASE("script screen drawing: a glyph wider than the largest image is skipped") {
auto const pixels{image(2, 1, {})};
auto const font{bitmap_font::parse(pixels, 2, 1, "//\nw5000\nv1\nx2147483647\n")};
REQUIRE(font.has_value());
// not in the font: the fallback, its first glyph
CHECK(font->find(U'w').width() == 1);
CHECK(font->find(U'x').width() == 1);
CHECK(font->find(U'v').width() == 1);
// the largest one is taken
auto const widest{bitmap_font::parse(pixels, 2, 1, "//\nw4096\n")};
REQUIRE(widest.has_value());
CHECK(widest->find(U'w').width() == 4096);
}
TEST_CASE("script screen drawing: a font of too many glyphs, too many columns or too large an image is refused") {
auto const pixels{image(2, 1, {})};
// more glyphs than any font has
std::string many{"//\n"};
for (char32_t character = 0x100; character < 0x100 + 5000; ++character) {
// the character in UTF-8 (two bytes up to 0x7FF, three above), then its width
if (character < 0x800) {
many += static_cast<char>(0xC0 | (character >> 6));
}
else {
many += static_cast<char>(0xE0 | (character >> 12));
many += static_cast<char>(0x80 | ((character >> 6) & 0x3F));
}
many += static_cast<char>(0x80 | (character & 0x3F));
many += "0\n";
}
CHECK_FALSE(bitmap_font::parse(pixels, 2, 1, many).has_value());
// glyphs side by side much wider than the largest image
CHECK_FALSE(bitmap_font::parse(pixels, 2, 1, "//\na4000\nb4000\nc4000\n").has_value());
// an image taller than the largest one
std::vector<std::uint8_t> const tall(static_cast<std::size_t>(1) * 5000 * 4, 255);
CHECK_FALSE(bitmap_font::parse(tall, 1, 5000, "//\na1\n").has_value());
}
TEST_CASE("script screen drawing: text is drawn from x rightwards and returns where it ends") {
auto const pixels{image(4, 1, {{0, 0}, {1, 0}, {3, 0}})};
auto const font{bitmap_font::parse(pixels, 4, 1, "//\na2\nb1\n")};