From 8476a2b409d7ed678cb24e1820244253f4b3b6e5 Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Thu, 26 Nov 2026 10:41:06 -0300 Subject: [PATCH 1/2] luaebpf: refuse a method call on a variable of two types A method a loop calls on a variable that a later pass sets to nil, or to another value, `p:getbyte(i)` in a loop that may write `p = nil`, compiled to a load through whatever word the register holds on that pass, a load the verifier refuses (check_mem_access, "invalid mem access 'scalar'"), where Lua raises. The call is refused at its line now: "'getbyte' takes the packet, not a packet that may be nil". The walk joins the method each pass reads at the call, and the join keeps the first pass's, whose receiver is the packet, while the register past it, which the call passes and the read goes through, holds the packet or nil. CALL holds that register to the method's receiver, as frame.same compares two values of the state, before it lowers the method. The kernel's answer is the verifier's refusal of a load through a register it does not know as a pointer, which the compiler gives at the line instead of an object that does not load. Every program file of Lunatik's tests/luaebpf, tests/tc and tests/xdp compiles to the same object. Co-Authored-By: Claude Opus 5.5 --- doc/design/luaebpf/compiler.md | 4 +++- doc/guide/08-luaebpf.md | 5 +++++ emit/call.lua | 10 ++++++++++ emit/frame.lua | 1 + 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/design/luaebpf/compiler.md b/doc/design/luaebpf/compiler.md index b1f9fb3..ce0649e 100644 --- a/doc/design/luaebpf/compiler.md +++ b/doc/design/luaebpf/compiler.md @@ -292,7 +292,9 @@ 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`. `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. * `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 diff --git a/doc/guide/08-luaebpf.md b/doc/guide/08-luaebpf.md index 8b0cec1..2f8cc43 100644 --- a/doc/guide/08-luaebpf.md +++ b/doc/guide/08-luaebpf.md @@ -599,6 +599,10 @@ 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 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 @@ -641,6 +645,7 @@ Each refusal reads `lunatik: :: `. | 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 | | 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 | diff --git a/emit/call.lua b/emit/call.lua index b791834..317a066 100644 --- a/emit/call.lua +++ b/emit/call.lua @@ -156,12 +156,22 @@ 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 = state[ins.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 +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 runtime.call(f, pc, ins, state, callee) diff --git a/emit/frame.lua b/emit/frame.lua index 28f7a68..c10bb2d 100644 --- a/emit/frame.lua +++ b/emit/frame.lua @@ -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 = {} From 3f7574aa6ab978ec42f862bc888c817fecd6a541 Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Thu, 26 Nov 2026 10:49:47 -0300 Subject: [PATCH 2/2] luaebpf: compile a method or field past the 256th constant A compiled function called no method, and read no field of a view or of a struct map value, once 256 constants preceded the name in its constant table, or for a name longer than 40 bytes: Lua writes `x:name(...)` as SELF and `x.name` as GETFIELD only while the name is a short string whose index fits the instruction, and otherwise as the MOVE of x past the result and a GETTABLE of the name in a register, or as that GETTABLE alone (luaK_self and luaK_indexed, lua/lcode.c). GETTABLE read the name off the receiver as an index into a table of the body, so `ctx:packet()`, `p:getbyte(0)`, `iphdr:view(p, 14)`, `limits:run(n)`, `ip.protocol` and `v.count` were refused by messages that named neither the name nor the limit. Each compiles now to what it compiles to below the 256th constant, and a name the receiver lacks takes the short form's refusal, "the packet has no method ''" and its kin, the context's "'data_end' is a packet bound" among them. GETTABLE with a key the compiler holds as a string reads it as SELF does where the instruction before is that MOVE and the container is a receiver of a method, a runtime, a proxy or a struct codec, and as GETFIELD does otherwise, sharing each step with the instruction it stands for. Lua's own answer is that OP_SELF is that MOVE and that GETTABLE in one instruction (lua/lvm.c). A key held as any string of the body, `v[k]` with `k` the string "count", reads the field it names, as Lua does, where it was refused as an index; a table of the body that is no such receiver, read after that MOVE, keeps the reading of a table, so `s = t; m = t[k]` compiles as it did. That MOVE and that GETTABLE are also what `k = "getbyte"; s = p; m = p[k]` writes on three locals declared `m, s, k`, so a method may now sit in a variable, and CALL, which read the receiver past the method on the word of SELF having written it, holds the method to it: called on another value, or with no argument, which passes nothing past the method, it is refused, "'getbyte' takes the packet, not a number", and called from another register than the one it was read into, "'getbyte' is called through a variable, not where it was read", since where two paths read two methods into one variable the join keeps the first, and both paths would call it. The run method of a runtime is held to the three alike, by the refusal it has, "a runtime is called through its method", and carries the runtime it was read off as a method carries its receiver, where it kept the runtime's own value in `k`, which every lowering reads as a value of the body: kept in a variable, the method would be taken for its runtime, `r.key` its script and `r:run(1)` the call. Part of #10, which the cases of Lunatik's tests/luaebpf close. Every program file of Lunatik's tests/luaebpf, tests/tc and tests/xdp compiles to the same object, the refusal rows of refuse.sh, view.sh and struct.sh answer the same when compiled on the host, and each program of the issue compiles to its control's instructions with those of the pad of constants inserted. Co-Authored-By: Claude Opus 5.5 --- doc/design/luaebpf/compiler.md | 13 ++++++-- doc/guide/08-luaebpf.md | 5 +++ doc/guide/09-luaebpf-by-example.md | 1 + emit/call.lua | 7 +++-- emit/loads.lua | 49 ++++++++++++++++++++++-------- emit/runtime.lua | 27 ++++++++++------ 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/doc/design/luaebpf/compiler.md b/doc/design/luaebpf/compiler.md index ce0649e..8b4e59d 100644 --- a/doc/design/luaebpf/compiler.md +++ b/doc/design/luaebpf/compiler.md @@ -292,9 +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`. `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. + 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 diff --git a/doc/guide/08-luaebpf.md b/doc/guide/08-luaebpf.md index 2f8cc43..96680e5 100644 --- a/doc/guide/08-luaebpf.md +++ b/doc/guide/08-luaebpf.md @@ -603,6 +603,10 @@ Give the variable one type on every path: `local v = 0`. These are refused too: 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 @@ -646,6 +650,7 @@ Each refusal reads `lunatik: :: `. | 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 | diff --git a/doc/guide/09-luaebpf-by-example.md b/doc/guide/09-luaebpf-by-example.md index 4f6acd8..6dbef52 100644 --- a/doc/guide/09-luaebpf-by-example.md +++ b/doc/guide/09-luaebpf-by-example.md @@ -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 | diff --git a/emit/call.lua b/emit/call.lua index 317a066..66b661c 100644 --- a/emit/call.lua +++ b/emit/call.lua @@ -158,11 +158,14 @@ 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 = state[ins.a + 1] + 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) @@ -173,7 +176,7 @@ function call.CALL(f, pc, ins, state) 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) diff --git a/emit/loads.lua b/emit/loads.lua index e56e1a9..207ef82 100644 --- a/emit/loads.lua +++ b/emit/loads.lua @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/emit/runtime.lua b/emit/runtime.lua index 96eabaf..17b7663 100644 --- a/emit/runtime.lua +++ b/emit/runtime.lua @@ -20,7 +20,7 @@ local CTXREG = 0 -- the program's parameter, which the call passes as local NUL = 1 -- the byte key__sz counts past the key, which the kfunc writes in place local INTSIZE = 4 -- the kfunc answers an int local ABSENT = -1 -- what the kfunc answers where Lua answers nil (luakfunc_action) -local SELFREG = 1 -- SELF puts the runtime past the method, and the arguments past it +local SELFREG = 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 @@ -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) @@ -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)