1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 
4 #include <linux/bpf.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7 #include <bpf/bpf_helpers.h>
8 #include <bpf/bpf_core_read.h>
9 
10 char _license[] SEC("license") = "GPL";
11 
12 struct {
13 	char in[256];
14 	char out[256];
15 	bool skip;
16 } data = {};
17 
18 enum named_unsigned_enum64 {
19 	UNSIGNED_ENUM64_VAL1 = 0x1ffffffffULL,
20 	UNSIGNED_ENUM64_VAL2 = 0x2ffffffffULL,
21 	UNSIGNED_ENUM64_VAL3 = 0x3ffffffffULL,
22 };
23 
24 enum named_signed_enum64 {
25 	SIGNED_ENUM64_VAL1 = 0x1ffffffffLL,
26 	SIGNED_ENUM64_VAL2 = -2,
27 	SIGNED_ENUM64_VAL3 = 0x3ffffffffLL,
28 };
29 
30 struct core_reloc_enum64val_output {
31 	bool unsigned_val1_exists;
32 	bool unsigned_val2_exists;
33 	bool unsigned_val3_exists;
34 	bool signed_val1_exists;
35 	bool signed_val2_exists;
36 	bool signed_val3_exists;
37 
38 	long unsigned_val1;
39 	long unsigned_val2;
40 	long signed_val1;
41 	long signed_val2;
42 };
43 
44 SEC("raw_tracepoint/sys_enter")
test_core_enum64val(void * ctx)45 int test_core_enum64val(void *ctx)
46 {
47 #if __clang_major__ >= 15
48 	struct core_reloc_enum64val_output *out = (void *)&data.out;
49 	enum named_unsigned_enum64 named_unsigned = 0;
50 	enum named_signed_enum64 named_signed = 0;
51 
52 	out->unsigned_val1_exists = bpf_core_enum_value_exists(named_unsigned, UNSIGNED_ENUM64_VAL1);
53 	out->unsigned_val2_exists = bpf_core_enum_value_exists(enum named_unsigned_enum64, UNSIGNED_ENUM64_VAL2);
54 	out->unsigned_val3_exists = bpf_core_enum_value_exists(enum named_unsigned_enum64, UNSIGNED_ENUM64_VAL3);
55 	out->signed_val1_exists = bpf_core_enum_value_exists(named_signed, SIGNED_ENUM64_VAL1);
56 	out->signed_val2_exists = bpf_core_enum_value_exists(enum named_signed_enum64, SIGNED_ENUM64_VAL2);
57 	out->signed_val3_exists = bpf_core_enum_value_exists(enum named_signed_enum64, SIGNED_ENUM64_VAL3);
58 
59 	out->unsigned_val1 = bpf_core_enum_value(named_unsigned, UNSIGNED_ENUM64_VAL1);
60 	out->unsigned_val2 = bpf_core_enum_value(named_unsigned, UNSIGNED_ENUM64_VAL2);
61 	out->signed_val1 = bpf_core_enum_value(named_signed, SIGNED_ENUM64_VAL1);
62 	out->signed_val2 = bpf_core_enum_value(named_signed, SIGNED_ENUM64_VAL2);
63 	/* NAMED_ENUM64_VAL3 value is optional */
64 
65 #else
66 	data.skip = true;
67 #endif
68 
69 	return 0;
70 }
71