pwru icon indicating copy to clipboard operation
pwru copied to clipboard

introduce builtin memcmp etc., like bpf in cilium

Open duanjiong opened this issue 4 years ago • 10 comments

When writing bpf programs, you may need to use some basic memory operations, such as memcmp, etc.

https://github.com/cilium/cilium/blob/master/bpf/include/bpf/builtins.h

duanjiong avatar Oct 25 '21 01:10 duanjiong

In Cilium those builtins are useful to (1) reduce the BPF complexity and (2) reduce runtime overhead. I doubt (1) is an issue for pwru today and it's not clear if (2) would be noticeable. In Cilium's case, (1) is regularly an issue, but we didn't measure the performance difference for (2).

pchaigno avatar Oct 25 '21 09:10 pchaigno

I think for now we could rely on __builtin_memcmp().

brb avatar Oct 25 '21 09:10 brb

Yes, I introduced this earlier, but I had a problem compiling it, so I removed it. I can see how to fix it later.

duanjiong avatar Oct 25 '21 09:10 duanjiong

Rewrite addr_equal using __builtin_memcmp as follows

static __always_inline bool
addr_equal(union addr a, u8 b[16]) {
	if (__builtin_memcmp(&a, b, sizeof(a)) != 0) {
	    return false;
	}
	return true;
}

After that, go generate works fine, and then the following error is reported when executing pwru

2021/10/26 15:36:24 Loading objects: field KprobeSkb1: program kprobe_skb_1: call at 367: reference to missing symbol "memcmp"

Looking at the symbol table by executing readelf, I found that __builtin_memcmp is not inlined

[duanjiong@fedora pwru]$ readelf -s  kprobepwru_bpfeb.o
 158: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND memcmp

My version of clang is as follows

[duanjiong@fedora pwru]$ clang --version
clang version 12.0.1 (Fedora 12.0.1-1.fc34)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

duanjiong avatar Oct 26 '21 07:10 duanjiong

Clang only supports those builtins in specific cases: https://clang.llvm.org/docs/LanguageExtensions.html#string-builtins.

pchaigno avatar Oct 26 '21 12:10 pchaigno

I encountered the same problem. __builtin_memcmp does not work any more, it was replaced by memcmp.

[root@host file_read]# bpftool prog load bpf_test.o /sys/fs/bpf/bpf_test
libbpf: elf: skipping unrecognized data section(5) .rodata.str1.1
libbpf: failed to find BTF for extern 'memcmp': -3
Error: failed to open object file
[root@host  file_read]# readelf -s bpf_test.o

Symbol table '.symtab' contains 6 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS bpf_test.c
     2: 00000000000000e0     0 NOTYPE  LOCAL  DEFAULT    3 LBB0_3
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    5
     4: 0000000000000000   240 FUNC    GLOBAL DEFAULT    3 kretprobe_sys_read
     5: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND memcmp
[root@host file_read]#

chenk008 avatar Jun 02 '22 02:06 chenk008

How do I use memcmp in a BPF program? What header would I include? I get the same error message as above @chenk008

thealexcons avatar Feb 02 '23 14:02 thealexcons

@thealexcons same issue for me. __builtin_memcmp is replaced by memcmp by clang and bpf loading fail. any update here?

mrpre avatar Feb 23 '23 11:02 mrpre

Is this related to https://github.com/llvm/llvm-project/issues/26592 ?

zouyonghao avatar Apr 18 '23 08:04 zouyonghao