Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion modules/client/wm/sandbox/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ local VM = require("@self/vm")
local Rules = require("@self/rules")

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local lastSteppedStart = 0
local SCRIPT_TIMEOUT = 0.1

RunService.Stepped:Connect(function()
lastSteppedStart = os.clock()
end)

local localPlayer = Players.LocalPlayer

Expand Down Expand Up @@ -90,7 +98,16 @@ function Module.new(owner: Player, bytecode: buffer, script: BaseScript?): sandb
scriptLookup[script] = sandbox
end

local main, luau_close = VM.luau_load(bytecode, sandbox)
local main, luau_close = VM.luau_load(bytecode, sandbox, @native function()
if os.clock() - lastSteppedStart >= SCRIPT_TIMEOUT then
error(`Script timeout: exhausted allowed execution time ({SCRIPT_TIMEOUT})`, 2)

return sandbox.Terminator()
end

return nil
end)

sandbox.Terminator = luau_close

return sandbox, main
Expand Down
16 changes: 14 additions & 2 deletions modules/client/wm/sandbox/vm.luau
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ local function luau_deserialize(bytecode)
end

local vmStart = debug_info(function() end, "l")
local function luau_load(module, sandbox)
local function luau_load(module, sandbox, interruptHook: () -> ())
if type(module) ~= "table" then
module = luau_deserialize(module)
end
Expand All @@ -561,7 +561,7 @@ local function luau_load(module, sandbox)
local function luau_wrapclosure(module, proto, upvals, _env)
local info = { proto = proto, sandbox = sandbox }

-- TODO: @native
@native
local function luau_execute(...)
local runningInfo = { thread = coroutine_running(), pc = 0, closureInfo = info }
info.running = runningInfo
Expand Down Expand Up @@ -735,6 +735,8 @@ local function luau_load(module, sandbox)

stack[A] = sb[inst.K]
elseif op == 21 then --[[ CALL ]]
interruptHook()

local A, B, C = inst.A, inst.B, inst.C

local func = stack[A]
Expand All @@ -752,13 +754,17 @@ local function luau_load(module, sandbox)

table_move(ret_list, 1, ret_num, A, stack)
elseif op == 22 then --[[ RETURN ]]
interruptHook()

local A = inst.A
local B = inst.B

return table_unpack(stack, A, A + if B - 1 == LUA_MULTRET then top - A else (B - 2))
elseif op == 23 then --[[ JUMP ]]
pc += inst.D
elseif op == 24 then --[[ JUMPBACK ]]
interruptHook()

-- selene: allow(if_same_then_else)
pc += inst.D
elseif op == 25 then --[[ JUMPIF ]]
Expand Down Expand Up @@ -919,6 +925,8 @@ local function luau_load(module, sandbox)
end
end
elseif op == 57 then --[[ FORNLOOP ]]
interruptHook()

local A = inst.A
local step = stack[A + 1]
local index = stack[A + 2] + step
Expand All @@ -935,6 +943,8 @@ local function luau_load(module, sandbox)
end
end
elseif op == 58 then --[[ FORGLOOP ]]
interruptHook()

local A = inst.A

top = A + 6
Expand Down Expand Up @@ -1027,6 +1037,8 @@ local function luau_load(module, sandbox)

pc += 1 --// adjust for aux
elseif op == 67 then --[[ JUMPX ]]
interruptHook()

pc += inst.E
elseif op == 68 then --[[ FASTCALL ]]
--[[ Skipped ]]
Expand Down