Skip to content

luaebpf: a function past 32767 instructions fails in string.pack #4

Open

Description

@lneto

A program file whose function compiles to more instructions than a jump's 16-bit offset spans is not refused at its line: the compile ends in the instruction buffer with an error of string.pack, and the message names the compiler's own file.

code:pack, in bin/luaebpf/insn.lua, resolves each jump as self.labels[record.target] - i and packs it as i2, the off of struct bpf_insn. Nothing holds a function to the 32767 instructions that field spans, so string.pack raises at the first jump past it. A program of 2000 lines s = s + p:getbyte(n) in one function, compiled with lunatik bpf, prints:

lunatik: .../luaebpf/insn.lua:319: bad argument luainkernel/lunatik#4 to 'pack' (integer overflow)

No object is written, so nothing wrong loads. The message names neither the program file nor a line of it, and says nothing of the size. Master before luainkernel/lunatik#2098's change fails the same way on the same file, at insn.lua:308; 1300 such lines compile and load on both, in 24715 and 55266 processed instructions on 6.8. A stop's block sits after the function's tail, the blocks in the order of their checks, so a jump to one spans the body plus what the blocks before it add: the limit is met somewhat sooner than before, which was read from the shape of the jumps and not measured between 1300 and 2000 lines.

A fix where there is one: a refusal where walk returns a function's code, in bin/luaebpf/emit/init.lua, which has the chunk and the line frame.refuse names, saying how many instructions the function compiles to and how many a jump reaches, held to the jumps that are out of range if the count alone would refuse a function whose jumps all fit. Whether the long jump, BPF_JMP32 | BPF_JA, is a way to lift the limit across the kernels and JITs of the range was not checked.

Contract: doc/guide/08-luaebpf.md: "Each refusal reads lunatik: <file>:<line>: <message>."

Found while implementing luainkernel/lunatik#2098.

Update: Groomed at f745bb5. The premise holds there: nothing refuses the function, and code:pack raises at the first jump whose offset string.pack's i2 does not take, now as insn.lua:315: bad argument #4 to 'pack' (integer overflow). Since 67ff1ec a stop's block is 31 instructions, so what brings a function to the reach is how many of its checks may stop more than its body: one function of 1056 lines s = s + p:getbyte(n) compiles, to 51762 instructions, and one of 1057 fails on a single jump, the last block's jump back to the tail, 32769 instructions behind it.

What is settled:

  • The refusal. A function is refused when a jump of the code the object would take, the code of the walk that settled, has an offset outside the __s16 off of struct bpf_insn, -32768 to 32767, the range string.pack raises past, and in no other case. Through the CLI it reads

    lunatik: <file>:<line>: the function compiles to 51811 instructions, and a jump in it spans more than the 32767 a jump reaches
    

    where <line> is the line the function is defined at, the program's or a callee's, as a vararg function cannot be compiled and a program takes one argument, the context name theirs: the line of the first jump out of range is the first read's in a function of 2000 reads and the last read's in one of 1057. lunatik bpf exits 1 and writes no object, as for every refusal.

  • Held to the jumps. A function whose jumps all fit compiles whatever its size, since the count alone would refuse functions that compile today: 1000 such lines compile to 49018 instructions with every jump in range, and 40000 lines s = s + 1 followed by one read to 160063.

  • The limit stays. The long jump was read, below, and is no answer across the range, so this change names the limit and lifts nothing.

  • The guide. doc/guide/08-luaebpf.md takes the message in Messages, with the way out, part of the function moved to a function it calls, which is compiled apart with jumps of its own, and says so where it tells what a function may hold.

  • Two pull requests. The compiler and the guide are this repository's, under #4, which it does not close. The cases are Lunatik's, in tests/luaebpf/refuse.sh, and come in the pull request of Lunatik that moves the mount once the first merges, which closes this issue, as luaebpf: name kernel scripts older than the compiler #41 says of luaebpf: kernel scripts older than the compiler stop lunatik bpf at an index error inside the compiler #9: a function refused at its line with the message, a function longer than a jump reaches whose jumps all fit compiled to an object, and a callee refused at its own line.

How each requirement of the issue is met:

  • "is not refused at its line", and the contract's "Each refusal reads lunatik: <file>:<line>: <message>": the refusal above names the program file and the line of the function.
  • "a refusal where walk returns a function's code, in bin/luaebpf/emit/init.lua, which has the chunk and the line frame.refuse names": the lowering of emit/init.lua, the file's path at this repository's root, raises it through frame.refuse on the code the last walk returns.
  • "saying how many instructions the function compiles to and how many a jump reaches": the message carries both figures.
  • "held to the jumps that are out of range if the count alone would refuse a function whose jumps all fit": it would, as the two functions above show, so the refusal is held to the jumps.
  • "Whether the long jump, BPF_JMP32 | BPF_JA, is a way to lift the limit across the kernels and JITs of the range was not checked": checked, at the tags v6.6 to v6.12 and v6.18. The verifier and the interpreter take it from v6.6 (check_subprogs in kernel/bpf/verifier.c, JMP32_JA in kernel/bpf/core.c). Of the JITs, x86-64, arm64 and riscv64 take it from v6.6, arm, s390 and loongarch from v6.7, powerpc32 and arc from v6.10, powerpc64 from v6.11, and x86-32, riscv32, mips, sparc64 and parisc not as of v6.18; where the JIT has no case for it the load fails under CONFIG_BPF_JIT_ALWAYS_ON, and for any program that calls a kfunc, as a for with a computed bound and a call into a runtime do (bpf_prog_select_runtime). Only the unconditional jump has the 32-bit form (Documentation/bpf/standardization/instruction-set.rst, "PC += imm" for JA alone), and 4943 of the 5887 jumps out of range in the function of 2000 reads are conditional: each would become a near conditional over a gotol, which moves every position after it. LLVM does that under -mcpu=v4 (adjustBranch in BPFMIPeephole.cpp, at half the range, since its own insertions move the targets) and otherwise stops at "Branch target out of insn range" (BPFAsmBackend.cpp), which is this refusal.

Outside this change: a function the compiler takes can still be refused at load, where a patch of the verifier's own lengthens a 16-bit jump past its range, "insn %d cannot be patched due to 16-bit range" (bpf_patch_insn_single in kernel/bpf/core.c), which is the guide's Refused at load.

Activity

  1. added
    severity: low
    on Nov 19, 2026
  2. added this to the 0.2 milestone on Nov 22, 2026
  3. transferred this issue fromluainkernel/lunatikon Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    severity: low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions