From d486a6c9464c74d2e23a7496f1b793b2a5322f0d Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Thu, 26 Nov 2026 12:39:34 -0300 Subject: [PATCH] luaebpf: refuse a function too long for its jumps 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 Co-Authored-By: Claude Fable 5.2 --- doc/guide/08-luaebpf.md | 7 ++++++- emit/init.lua | 5 +++++ insn.lua | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/doc/guide/08-luaebpf.md b/doc/guide/08-luaebpf.md index 8b0cec1..3c7be47 100644 --- a/doc/guide/08-luaebpf.md +++ b/doc/guide/08-luaebpf.md @@ -279,7 +279,11 @@ means the program returns `XDP_ABORTED` or `TC_ACT_SHOT`, which drop the packet; Besides what the table refuses, these are refused: a table constructor, `..`, a closure, an assignment to an upvalue or a global, a write to a table that is not a map, a generic `for`, -`while`, `repeat`, a vararg function and a to-be-closed variable. +`while`, `repeat`, a vararg function and a to-be-closed variable. So is a function with a jump +that spans more than the 32767 instructions an eBPF jump reaches, at the line the function is +defined at: a read of the packet compiles to some fifty instructions, so one function of eleven +hundred reads is refused. Moved in part to a function it calls, it compiles, since each function +is compiled apart, with jumps of its own. [Messages](#messages) lists what each refusal says. ## The packet @@ -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 | +| the function compiles to 53918 instructions, and a jump in it spans more than the 32767 a jump reaches | a function of eleven hundred reads of the packet | part of it in a function it calls | | 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/init.lua b/emit/init.lua index fa01d66..4dd6f97 100644 --- a/emit/init.lua +++ b/emit/init.lua @@ -217,6 +217,11 @@ local function compile(unit, fn, read, name, params, isprogram) unit.functions[fn] = f table.insert(unit.order, f) lower(f) + if not f.code:reaches() then + frame.refuse(f, nil, + "the function compiles to %d instructions, and a jump in it spans more than the %d a jump reaches", + f.code:len(), insn.REACH) + end unit.lowering[fn] = nil return f end diff --git a/insn.lua b/insn.lua index 2343f10..bd3aa36 100644 --- a/insn.lua +++ b/insn.lua @@ -33,6 +33,7 @@ local TO_BE = 0x08 local PSEUDO_CALL = 1 local MAY_GOTO = 0 local SIZE = 8 +local REACH = 0x7fff local WORD = 8 local BYTE = 8 @@ -78,6 +79,11 @@ insn.reg = {R0 = 0, R1 = 1, R2 = 2, R3 = 3, R4 = 4, R5 = 5, R6 = 6, R7 = 7, R8 = -- @field luaebpf.insn.SIZE insn.SIZE = SIZE +--- Instructions a jump reaches forward, the most `bpf_insn`'s 16-bit `off` holds; back, it reaches one +-- more. +-- @field luaebpf.insn.REACH +insn.REACH = REACH + --- -- Whether a load or a store takes `n` bytes: eBPF has one of 1, 2, 4 and 8, so whatever a compiled -- function reads as one number is one of those widths. @@ -295,6 +301,21 @@ function code:relocations(kind) return found end +--- +-- Whether every jump of the buffer reaches its label, which `pack` writes in `bpf_insn`'s 16-bit `off`. +-- @function luaebpf.insn.code:reaches +-- @treturn boolean +function code:reaches() + for i = 1, self.n do + local target = self.records[i].target + local off = target and self.labels[target] - i or 0 + if off < -REACH - 1 or off > REACH then + return false + end + end + return true +end + --- -- The buffer as object bytes, with every jump resolved. A call keeps the `-1` its relocation -- replaces, as the compiler libbpf was built for leaves it.