Work around LuaJIT issues on aarch64 (#9614)
authorsfan5 <sfan5@live.de>
Wed, 8 Apr 2020 18:14:08 +0000 (20:14 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Apr 2020 18:14:08 +0000 (20:14 +0200)
- Move the text segment below the 47-bit limit, needed for script_exception_wrapper which must be lightuserdata
- Replace CUSTOM_RIDX_SCRIPTAPI with full userdata

src/CMakeLists.txt
src/script/common/c_internal.h
src/script/cpp_api/s_base.cpp
src/script/cpp_api/s_security.cpp
src/script/lua_api/l_base.cpp

index 6afa5b8fe99c9781f98ca8adc9fce9e630165168..d579bb9659d09327d56f99cef6b253177214028a 100644 (file)
@@ -661,7 +661,7 @@ endif()
 
 # Set some optimizations and tweaks
 
-include(CheckCXXCompilerFlag)
+include(CheckCSourceCompiles)
 
 if(MSVC)
        # Visual Studio
@@ -695,13 +695,19 @@ else()
        else()
                set(RELEASE_WARNING_FLAGS "")
        endif()
-
        if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
                set(WARNING_FLAGS "${WARNING_FLAGS} -Wsign-compare")
        endif()
+
        if(APPLE AND USE_LUAJIT)
                # required per http://luajit.org/install.html
                SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -pagezero_size 10000 -image_base 100000000")
+       elseif(UNIX AND USE_LUAJIT)
+               check_c_source_compiles("#ifndef __aarch64__\n#error\n#endif\nint main(){}" IS_AARCH64)
+               if(IS_AARCH64)
+                       # Move text segment below LuaJIT's 47-bit limit (see issue #9367)
+                       SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Ttext-segment=0x200000000")
+               endif()
        endif()
 
        if(MINGW)
index d8cf3fe767e33c0f5a8f9990d801c625031db35d..747400769af3f12cae88a4698a0e49f43d5e18c0 100644 (file)
@@ -54,6 +54,15 @@ extern "C" {
 #define CUSTOM_RIDX_CURRENT_MOD_NAME    (CUSTOM_RIDX_BASE + 2)
 #define CUSTOM_RIDX_BACKTRACE           (CUSTOM_RIDX_BASE + 3)
 
+// Determine if CUSTOM_RIDX_SCRIPTAPI will hold a light or full userdata
+#if defined(__aarch64__) && USE_LUAJIT
+/* LuaJIT has a 47-bit limit for lightuserdata on this platform and we cannot
+ * assume that the ScriptApi class was allocated at a fitting address. */
+#define INDIRECT_SCRIPTAPI_RIDX 1
+#else
+#define INDIRECT_SCRIPTAPI_RIDX 0
+#endif
+
 // Pushes the error handler onto the stack and returns its index
 #define PUSH_ERROR_HANDLER(L) \
        (lua_rawgeti((L), LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE), lua_gettop((L)))
index ecb1ba39b6a8dc071765843968b94b094bdb59ce..f234a15d4565e2b02f8fb7c974fcc8468fcafa32 100644 (file)
@@ -90,7 +90,11 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
                luaL_openlibs(m_luastack);
 
        // Make the ScriptApiBase* accessible to ModApiBase
+#if INDIRECT_SCRIPTAPI_RIDX
+       *(void **)(lua_newuserdata(m_luastack, sizeof(void *))) = this;
+#else
        lua_pushlightuserdata(m_luastack, this);
+#endif
        lua_rawseti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
 
        // Add and save an error handler
index b5abcfb5da8f5fa05a0056b70113f9e42b02faf0..2afa3a191d1ce7b8b451c89cebba44570dc24caa 100644 (file)
@@ -499,7 +499,12 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
 
        // Get server from registry
        lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
-       ScriptApiBase *script = (ScriptApiBase *) lua_touserdata(L, -1);
+       ScriptApiBase *script;
+#if INDIRECT_SCRIPTAPI_RIDX
+       script = (ScriptApiBase *) *(void**)(lua_touserdata(L, -1));
+#else
+       script = (ScriptApiBase *) lua_touserdata(L, -1);
+#endif
        lua_pop(L, 1);
        const IGameDef *gamedef = script->getGameDef();
        if (!gamedef)
index 8486fc7bc5d64cd7d1acdd61aec86f7fa2feddd2..c980bba3922e29fed40204ca7119a2c385560c78 100644 (file)
@@ -30,7 +30,12 @@ ScriptApiBase *ModApiBase::getScriptApiBase(lua_State *L)
 {
        // Get server from registry
        lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
-       ScriptApiBase *sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
+       ScriptApiBase *sapi_ptr;
+#if INDIRECT_SCRIPTAPI_RIDX
+       sapi_ptr = (ScriptApiBase*) *(void**)(lua_touserdata(L, -1));
+#else
+       sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
+#endif
        lua_pop(L, 1);
        return sapi_ptr;
 }