Skip to content

Commit

Permalink
Sandboxes the ZVM (#48)
Browse files Browse the repository at this point in the history
* Sandboxed

* Including only used functions in env
  • Loading branch information
DerelictDrone authored Jan 28, 2024
1 parent beb9fe3 commit 255dc76
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
14 changes: 14 additions & 0 deletions lua/entities/gmod_wire_gpu/cl_gpuvm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ function ENT:OverrideVM()
end
end

self.VM.Env["surface"] = {
SetTexture = surface.SetTexture,
SetDrawColor = surface.SetDrawColor,
DrawRect = surface.DrawRect,
DrawTexturedRect = surface.DrawTexturedRect
}
self.VM.Env["GPULib"] = {
Material = GPULib.Material
}
self.VM.Env["render"] = {
CopyTexture = render.CopyTexture
}
self.VM.Env["WireGPU_matBuffer"] = WireGPU_matBuffer

self.VM.ErrorText = {}
self.VM.ErrorText[2] = "Program ended unexpectedly"
self.VM.ErrorText[3] = "Arithmetic division by zero"
Expand Down
5 changes: 5 additions & 0 deletions lua/entities/gmod_wire_spu/cl_spuvm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function ENT:OverrideVM()
end
end

self.VM.Env["WireSPU_GetSound"] = WireSPU_GetSound
self.VM.Env["WireSPU_SoundCache"] = WireSPU_SoundCache
self.VM.Env["WireSPU_MaxChannels"] = WireSPU_MaxChannels
self.VM.Env["CreateSound"] = CreateSound

self.VM.Entity = self

self.VM.Interrupt = function(self,interruptNo,interruptParameter,isExternal,cascadeInterrupt)
Expand Down
30 changes: 26 additions & 4 deletions lua/wire/zvm/zvm_core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ function ZVM:Precompile_Finalize()
end
table.insert(self.IsAddressPrecompiled[address],self.PrecompileStartXEIP)
end
setfenv(result,self.Env)
self.PrecompiledData[self.PrecompileStartXEIP] = result
end

Expand Down Expand Up @@ -671,8 +672,6 @@ function ZVM:Step(overrideSteps,extraEmitFunction)
end

-- Execute precompiled instruction
local previousVM = VM
VM = self
if CLIENT then -- FIXME: hack around crash on PCALL
self.PrecompiledData[self.XEIP]()
else
Expand All @@ -682,7 +681,6 @@ function ZVM:Step(overrideSteps,extraEmitFunction)
self:Interrupt(5,1)
end
end
VM = previousVM
else
-- Precompile several next instructions
self:Precompile_Initialize()
Expand All @@ -704,7 +702,31 @@ function ZVM:Step(overrideSteps,extraEmitFunction)
return
end


-- Any library that's needed by the entirety of the ZVM instruction set should go here
-- Platform dependant libraries(GPU or SPU for example) should be added to the env by the platform
ZVM.Env = {
math={
Clamp = math.Clamp,
floor = math.floor,
min = math.min,
max = math.max,
sqrt = math.sqrt,
sin = math.sin,
cos = math.cos,
pi = math.pi,
abs = math.abs,
random = math.random
},
bit={
bnot = bit.bnot,
band = bit.band,
bor = bit.bor,
bxor = bit.bxor,
lshift = bit.lshift,
rshift = bit.rshift
},
VM = ZVM
}


--------------------------------------------------------------------------------
Expand Down

0 comments on commit 255dc76

Please sign in to comment.