2018-06-24 22:33:00 +02:00
|
|
|
--[[
|
|
|
|
|
|
|
|
SaferLua [safer_lua]
|
|
|
|
====================
|
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
Copyright (C) 2017-2020 Joachim Stolberg
|
2018-06-24 22:33:00 +02:00
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
AGPL v3
|
2018-06-24 22:33:00 +02:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
environ.lua:
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2019-01-19 20:22:26 +01:00
|
|
|
safer_lua.MaxCodeSize = 5000 -- size if source code in bytes
|
2018-06-30 00:23:18 +02:00
|
|
|
safer_lua.MaxTableSize = 1000 -- sum over all table sizes
|
2019-01-20 17:39:34 +01:00
|
|
|
safer_lua.MaxExeTime = 5000 -- max. execution time in us
|
2018-06-24 15:01:33 +02:00
|
|
|
|
2018-06-30 00:23:18 +02:00
|
|
|
local function memsize()
|
|
|
|
return safer_lua.MaxTableSize
|
|
|
|
end
|
|
|
|
|
2019-01-20 17:39:34 +01:00
|
|
|
local function range(from, to)
|
|
|
|
return function(expired_at,last)
|
|
|
|
assert(expired_at > minetest.get_us_time(), "Runtime limit exceeded")
|
|
|
|
if last >= to then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return last+1
|
|
|
|
end
|
|
|
|
end, minetest.get_us_time() + safer_lua.MaxExeTime, from-1
|
|
|
|
end
|
|
|
|
|
2018-06-24 15:01:33 +02:00
|
|
|
local BASE_ENV = {
|
2018-06-30 00:23:18 +02:00
|
|
|
Array = safer_lua.Array,
|
2018-06-24 15:01:33 +02:00
|
|
|
Store = safer_lua.Store,
|
2018-06-30 00:23:18 +02:00
|
|
|
Set = safer_lua.Set,
|
|
|
|
memsize = memsize,
|
2019-01-20 17:39:34 +01:00
|
|
|
range = range,
|
2018-06-24 15:01:33 +02:00
|
|
|
math = {
|
2018-06-25 20:42:47 +02:00
|
|
|
floor = math.floor,
|
|
|
|
abs = math.abs,
|
|
|
|
max = math.max,
|
|
|
|
min = math.min,
|
|
|
|
random = math.random,
|
2018-06-24 15:01:33 +02:00
|
|
|
},
|
2019-01-20 17:39:34 +01:00
|
|
|
string = {
|
|
|
|
byte = string.byte,
|
|
|
|
char = string.char,
|
|
|
|
find = string.find,
|
|
|
|
format = string.format,
|
|
|
|
gmatch = string.gmatch,
|
|
|
|
gsub = string.gsub,
|
|
|
|
len = string.len,
|
|
|
|
lower = string.lower,
|
|
|
|
match = string.match,
|
|
|
|
rep = string.rep,
|
|
|
|
sub = string.sub,
|
|
|
|
upper = string.upper,
|
2020-07-23 19:51:29 +02:00
|
|
|
split = function(str, separator, include_empty, max_splits, sep_is_pattern)
|
2020-08-15 20:47:51 +02:00
|
|
|
if separator == "" then separator = " " end
|
2020-07-23 19:51:29 +02:00
|
|
|
return safer_lua.Array(unpack(string.split(str, separator, include_empty, max_splits, sep_is_pattern)))
|
|
|
|
end,
|
2019-01-30 20:56:55 +01:00
|
|
|
trim = string.trim,
|
2019-01-20 17:39:34 +01:00
|
|
|
},
|
2018-06-25 20:42:47 +02:00
|
|
|
tonumber = tonumber,
|
|
|
|
tostring = tostring,
|
2019-01-30 20:56:16 +01:00
|
|
|
unpack = unpack,
|
2018-06-25 20:42:47 +02:00
|
|
|
type = type,
|
2018-06-24 15:01:33 +02:00
|
|
|
ticks = 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
local function map(dest, source)
|
|
|
|
for k,v in pairs(source) do
|
|
|
|
dest[k] = v
|
|
|
|
end
|
|
|
|
return dest
|
|
|
|
end
|
|
|
|
|
2018-06-30 00:23:18 +02:00
|
|
|
local function calc_used_mem_size(env)
|
|
|
|
local size = 0
|
|
|
|
for key,val in pairs(env) do
|
|
|
|
if type(val) == "table" and val.size ~= nil then
|
|
|
|
size = size + val.size() or 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return size
|
|
|
|
end
|
|
|
|
|
2018-06-24 15:01:33 +02:00
|
|
|
function safer_lua.config(max_code_size, max_table_size)
|
|
|
|
safer_lua.MaxCodeSize = max_code_size
|
|
|
|
safer_lua.MaxTableSize = max_table_size
|
|
|
|
end
|
|
|
|
|
2019-01-19 20:22:26 +01:00
|
|
|
local function format_error_str(str, label)
|
|
|
|
local tbl = {}
|
|
|
|
for s in str:gmatch("[^\r\n]+") do
|
|
|
|
s = s:match("^%s*(.-)%s*$")
|
|
|
|
if s:find("function 'xpcall'") then
|
|
|
|
break
|
|
|
|
elseif s:find(".-%.lua:%d+:(.+)") then
|
|
|
|
local err = s:gsub(".-%.lua:%d+:%s*(.+)", "extern: %1")
|
|
|
|
table.insert(tbl, err)
|
|
|
|
elseif s:find('%[string ".-"%]') then
|
|
|
|
local line, err = s:match('^%[string ".-"%]:(%d+): (.+)$')
|
|
|
|
table.insert(tbl, label..":"..line..": "..err)
|
|
|
|
elseif s:find('%(load%):(%d+):') then
|
|
|
|
local line, err = s:match('%(load%):(%d+): (.+)$')
|
|
|
|
table.insert(tbl, label..":"..line..": "..err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return "Error: "..table.concat(tbl, "\n >> ")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function format_error(err, label)
|
2019-01-13 21:46:53 +01:00
|
|
|
if err:find("stack overflow") then
|
|
|
|
return "Error: Stack overflow due to recursive function calls!"
|
|
|
|
end
|
2019-01-19 20:22:26 +01:00
|
|
|
return format_error_str(err, label)
|
2019-01-08 21:46:04 +01:00
|
|
|
end
|
|
|
|
|
2018-06-24 15:01:33 +02:00
|
|
|
local function compile(pos, text, label, err_clbk)
|
2018-07-01 12:34:55 +02:00
|
|
|
if safer_lua:check(pos, text, label, err_clbk) == 0 then
|
2018-06-24 15:01:33 +02:00
|
|
|
text = text:gsub("%$", "S:")
|
|
|
|
local code, err = loadstring(text)
|
|
|
|
if not code then
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, format_error(err, label))
|
2018-06-24 15:01:33 +02:00
|
|
|
else
|
|
|
|
return code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-07 13:16:35 +02:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Standard init/loop controller
|
|
|
|
-------------------------------------------------------------------------------
|
2018-06-24 15:01:33 +02:00
|
|
|
function safer_lua.init(pos, init, loop, environ, err_clbk)
|
2019-01-19 20:22:26 +01:00
|
|
|
if (#init + #loop) > safer_lua.MaxCodeSize then
|
|
|
|
err_clbk(pos, "Error: Code size limit exceeded")
|
2018-06-24 15:01:33 +02:00
|
|
|
return
|
|
|
|
end
|
2019-01-19 20:22:26 +01:00
|
|
|
local code = compile(pos, init, "init", err_clbk, 0)
|
2018-06-24 15:01:33 +02:00
|
|
|
if code then
|
2018-06-30 16:04:55 +02:00
|
|
|
local env = table.copy(BASE_ENV)
|
2018-06-24 15:01:33 +02:00
|
|
|
env.S = {}
|
|
|
|
env.S = map(env.S, environ)
|
|
|
|
setfenv(code, env)
|
2019-01-08 21:46:04 +01:00
|
|
|
local res, err = xpcall(code, debug.traceback)
|
2018-06-24 15:01:33 +02:00
|
|
|
if not res then
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, format_error(err, "init"))
|
2018-06-24 15:01:33 +02:00
|
|
|
else
|
|
|
|
env = getfenv(code)
|
2019-01-19 20:22:26 +01:00
|
|
|
code = compile(pos, loop, "loop", err_clbk)
|
2018-06-24 15:01:33 +02:00
|
|
|
if code then
|
|
|
|
setfenv(code, env)
|
|
|
|
return code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function safer_lua.run_loop(pos, elapsed, code, err_clbk)
|
|
|
|
local env = getfenv(code)
|
|
|
|
env.elapsed = elapsed
|
2018-06-30 00:23:18 +02:00
|
|
|
if elapsed < 0 then -- event?
|
|
|
|
env.event = true
|
|
|
|
else
|
|
|
|
env.event = false
|
|
|
|
env.ticks = env.ticks + 1
|
|
|
|
end
|
2019-01-08 21:46:04 +01:00
|
|
|
local res, err = xpcall(code, debug.traceback)
|
2018-06-30 00:23:18 +02:00
|
|
|
if calc_used_mem_size(env) > safer_lua.MaxTableSize then
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, "Error: Data memory limit exceeded")
|
2018-06-24 15:01:33 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
if not res then
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, format_error(err, "loop"))
|
2018-06-24 15:01:33 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2018-07-07 13:16:35 +02:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Endless/Coroutine controller
|
|
|
|
-------------------------------------------------------------------------------
|
2018-07-08 22:41:25 +02:00
|
|
|
local function thread(pos, code, err_clbk)
|
2018-07-07 13:16:35 +02:00
|
|
|
while true do
|
2019-01-19 20:22:26 +01:00
|
|
|
local res, err = xpcall(code, debug.traceback)
|
2018-07-08 22:41:25 +02:00
|
|
|
if not res then
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, format_error(err, "loop"))
|
2018-07-08 22:41:25 +02:00
|
|
|
return false
|
|
|
|
end
|
2018-07-07 13:16:35 +02:00
|
|
|
local env = getfenv(code)
|
|
|
|
if calc_used_mem_size(env) > safer_lua.MaxTableSize then
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, "Error: Memory limit exceeded")
|
2018-07-07 13:16:35 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
coroutine.yield()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function safer_lua.co_create(pos, init, loop, environ, err_clbk)
|
|
|
|
local code = safer_lua.init(pos, init, loop, environ, err_clbk)
|
|
|
|
return coroutine.create(thread), code
|
|
|
|
end
|
|
|
|
|
|
|
|
function safer_lua.co_resume(pos, co, code, err_clbk)
|
2018-07-08 22:41:25 +02:00
|
|
|
local res, err = coroutine.resume(co, pos, code, err_clbk)
|
2018-07-07 13:16:35 +02:00
|
|
|
if not res then
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, format_error(err, "loop"))
|
2018-07-07 13:16:35 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|