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.