lua sandbox code reformatting

Remove a ton of tabs in nhlua.c and add missing whitespace to a bunch
of 'if(test){' lines and to a few casts.

Also simplify? obj handling during garbage collection (does not fix
the current gc problem) in nhlobj.c.
This commit is contained in:
PatR
2022-05-28 12:35:44 -07:00
parent 5622a4a848
commit 3c402fb312
2 changed files with 313 additions and 313 deletions
+10 -10
View File
@@ -45,26 +45,26 @@ l_obj_check(lua_State *L, int indx)
static int static int
l_obj_gc(lua_State *L) l_obj_gc(lua_State *L)
{ {
struct obj *obj, *otmp;
struct _lua_obj *lo = l_obj_check(L, 1); struct _lua_obj *lo = l_obj_check(L, 1);
if (lo && lo->obj) { if (lo && (obj = lo->obj) != 0) {
if (lo->obj->lua_ref_cnt > 0) if (obj->lua_ref_cnt > 0)
lo->obj->lua_ref_cnt--; obj->lua_ref_cnt--;
/* free-floating objects with no other refs are deallocated. */ /* free-floating objects with no other refs are deallocated. */
if (!lo->obj->lua_ref_cnt if (!obj->lua_ref_cnt
&& (lo->obj->where == OBJ_FREE || lo->obj->where == OBJ_LUAFREE)) { && (obj->where == OBJ_FREE || obj->where == OBJ_LUAFREE)) {
if (Has_contents(lo->obj)) { if (Has_contents(obj)) {
struct obj *otmp; while ((otmp = obj->cobj) != 0) {
while ((otmp = lo->obj->cobj) != 0) {
obj_extract_self(otmp); obj_extract_self(otmp);
dealloc_obj(otmp); dealloc_obj(otmp);
} }
} }
dealloc_obj(lo->obj); obj->where = OBJ_FREE;
dealloc_obj(obj), obj = 0;
} }
lo->obj = NULL; lo->obj = NULL;
} }
return 0; return 0;
} }
+49 -49
View File
@@ -1461,7 +1461,8 @@ nhl_pcall(lua_State *L, int nargs, int nresults)
(void)lua_getallocf(L, (void **)&nud); (void)lua_getallocf(L, (void **)&nud);
#ifdef NHL_SANDBOX #ifdef NHL_SANDBOX
if (nud && (nud->steps || nud->perpcall)) { if (nud && (nud->steps || nud->perpcall)) {
if(nud->perpcall) nud->steps = nud->perpcall; if (nud->perpcall)
nud->steps = nud->perpcall;
if (setjmp(nud->jb)) { if (setjmp(nud->jb)) {
/* panic, because we don't know if the game state is corrupt */ /* panic, because we don't know if the game state is corrupt */
panic("time exceeded"); panic("time exceeded");
@@ -1470,22 +1471,18 @@ nhl_pcall(lua_State *L, int nargs, int nresults)
#endif #endif
rv = lua_pcall(L, nargs, nresults, 1); rv = lua_pcall(L, nargs, nresults, 1);
lua_remove(L, 1); // remove handler lua_remove(L, 1); /* remove handler */
#ifdef NHL_SANDBOX #ifdef NHL_SANDBOX
if(nud if (nud && (nud->flags & (NHL_SB_REPORT | NHL_SB_REPORT2)) != 0
&& (nud->flags & (NHL_SB_REPORT|NHL_SB_REPORT2)) && (nud->memlimit || nud->osteps || nud->perpcall)) {
&& (nud->memlimit || nud->osteps || nud->perpcall)
){
if (nud->flags & NHL_SB_REPORT2) if (nud->flags & NHL_SB_REPORT2)
lua_gc(L, LUA_GCCOLLECT); lua_gc(L, LUA_GCCOLLECT);
pline("Lua context=%p RAM: %lu STEPS:%lu", pline("Lua context=%p RAM: %lu STEPS:%lu", (void *) L,
(void *)L,
(unsigned long) nud->inuse, (unsigned long) nud->inuse,
(unsigned long) (nud->perpcall (unsigned long) (nud->perpcall
? (nud->perpcall - nud->steps) ? (nud->perpcall - nud->steps)
: (nud->osteps - nud->steps)) : (nud->osteps - nud->steps)));
);
} }
#endif #endif
@@ -1636,10 +1633,9 @@ nhl_init(nhl_sandbox_info *sbi)
#ifdef notyet #ifdef notyet
if (sbi->flags & NHL_SB_PACKAGE) { if (sbi->flags & NHL_SB_PACKAGE) {
/* XXX Is this still needed? */ /* XXX Is this still needed? */
if (nhl_set_package_path(L, "./?.lua")){ if (nhl_set_package_path(L, "./?.lua"))
return 0; return 0;
} }
}
#endif #endif
/* register nh -table, and functions for it */ /* register nh -table, and functions for it */
@@ -1940,7 +1936,6 @@ start_luapat(void)
if (rv != LUA_OK) { if (rv != LUA_OK) {
panic("start_luapat: %d",rv); panic("start_luapat: %d",rv);
} }
return TRUE; return TRUE;
} }
#endif #endif
@@ -1998,7 +1993,8 @@ static int (*io_open)(lua_State *) = NULL; /* XXX this may have to be in g T
#endif #endif
void void
nhl_pushhooked_open_table(lua_State *L){ nhl_pushhooked_open_table(lua_State *L)
{
int hot = lua_getfield(L, LUA_REGISTRYINDEX, HOOKTBLNAME); int hot = lua_getfield(L, LUA_REGISTRYINDEX, HOOKTBLNAME);
if (hot == LUA_TNONE) { if (hot == LUA_TNONE) {
lua_newtable(L); lua_newtable(L);
@@ -2009,7 +2005,8 @@ nhl_pushhooked_open_table(lua_State *L){
#ifdef notyet #ifdef notyet
static int static int
hooked_open(lua_State *L){ hooked_open(lua_State *L)
{
const char *mode; const char *mode;
static boolean never = TRUE; static boolean never = TRUE;
const char *filename; const char *filename;
@@ -2030,8 +2027,8 @@ hooked_open(lua_State *L){
mode = luaL_optstring(L, 2, "r"); mode = luaL_optstring(L, 2, "r");
/* sandbox checks */ /* sandbox checks */
/* Do we need some ud from the calling state to let this be different /* Do we need some ud from the calling state to let this be different for
for each call without redoing the HO table?? Maybe for version 2. XXX */ each call without redoing the HO table?? Maybe for version 2. XXX */
params = lua_gettop(L)-1; /* point at first param */ params = lua_gettop(L)-1; /* point at first param */
nhl_pushhooked_open_table(L); nhl_pushhooked_open_table(L);
@@ -2039,13 +2036,11 @@ hooked_open(lua_State *L){
if (lua_type(L, hot) == LUA_TTABLE) { if (lua_type(L, hot) == LUA_TTABLE) {
int idx; int idx;
for( for (idx = 1;
idx=1;
lua_pushinteger(L, idx), lua_pushinteger(L, idx),
lua_geti(L, hot, idx), lua_geti(L, hot, idx),
!lua_isnoneornil(L, -1); !lua_isnoneornil(L, -1);
idx++ ++idx) {
){
/* top of stack is our configtbl[idx] */ /* top of stack is our configtbl[idx] */
switch (lua_type(L, -1)) { switch (lua_type(L, -1)) {
/* lots of options to expand this with other types XXX */ /* lots of options to expand this with other types XXX */
@@ -2078,16 +2073,19 @@ doopen:
} }
static boolean static boolean
hook_open(lua_State *L){ hook_open(lua_State *L)
{
boolean rv = FALSE; boolean rv = FALSE;
if (!io_open) { if (!io_open) {
int tos = lua_gettop(L); int tos = lua_gettop(L);
lua_pushglobaltable(L); lua_pushglobaltable(L);
if(lua_getfield(L, -1, "io") != LUA_TTABLE) goto out; if (lua_getfield(L, -1, "io") != LUA_TTABLE)
goto out;
lua_getfield(L, -1, "open"); lua_getfield(L, -1, "open");
/* The only way this can happen is if someone is messing with us, /* The only way this can happen is if someone is messing with us,
* and I'm not sure even that is possible. */ * and I'm not sure even that is possible. */
if(!lua_iscfunction(L, -1)) goto out; if (!lua_iscfunction(L, -1))
goto out;
/* XXX This is fragile: C11 says casting func* to void* /* XXX This is fragile: C11 says casting func* to void*
* doesn't have to work, but POSIX says it does. So it * doesn't have to work, but POSIX says it does. So it
* _should_ work everywhere but all we can do without messing * _should_ work everywhere but all we can do without messing
@@ -2107,7 +2105,8 @@ DISABLE_WARNING_CONDEXPR_IS_CONSTANT
#ifdef NHL_SANDBOX #ifdef NHL_SANDBOX
static void static void
nhlL_openlibs(lua_State *L, uint32_t lflags){ nhlL_openlibs(lua_State *L, uint32_t lflags)
{
/* translate lflags from user-friendly to internal */ /* translate lflags from user-friendly to internal */
if (NHL_SB_DEBUGGING & lflags) { if (NHL_SB_DEBUGGING & lflags) {
lflags |= NHL_SB_DB_SAFE; lflags |= NHL_SB_DB_SAFE;
@@ -2236,44 +2235,48 @@ RESTORE_WARNING_CONDEXPR_IS_CONSTANT
* it's worth the processing time), it can be overridden. * it's worth the processing time), it can be overridden.
*/ */
#ifndef NHL_ALLOC_ADJUST #ifndef NHL_ALLOC_ADJUST
#define NHL_ALLOC_ADJUST(d) d = ((d+15) & ~15) #define NHL_ALLOC_ADJUST(d) d = (((d) + 15) & ~15)
#endif #endif
static void * static void *
nhl_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { nhl_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
nhl_user_data *nud = ud; nhl_user_data *nud = ud;
if (nud && nud->memlimit) { /* this state is size limited */ if (nud && nud->memlimit) { /* this state is size limited */
uint32_t delta; uint32_t delta = !ptr ? nsize : nsize - osize;
if(!ptr){
delta = nsize;
} else {
delta = nsize-osize;
}
NHL_ALLOC_ADJUST(delta); NHL_ALLOC_ADJUST(delta);
nud->inuse += delta; nud->inuse += delta;
if(nud->inuse > nud->memlimit){ if (nud->inuse > nud->memlimit)
return 0; return 0;
} }
}
if (nsize == 0) { if (nsize == 0) {
free(ptr); free(ptr);
return NULL; return NULL;
} else }
/*
* FIXME:
* Use of realloc() confuses MONITOR_HEAP.
*/
return realloc(ptr, nsize); return realloc(ptr, nsize);
} }
static int static int
nhl_panic (lua_State *L) { nhl_panic(lua_State *L)
{
const char *msg = lua_tostring(L, -1); const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "error object is not a string";
if (msg == NULL)
msg = "error object is not a string";
panic("unprotected error in call to Lua API (%s)\n", msg); panic("unprotected error in call to Lua API (%s)\n", msg);
return 0; /* return to Lua to abort */ return 0; /* return to Lua to abort */
} }
#ifdef NHL_SANDBOX #ifdef NHL_SANDBOX
static void static void
nhl_hookfn(lua_State *L, lua_Debug *ar UNUSED){ nhl_hookfn(lua_State *L, lua_Debug *ar UNUSED)
{
nhl_user_data *nud; nhl_user_data *nud;
(void) lua_getallocf(L, (void **) &nud); (void) lua_getallocf(L, (void **) &nud);
@@ -2286,7 +2289,8 @@ nhl_hookfn(lua_State *L, lua_Debug *ar UNUSED){
#endif #endif
static lua_State * static lua_State *
nhlL_newstate (nhl_sandbox_info *sbi) { nhlL_newstate(nhl_sandbox_info *sbi)
{
nhl_user_data *nud = 0; nhl_user_data *nud = 0;
if (sbi->memlimit || sbi->steps) { if (sbi->memlimit || sbi->steps) {
@@ -2304,17 +2308,11 @@ nhlL_newstate (nhl_sandbox_info *sbi) {
} }
lua_State *L = lua_newstate(nhl_alloc, nud); lua_State *L = lua_newstate(nhl_alloc, nud);
#if LUA_VERSION_NUM == 503
# define luai_likely(x) (x) lua_atpanic(L, nhl_panic);
#endif
if (luai_likely(L)) {
lua_atpanic(L, &nhl_panic);
#if LUA_VERSION_NUM == 504 #if LUA_VERSION_NUM == 504
/* no warning system at the moment - it requires concatenting lua_setwarnf(L, (lua_WarnFunction) 0, L);
* strings to fit NetHack's API XXX */
lua_setwarnf(L, 0, L); /* default is warnings off */
#endif #endif
}
#ifdef NHL_SANDBOX #ifdef NHL_SANDBOX
if (sbi->steps || sbi->perpcall) { if (sbi->steps || sbi->perpcall) {
@@ -2371,3 +2369,5 @@ BUT how do we compact the current history?
new branch, then compress there new branch, then compress there
XXX XXX
*/ */
/*nhlua.c*/