Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools, bpf_asm: Warn when jumps are out of range

When compiling larger programs with bpf_asm, it's possible to
accidentally exceed jt/jf range, in which case it won't complain, but
rather silently emit a truncated offset, leading to a "happy debugging"
situation.

Add a warning to help detecting such issues. It could be made an error
instead, but this might break compilation of existing code (which might
be working by accident).

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20191107100349.88976-1-iii@linux.ibm.com

authored by

Ilya Leoshkevich and committed by
Daniel Borkmann
7e22077d 85d31dd0

+12 -2
+12 -2
tools/bpf/bpf_exp.y
··· 545 545 } 546 546 } 547 547 548 + static uint8_t bpf_encode_jt_jf_offset(int off, int i) 549 + { 550 + int delta = off - i - 1; 551 + 552 + if (delta < 0 || delta > 255) 553 + fprintf(stderr, "warning: insn #%d jumps to insn #%d, " 554 + "which is out of range\n", i, off); 555 + return (uint8_t) delta; 556 + } 557 + 548 558 static void bpf_reduce_jt_jumps(void) 549 559 { 550 560 int i; ··· 562 552 for (i = 0; i < curr_instr; i++) { 563 553 if (labels_jt[i]) { 564 554 int off = bpf_find_insns_offset(labels_jt[i]); 565 - out[i].jt = (uint8_t) (off - i -1); 555 + out[i].jt = bpf_encode_jt_jf_offset(off, i); 566 556 } 567 557 } 568 558 } ··· 574 564 for (i = 0; i < curr_instr; i++) { 575 565 if (labels_jf[i]) { 576 566 int off = bpf_find_insns_offset(labels_jf[i]); 577 - out[i].jf = (uint8_t) (off - i - 1); 567 + out[i].jf = bpf_encode_jt_jf_offset(off, i); 578 568 } 579 569 } 580 570 }