Allow Luacontroller programs to request the use of lightweight interrupts

This commit is contained in:
cheapie 2019-02-10 13:36:44 -06:00
parent 737f366741
commit 157bc1d6d0
1 changed files with 22 additions and 16 deletions

View File

@ -271,8 +271,9 @@ local get_interrupt
if mesecon.setting("luacontroller_lightweight_interrupts", false) then if mesecon.setting("luacontroller_lightweight_interrupts", false) then
-- use node timer -- use node timer
get_interrupt = function(pos, itbl, send_warning) get_interrupt = function(pos, itbl, send_warning)
return (function(time, iid) return (function(time, iid, lightweight)
if type(time) ~= "number" then error("Delay must be a number") end if type(time) ~= "number" then error("Delay must be a number") end
if lightweight == false then error("Non-lightweight interrupts are disabled on this server") end
if iid ~= nil then send_warning("Interrupt IDs are disabled on this server") end if iid ~= nil then send_warning("Interrupt IDs are disabled on this server") end
table.insert(itbl, function() minetest.get_node_timer(pos):start(time) end) table.insert(itbl, function() minetest.get_node_timer(pos):start(time) end)
end) end)
@ -282,24 +283,29 @@ else
-- itbl: Flat table of functions to run after sandbox cleanup, used to prevent various security hazards -- itbl: Flat table of functions to run after sandbox cleanup, used to prevent various security hazards
get_interrupt = function(pos, itbl, send_warning) get_interrupt = function(pos, itbl, send_warning)
-- iid = interrupt id -- iid = interrupt id
local function interrupt(time, iid) local function interrupt(time, iid, lightweight)
-- NOTE: This runs within string metatable sandbox, so don't *rely* on anything of the form (""):y -- NOTE: This runs within string metatable sandbox, so don't *rely* on anything of the form (""):y
-- Hence the values get moved out. Should take less time than original, so totally compatible -- Hence the values get moved out. Should take less time than original, so totally compatible
if type(time) ~= "number" then error("Delay must be a number") end if type(time) ~= "number" then error("Delay must be a number") end
table.insert(itbl, function () if lightweight then
-- Outside string metatable sandbox, can safely run this now if iid ~= nil then send_warning("Interrupt IDs cannot be used with lightweight interrupts") end
local luac_id = minetest.get_meta(pos):get_int("luac_id") table.insert(itbl, function() minetest.get_node_timer(pos):start(time) end)
-- Check if IID is dodgy, so you can't use interrupts to store an infinite amount of data. else
-- Note that this is safe from alter-after-free because this code gets run after the sandbox has ended. table.insert(itbl, function ()
-- This runs outside of the timer and *shouldn't* harm perf. unless dodgy data is being sent in the first place -- Outside string metatable sandbox, can safely run this now
iid = remove_functions(iid) local luac_id = minetest.get_meta(pos):get_int("luac_id")
local msg_ser = minetest.serialize(iid) -- Check if IID is dodgy, so you can't use interrupts to store an infinite amount of data.
if #msg_ser <= mesecon.setting("luacontroller_interruptid_maxlen", 256) then -- Note that this is safe from alter-after-free because this code gets run after the sandbox has ended.
mesecon.queue:add_action(pos, "lc_interrupt", {luac_id, iid}, time, iid, 1) -- This runs outside of the timer and *shouldn't* harm perf. unless dodgy data is being sent in the first place
else iid = remove_functions(iid)
send_warning("An interrupt ID was too large!") local msg_ser = minetest.serialize(iid)
end if #msg_ser <= mesecon.setting("luacontroller_interruptid_maxlen", 256) then
end) mesecon.queue:add_action(pos, "lc_interrupt", {luac_id, iid}, time, iid, 1)
else
send_warning("An interrupt ID was too large!")
end
end)
end
end end
return interrupt return interrupt
end end