Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State.NewLib State.SetFuncs: Attempted to read or write protected memory #67

Closed
Ale32bit opened this issue Nov 6, 2020 · 1 comment
Closed

Comments

@Ale32bit
Copy link

Ale32bit commented Nov 6, 2020

KeraLua Version: 1.2.9
.NET Core Version: netcoreapp3.1

Upon trying to call Lua.NewLib this exception happen:
Note: It rarely works

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at KeraLua.NativeMethods.luaL_setfuncs(IntPtr, KeraLua.LuaRegister[], Int32)
   at KeraLua.NativeMethods.luaL_setfuncs(IntPtr, KeraLua.LuaRegister[], Int32)
   at KeraLua.Lua.SetFuncs(KeraLua.LuaRegister[], Int32)
   at KeraLua.Lua.NewLib(KeraLua.LuaRegister[])
   at TestLua.Program.OpenFoo(IntPtr)
   at KeraLua.NativeMethods.luaL_requiref(IntPtr, System.String, IntPtr, Int32)
   at KeraLua.NativeMethods.luaL_requiref(IntPtr, System.String, IntPtr, Int32)
   at KeraLua.Lua.RequireF(System.String, KeraLua.LuaFunction, Boolean)
   at TestLua.Program.Main()

Process finished with exit code -1,073,741,819.

This is the code I used to reproduce the bug:

using System;
using KeraLua;

namespace TestLua {
    class Program {
        private static LuaRegister[] fooReg = {
            new LuaRegister {
                name = "foo",
                function = Foo,
            }
        };
            
        static void Main() {
            var L = new Lua();
            L.RequireF("foobar", OpenFoo, true);

            L.DoFile("init.lua");
        }

        static int OpenFoo(IntPtr state) {
            var L = Lua.FromIntPtr(state);
            L.NewLib(fooReg);
            return 1;
        }
        
        static int Foo(IntPtr state) {
            var L = Lua.FromIntPtr(state);
            
            L.PushString("bar");
            return 1;
        }
    }
}

Tested with this Lua script:

print("Script started")

print(foobar.bar())

print("Script ended")
@viniciusjarina
Copy link
Member

viniciusjarina commented Nov 8, 2020

Hi @Ale32bit thank you for reporting this issue.

I couldn't repro here.

I guess the issue with your sample code you are missing a {null, null} terminator. 27f02d2#diff-b3658889284edf344fe657524d73ff6bd7b4ed87d3d9e087c10ddf500d523339R463

KeraLua is quite low-level and doesn't add (or try not to) any intelligence, is just a 1:1 map of the original Lua API, so you would have to do exactly as you would do using the C API.

In any case I've added a unit test testing the newlib to avoid any regressions.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants