Skip to content

luaebpf: refuse a function too long for its jumps - #57

Open
lneto wants to merge 1 commit into
masterfrom
refuse-far-jump
Open

lneto wants to merge 1 commit into
masterfrom
refuse-far-jump

Conversation

@lneto

@lneto lneto commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A program file whose function is too long for an eBPF jump fails to compile with an error of the compiler's own file, insn.lua:315: bad argument #4 to 'pack' (integer overflow), which names no line of the program file and says nothing of its size.

Such a function is now refused as every other refusal of lunatik bpf reads, lunatik: <file>:<line>: <message>, at the line the function is defined at, with how many instructions it compiles to and how far a jump reaches:

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

return xdp.program(function(ctx)
	local p = ctx:packet()
	local s = #p
	s = s + p:getbyte(1)
	-- and 1099 more lines like the one above
	if s > 100 then return action.DROP end
	return action.PASS
end, {default = action.PASS})
$ lunatik bpf far.bpf.lua
lunatik: far.bpf.lua:4: the function compiles to 53918 instructions, and a jump in it spans more than the 32767 a jump reaches

The refusal reads the jumps and not the size, so a function whose jumps all fit compiles as it does today, to the same object, however long it is. The guide lists the message, and the way out: part of the function moved to a function it calls, which is compiled apart, with jumps of its own. A callee too long is refused at its own line.

Nothing is lifted: the long jump, gotol, has no conditional form and not every JIT of the kernels Lunatik supports takes it, so the limit stays and is named. The tests are in Lunatik's tests/luaebpf/refuse.sh and come with the Lunatik pull request that updates its luaebpf/ submodule to this change and closes luaebpf#4. This one answers luaebpf#4, which it does not close.

A function with a jump past the 16-bit off of struct bpf_insn, -32768
to 32767 instructions, ended the compile in code:pack, whose
string.pack raised "insn.lua:315: bad argument #4 to 'pack' (integer
overflow)", naming the compiler's file and no line of the program file.
Each check that may stop takes a block of 31 instructions after the
tail, so one function of 1057 reads of the packet compiles to 51811
instructions, and the last block's jump back to the tail spans 32769.

Such a function is now refused at the line it is defined at, the
program's or a callee's:

  lunatik: far.bpf.lua:3: the function compiles to 51811 instructions,
  and a jump in it spans more than the 32767 a jump reaches

The refusal reads the jumps of the code the settled walk leaves,
code:reaches, which resolves each off as pack does: a count of
instructions alone would refuse functions that compile today, 1000
reads in 49018 instructions and 40000 additions before one read in
160067, whose jumps all fit. The program files of Lunatik's tests
compile to the same objects, byte for byte.

The long jump, BPF_JMP32 | BPF_JA, lifts nothing across the range: it
has no conditional form, so a far conditional becomes a near one over a
gotol, which moves every later position, and the JITs of x86-32,
riscv32, mips, sparc64 and parisc take none as of v6.18, where the load
then fails under CONFIG_BPF_JIT_ALWAYS_ON or for a program that calls a
kfunc. LLVM refuses the same way below cpu v4, "Branch target out of
insn range" (BPFAsmBackend.cpp).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <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