From fe55b94ebd8bc6eafae0dea3446cbf367b5aabb2 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 12 Jun 2024 19:39:31 +0300 Subject: [PATCH] fix: lua setInterval stack overflow --- src/frontend/debug_panel.cpp | 4 ++++ src/logic/scripting/lua/lua_engine.cpp | 19 ++++++++++--------- src/logic/scripting/lua/lua_util.cpp | 7 ++++--- src/logic/scripting/scripting.cpp | 4 ++++ src/logic/scripting/scripting.hpp | 3 ++- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index 24caea8d..16e4afcd 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -8,6 +8,7 @@ #include "../graphics/ui/elements/TrackBar.hpp" #include "../graphics/ui/elements/InputBindBox.hpp" #include "../graphics/render/WorldRenderer.hpp" +#include "../logic/scripting/scripting.hpp" #include "../objects/Player.hpp" #include "../physics/Hitbox.hpp" #include "../util/stringutil.hpp" @@ -65,6 +66,9 @@ std::shared_ptr create_debug_panel( return L"speakers: " + std::to_wstring(audio::count_speakers())+ L" streams: " + std::to_wstring(audio::count_streams()); })); + panel->add(create_label([](){ + return L"lua-stack: " + std::to_wstring(scripting::get_values_on_stack()); + })); panel->add(create_label([=](){ auto& settings = engine->getSettings(); bool culling = settings.graphics.frustumCulling.get(); diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index bc4d4423..096109df 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -22,6 +22,7 @@ static void remove_lib_funcs(lua::State* L, const char* libname, const char* fun setfield(L, funcs[i], -2); } } + pop(L); } static void create_libs(lua::State* L) { @@ -44,6 +45,7 @@ static void create_libs(lua::State* L) { addfunc(L, "print", lua::wrap); } +#include void lua::initialize() { logger.info() << LUA_VERSION; logger.info() << LUAJIT_VERSION; @@ -54,15 +56,14 @@ void lua::initialize() { } main_thread = L; // Allowed standard libraries - luaopen_base(L); - luaopen_math(L); - luaopen_string(L); - luaopen_table(L); - luaopen_debug(L); - luaopen_jit(L); - luaopen_bit(L); - - luaopen_os(L); + pop(L, luaopen_base(L)); + pop(L, luaopen_math(L)); + pop(L, luaopen_string(L)); + pop(L, luaopen_table(L)); + pop(L, luaopen_debug(L)); + pop(L, luaopen_jit(L)); + pop(L, luaopen_bit(L)); + pop(L, luaopen_os(L)); const char* removed_os[] { "execute", "exit", diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index e2e9a929..198ba280 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -145,7 +145,7 @@ void lua::dump_stack(State* L) { } } -static std::shared_ptr createLambdaHandler(State* L) { +static std::shared_ptr create_lambda_handler(State* L) { auto ptr = reinterpret_cast(topointer(L, -1)); auto name = util::mangleid(ptr); getglobal(L, LAMBDAS_TABLE); @@ -163,16 +163,17 @@ static std::shared_ptr createLambdaHandler(State* L) { } runnable lua::create_runnable(State* L) { - auto funcptr = createLambdaHandler(L); + auto funcptr = create_lambda_handler(L); return [=]() { getglobal(L, LAMBDAS_TABLE); getfield(L, *funcptr); call_nothrow(L, 0); + pop(L); }; } scripting::common_func lua::create_lambda(State* L) { - auto funcptr = createLambdaHandler(L); + auto funcptr = create_lambda_handler(L); return [=](const std::vector& args) { getglobal(L, LAMBDAS_TABLE); getfield(L, *funcptr); diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 6fc0f32e..850dca99 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -277,6 +277,10 @@ bool scripting::register_event(int env, const std::string& name, const std::stri return false; } +int scripting::get_values_on_stack() { + return lua::gettop(lua::get_main_thread()); +} + void scripting::load_block_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, block_funcs_set& funcsset) { int env = *senv; std::string src = files::read_string(file); diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 6b7f9b3d..6ac25e7b 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -41,7 +41,8 @@ namespace scripting { void initialize(Engine* engine); - extern bool register_event(int env, const std::string& name, const std::string& id); + bool register_event(int env, const std::string& name, const std::string& id); + int get_values_on_stack(); scriptenv get_root_environment(); scriptenv create_pack_environment(const ContentPack& pack);