lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

So I tried a simple test to work off a char array and back it up:

#include <string>

extern "C" {
    #include <lua.h>
    #include <lauxlib.h>
    #include <lualib.h>
}

char buffer[4096];
char backupBuffer[4096];
char *pTop = buffer;

void* customLuaAlloc(void* userData, void* ptr, size_t oldSize, size_t newSize)
{
    if (newSize == 0)
    {
        //free(ptr);
        return 0;
    }
    else {
        //return realloc(ptr, newSize);
        if (ptr != NULL) {
            // Copy contents
            memcpy(pTop, ptr, oldSize);
        }
        ptr = NULL;
        char *pOldTop = pTop;
        pTop += newSize;
        return (void*)pOldTop;
    }
}

int main()
{
    lua_State* L = luaL_newstate();
    lua_setallocf(L, customLuaAlloc, NULL);

    luaL_openlibs(L);

    luaL_dostring(L, "a = 1");
    memcpy(backupBuffer, buffer, 4096);   // this causes a crash

    luaL_dostring(L, "a = 2");

    //memcpy(buffer, backupBuffer, 4096);  // try restoring 'a' here once back up works
    luaL_dostring(L, "print(a)");

    lua_close(L);
}

If I comment out:  memcpy(backupBuffer, buffer, 4096)    then things are fine. Otherwise a crash at:
> LuaTTD.exe!luaH_getshortstr(Table * t, TString * key) Line 544 C
  LuaTTD.exe!luaT_gettmbyobj(lua_State * L, const lua_TValue * o, TMS event) Line 82 C
  LuaTTD.exe!GCTM(lua_State * L, int propagateerrors) Line 812 C
  LuaTTD.exe!callallpendingfinalizers(lua_State * L) Line 862 C
  LuaTTD.exe!luaC_freeallobjects(lua_State * L) Line 971 C
  LuaTTD.exe!close_state(lua_State * L) Line 245 C
  LuaTTD.exe!lua_close(lua_State * L) Line 344 C
  LuaTTD.exe!main() Line 50 C++
  LuaTTD.exe!invoke_main() Line 78 C++
  LuaTTD.exe!__scrt_common_main_seh() Line 288 C++
  LuaTTD.exe!__scrt_common_main() Line 331 C++
  LuaTTD.exe!mainCRTStartup() Line 17 C++

Trying to figure out why.

Thanks,
Abhijit


On Fri, Nov 1, 2019 at 5:09 PM Abhijit Nandy <abhijit.nandy@gmail.com> wrote:
Hi Luiz,

Thanks for your reply. Maybe I can focus on Lua code only, so ignoring user values for now. So using the custom memory allocator, I could work off a C array for the memory contents. If I save the array to a binary file and then let the interpreter proceed, then it continues to make changes to the array. Now maybe I pause execution somehow by using a co-routine or the debug library.

Then later can I simply restore the array contents from the file and then resume the interpretor, and the interpretor would continue executing from the saved point? And I could repeat this as many times as I want maybe after changing a value of a variable using the debug library :) ?

Thanks,
Abhijit


On Fri, Nov 1, 2019 at 4:15 PM Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> Is it possible to save the Lua VM state and restore it?

In principle, it can be done since the Lua API accepts a memory
allocator. Like you've said, it is tricky to handle userdata.

I've experimented with LPSM to make a Lua state persistent across
invocations of the interpreter. It worked reasonably well for a while.
The LPSM library is available at http://freshmeat.net/projects/lpsm/
but it does not seem to work on modern systems. I'd love to hear about
a modern replacement.