You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by hu...@apache.org on 2022/06/01 04:45:42 UTC

[incubator-kvrocks] branch unstable updated: Compile lua by C++ compilers to avoid memory leaks (#614)

This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new b9f7863  Compile lua by C++ compilers to avoid memory leaks (#614)
b9f7863 is described below

commit b9f7863abc3d15f5a489c7c210eb83712c3cf05e
Author: Twice <i...@twice.moe>
AuthorDate: Wed Jun 1 12:45:37 2022 +0800

    Compile lua by C++ compilers to avoid memory leaks (#614)
    
    As mentioned in issue #609, the lua_error will use the longjump internal
    and ignore the C++ destructor if we built with c mode. This would break
    the RAII mode and cause memory leak, so we workaround this issue by
    building with the c++ compiler.
---
 .github/workflows/kvrocks.yaml |  2 +-
 cmake/lua.cmake                | 10 +++++++---
 src/scripting.cc               |  4 +---
 src/scripting.h                |  4 +---
 src/server.h                   |  2 --
 5 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/.github/workflows/kvrocks.yaml b/.github/workflows/kvrocks.yaml
index 0b99f5e..d9ef962 100644
--- a/.github/workflows/kvrocks.yaml
+++ b/.github/workflows/kvrocks.yaml
@@ -88,7 +88,7 @@ jobs:
 
       - name: Install Dependencies
         run: |
-          brew install gcc autoconf automake libtool cmake
+          brew install autoconf automake libtool cmake
           mkdir build
 
       - name: Build
diff --git a/cmake/lua.cmake b/cmake/lua.cmake
index 3f58041..c829405 100644
--- a/cmake/lua.cmake
+++ b/cmake/lua.cmake
@@ -21,16 +21,20 @@ include(FetchContent)
 
 FetchContent_Declare(lua
   GIT_REPOSITORY https://github.com/KvrocksLabs/lua
-  GIT_TAG 6f73d72d45c2e3915ee961e41705f35526608735
+  GIT_TAG c8e4bbfa25f7202f3b778ccb88e54ab84a1861fb
 )
 
 FetchContent_GetProperties(lua)
 if(NOT lua_POPULATED)
   FetchContent_Populate(lua)
 
-  set(LUA_CFLAGS "${CMAKE_C_FLAGS} -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC= -DLUA_USE_MKSTEMP")
+  set(LUA_CXX ${CMAKE_CXX_COMPILER})
+  set(LUA_CFLAGS "${CMAKE_CXX_FLAGS} -fpermissive -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC= -DLUA_USE_MKSTEMP")
+  if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
+    set(LUA_CFLAGS "${LUA_CFLAGS} -isysroot ${CMAKE_OSX_SYSROOT}")
+  endif()
 
-  add_custom_target(make_lua COMMAND make "CFLAGS=${LUA_CFLAGS}" liblua.a
+  add_custom_target(make_lua COMMAND make "CC=${LUA_CXX}" "CFLAGS=${LUA_CFLAGS}" liblua.a
     WORKING_DIRECTORY ${lua_SOURCE_DIR}/src
   )
   
diff --git a/src/scripting.cc b/src/scripting.cc
index 15384cc..c84c814 100644
--- a/src/scripting.cc
+++ b/src/scripting.cc
@@ -74,12 +74,10 @@ enum {
   LL_WARNING,
 };
 
-extern "C" {
 LUALIB_API int (luaopen_cjson)(lua_State *L);
 LUALIB_API int (luaopen_struct)(lua_State *L);
 LUALIB_API int (luaopen_cmsgpack)(lua_State *L);
 LUALIB_API int (luaopen_bit)(lua_State *L);
-}
 
 namespace Lua {
   lua_State* CreateState() {
@@ -778,7 +776,7 @@ std::string replyToRedisReply(lua_State *lua) {
  * by the non-error-trapping version of redis.pcall(), which is redis.call(),
  * this function will raise the Lua error so that the execution of the
  * script will be halted. */
-int raiseError(lua_State *lua) {
+[[noreturn]] int raiseError(lua_State *lua) {
   lua_pushstring(lua, "err");
   lua_gettable(lua, -2);
   return lua_error(lua);
diff --git a/src/scripting.h b/src/scripting.h
index bc34ccd..d021184 100644
--- a/src/scripting.h
+++ b/src/scripting.h
@@ -23,11 +23,9 @@
 #include <string>
 #include <vector>
 
-extern "C" {
 #include <lua.h>
 #include <lauxlib.h>
 #include <lualib.h>
-}
 
 #include "status.h"
 #include "redis_connection.h"
@@ -67,7 +65,7 @@ const char *redisProtocolToLuaType_Bool(lua_State *lua, const char *reply, int t
 const char *redisProtocolToLuaType_Double(lua_State *lua, const char *reply);
 std::string replyToRedisReply(lua_State *lua);
 void pushError(lua_State *lua, const char *err);
-int raiseError(lua_State *lua);
+[[noreturn]] int raiseError(lua_State *lua);
 void sortArray(lua_State *lua);
 void setGlobalArray(lua_State *lua, const std::string &var, const std::vector<std::string> &elems);
 
diff --git a/src/server.h b/src/server.h
index ca61212..2234890 100644
--- a/src/server.h
+++ b/src/server.h
@@ -30,9 +30,7 @@
 #include <memory>
 #include <unordered_map>
 
-extern "C" {
 #include <lua.h>
-}
 
 #include "stats.h"
 #include "storage.h"