Skip to content

luaebpf: refuse a method call on a variable of two types - #50

Open
lneto wants to merge 1 commit into
masterfrom
method-nil-receiver
Open

lneto wants to merge 1 commit into
masterfrom
method-nil-receiver

Conversation

@lneto

@lneto lneto commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A method called in a loop on a variable that a later iteration sets to nil, or to a number,
compiles today to an object the verifier refuses to load, "R3 invalid mem access 'scalar'", where
Lua would raise on the iteration after it.

local xdp    = require("bpf.xdp")
local action = require("linux.xdp").action

return xdp.program(function(ctx)
	local p = ctx:packet()
	local n = 0
	for i = 1, 3 do
		n = n + p:getbyte(i)
		if n > 5 then p = nil end
	end
	return action.PASS
end, {default = action.PASS})

lunatik bpf now refuses such a call at its line, naming the method, what it takes and what the
variable may hold when it is called. Here, where p is the packet on the first iteration and may
be nil after it, it says "'getbyte' takes the packet, not a packet that may be nil"; with p = 1
in place of p = nil, "'getbyte' takes the packet, not a number that may be a packet". A loop that sets p to ctx:packet() again compiles as it does today.
doc/guide/08-luaebpf.md lists the first message among its refusals.

It needs no kernel and no build step of its own.

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>
@lneto lneto added complexity: small One line or one file, with a trivial or existing test 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: small One line or one file, with a trivial or existing test 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