1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
4 #include <string.h>
5 #include <stdio.h>
6
7 #define __CHECK_STR(str, name) \
8 do { \
9 if (!ASSERT_HAS_SUBSTR(fix->output, (str), (name))) \
10 goto out; \
11 } while (0)
12
13 struct fixture {
14 char tmpfile[80];
15 int fd;
16 char *output;
17 size_t sz;
18 char veristat[80];
19 };
20
init_fixture(void)21 static struct fixture *init_fixture(void)
22 {
23 struct fixture *fix = malloc(sizeof(struct fixture));
24
25 /* for no_alu32 and cpuv4 veristat is in parent folder */
26 if (access("./veristat", F_OK) == 0)
27 strcpy(fix->veristat, "./veristat");
28 else if (access("../veristat", F_OK) == 0)
29 strcpy(fix->veristat, "../veristat");
30 else
31 PRINT_FAIL("Can't find veristat binary");
32
33 snprintf(fix->tmpfile, sizeof(fix->tmpfile), "/tmp/test_veristat.XXXXXX");
34 fix->fd = mkstemp(fix->tmpfile);
35 fix->sz = 1000000;
36 fix->output = malloc(fix->sz);
37 return fix;
38 }
39
teardown_fixture(struct fixture * fix)40 static void teardown_fixture(struct fixture *fix)
41 {
42 free(fix->output);
43 close(fix->fd);
44 remove(fix->tmpfile);
45 free(fix);
46 }
47
test_set_global_vars_succeeds(void)48 static void test_set_global_vars_succeeds(void)
49 {
50 struct fixture *fix = init_fixture();
51
52 SYS(out,
53 "%s set_global_vars.bpf.o"\
54 " -G \"var_s64 = 0xf000000000000001\" "\
55 " -G \"var_u64 = 0xfedcba9876543210\" "\
56 " -G \"var_s32 = -0x80000000\" "\
57 " -G \"var_u32 = 0x76543210\" "\
58 " -G \"var_s16 = -32768\" "\
59 " -G \"var_u16 = 60652\" "\
60 " -G \"var_s8 = -128\" "\
61 " -G \"var_u8 = 255\" "\
62 " -G \"var_ea = EA2\" "\
63 " -G \"var_eb = EB2\" "\
64 " -G \"var_ec = EC2\" "\
65 " -G \"var_b = 1\" "\
66 "-vl2 > %s", fix->veristat, fix->tmpfile);
67
68 read(fix->fd, fix->output, fix->sz);
69 __CHECK_STR("_w=0xf000000000000001 ", "var_s64 = 0xf000000000000001");
70 __CHECK_STR("_w=0xfedcba9876543210 ", "var_u64 = 0xfedcba9876543210");
71 __CHECK_STR("_w=0x80000000 ", "var_s32 = -0x80000000");
72 __CHECK_STR("_w=0x76543210 ", "var_u32 = 0x76543210");
73 __CHECK_STR("_w=0x8000 ", "var_s16 = -32768");
74 __CHECK_STR("_w=0xecec ", "var_u16 = 60652");
75 __CHECK_STR("_w=128 ", "var_s8 = -128");
76 __CHECK_STR("_w=255 ", "var_u8 = 255");
77 __CHECK_STR("_w=11 ", "var_ea = EA2");
78 __CHECK_STR("_w=12 ", "var_eb = EB2");
79 __CHECK_STR("_w=13 ", "var_ec = EC2");
80 __CHECK_STR("_w=1 ", "var_b = 1");
81
82 out:
83 teardown_fixture(fix);
84 }
85
test_set_global_vars_from_file_succeeds(void)86 static void test_set_global_vars_from_file_succeeds(void)
87 {
88 struct fixture *fix = init_fixture();
89 char input_file[80];
90 const char *vars = "var_s16 = -32768\nvar_u16 = 60652";
91 int fd;
92
93 snprintf(input_file, sizeof(input_file), "/tmp/veristat_input.XXXXXX");
94 fd = mkstemp(input_file);
95 if (!ASSERT_GE(fd, 0, "valid fd"))
96 goto out;
97
98 write(fd, vars, strlen(vars));
99 syncfs(fd);
100 SYS(out, "%s set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
101 fix->veristat, input_file, fix->tmpfile);
102 read(fix->fd, fix->output, fix->sz);
103 __CHECK_STR("_w=0x8000 ", "var_s16 = -32768");
104 __CHECK_STR("_w=0xecec ", "var_u16 = 60652");
105
106 out:
107 close(fd);
108 remove(input_file);
109 teardown_fixture(fix);
110 }
111
test_set_global_vars_out_of_range(void)112 static void test_set_global_vars_out_of_range(void)
113 {
114 struct fixture *fix = init_fixture();
115
116 SYS_FAIL(out,
117 "%s set_global_vars.bpf.o -G \"var_s32 = 2147483648\" -vl2 2> %s",
118 fix->veristat, fix->tmpfile);
119
120 read(fix->fd, fix->output, fix->sz);
121 __CHECK_STR("is out of range [-2147483648; 2147483647]", "out of range");
122
123 out:
124 teardown_fixture(fix);
125 }
126
test_veristat(void)127 void test_veristat(void)
128 {
129 if (test__start_subtest("set_global_vars_succeeds"))
130 test_set_global_vars_succeeds();
131
132 if (test__start_subtest("set_global_vars_out_of_range"))
133 test_set_global_vars_out_of_range();
134
135 if (test__start_subtest("set_global_vars_from_file_succeeds"))
136 test_set_global_vars_from_file_succeeds();
137 }
138
139 #undef __CHECK_STR
140