PANIC: unprotected error in call to Lua API (math)

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

PANIC: unprotected error in call to Lua API (math)

Leinen, Rick

Greetings,

 

While I have some experience writing lua scripts, I am new to implementing lua on a hardware platform.  I am trying to implement lua on a Freescale Tower with a Kinetis K65 microcontroller.  It has 2 MB of flash and 256 KB of SRAM on board.  I have successfully built a project using IAR’s ARM IDE using one of the Tower’s simple demo projects as a launching point.

 

From main(), the project successfully runs the following line of code:

 

   lua_State *L = luaL_newstate();

 

Next, the code attempts to open the lua libraries with this line of code:

 

   luaL_openlibs(L);

 

It makes as far as the math library when the following is sent out the serial port from lua:

 

    PANIC: unprotected error in call to Lua API (math)

 

I’ve stepped through the code and found that the error appears to occur in the following code:

 

   void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {

     void *newblock;

     global_State *g = G(L);

     size_t realosize = (block) ? osize : 0;

     lua_assert((realosize == 0) == (block == NULL));

   #if defined(HARDMEMTESTS)

     if (nsize > realosize && g->gcrunning)

       luaC_fullgc(L, 1);  /* force a GC whenever possible */

   #endif

     newblock = (*g->frealloc)(g->ud, block, osize, nsize);

     if (newblock == NULL && nsize > 0) {

       api_check(L, nsize > realosize,

                    "realloc cannot fail when shrinking a block");

       if (g->gcrunning) {

         luaC_fullgc(L, 1);  /* try to free some memory... */

         newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */

       }

       if (newblock == NULL)

         luaD_throw(L, LUA_ERRMEM);

     }

     lua_assert((nsize == 0) == (newblock == NULL));

     g->GCdebt = (g->GCdebt + nsize) - realosize;

     return newblock;

   }

 

“newblock” is evidently set to NULL and the code calls luaD_throw() which deals with the error.

 

Anyone have any ideas to help resolve this?

 

Thanks,

 

Rick

 

 

 

 

 

Reply | Threaded
Open this post in threaded view
|

Re: PANIC: unprotected error in call to Lua API (math)

Rena
On Thu, May 8, 2014 at 8:50 PM, Leinen, Rick <[hidden email]> wrote:

     newblock = (*g->frealloc)(g->ud, block, osize, nsize); 

[...]


       if (newblock == NULL)

         luaD_throw(L, LUA_ERRMEM);

[...]

“newblock” is evidently set to NULL and the code calls luaD_throw() which deals with the error.

 


That would imply the allocation failed, i.e. it's run out of memory (or the allocator is broken).

--
Sent from my Game Boy.
Reply | Threaded
Open this post in threaded view
|

Re: PANIC: unprotected error in call to Lua API (math)

Lourival Vieira Neto
In reply to this post by Leinen, Rick
On Thu, May 8, 2014 at 9:50 PM, Leinen, Rick <[hidden email]> wrote:
> Anyone have any ideas to help resolve this?

Did you define a custom lua_Alloc function? If yes, are you complying
with the contract [1]?

[1] http://www.lua.org/manual/5.2/manual.html#lua_Alloc

Regards,
--
Lourival Vieira Neto

Reply | Threaded
Open this post in threaded view
|

Re: PANIC: unprotected error in call to Lua API (math)

Philipp Janda
In reply to this post by Leinen, Rick
Am 09.05.2014 02:50 schröbte Leinen, Rick:
> Greetings,

Hi!

>
> While I have some experience writing lua scripts, I am new to implementing lua on a hardware platform.  I am trying to implement lua on a Freescale Tower with a Kinetis K65 microcontroller.  It has 2 MB of flash and 256 KB of SRAM on board.  I have successfully built a project using IAR's ARM IDE using one of the Tower's simple demo projects as a launching point.
>
>>From main(), the project successfully runs the following line of code:
>
>     lua_State *L = luaL_newstate();
>
> Next, the code attempts to open the lua libraries with this line of code:
>
>     luaL_openlibs(L);
>
> It makes as far as the math library when the following is sent out the serial port from lua:
>
>      PANIC: unprotected error in call to Lua API (math)
>
> Anyone have any ideas to help resolve this?
>

As Rena said, probably a memory allocation issue (although 256K should
be plenty). If you have a look at the source of `lua.c`, you will see
that the first thing after `luaL_newstate` is a call to `lua_pcall` to
avoid such unprotected errors in case of allocation failures. This
should at least give you a more useful error message.

If this really is a memory problem, you might also want to have a look
at elua[1] which includes a patch[2] to store module tables readonly in
ROM instead of RAM to save memory. Elua has a port[3] for Kinetis which
you could use as a starting point (Elua is Lua 5.1, though) ...

> Thanks,
>
> Rick
>

Philipp

   [1]: http://www.eluaproject.net/
   [2]: http://www.eluaproject.net/doc/v0.9/en_arch_ltr.html
   [3]: http://wiki.eluaproject.net/Boards#ARM_Cortex-M4



Reply | Threaded
Open this post in threaded view
|

RE: PANIC: unprotected error in call to Lua API (math)

Leinen, Rick
In reply to this post by Lourival Vieira Neto
I did not.  I used the lua code as is.

Rick
 


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Lourival Vieira Neto
Sent: Thursday, May 08, 2014 8:48 PM
To: Lua mailing list
Subject: Re: PANIC: unprotected error in call to Lua API (math)

On Thu, May 8, 2014 at 9:50 PM, Leinen, Rick <[hidden email]> wrote:
> Anyone have any ideas to help resolve this?

Did you define a custom lua_Alloc function? If yes, are you complying with the contract [1]?

[1] http://www.lua.org/manual/5.2/manual.html#lua_Alloc

Regards,
--
Lourival Vieira Neto

Reply | Threaded
Open this post in threaded view
|

RE: PANIC: unprotected error in call to Lua API (math)

Leinen, Rick
In reply to this post by Philipp Janda
Phillipp,

I see what you mean with lua_pcall, I will try it.  The elua patch is a great idea.  Thanks for suggesting.

Rick
 



-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Philipp Janda
Sent: Thursday, May 08, 2014 11:15 PM
To: [hidden email]
Subject: Re: PANIC: unprotected error in call to Lua API (math)

Am 09.05.2014 02:50 schröbte Leinen, Rick:
> Greetings,

Hi!

>
> While I have some experience writing lua scripts, I am new to implementing lua on a hardware platform.  I am trying to implement lua on a Freescale Tower with a Kinetis K65 microcontroller.  It has 2 MB of flash and 256 KB of SRAM on board.  I have successfully built a project using IAR's ARM IDE using one of the Tower's simple demo projects as a launching point.
>
>>From main(), the project successfully runs the following line of code:
>
>     lua_State *L = luaL_newstate();
>
> Next, the code attempts to open the lua libraries with this line of code:
>
>     luaL_openlibs(L);
>
> It makes as far as the math library when the following is sent out the serial port from lua:
>
>      PANIC: unprotected error in call to Lua API (math)
>
> Anyone have any ideas to help resolve this?
>

As Rena said, probably a memory allocation issue (although 256K should be plenty). If you have a look at the source of `lua.c`, you will see that the first thing after `luaL_newstate` is a call to `lua_pcall` to avoid such unprotected errors in case of allocation failures. This should at least give you a more useful error message.

If this really is a memory problem, you might also want to have a look at elua[1] which includes a patch[2] to store module tables readonly in ROM instead of RAM to save memory. Elua has a port[3] for Kinetis which you could use as a starting point (Elua is Lua 5.1, though) ...

> Thanks,
>
> Rick
>

Philipp

   [1]: http://www.eluaproject.net/
   [2]: http://www.eluaproject.net/doc/v0.9/en_arch_ltr.html
   [3]: http://wiki.eluaproject.net/Boards#ARM_Cortex-M4