summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/get_func_args_test.c
blob: e0f34a55e697f9134d8bc2c2c36826d57492a071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <errno.h>

char _license[] SEC("license") = "GPL";

__u64 test1_result = 0;
SEC("fentry/bpf_fentry_test1")
int BPF_PROG(test1)
{
	__u64 cnt = bpf_get_func_arg_cnt(ctx);
	__u64 a = 0, z = 0, ret = 0;
	__s64 err;

	test1_result = cnt == 1;

	/* valid arguments */
	err = bpf_get_func_arg(ctx, 0, &a);

	/* We need to cast access to traced function argument values with
	 * proper type cast, because trampoline uses type specific instruction
	 * to save it, like for 'int a' with 32-bit mov like:
	 *
	 *   mov %edi,-0x8(%rbp)
	 *
	 * so the upper 4 bytes are not zeroed.
	 */
	test1_result &= err == 0 && ((int) a == 1);

	/* not valid argument */
	err = bpf_get_func_arg(ctx, 1, &z);
	test1_result &= err == -EINVAL;

	/* return value fails in fentry */
	err = bpf_get_func_ret(ctx, &ret);
	test1_result &= err == -EOPNOTSUPP;
	return 0;
}

__u64 test2_result = 0;
SEC("fexit/bpf_fentry_test2")
int BPF_PROG(test2)
{
	__u64 cnt = bpf_get_func_arg_cnt(ctx);
	__u64 a = 0, b = 0, z = 0, ret = 0;
	__s64 err;

	test2_result = cnt == 2;

	/* valid arguments */
	err = bpf_get_func_arg(ctx, 0, &a);
	test2_result &= err == 0 && (int) a == 2;

	err = bpf_get_func_arg(ctx, 1, &b);
	test2_result &= err == 0 && b == 3;

	/* not valid argument */
	err = bpf_get_func_arg(ctx, 2, &z);
	test2_result &= err == -EINVAL;

	/* return value */
	err = bpf_get_func_ret(ctx, &ret);
	test2_result &= err == 0 && ret == 5;
	return 0;
}

__u64 test3_result = 0;
SEC("fmod_ret/bpf_modify_return_test")
int BPF_PROG(fmod_ret_test, int _a, int *_b, int _ret)
{
	__u64 cnt = bpf_get_func_arg_cnt(ctx);
	__u64 a = 0, b = 0, z = 0, ret = 0;
	__s64 err;

	test3_result = cnt == 2;

	/* valid arguments */
	err = bpf_get_func_arg(ctx, 0, &a);
	test3_result &= err == 0 && ((int) a == 1);

	err = bpf_get_func_arg(ctx, 1, &b);
	test3_result &= err == 0 && ((int *) b == _b);

	/* not valid argument */
	err = bpf_get_func_arg(ctx, 2, &z);
	test3_result &= err == -EINVAL;

	/* return value */
	err = bpf_get_func_ret(ctx, &ret);
	test3_result &= err == 0 && ret == 0;

	/* change return value, it's checked in fexit_test program */
	return 1234;
}

__u64 test4_result = 0;
SEC("fexit/bpf_modify_return_test")
int BPF_PROG(fexit_test, int _a, int *_b, int _ret)
{
	__u64 cnt = bpf_get_func_arg_cnt(ctx);
	__u64 a = 0, b = 0, z = 0, ret = 0;
	__s64 err;

	test4_result = cnt == 2;

	/* valid arguments */
	err = bpf_get_func_arg(ctx, 0, &a);
	test4_result &= err == 0 && ((int) a == 1);

	err = bpf_get_func_arg(ctx, 1, &b);
	test4_result &= err == 0 && ((int *) b == _b);

	/* not valid argument */
	err = bpf_get_func_arg(ctx, 2, &z);
	test4_result &= err == -EINVAL;

	/* return value */
	err = bpf_get_func_ret(ctx, &ret);
	test4_result &= err == 0 && ret == 1234;
	return 0;
}