Function to detect Lua state and heap corruption

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Function to detect Lua state and heap corruption

Abhijit Nandy
Hi,

Is there any function in Lua or library or any code modification to Lua itself, that can be called to verify the Lua state, internal pointers and state of the stack and heap at runtime?

I have a medium sized application, and sometimes the Lua state gets corrupted probably due to forgetting to pop the correct values or multiple threads accessing the state. While I have taken measures to prevent this, we still sometimes get a heap corruption that leads to a crash later, making the problematic area difficult to pin point.

If there is a function that can be run to verify if everything is fine after every Lua code execution request, that can help to narrow down the bad code faster.

Thanks
Abhi


Reply | Threaded
Open this post in threaded view
|

Re: Function to detect Lua state and heap corruption

Andrew Gierth
>>>>> "Abhijit" == Abhijit Nandy <[hidden email]> writes:

 Abhijit> I have a medium sized application, and sometimes the Lua state
 Abhijit> gets corrupted probably due to forgetting to pop the correct
 Abhijit> values or multiple threads accessing the state. While I have
 Abhijit> taken measures to prevent this, we still sometimes get a heap
 Abhijit> corruption that leads to a crash later, making the problematic
 Abhijit> area difficult to pin point.

 Abhijit> If there is a function that can be run to verify if everything
 Abhijit> is fine after every Lua code execution request, that can help
 Abhijit> to narrow down the bad code faster.

Did you already try enabling the API checks? That doesn't check the
whole state, but it does check the sanity of the arguments to every API
call.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: Function to detect Lua state and heap corruption

Roberto Ierusalimschy
In reply to this post by Abhijit Nandy
> Is there any function in Lua or library or any code modification to Lua
> itself, that can be called to verify the Lua state, internal pointers and
> state of the stack and heap at runtime?

You may have a look at 'lua_checkmemory', implemented in the file
'ltests.c' from the test suite [1].

[1] https://www.lua.org/tests/

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Function to detect Lua state and heap corruption

Abhijit Nandy
Thanks Roberto! I will incorporate those as a check in my application.

Andrew, 
By enabling the API checks you mean these lines in lapi.h? :

#define api_incr_top(L)   {L->top++; api_check(L->top <= L->ci->top, \
"stack overflow");}

#define adjustresults(L,nres) \
    { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }

#define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \
 "not enough elements in the stack")

On Mon, Jun 17, 2019 at 7:38 PM Roberto Ierusalimschy <[hidden email]> wrote:
> Is there any function in Lua or library or any code modification to Lua
> itself, that can be called to verify the Lua state, internal pointers and
> state of the stack and heap at runtime?

You may have a look at 'lua_checkmemory', implemented in the file
'ltests.c' from the test suite [1].

[1] https://www.lua.org/tests/

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Function to detect Lua state and heap corruption

Roberto Ierusalimschy
> By enabling the API checks you mean these lines in lapi.h? :
>
> #define api_incr_top(L)   {L->top++; api_check(L->top <= L->ci->top, \
> "stack overflow");}
>
> #define adjustresults(L,nres) \
>     { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top =
> L->top; }
>
> #define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \
>  "not enough elements in the stack")

He probably means this lines in luaconf.h:

/*
@@ LUA_USE_APICHECK turns on several consistency checks on the C API.
** Define it as a help when debugging C code.
*/
#if defined(LUA_USE_APICHECK)
#include <assert.h>
#define luai_apicheck(l,e)      assert(e)
#endif

You only have to define LUA_USE_APICHECK.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Function to detect Lua state and heap corruption

Abhijit Nandy
Ah thanks again :)

On Mon, Jun 17, 2019 at 7:54 PM Roberto Ierusalimschy <[hidden email]> wrote:
> By enabling the API checks you mean these lines in lapi.h? :
>
> #define api_incr_top(L)   {L->top++; api_check(L->top <= L->ci->top, \
> "stack overflow");}
>
> #define adjustresults(L,nres) \
>     { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top =
> L->top; }
>
> #define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \
>  "not enough elements in the stack")

He probably means this lines in luaconf.h:

/*
@@ LUA_USE_APICHECK turns on several consistency checks on the C API.
** Define it as a help when debugging C code.
*/
#if defined(LUA_USE_APICHECK)
#include <assert.h>
#define luai_apicheck(l,e)      assert(e)
#endif

You only have to define LUA_USE_APICHECK.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Function to detect Lua state and heap corruption

Paul Ducklin
In reply to this post by Roberto Ierusalimschy

> [1] https://www.lua.org/tests/
>
> -- Roberto

Speaking of test suites, can we get an tarball of official “authors’ tests” for 5.4.0-alpha, please?

The most recent test suites published are for 5.4.0-w2 and lots of things have changed in the past year so that the alpha release cannot and does not pass them. (In fact, many test files fail in several places.)

Error messages have changed; string coercion has changed; compiled chunk headers have changed; gc has changed; important-to-test features such as to-be-closed have been added; and more.

I have a range of tests of my own but I like to put every rebuild I perform through the official tests, too.

Also, there’s no official test suite published for 5.3.5 either, which is a bad look for newcomers to Lua who want to build and test their own officially-release flavour.

In 2019 a mainstream code project really needs to have a comprehensive set of tests that keeps pace with the project releases...