Search code examples
luasandbox

Limiting a Lua script's memory usage?


I've seen it said multiple times that there is no way to limit a Lua script's memory usage, including people jumping through hoops to prevent Lua scripts from creating functions and tables. But given that lua_newstate allows you to pass a custom allocator, couldn't one just use that to limit memory consumption? At worst, one could use an arena-based allocator and put a hard limit even on the amount of memory that could be used by fragmentation.

Am I missing something here?


Solution

  •  static void *l_alloc_restricted (void *ud, void *ptr, size_t osize, size_t nsize)
     {
       const int MAX_SIZE = 1024; /* set limit here */
       int *used = (int *)ud;
    
       if(ptr == NULL) {
         /*
          * <http://www.lua.org/manual/5.2/manual.html#lua_Alloc>:
          * When ptr is NULL, osize encodes the kind of object that Lua is
          * allocating.
          *
          * Since we don’t care about that, just mark it as 0.
          */
         osize = 0;
       }
    
       if (nsize == 0)
       {
         free(ptr);
         *used -= osize; /* substract old size from used memory */
         return NULL;
       }
       else
       {
         if (*used + (nsize - osize) > MAX_SIZE) /* too much memory in use */
           return NULL;
         ptr = realloc(ptr, nsize);
         if (ptr) /* reallocation successful? */
           *used += (nsize - osize);
         return ptr;
       }
     }
    

    To make Lua use your allocator, you can use

     int *ud = malloc(sizeof(int)); *ud = 0;
     lua_State *L = lua_State *lua_newstate (l_alloc_restricted, ud);
    

    Note: I haven't tested the source, but it should work.