From a3dae27a72750c24abd2cc5d570fecce2705af18 Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Thu, 18 Jun 2026 20:49:08 -0300 Subject: [PATCH] Disallow requiring love.graphics and love.window in threads. --- src/common/runtime.cpp | 7 +++++++ src/common/runtime.h | 8 ++++++++ src/modules/thread/LuaThread.cpp | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index 8833130d7..5609d732e 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -1064,6 +1064,13 @@ lua_State *luax_getpinnedthread(lua_State *L) return thread; } +extern "C" int luax_module_threaderror(lua_State *L) +{ + // Module name is the first param in the stack. + const char *name = luaL_checkstring(L, 1); + return luaL_error(L, "The %s module cannot be used in thread code.", name); +} + void luax_markdeprecated(lua_State *L, int level, const char *name, APIType api) { luax_markdeprecated(L, level, name, api, DEPRECATED_NO_REPLACEMENT, nullptr); diff --git a/src/common/runtime.h b/src/common/runtime.h index c79d2b3e5..1228ce010 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -493,6 +493,14 @@ lua_State *luax_insistpinnedthread(lua_State *L); **/ lua_State *luax_getpinnedthread(lua_State *L); +/** + * Lua C function to use with luax_preload when the given module should not + * be allowed on a thread. Must be called from the thread's code. + **/ +extern "C" { + int luax_module_threaderror(lua_State *L); +} + /** * Mark a function as deprecated. Should only be called inside wrapper function * code. diff --git a/src/modules/thread/LuaThread.cpp b/src/modules/thread/LuaThread.cpp index 926ebd509..e4fa95e5c 100644 --- a/src/modules/thread/LuaThread.cpp +++ b/src/modules/thread/LuaThread.cpp @@ -68,6 +68,10 @@ void LuaThread::threadFunction() lua_pop(L, 1); #endif // LOVE_BUILD_STANDALONE + // We might want to add more input-related modules to this list. + luax_preload(L, luax_module_threaderror, "love.window"); + luax_preload(L, luax_module_threaderror, "love.graphics"); + luax_require(L, "love.thread"); lua_pop(L, 1);