iovisor / iovisor/bcc

"invalid BPF_LD_IMM insn" when accessing array using data from a map

Open
#601 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
22.7k
Forks
4.1k
Avg merge
10d 4h
Merged PRs (30d)
3

Description

Hi,

I stumbled over the following issue, the compiler seems to create an invalid opcode for the following:

```
from bcc import BPF

# load BPF program
b = BPF(text = """

struct Key {
unsigned char p[64];
};

struct Leaf {
uint16_t len;
};

BPF_TABLE("hash", struct Key, struct Leaf, store, 128);

void test(struct __sk_buff* skb) {
struct Key key;
__builtin_memset(&key, 0, sizeof(key));

struct Leaf * lookup_leaf = store.lookup(&key);
if(lookup_leaf) {
uint8_t lookup_list[32] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,7,8,19,20,21,22,23,24,25,26,27,28,29,30,31};
if(lookup_leaf->len < 32) {
uint16_t elem_len = lookup_leaf->len;

uint8_t look = lookup_list[elem_len];

bpf_trace_printk("array value is %d\\n", look);
}
}
}

""")
f = open("test.o", "w+")
f.write(b.dump_func("test"))
f.close()

b.load_func("test", BPF.SCHED_ACT)
```

I want to use lookup_leaf->len to index the static array, however if I try to load it I get a:

```
bpf: Invalid argument
0: (b7) r1 = 0
1: (7b) *(u64 *)(r10 -8) = r1
2: (7b) *(u64 *)(r10 -16) = r1
3: (7b) *(u64 *)(r10 -24) = r1
4: (7b) *(u64 *)(r10 -32) = r1
5: (7b) *(u64 *)(r10 -40) = r1
6: (7b) *(u64 *)(r10 -48) = r1
7: (7b) *(u64 *)(r10 -56) = r1
8: (7b) *(u64 *)(r10 -64) = r1
9: (18) r1 = 0xb61b3080
11: (bf) r2 = r10
12: (07) r2 += -64
13: (85) call 1
14: (15) if r0 == 0x0 goto pc+17
R0=map_value(ks=64,vs=2) R10=fp
15: (69) r1 = *(u16 *)(r0 +0)
16: (25) if r1 > 0x1f goto pc+15
R0=map_value(ks=64,vs=2) R1=inv R10=fp
17: (b7) r2 = 0
18: (73) *(u8 *)(r10 -70) = r2
19: (b7) r2 = 2660
20: (6b) *(u16 *)(r10 -72) = r2
21: (18) r2 = 0x636e7973
23: (7b) *(u64 *)(r10 -80) = r2
24: (00) r0 = 0x0
invalid BPF_LD_IMM insn
```

When inspecting the produced ebpf code I see that there is indeed an opcode 00 at that address.
Here ins 23 and 24 taken from dump_func():

```
7B2AB0FF 00000000 *00*70B24F 00000000
```

Why is this happening? Is it impossible to index an array from a map item?

Thanks!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.