Skip to content

luaebpf: compile a method or field past the 256th constant - #51

Open
lneto wants to merge 2 commits into
masterfrom
emit-gettable-name
Open

lneto wants to merge 2 commits into
masterfrom
emit-gettable-name

Conversation

@lneto

@lneto lneto commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A program function cannot call a method, or read a field of a view or of a struct map value, whose
name comes after 256 other constants in it, the distinct strings and numbers it writes, small
integers aside. Nor can any function for a name longer than 40 bytes. lunatik bpf refuses both
with a message that names neither the name nor the limit.

Each now compiles as it does in a shorter function. This script writes such a program file, 300
lines of n = n + <number> before the calls, which lunatik bpf long.bpf.lua refuses today at
iphdr:view(p, 14) with "argument #1 is a table, and a compiled call passes numbers and packets":

local pad = {}
for i = 1, 300 do
	pad[i] = string.format("\tn = n + %d\n", 100000 + i)
end

local program = [[
local xdp     = require("bpf.xdp")
local struct  = require("struct")
local vmlinux = require("bpf.vmlinux")
local lunatik = require("lunatik")
local action  = require("linux.xdp").action

local iphdr  = struct(vmlinux.layout("iphdr"))
local limits = lunatik.runtime("limits")

return xdp.program(function(ctx)
	local p = ctx:packet()
	local n = 0
%s	local ip = iphdr:view(p, 14)
	local v = limits:run(n + ip.protocol + p:getbyte(0))
	if v then return v end
	return action.PASS
end, {default = action.PASS})
]]

local file <close> = io.open("long.bpf.lua", "w")
file:write(string.format(program, table.concat(pad)))

A name the receiver lacks gets the message a shorter function gets: p:setbyte(0) past those
constants says "the packet has no method 'setbyte'", where today it says "'packet' is read from a
value the compiler cannot resolve". The same change lets a field be named by a string held in a
variable, local key = "protocol" and then ip[key], which compiles as ip.protocol in a function
of any length, where today every function refuses it.

Its first commit is the one of #50, which refuses a method call on a variable a
loop may set to nil or to a number, and is reviewed there. This change answers
#10, which it does not close: a Lunatik pull request, opened once this one
merges, moves Lunatik's luaebpf mount past this change, adds the tests to tests/luaebpf and closes
the issue. It needs no kernel and no build step of its own.

lneto and others added 2 commits November 26, 2026 12:19
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 <noreply@anthropic.com>
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 '<name>'" 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 <noreply@anthropic.com>
@lneto lneto added complexity: medium Several call sites, a new test, or a core path to audit workflow-reviewed reviewed end to end by a review workflow labels Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

complexity: medium Several call sites, a new test, or a core path to audit workflow-reviewed reviewed end to end by a review workflow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant