Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce builtin memcmp etc., like bpf in cilium #32

Open
duanjiong opened this issue Oct 25, 2021 · 10 comments
Open

introduce builtin memcmp etc., like bpf in cilium #32

duanjiong opened this issue Oct 25, 2021 · 10 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@duanjiong
Copy link
Contributor

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

@pchaigno
Copy link
Member

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).

@brb
Copy link
Member

brb commented Oct 25, 2021

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

@duanjiong
Copy link
Contributor Author

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
Copy link
Contributor Author

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

@pchaigno
Copy link
Member

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

@brb brb added enhancement New feature or request help wanted Extra attention is needed labels Jan 24, 2022
@chenk008
Copy link

chenk008 commented Jun 2, 2022

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]#

@thealexcons
Copy link

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

@mrpre
Copy link

mrpre commented Feb 23, 2023

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

@zouyonghao
Copy link

Is this related to llvm/llvm-project#26592 ?

@bobrik
Copy link

bobrik commented May 25, 2024

I am fairly sure that memcmp is fixed by using clang 15 or newer:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

8 participants