rlane / ubpf

Userspace eBPF VM (main repo is https://github.com/iovisor/ubpf)
Apache License 2.0
92 stars 144 forks source link

Functions with char * arguments #2

Open GerardGarcia opened 8 years ago

GerardGarcia commented 8 years ago

I don't know if that is a limitation of the eBPF instruction set or a limitation of the ubpf virtual machine but seems that is not possible to load programs that use external functions that have arguments type char *. Clang can compile the programs correctly but when loaded the ubp_load_elf functions exits with the error: Fialed to load code: bad relocation type.

If it is not a limitation of the eBPF instruction set I could try to look into it.

rlane commented 8 years ago

This should be doable. Right now the ELF loader only loads the text sections but it could do the same for data/rodata/bss.

A simple testcase:

extern int strcmp_ext(const char *a, const char *b);

int entry(int *mem)
{
    return strcmp_ext("abcx", "abcy");
}

produces this assembly:

lddw r1, 0x0
lddw r2, 0x0
call 0x0
exit

and a few relocations:

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE 
0000000000000000 UNKNOWN           .L.str
0000000000000010 UNKNOWN           .L.str.1
0000000000000020 UNKNOWN           strcmp_ext

The relocation type is 1. This could mean "absolute 64-bit".

GerardGarcia commented 8 years ago

I guess then that the simplest way would be to just keep the loaded strings form the data segment stored somewhere in the vm struct and then reference them into the lddw instructions when relocating. But although this would work, I'm not sure this would be legal eBPF code as the verifier should not allow arbitrary pointers.

iovisor/bcc tools first write the strings into the stack and then the stack address pointing to the string is passed to the external function but this involves generating code and seems that they have modified Clang/LLVM to achieve this.