From 8476a2b409d7ed678cb24e1820244253f4b3b6e5 Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Thu, 26 Nov 2026 10:41:06 -0300 Subject: [PATCH] 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 = {}