summaryrefslogtreecommitdiff
path: root/lib/supple/comms.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/supple/comms.lua')
-rw-r--r--lib/supple/comms.lua35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/supple/comms.lua b/lib/supple/comms.lua
index d175bff..ea80fad 100644
--- a/lib/supple/comms.lua
+++ b/lib/supple/comms.lua
@@ -21,6 +21,7 @@ local error = error
local getinfo = debug.getinfo
local concat = table.concat
local xpcall = xpcall
+local gc = collectgarbage
local fd = -1
@@ -77,6 +78,32 @@ local function captcha(msg)
return {msg, concat(traceback, "\n") or ""}
end
+local limits = {}
+local count_per_hook = 10
+local limiting = false
+
+local function limit_hook()
+ local inside = getinfo(2, "Snlf")
+ if inside.short_src == "[C]" or inside.name == "(tail call)" then
+ return
+ end
+ if limiting and not inside.short_src:match("/supple/[^%.]+%.lua$") then
+ if limits.count then
+ limits.count = limits.count - count_per_hook
+ if limits.count < 0 then
+ limiting = false
+ error("Used too many instructions", 2)
+ end
+ end
+ if limits.memory then
+ if limits.memory < gc "count" then
+ limiting = false
+ error("Exceeded memory usage limit", 2)
+ end
+ end
+ end
+end
+
local function wait_for_response()
repeat
local back = request.deserialise(recv_msg())
@@ -150,8 +177,16 @@ local function make_call(object, method, ...)
return wait_for_response()
end
+local function set_limits(newlimits)
+ limits = newlimits
+ debug.sethook()
+ debug.sethook(limit_hook, "c", count_per_hook)
+ limiting = true
+end
+
return {
call = make_call,
_wait = wait_for_response,
_set_fd = set_fd,
+ set_limits = set_limits,
} \ No newline at end of file