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
11 changes: 10 additions & 1 deletion doc/design/luaebpf/compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,16 @@ what `frame.lua` holds:
`LEN`. A global or an upvalue resolves to the value the body left, a field of a table the
compiler holds to that field's value, read when the function is compiled, and an index into a
map to the map's helpers; a field of the context or of a struct map value goes to `context.lua`
or `map.lua`, and a method call on a proxy is read back by `CALL`.
or `map.lua`, and a method call on a proxy is read back by `CALL`. A `GETTABLE` whose key is a
string the compiler holds reads the name as `GETFIELD` does, and as `SELF` does where the `MOVE`
of a receiver past its result comes before it: Lua writes `x.name` and `x:name` so past the
function's 256th constant, or for a name longer than 40 bytes (`luaK_indexed`, `luaK_self`).
`CALL` holds a method to the value past it, the one it was read from: a loop's first pass may
have read the method before a later pass widened that value. It holds it to the register it was
read into too, since a variable may hold either of two methods where paths join, and to a call
that passes that value, since one with no argument passes nothing past the method. The `run` of
a runtime is held alike in `runtime.lua`, which keeps the runtime it was read off as a method
keeps its receiver, outside the field a table of the body is read from.
* `arithmetic.lua`: the arithmetic and bitwise opcodes and their forms with a constant, `UNM`,
`BNOT` and `NOT`, with Lua's semantics over 64-bit integers. `MMBIN` and its kin, which the VM
runs only when an operand is not a number, emit nothing, since an operand that is not a number is
Expand Down
10 changes: 10 additions & 0 deletions doc/guide/08-luaebpf.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,14 @@ Give the variable one type on every path: `local v = 0`. These are refused too:
* A view read in a function that has assigned the parameter its context or its packet came in: "a
view is read through the context or a packet, which no parameter of the function holds here". A
view keeps the offset it starts at and no packet, and its fields are read through that parameter.
* A method called on a variable that a loop sets to nil, or to another value, after a pass that
called it on the packet: "'getbyte' takes the packet, not a packet that may be nil". Lua raises
on the pass where the variable is not the packet, and the read would go through whatever word the
register holds there.
* A method kept in a variable and called through it: "'getbyte' is called through a variable, not
where it was read", and for the `run` of a runtime, "a runtime is called through its method; write
'limits:run(...)'". Lua calls whatever the variable holds, which where two paths join may be
either of two methods, and the compiler lowers a method for the call it is read for.
* A verdict that may be nil: "the verdict may be nil here; test it first". The kernel would read
nil as 0, which is an action.
* A verdict that may be a boolean: "the verdict may be a boolean here; return an action". The
Expand Down Expand Up @@ -641,6 +649,8 @@ Each refusal reads `lunatik: <file>:<line>: <message>`.
| a runtime is called through its method; write 'limits:run(...)' | `limits(1)` | `limits:run(1)` |
| a runtime has no method 'call'; write 'limits:run(...)' | `limits:call(1)` | `limits:run(1)` |
| the function needs 528 bytes of stack, over the 512 eBPF allows | a call into a runtime named by 480 bytes | a shorter name |
| 'getbyte' takes the packet, not a packet that may be nil | `p:getbyte(i)` in a loop that sets `p = nil` | a variable of its own for nil |
| 'getbyte' is called through a variable, not where it was read | `local m, s, k`, then `k = "getbyte"; s = p; m = p[k]` and `m(s, 0)` | `p:getbyte(0)` |
| a number that may be nil cannot be compared with a number here | `==` on a variable nil on one path | `local v = 0` |
| a number that may be nil has no truth value in a compiled function | `if v then`, `v` nil on one path | `local v = 0`, then `if v ~= 0 then` |
| the verdict may be nil here; test it first | `return v`, `v` nil on one path | a verdict on every path |
Expand Down
1 change: 1 addition & 0 deletions doc/guide/09-luaebpf-by-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ instruction alone computes something else, the compiler emits what lies between:
| `p:getint16(n)` | two bytes at `n`, signed; raises past the packet | a load the verifier refuses until the program tests the end | the offset tested unsigned against 65535 less the width, the end against `data_end`, and the sign extended by a shift left and an arithmetic shift right, where `BPF_MEMSX` would extend it |
| `p:getint16(n)` on TC | the same, of the whole packet, past the skb's linear head too | a load stops at `data_end`, the end of the linear head | the offset tested unsigned against `INT_MAX`, the helper's own bound, since it takes the low 32 bits, then `bpf_skb_load_bytes` into the frame, which copies the bytes wherever the skb holds them, a stop where it answers non-zero, and the sign extended alike |
| `h.field`, `h` a view | the field `codec:view` reads at the view's offset plus the field's, most significant byte first where the layout marks it `bigendian`, a bitfield out of its load; raises past the packet | a load the verifier refuses until the program tests the end, in the kernel's byte order | the view's offset, which its register holds, plus the field's, read as `p:getint16(n)` reads, of the field's width or of the load that covers a bitfield, through the parameter that holds the context or a packet; then what `v.field` of a struct value emits after its load |
| `p:getbyte(n)`, `h.field`, the name past the function's 256th constant or longer than 40 bytes | what the same call and the same field compute below it: Lua writes the name into a register, `x.name` as a `GETTABLE` and `x:name` as the `MOVE` of `x` past the result before that `GETTABLE`, where `SELF` and `GETFIELD` index the name in the constants | no instruction: the name is a string of the chunk | what the short form emits, the `GETTABLE` read as `SELF` where that `MOVE` comes before it on a receiver of a method, and as `GETFIELD` otherwise |
| `#p` | the bytes the `data` object views | no instruction: the context holds the two bounds | `data_end` less `data`, both read from the context; on TC `len`, as `#skb` |
| `skb:data()` on an skb that is not linear | the whole packet, in a tc callback, read from a copy of it | `data_end` ends the linear head, short of the packet | the context register, which the reads hand to `bpf_skb_load_bytes` |
| `skb:priority(v)` | raises outside 0 to `U32_MAX` | a four-byte store keeps the low 32 bits | a value the compiler holds checked against the field's width, and one the program computes tested, which stops the program |
Expand Down
15 changes: 14 additions & 1 deletion emit/call.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,27 @@ local function subprogram(f, pc, ins, state, callee)
end
end

-- the call passes A+1 and the method is lowered for its recv, which a join keeps from a loop's first pass
local function received(f, pc, ins, state, callee)
local recv = ins.b > 1 and state[ins.a + 1] or nil -- a call with no argument passes nothing at A+1
if not frame.same(recv, callee.recv) then
frame.refuse(f, pc, "'%s' takes the %s, not a %s", callee.name, frame.typename(callee.recv),
frame.typename(recv))
end
if callee.register ~= ins.a then -- a variable may hold either of two methods, of which a join keeps one
frame.refuse(f, pc, "'%s' is called through a variable, not where it was read", callee.name)
end
end

function call.CALL(f, pc, ins, state)
local callee = state[ins.a]
if ins.b == 0 or ins.c == 0 then
frame.refuse(f, pc, "a call with a variable number of values cannot be compiled")
end
if callee ~= nil and callee.t == METHOD then
received(f, pc, ins, state, callee)
packet.method(f, pc, ins, state, callee)
elseif callee ~= nil and runtime.isproxy(callee.k) then
elseif callee ~= nil and runtime.iscallee(callee) then
runtime.call(f, pc, ins, state, callee)
else
subprogram(f, pc, ins, state, callee)
Expand Down
1 change: 1 addition & 0 deletions emit/frame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ local function same(a, b)
end
return a.t == b.t and a.k == b.k and a.map == b.map and a.codec == b.codec
end
frame.same = same

local function copy(state)
local out = {}
Expand Down
49 changes: 37 additions & 12 deletions emit/loads.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ function loads.GETTABUP(f, pc, ins, state)
frame.resolve(f, pc, state, ins.a, value, key)
end

function loads.GETFIELD(f, pc, ins, state)
local key = tostring(frame.constant(f, pc, ins.c))
local container = state[ins.b]
local function field(f, pc, ins, state, container, key)
if container ~= nil and container.t == CTX then
return context.contextfield(f, pc, key)
end
Expand All @@ -145,6 +143,10 @@ function loads.GETFIELD(f, pc, ins, state)
frame.resolve(f, pc, state, ins.a, lookup(f, pc, container, key, name), key)
end

function loads.GETFIELD(f, pc, ins, state)
field(f, pc, ins, state, state[ins.b], tostring(frame.constant(f, pc, ins.c)))
end

function loads.SETFIELD(f, pc, ins, state)
local container = state[ins.a]
if container ~= nil and container.t == CTX then
Expand Down Expand Up @@ -191,9 +193,40 @@ function loads.GETI(f, pc, ins, state)
frame.resolve(f, pc, state, ins.a, lookup(f, pc, container, ins.c, name))
end

-- what a compiled method is called on: a runtime, a proxy, or a struct codec of the body, whose view the call makes
local function receiver(value)
return value ~= nil and (runtime.isproxy(value.k) or (value.t & frame.classes.proxy) ~= 0
or packet.iscodec(value))
end

-- the call reads the method back, held to the register it is read into and to recv at A+1
local function method(f, pc, ins, state, recv, name)
if runtime.isproxy(recv.k) then
return runtime.self(f, pc, ins, state, name)
end
state[ins.a] = {t = METHOD, name = name, recv = recv, register = ins.a}
end

-- whether the instruction before is the MOVE luaK_self writes of the receiver past the method
local function isself(f, pc, ins)
local move = f.proto.code[pc - 1]
return move ~= nil and move.op == proto.opcodes.MOVE and move.a == ins.a + 1 and move.b == ins.b
end

-- Lua writes x.name past the 256th constant, or for a name past 40 bytes, as GETTABLE, and x:name as MOVE and GETTABLE
local function byname(f, pc, ins, state, container, name)
if isself(f, pc, ins) and receiver(container) then
return method(f, pc, ins, state, container, name)
end
field(f, pc, ins, state, container, name)
end

function loads.GETTABLE(f, pc, ins, state)
local key = state[ins.c]
local container = state[ins.b]
if key ~= nil and type(key.k) == "string" then
return byname(f, pc, ins, state, container, key.k)
end
indexable(f, pc, container)
if container ~= nil and maps.declares(container.k) then
return map.mapread(f, pc, ins, state, container.k)
Expand All @@ -205,22 +238,14 @@ function loads.GETTABLE(f, pc, ins, state)
frame.resolve(f, pc, state, ins.a, lookup(f, pc, container, key.k, name))
end

-- what a compiled method is called on: a proxy, or a struct codec of the body, whose view the call makes
local function receiver(value)
return value ~= nil and ((value.t & frame.classes.proxy) ~= 0 or packet.iscodec(value))
end

-- the receiver goes to A+1 and the method to A, which the call then reads back
function loads.SELF(f, pc, ins, state)
local recv = state[ins.b]
if recv ~= nil and runtime.isproxy(recv.k) then
return runtime.self(f, pc, ins, state)
end
if not receiver(recv) then
frame.refuse(f, pc, "a method call cannot be compiled")
end
moveslot(f, state, ins.a + 1, ins.b)
state[ins.a] = {t = METHOD, name = tostring(frame.constant(f, pc, ins.c)), recv = recv}
method(f, pc, ins, state, recv, tostring(frame.constant(f, pc, ins.c)))
end

function loads.LEN(f, pc, ins, state)
Expand Down
27 changes: 18 additions & 9 deletions emit/runtime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local CTXREG <const> = 0 -- the program's parameter, which the call passes as
local NUL <const> = 1 -- the byte key__sz counts past the key, which the kfunc writes in place
local INTSIZE <const> = 4 -- the kfunc answers an int
local ABSENT <const> = -1 -- what the kfunc answers where Lua answers nil (luakfunc_action)
local SELFREG <const> = 1 -- SELF puts the runtime past the method, and the arguments past it
local SELFREG <const> = 1 -- a method call puts the runtime past the method, and the arguments past it

local proxy = {} -- no method: it marks what lunatik.runtime returns, which SELF and CALL look for

Expand Down Expand Up @@ -48,16 +48,18 @@ local function written(value)
return string.format("'%s:run(...)'", value.name or "runtime")
end

-- 'r:run' reads the one method a runtime has, the kfunc's own verb; r goes to A+1, as SELF puts any
-- receiver, a value the kernel never sees
function runtime.self(f, pc, ins, state)
-- 'r:run' reads the one method a runtime has, the kfunc's own verb
function runtime.self(f, pc, ins, state, name)
local recv = state[ins.b]
local name = frame.constant(f, pc, ins.c)
if name ~= "run" then
frame.refuse(f, pc, "a runtime has no method '%s'; write %s", name, written(recv))
end
state[ins.a + SELFREG] = recv
state[ins.a] = {t = RUN, k = recv.k, name = recv.name}
state[ins.a] = {t = RUN, name = recv.name, recv = recv, register = ins.a}
end

-- whether a register holds a runtime, a call of which is refused, or the run method read off one
function runtime.iscallee(value)
return value.t == RUN or runtime.isproxy(value.k)
end

local function arguments(f, pc, ins, state)
Expand Down Expand Up @@ -113,12 +115,19 @@ local function pass(f, key, at, args, nargs)
code:set(insn.reg.R5, nargs * frame.SLOT)
end

-- whether the call is the run method, from the register it was read into, and passes the runtime it was
-- read off: a method read by index leaves both to the program
local function ismethod(ins, state, callee)
return callee.t == RUN and callee.register == ins.a and ins.b > SELFREG
and frame.same(state[ins.a + SELFREG], callee.recv)
end

function runtime.call(f, pc, ins, state, callee)
if callee.t ~= RUN then
if not ismethod(ins, state, callee) then
frame.refuse(f, pc, "a runtime is called through its method; write %s", written(callee))
end
placed(f, pc, state, callee)
local code, key, nargs = f.code, callee.k.key, arguments(f, pc, ins, state)
local code, key, nargs = f.code, callee.recv.k.key, arguments(f, pc, ins, state)
local at, args = stage(f, pc, ins, state, key, nargs)
pass(f, key, at, args, nargs)
code:kfunc(f.unit.kfunc)
Expand Down
Loading