Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/guide/08-luaebpf.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -641,6 +645,7 @@ Each refusal reads `lunatik: <file>:<line>: <message>`.
| 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 |
Expand Down
5 changes: 5 additions & 0 deletions emit/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions insn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ local TO_BE <const> = 0x08
local PSEUDO_CALL <const> = 1
local MAY_GOTO <const> = 0
local SIZE <const> = 8
local REACH <const> = 0x7fff
local WORD <const> = 8
local BYTE <const> = 8

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Loading