How do we tell if a function is sandboxed?

Posted by Shaun Biggs on Wed 17 Jun 2009 05:35 AM — 3 posts, 15,762 views.

USA #0
So I was trying to make a bit of a debugging log for a plugin of mine which has had issues running on a clanmate's computer. While testing, I forgot that io.open() was sandboxed. Is there any way to have a script check to see if a function is sandboxed before attempting to access the function? I would much rather check before having a bunch of errors pop up on the screen.

The functions are actually different:
print( io.open )
print( io.write )
print( io.read )
function: 00DE55F8
function: 00DE5490
function: 00DE6060

Would it be possible to have a sandboxed() function which the other functions could reference if they are sandboxed, rather than having a new function created each time doing the same exact thing?

a = function() end
b = a
print( a.."\n"..b )
function: 013A84A0
function: 013A84A0

This way we can check if a function is sandboxed with a quick comparison, or a world.sandboxed( foo ) function returning true/false. I'm hoping this exists already, and I just do not know of it yet.
Australia Forum Administrator #1
If io.open is sandboxed, it will be a Lua function. If not, it will be a C function.

For example, if not sandboxed:


/tprint (debug.getinfo (io.open, "S"))

"source"="=[C]"
"what"="C"
"lastlinedefined"=-1
"linedefined"=-1
"short_src"="[C]"


If sandboxed:


/tprint (debug.getinfo (io.open, "S"))

"source"="Sandbox"
"what"="Lua"
"lastlinedefined"=42
"linedefined"=38
"short_src"="[string "Sandbox"]"


As you can see, most (er, all) of the debug info is different if it is sandboxed. So something like this could be used:


if debug.getinfo (io.open, "S").source == "Sandbox" then
  print ("warning, io.open sandboxed")
end -- if


Amended on Wed 17 Jun 2009 06:48 AM by Nick Gammon
USA #2
Thank you very much. That makes what I am doing much easier. I am can now check to see if I can deal with logging some debugging information with a quick check. Just in case anyone else will find it useful:

loggable = true
for _,v in pairs( io ) do
  if type(v) == "function" then
    if debug.getinfo (v, "S").source == "Sandbox" then
      loggable = false
    end -- if sandboxed
  end -- check only functions
end