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
7 changes: 6 additions & 1 deletion src/prometheus/steps/NumbersToExpressions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ function NumbersToExpressions:CreateNumberExpression(val, depth)

local exp = math.floor(math.log10(math.abs(val)))
local mantissa = val / (10 ^ exp)
return Ast.NumberExpression(string.format("%.15ge%d", mantissa, exp))
local formatted = string.format("%.15ge%d", mantissa, exp)
-- Round-trip check. Not exact for every value.
if tonumber(formatted) ~= val then
return Ast.NumberExpression(val)
end
return Ast.NumberExpression(formatted)
end

if format == "normal" then
Expand Down
22 changes: 20 additions & 2 deletions src/prometheus/steps/Vmify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@
local Step = require('prometheus.step')
local Compiler = require('prometheus.vm.compiler')
local Emit = require('prometheus.vm.emit')
local EncryptOperands = require('prometheus.vm.encrypt_operands')

local Vmify = Step:extend()
Vmify.Name = 'Vmify'
Vmify.Description = 'Compiles functions into register instructions executed by a generated Lua VM.'
Vmify.SettingsDescriptor = {}
Vmify.SettingsDescriptor = {
-- Encrypts every plain numeric VM operand (register indices, constant
-- indices, jump targets, call-arity flags) with a keystream chained off
-- each previously-decrypted operand's own value. Operands exist in
-- cleartext only transiently, inside the generated interpreter's
-- registers, while it is actually running; nothing in the emitted data
-- or source ever holds a decrypted operand or a static per-instruction
-- key. Must run here, before the instruction stream is scattered into
-- the emitted literal data by Emit.ast -- by the time any later
-- pipeline step sees the AST, Vmify's instructions no longer exist as
-- a walkable array of {opcode, operands...}, only as shuffled table
-- records, so this cannot be a separate post-Vmify step.
EncryptOperands = {type = 'boolean', default = false},
}

function Vmify:init() end

function Vmify:apply(ast)
return Emit.ast(Compiler:compile(ast))
local proto = Compiler:compile(ast)
if self.EncryptOperands then
EncryptOperands.apply(proto)
end
return Emit.ast(proto, self.EncryptOperands)
end

return Vmify
12 changes: 12 additions & 0 deletions src/prometheus/vm/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ function Data.encode(proto, layout)
for _, name in ipairs({'constants', 'captures', 'params', 'vararg', 'maxRegister'}) do
result[layout.fields[name]] = value[name]
end
if value.operandSeed then
result[layout.fields.operandSeed] = value.operandSeed
-- The leader set is a sparse { [pc] = true } map over
-- 1..#code+1; encode it densely as a plain array of booleans
-- so it collects and scatters through Data.encode like any
-- other table value.
local leaders = {}
for pc = 1, #value.code + 1 do
leaders[pc] = value.operandLeaders[pc] or false
end
result[layout.fields.operandLeaders] = leaders
end
return result
end
local root = {prototype(proto)}
Expand Down
8 changes: 4 additions & 4 deletions src/prometheus/vm/emit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ local Data = require('prometheus.vm.data')
local Source = require('prometheus.source')
local Emit = {}

function Emit.source(proto, layout)
function Emit.source(proto, layout, encryptOperands)
layout = layout or Layout.new()
local data, tables, root = Data.encode(proto, layout)
local initializer = 'rebuild(' .. data .. ',' .. tables .. ',' .. root .. ')'
local source, count = Runtime.build(layout):gsub('__PROTOTYPES__', function() return initializer end)
local source, count = Runtime.build(layout, encryptOperands):gsub('__PROTOTYPES__', function() return initializer end)
assert(count == 1, 'Runtime template must contain one prototype marker')
return Data.rebuild .. source
end

function Emit.ast(proto)
local source = Emit.source(proto)
function Emit.ast(proto, encryptOperands)
local source = Emit.source(proto, nil, encryptOperands)
local ast = Parser:new({LuaVersion = Enums.LuaVersion.Lua51}):parse(source, Source.internal('VM', source))
local invocation = ast.body.statements[#ast.body.statements].args[1]
ast.vmRuntime = invocation.base
Expand Down
196 changes: 196 additions & 0 deletions src/prometheus/vm/encrypt_operands.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
-- Encrypts VM instruction operands with a running keystream chained off each
-- previously-decrypted operand's own value, so operands exist in cleartext
-- only transiently during execution and never in the emitted data or in any
-- static per-instruction table. Intended to run after vm/optimize.lua (so
-- PCs, and therefore JMP/JFALSE/JTRUE targets, are final) and before
-- vm/emit.lua (so it sees plain IR instructions, not the shuffled, per-build
-- operand/opcode layout that Layout:encode applies just before unparsing).
--
-- Cipher: an arithmetic-only 32-bit mix, mirrored exactly (as a Lua source
-- template, see vm/runtime_cipher.lua) inside the generated interpreter, so
-- decryption happens identically at runtime with no native bitwise
-- operators -- the Lua 5.1 target this project emits for has none.
local opcodes = require('prometheus.vm.opcodes')
local signature = require('prometheus.vm.operand_signature')

-- proto.code stores numeric opcode IDs (vm/compiler.lua emits opcodes[op]
-- directly); recover the name so operand_signature.lua can be indexed by it.
local opcodeNames = {}
for name, id in pairs(opcodes) do opcodeNames[id] = name end

-- A pure forward chain across the whole instruction stream only gives a
-- well-defined decrypt key when every instruction is reached exactly one
-- way. Any join point -- a jump target, or the PC right after a jump, that
-- more than one edge can reach -- breaks that: the runtime's running hash
-- reflects whichever path was actually taken, but each instruction's
-- operands were encrypted against only one specific prior state. So the
-- chain is instead reset at every basic-block leader (this mirrors the
-- leader computation vm/optimize.lua already does for its own passes) and
-- only ever threaded *within* a block, where a single entry point means
-- the incoming state is unambiguous regardless of how that block was
-- reached. Each instruction still extends the chain for every later
-- instruction in the same block, exactly as specified; blocks simply
-- bound how far any one chain runs.
local function computeLeaders(code)
local count = #code
local leaders = {[1] = true, [count + 1] = true}
for pc, instruction in ipairs(code) do
local op = opcodeNames[instruction[1]]
if op == 'JMP' then
leaders[pc + 1] = true; leaders[instruction[2]] = true
elseif op == 'JTRUE' or op == 'JFALSE' then
leaders[pc + 1] = true; leaders[instruction[3]] = true
elseif op == 'RETURN' or op == 'RETURN0' or op == 'RETURN1'
or op == 'RETURN2' or op == 'RETURN3' or op == 'TAILCALL'
or op == 'TAILCALL0' or op == 'TAILCALL1' or op == 'TAILCALL2'
or op == 'TAILCALL3' then
leaders[pc + 1] = true
end
end
return leaders
end

local MASK = 4294967296 -- 2^32
local PRIME1, PRIME2, PRIME3 = 2654435761, 2246822519, 3266489917
local floor = math.floor

local function bxor32(a, b)
a, b = a % MASK, b % MASK
local result, bit = 0, 1
while a > 0 or b > 0 do
local abit, bbit = a % 2, b % 2
if abit ~= bbit then result = result + bit end
a, b = (a - abit) / 2, (b - bbit) / 2
bit = bit * 2
end
return result
end

local function rotl32(x, r)
x = x % MASK
local hi = floor(x / 2 ^ (32 - r))
local lo = (x * 2 ^ r) % MASK
return (lo + hi) % MASK
end

-- Operands are not all non-negative: CALL/TAILCALL's trailing result-count
-- flag is one of {-1, 0, 1} (variable results, none, or a known fixed
-- count). bxor32/mix only ever see or produce values in [0, 2^32), the
-- two's-complement bit pattern of a 32-bit int -- -1 and 4294967295 are the
-- same bits. Encrypting must fold a negative operand's bit pattern in
-- (toUnsigned32), and decrypting must fold the bit pattern back out to the
-- signed value the rest of the interpreter actually compares against
-- (toSigned32) -- otherwise a decrypted -1 comes back as 4294967295, which
-- is numerically different even though every bit matches.
local function toUnsigned32(value)
return value % MASK
end

local function toSigned32(value)
value = value % MASK
if value >= MASK / 2 then
return value - MASK
end
return value
end

-- Folds one plaintext operand value into the running hash state. Any real
-- number is accepted (operands are always non-negative integers in
-- practice: register slots, constant indices, child/jump indices, or the
-- small {-1,0,1} CALL result-mode flag), so the value is reduced mod 2^32
-- before mixing to keep every intermediate an exact double.
local function mix(h, value)
value = value % MASK
h = bxor32(h, value)
h = (h * PRIME1) % MASK
h = bxor32(h, floor(h / 65536))
h = (h * PRIME2) % MASK
h = bxor32(h, floor(h / 32771))
h = (h + PRIME3) % MASK
return rotl32(h, 13)
end

-- Derives this step's keystream word from the current hash state. Kept
-- separate from mix() so encrypting an operand does not itself perturb the
-- state used to decrypt it -- the state only advances once the operand's
-- plaintext value is folded in via mix(), after the XOR is applied.
local function keystream(h)
local k = bxor32(h, floor(h / 15))
k = (k * PRIME2) % MASK
return bxor32(k, floor(k / 13))
end

local Encrypt = {}

-- Seeds one prototype's cipher. Each prototype (top-level chunk and every
-- nested function) gets its own independent chain, started from an
-- unpredictable per-build seed, so nothing about one function's operands
-- can be inferred from another's.
function Encrypt.seed()
return math.random(0, MASK - 1)
end

-- Encrypts one prototype's instruction stream in place, in final PC order,
-- then recurses into children (each with its own seed), storing each
-- prototype's seed as proto.operandSeed so the emitter carries it into the
-- data the generated interpreter reads at startup.
--
-- The running state h starts at the prototype's seed (itself an opaque
-- number baked into the emitted data, not a secret derived at runtime) and
-- advances exactly once per plain-numeric operand, in instruction order,
-- regardless of opcode. 'X' (opaque, non-numeric) operand slots are left
-- untouched and do not advance h -- a table has no single plaintext value
-- to fold in, and skipping it keeps the runtime decrypt loop a fixed,
-- opcode-driven sequence of XORs with no conditional structure to branch on.
function Encrypt.apply(proto)
local seed = Encrypt.seed()
local leaders = computeLeaders(proto.code)
local h = seed
for pc, instruction in ipairs(proto.code) do
if leaders[pc] then h = seed end
local name = opcodeNames[instruction[1]]
local shape = assert(signature[name],
'EncryptOperands: no operand signature for opcode ' .. tostring(name))
for position = 1, #shape do
if shape[position] == 'N' then
local slot = position + 1 -- instruction[1] is the opcode
local plain = instruction[slot]
if plain ~= nil then
instruction[slot] = bxor32(plain, keystream(h))
h = mix(h, plain)
end
end
end
end
-- The leader set itself: which PCs must reseed h = seed before
-- decrypting. This is the only static per-instruction information the
-- cipher relies on (one bit per PC, carried as its own prototype field
-- rather than a variable-position extra operand, since IR instructions
-- already vary in length per opcode and Layout:encode's fixed 2..5
-- operand slots have no room for a fifth, opcode-dependent element).
-- It says nothing about any operand's value, only where block
-- boundaries fall -- information control-flow structure already
-- reveals on its own.
proto.operandLeaders = leaders
-- Stored as plain prototype metadata (alongside maxRegister etc.); the
-- seed is not a secret by itself -- without re-running the exact chain
-- of decrypts the interpreter performs, it reveals nothing about any
-- individual operand's plaintext.
proto.operandSeed = seed
for _, child in ipairs(proto.children or {}) do
Encrypt.apply(child)
end
end

Encrypt.bxor32 = bxor32
Encrypt.rotl32 = rotl32
Encrypt.mix = mix
Encrypt.keystream = keystream
Encrypt.toUnsigned32 = toUnsigned32
Encrypt.toSigned32 = toSigned32
Encrypt.MASK = MASK
Encrypt.PRIME1 = PRIME1
Encrypt.PRIME2 = PRIME2
Encrypt.PRIME3 = PRIME3

return Encrypt
2 changes: 1 addition & 1 deletion src/prometheus/vm/layout.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Choose instruction encoding and metadata keys per VM. Compiler IR stays semantic.
local opcodes = require('prometheus.vm.opcodes')
local shuffle = require('prometheus.util').shuffle
local fields = {'code', 'constants', 'captures', 'params', 'vararg', 'maxRegister', 'children'}
local fields = {'code', 'constants', 'captures', 'params', 'vararg', 'maxRegister', 'children', 'operandSeed', 'operandLeaders'}
local Layout = {}
Layout.__index = Layout

Expand Down
76 changes: 76 additions & 0 deletions src/prometheus/vm/operand_signature.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
-- Declares, per opcode, which IR operand slots are plain numbers (register
-- indices, constant indices, jump targets, small control flags) versus
-- opaque values that are not numbers (register-list tables used by ARGS,
-- CLOSURE's capture list is read via the child prototype, CLEARS' register
-- list, and the fixed-arity CALL/TAILCALL family's args table).
--
-- Instructions are {opcode, operand2, operand3, operand4, operand5} in
-- compiler IR (see vm/compiler.lua Compiler:emit). This table is indexed by
-- opcode *name* and lists, for operand positions 2..5, whether the operand
-- at that position is a plain number ('N') or must be left alone ('X').
-- Positions beyond the listed length do not occur for that opcode.
--
-- EncryptOperands (vm/encrypt_operands.lua) uses this to decide which
-- operand values join the running cipher stream; the generated runtime
-- decrypts exactly those same positions, for exactly those opcodes, using
-- a layout-independent per-opcode arity table derived from this file.

local signatures = {
LOAD = {'N', 'N'},
NIL = {'N'},
MOVE = {'N', 'N'},
CLEAR = {'N'},
CELL = {'N', 'N'},
GETCELL = {'N', 'N'},
SETCELL = {'N', 'N'},
GETUP = {'N', 'N'},
SETUP = {'N', 'N'},
GLOBAL = {'N', 'N'},
SETGLOBAL = {'N', 'N'},
GETTABLE = {'N', 'N', 'N'},
SETTABLE = {'N', 'N', 'N'},
TABLE = {'N'},
EXPAND = {'N', 'N', 'N'},
ARGS = {'N', 'X', 'N'},
TAKE = {'N', 'N', 'N'},
VARARG = {'N'},
RETURN = {'N'},
CLOSURE = {'N', 'N'},
JMP = {'N'},
JFALSE = {'N', 'N'},
JTRUE = {'N', 'N'},
ADD = {'N', 'N', 'N'},
SUB = {'N', 'N', 'N'},
MUL = {'N', 'N', 'N'},
DIV = {'N', 'N', 'N'},
MOD = {'N', 'N', 'N'},
POW = {'N', 'N', 'N'},
CONCAT = {'N', 'N', 'N'},
LT = {'N', 'N', 'N'},
LE = {'N', 'N', 'N'},
GT = {'N', 'N', 'N'},
GE = {'N', 'N', 'N'},
EQ = {'N', 'N', 'N'},
NE = {'N', 'N', 'N'},
NOT = {'N', 'N'},
NEG = {'N', 'N'},
LEN = {'N', 'N'},
FORPREP = {'N', 'N', 'N'},
FORCHECK = {'N', 'N', 'N', 'N'},
CALL = {'N', 'N', 'X', 'N'},
TAILCALL = {'N', 'X'},
RETURN0 = {},
RETURN1 = {'N'},
RETURN2 = {'N', 'N'},
RETURN3 = {'N', 'N', 'N'},
CLEARS = {'X'},
}

-- Fixed-arity call/tailcall families: args/registers table is opaque ('X');
-- everything else numeric.
for arity = 0, 3 do
signatures['CALL' .. arity] = {'N', 'N', 'X', 'N'}
signatures['TAILCALL' .. arity] = {'N', 'X'}
end

return signatures
Loading