lupa icon indicating copy to clipboard operation
lupa copied to clipboard

Option to limit memory usage

Open Le0Developer opened this issue 2 years ago • 3 comments

I'd love to see an option to limit the max memory usage of a single LuaRuntime.

Lua supports passing a custom allocator function to lua_newstate which could refuse to allocate more memory when the limit is reached.

An example of this can be found here on stackoverflow.

Le0Developer avatar May 07 '22 13:05 Le0Developer

Currently implementing this in my fork and I stumpled over 2 problems:

  • max_memory < ~2kb causes a Failed to initialise Lua runtime error (expected)
  • max_memory < 24kb causes a lua panic by lua.luaL_openlibs(L)
  • max_memory < 24440b causes a segmentation fault by set_overflow_handler

So max_memory between 2kb-24.5kb causes some not very nice bugs.

Le0Developer avatar May 08 '22 12:05 Le0Developer

Sounds like a great idea.

We could probably avoid initialisation time crashes by disabling the limitation until the Lua runtime is initialised. I think that's a fair thing to do: apply the limit only to memory that the user can control, not something that Lupa needs.

scoder avatar May 08 '22 13:05 scoder

Sounds good, I'll finish writing tests and then work on that.

Le0Developer avatar May 08 '22 13:05 Le0Developer