summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/verifier_aggregate_ret.c
blob: f5c1166027a1dd9ecb06da983074ac8e9177119b (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "../test_kmods/bpf_testmod_kfunc.h"
#include "bpf_misc.h"

#if defined(__clang_major__) && __clang_major__ >= 23

#define MIX_A	0xdeadbeefcafef00dULL
#define MIX_B	0x0123456789abcdefULL

typedef unsigned __int128 u128;

struct pair {
	__u64 lo;	/* R0 */
	__u64 hi;	/* R2 */
};

union upair {
	__u64 halves[2];
	struct {
		__u64 lo;	/* R0 */
		__u64 hi;	/* R2 */
	} parts;
};

static __noinline u128 make_i128(__u64 a, __u64 b)
{
	return ((u128)(a + b) << 64) | (a - b);
}

SEC("tc")
__load_if_JITed()
__success __retval(0)
int aggregate_ret_int128_c_test(struct __sk_buff *skb)
{
	__u64 a = skb->len ^ MIX_A;
	__u64 b = skb->len ^ MIX_B;
	u128 v;

	v = make_i128(a, b);
	if ((__u64)(v >> 64) != a + b)
		return 1;
	if ((__u64)v != a - b)
		return 2;

	return 0;
}

static __noinline struct pair make_pair(__u64 a, __u64 b)
{
	struct pair p = { .lo = a + b, .hi = a - b };

	return p;
}

SEC("tc")
__load_if_JITed()
__success __retval(0)
int aggregate_ret_struct_c_test(struct __sk_buff *skb)
{
	__u64 a = skb->len ^ MIX_A;
	__u64 b = skb->len ^ MIX_B;
	struct pair p;

	p = make_pair(a, b);
	if (p.lo != a + b)
		return 1;
	if (p.hi != a - b)
		return 2;

	return 0;
}

__noinline struct pair make_pair_global(__u64 a, __u64 b)
{
	struct pair p = { .lo = a + b, .hi = a - b };

	return p;
}

SEC("tc")
__load_if_JITed()
__success __retval(0)
int aggregate_ret_global_struct_c_test(struct __sk_buff *skb)
{
	__u64 a = skb->len ^ MIX_A;
	__u64 b = skb->len ^ MIX_B;
	struct pair p;

	p = make_pair_global(a, b);
	if (p.lo != a + b)
		return 1;
	if (p.hi != a - b)
		return 2;

	return 0;
}

static __noinline union upair make_upair(__u64 a, __u64 b)
{
	union upair p;

	p.halves[0] = a + b;
	p.halves[1] = a - b;
	return p;
}

SEC("tc")
__load_if_JITed()
__success __retval(0)
int aggregate_ret_union_c_test(struct __sk_buff *skb)
{
	__u64 a = skb->len ^ MIX_A;
	__u64 b = skb->len ^ MIX_B;
	union upair p;

	p = make_upair(a, b);
	if (p.parts.lo != a + b)
		return 1;
	if (p.parts.hi != a - b)
		return 2;

	return 0;
}

SEC("tc")
__arch_x86_64 __arch_arm64
__load_if_JITed()
__success __retval(0)
int aggregate_ret_kfunc_int128_c_test(struct __sk_buff *skb)
{
	__u64 a = skb->len ^ MIX_A;
	__u64 b = skb->len ^ MIX_B;
	u128 v;

	v = bpf_kfunc_call_test_i128(a, b);
	if ((__u64)(v >> 64) != a + b)
		return 1;
	if ((__u64)v != a - b)
		return 2;

	return 0;
}

SEC("tc")
__arch_x86_64 __arch_arm64
__load_if_JITed()
__success __retval(0)
int aggregate_ret_kfunc_struct_c_test(struct __sk_buff *skb)
{
	__u64 a = skb->len ^ MIX_A;
	__u64 b = skb->len ^ MIX_B;
	struct prog_test_ret_pair p;

	p = bpf_kfunc_call_test_ret_pair(a, b);
	if (p.lo != a + b)
		return 1;
	if (p.hi != a - b)
		return 2;

	return 0;
}

#else

SEC("socket")
__description("verifier_aggregate_ret: needs LLVM 23, dummy test")
__skip("needs LLVM 23")
__success
int dummy_test(void)
{
	return 0;
}

#endif

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