1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021. Huawei Technologies Co., Ltd */
3 #include <linux/types.h>
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 #define STRNCMP_STR_SZ 4096
9 
10 /* Will be updated by benchmark before program loading */
11 const volatile unsigned int cmp_str_len = 1;
12 const char target[STRNCMP_STR_SZ];
13 
14 long hits = 0;
15 char str[STRNCMP_STR_SZ];
16 
17 char _license[] SEC("license") = "GPL";
18 
local_strncmp(const char * s1,unsigned int sz,const char * s2)19 static __always_inline int local_strncmp(const char *s1, unsigned int sz,
20 					 const char *s2)
21 {
22 	int ret = 0;
23 	unsigned int i;
24 
25 	for (i = 0; i < sz; i++) {
26 		/* E.g. 0xff > 0x31 */
27 		ret = (unsigned char)s1[i] - (unsigned char)s2[i];
28 		if (ret || !s1[i])
29 			break;
30 	}
31 
32 	return ret;
33 }
34 
35 SEC("tp/syscalls/sys_enter_getpgid")
strncmp_no_helper(void * ctx)36 int strncmp_no_helper(void *ctx)
37 {
38 	if (local_strncmp(str, cmp_str_len + 1, target) < 0)
39 		__sync_add_and_fetch(&hits, 1);
40 	return 0;
41 }
42 
43 SEC("tp/syscalls/sys_enter_getpgid")
strncmp_helper(void * ctx)44 int strncmp_helper(void *ctx)
45 {
46 	if (bpf_strncmp(str, cmp_str_len + 1, target) < 0)
47 		__sync_add_and_fetch(&hits, 1);
48 	return 0;
49 }
50 
51