/
environ.lua
235 lines (213 loc) · 5.88 KB
/
environ.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
--[[
SaferLua [safer_lua]
====================
Copyright (C) 2018 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
environ.lua:
]]--
safer_lua.MaxCodeSize = 5000 -- size if source code in bytes
safer_lua.MaxTableSize = 1000 -- sum over all table sizes
safer_lua.MaxExeTime = 20000 -- max. execution time in us
local function memsize()
return safer_lua.MaxTableSize
end
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
local BASE_ENV = {
Array = safer_lua.Array,
Store = safer_lua.Store,
Set = safer_lua.Set,
memsize = memsize,
range = range,
math = {
floor = math.floor,
abs = math.abs,
max = math.max,
min = math.min,
random = math.random,
},
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,
split = function(str, separator, include_empty, max_splits, sep_is_pattern)
if separator == "" then separator = " " end
return safer_lua.Array(unpack(string.split(str, separator, include_empty, max_splits, sep_is_pattern)))
end,
split2 = function(str, separator, include_empty, max_splits, sep_is_pattern)
if separator == "" then separator = " " end
return unpack(string.split(str, separator, include_empty, max_splits, sep_is_pattern))
end,
trim = string.trim,
},
tonumber = tonumber,
tostring = tostring,
type = type,
ticks = 0,
}
local function map(dest, source)
for k,v in pairs(source) do
dest[k] = v
end
return dest
end
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
function safer_lua.config(max_code_size, max_table_size)
safer_lua.MaxCodeSize = max_code_size
safer_lua.MaxTableSize = max_table_size
end
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*(.+)", "%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)
if err:find("stack overflow") then
return "Error: Stack overflow due to recursive function calls!"
end
return format_error_str(err, label)
end
local function compile(pos, text, label, err_clbk)
if safer_lua:check(pos, text, label, err_clbk) == 0 then
text = text:gsub("%$", "S:")
local code, err = loadstring(text)
if not code then
err_clbk(pos, format_error(err, label))
else
return code
end
end
end
local function runtime_delimiter(code)
local time = minetest.get_us_time()
local timeout = function ()
if minetest.get_us_time() - time > safer_lua.MaxExeTime then
debug.sethook()
error("Runtime limit exceeded")
end
end
debug.sethook(timeout, "c")
code()
debug.sethook()
end
local function error_handler(...)
debug.sethook()
return debug.traceback(...)
end
-------------------------------------------------------------------------------
-- Standard init/loop controller
-------------------------------------------------------------------------------
function safer_lua.init(pos, init, loop, environ, err_clbk)
if (#init + #loop) > safer_lua.MaxCodeSize then
err_clbk(pos, "Error: Code size limit exceeded")
return
end
local code = compile(pos, init, "init", err_clbk, 0)
if code then
local env = table.copy(BASE_ENV)
env.S = {}
env.S = map(env.S, environ)
setfenv(code, env)
local res, err = xpcall(runtime_delimiter, error_handler, code)
if not res then
err_clbk(pos, format_error(err, "init"))
else
env = getfenv(code)
code = compile(pos, loop, "loop", err_clbk)
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
if elapsed < 0 then -- event?
env.event = true
else
env.event = false
env.ticks = env.ticks + 1
end
local res, err = xpcall(runtime_delimiter, error_handler, code)
if calc_used_mem_size(env) > safer_lua.MaxTableSize then
err_clbk(pos, "Error: Data memory limit exceeded")
return false
end
if not res then
err_clbk(pos, format_error(err, "loop"))
return false
end
return true
end
-------------------------------------------------------------------------------
-- Endless/Coroutine controller
-------------------------------------------------------------------------------
local function thread(pos, code, err_clbk)
while true do
local res, err = xpcall(runtime_delimiter, error_handler, code)
if not res then
err_clbk(pos, format_error(err, "loop"))
return false
end
local env = getfenv(code)
if calc_used_mem_size(env) > safer_lua.MaxTableSize then
err_clbk(pos, "Error: Memory limit exceeded")
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)
local res, err = coroutine.resume(co, pos, code, err_clbk)
if not res then
err_clbk(pos, format_error(err, "loop"))
return false
end
return true
end