Commit 7e22077d authored by Ilya Leoshkevich's avatar Ilya Leoshkevich Committed by Daniel Borkmann

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: default avatarIlya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20191107100349.88976-1-iii@linux.ibm.com
parent 85d31dd0
...@@ -545,6 +545,16 @@ static void bpf_reduce_k_jumps(void) ...@@ -545,6 +545,16 @@ static void bpf_reduce_k_jumps(void)
} }
} }
static uint8_t bpf_encode_jt_jf_offset(int off, int i)
{
int delta = off - i - 1;
if (delta < 0 || delta > 255)
fprintf(stderr, "warning: insn #%d jumps to insn #%d, "
"which is out of range\n", i, off);
return (uint8_t) delta;
}
static void bpf_reduce_jt_jumps(void) static void bpf_reduce_jt_jumps(void)
{ {
int i; int i;
...@@ -552,7 +562,7 @@ static void bpf_reduce_jt_jumps(void) ...@@ -552,7 +562,7 @@ static void bpf_reduce_jt_jumps(void)
for (i = 0; i < curr_instr; i++) { for (i = 0; i < curr_instr; i++) {
if (labels_jt[i]) { if (labels_jt[i]) {
int off = bpf_find_insns_offset(labels_jt[i]); int off = bpf_find_insns_offset(labels_jt[i]);
out[i].jt = (uint8_t) (off - i -1); out[i].jt = bpf_encode_jt_jf_offset(off, i);
} }
} }
} }
...@@ -564,7 +574,7 @@ static void bpf_reduce_jf_jumps(void) ...@@ -564,7 +574,7 @@ static void bpf_reduce_jf_jumps(void)
for (i = 0; i < curr_instr; i++) { for (i = 0; i < curr_instr; i++) {
if (labels_jf[i]) { if (labels_jf[i]) {
int off = bpf_find_insns_offset(labels_jf[i]); int off = bpf_find_insns_offset(labels_jf[i]);
out[i].jf = (uint8_t) (off - i - 1); out[i].jf = bpf_encode_jt_jf_offset(off, i);
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment