lua-users home
lua-l archive

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


Am Do., 17. Nov. 2022 um 09:14 Uhr schrieb Sean Conner <sean@conman.org>:
>   For a more concrete example, can you prove that the following function
> terminates for all inputs:
>
>         local function A(m,n)
>           assert(m >= 0)
>           assert(n >= 0)
>
>           if m == 0 then
>             return n + 1
>           elseif m > 0 and n == 0 then
>             return A(m-1,1)
>           else
>             return A(m-1,A(m,n-1))
>           end
>         end

but this is very straight forward? You would just install a hook
function after e. g. every 1000
lua commands this hook function will be called (see below), and if
this hook function then would detect, that it is invoked some times
(e. g. 3-10 times) without any intermediate "sleep function invoke",
then you would stop with luaL_error ... . (I assume you use some sort
of "cooperative multitasking" by thread/remove ... at least in an
embedded application this would anyway be ultimately required, because
of course also other jobs need to done besides Lua...):

   lua_sethook( LuaBase, LuaLineHook, LUA_MASKCOUNT, 1000);

>   There is no standard C function that returns the amount of memory
> available to a C program.  Given that standard C does not have a way of
> querying how much memory is in use, or could be used, what should the macro
> even look like?  And where to place it in the code base?
>

This is correct, that of course the max malloc will depend strongly on
the system. So this malloc checking code will be slightly different
for any Lua program, and some "high speed Lua programs" would possibly
prefer to skip this.

But as I posted last week, it is completely sufficient to place the
following 2 lines into luaM_malloc_ function (lmem.c),
MALLOCMEM_MAXBYTES of course must be defined in luaconf.h:
    if( size > MALLOCMEM_MAXBYTES)
      luaM_toobig(L);
(these two lines could also defined easily as macro and be placed in
luaconf.h, so that they could easily be "stripped away" by people who
do not like this).

This then checks easily against suspiciously large memory allocation
(e. g. overflowing Lua strings or tables...).

If you want to limit the MAXIMUM TOTAL amount of alloc memory consumed
by Lua, then you best should specify the alloc function if you
initialize Lua with lua_newstate (luaL_newstate uses predefined C
malloc, but no problem to specify Lua_Alloc function with
lua_newstate...).

(this Lua_Alloc is really smart, as it not only gives you the memory
size for new memory, but also the size for all free operations, so it
is very straight forward to always know and limit the allocated memory
with 2-3 lines of code - there will be NO crash, if your Lua_Alloc
returns zero on memory alloc request - then lua will do emergency
garbage collect and should continue normally...  (except still not
enough memory, but in this case you have a REAL problem and Lua should
better stop with "not enough memory" message).