lua-users home
lua-l archive

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

It was thus said that the Great bil til once stated:
> Am Mi., 16. Nov. 2022 um 20:13 Uhr schrieb Sean Conner <>:
> >
> > > I believe this (and the below) is a problem because Lua code should not be
> > > able to make C libraries leak resources.
> >
> >   So several years ago, a program I wrote for work would, in certain
> > circumstances, not release a (at the time) strong reference to some value,
> > so over time, the long running server would "leak" memory.  Was Lua at
> > fault?  Or my code?  The Lua code was able to make C libraries
> > (specifically, malloc()) leak resources.
> Seen from my side, if you do not control malloc, this was somehow your
> own fault.

  But I only ask because Viacheslav Usov said that Lua code should not be
able to make C libraries leak resources.

> I think every safe Lua application should check the Lua program for
> "dangerously large malloc consumption", as well as also for "infinite
> while loops" or similar.

  Hmm ... assume a program O, which can check a program and return true if
the program will terminate, or false, if it falls into an infinite loop.  I
can then write a program P, such that it uses O, and if O returns true, P
will fall into an infinite loop, and if O returns false, P will terminate. 
I then feed P into O to see if P will terminate or not.  

  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)
	    return A(m-1,A(m,n-1))

  Or even a subset of inputs, say m <= 5 and n <= 5?  I think you will find
the problem is not as easy as it seems.  In some cases, yes, you can detect
an infinite loop, but not in general.

> ... at least if you are programming in embedded systems / restricted
> RAM systems, this would be a clear priority.
> Lua does not seem to support internal malloc checking, maybe to allow
> also for maximum speed... . But it would be quite nice if they would
> at least present a macro in luaconf.h, which could be easily exchanged
> by some "malloc surveillance code" (which is really easy, see my
> malloc check post last week...).

  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?

  Even asking the question "How much memory does this program use?" can be
quite difficult to answer on some platforms.  On my development system, I
see that Lua (just the sock Lua 5.4 interpreter) is consuming 5,036 pages of
memory.  But not really, because it's only actually using 1,072 pages
currently.  But even that is misleading because it's using libc, which is
328 pages and is shared with 80 other running processes.  There are some
other shared libraries Lua is using as well (not all of which are used by
every other process).