diff mbox series

[bpf-next,v2,2/2] selftests/bpf: introduce veristat test

Message ID 20250210135129.719119-3-mykyta.yatsenko5@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series selftests/bpf: implement setting global variables in veristat | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success Errors and warnings before: 26 (+1) this patch: 26 (+1)
netdev/cc_maintainers warning 11 maintainers not CCed: kpsingh@kernel.org sdf@fomichev.me jolsa@kernel.org yonghong.song@linux.dev song@kernel.org shuah@kernel.org john.fastabend@gmail.com linux-kselftest@vger.kernel.org haoluo@google.com mykolal@fb.com martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 47 this patch: 47
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 10 this patch: 10
netdev/checkpatch warning WARNING: Avoid unnecessary line continuations WARNING: Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 82 exceeds 80 columns WARNING: line length of 88 exceeds 80 columns WARNING: line length of 89 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-11 success Logs for aarch64-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-12 success Logs for aarch64-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-18 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for s390x-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-20 success Logs for s390x-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-21 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-50 success Logs for x86_64-llvm-18 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-51 success Logs for x86_64-llvm-18 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-17 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-44 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-17 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-43 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 fail Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 fail Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-gcc / veristat-meta / x86_64-gcc veristat_meta
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-gcc / veristat-kernel / x86_64-gcc veristat_kernel
bpf/vmtest-bpf-next-VM_Test-49 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-45 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 fail Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-48 fail Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-38 fail Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-47 fail Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-46 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18

Commit Message

Mykyta Yatsenko Feb. 10, 2025, 1:51 p.m. UTC
From: Mykyta Yatsenko <yatsenko@meta.com>

Introducing test for veristat, part of test_progs.
Test cases cover functionality of setting global variables in BPF
program.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
 tools/testing/selftests/bpf/Makefile          |   1 +
 .../selftests/bpf/prog_tests/test_veristat.c  | 122 ++++++++++++++++++
 .../selftests/bpf/progs/set_global_vars.c     |  47 +++++++
 3 files changed, 170 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_veristat.c
 create mode 100644 tools/testing/selftests/bpf/progs/set_global_vars.c

Comments

Eduard Zingerman Feb. 11, 2025, 3:01 a.m. UTC | #1
On Mon, 2025-02-10 at 13:51 +0000, Mykyta Yatsenko wrote:

[...]

> +void test_veristat_set_global_vars_succeeds(void)
> +{

test_progs based tests are usually organized as a hierarchy of tests and sub-tests.
E.g. take a look at tools/testing/selftests/bpf/prog_tests/ksyms_btf.c:
- it defines an entry point test_ksyms_btf;
- and a bunch of sub-tests declared as static void functions,
  called from entry point;
- test__start_subtest() function is used to check if sub-test has to
  be executed.

> +	char command[512];
> +	struct fixture *fix = init_fixture();
> +
> +	snprintf(command, sizeof(command),
> +		 "./veristat set_global_vars.bpf.o"\
> +		 " -G \"var_s64 = 0xf000000000000001\" "\
> +		 " -G \"var_u64 = 0xfedcba9876543210\" "\
> +		 " -G \"var_s32 = -0x80000000\" "\
> +		 " -G \"var_u32 = 0x76543210\" "\
> +		 " -G \"var_s16 = -32768\" "\
> +		 " -G \"var_u16 = 60652\" "\
> +		 " -G \"var_s8 = -128\" "\
> +		 " -G \"var_u8 = 255\" "\
> +		 " -G \"var_ea = EA2\" "\
> +		 " -G \"var_eb = EB2\" "\
> +		 " -G \"var_ec = EC2\" "\
> +		 " -G \"var_b = 1\" "\
> +		 "-vl2 > %s", fix->tmpfile);
> +	if (!ASSERT_EQ(0, system(command), "command"))
> +		goto out;

Nit: there is SYS macro in test_progs.h, it combines
     snprintf/system/ASSERT_OK/goto.

> +
> +	read(fix->fd, fix->output, fix->sz);

Nit: check error for read() call (same read()/write() in tests below).

> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xf000000000000001 "),
> +		   "var_s64 = 0xf000000000000001");

Nit: I'd do these checks as below:

#define __CHECK_STR(str, name) \
	if (!ASSERT_HAS_SUBSTR(fix->output, (str), (str))) goto out
        __CHECK_STR("_w=0xf000000000000001 ");
        ...
#undef __CHECK_STR

     this way fix->output would be printed if sub-string is not found.
     For other tests I suggest using ASSERT_HAS_SUBSTR as well,
     as it prints the string where sub-string was looked for.

> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xfedcba9876543210 "),
> +		   "var_u64 = 0xfedcba9876543210");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x80000000 "), "var_s32 = -0x80000000");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x76543210 "), "var_u32 = 0x76543210");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x8000 "), "var_s16 = -32768");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xecec "), "var_u16 = 60652");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=128 "), "var_s8 = -128");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=255 "), "var_u8 = 255");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=11 "), "var_ea = EA2");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=12 "), "var_eb = EB2");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=13 "), "var_ec = EC2");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=1 "), "var_b = 1");
> +
> +out:
> +	teardown_fixture(fix);
> +}
> +
> +void test_veristat_set_global_vars_from_file_succeeds(void)
> +{
> +	struct fixture *fix = init_fixture();
> +	char command[512];
> +	char input_file[80];
> +	const char *vars = "var_s16 = -32768\nvar_u16 = 60652";
> +	int fd;
> +
> +	snprintf(input_file, sizeof(input_file), "/tmp/veristat_input.XXXXXX");
> +	fd = mkstemp(input_file);
> +	if (!ASSERT_GT(fd, 0, "valid fd"))

Nit: ASSERT_GE.

> +		goto out;
> +
> +	write(fd, vars, strlen(vars));
> +	snprintf(command, sizeof(command),
> +		 "./veristat set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
> +		 input_file, fix->tmpfile);
> +
> +	ASSERT_EQ(0, system(command), "command");
> +	read(fix->fd, fix->output, fix->sz);
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x8000 "), "var_s16 = -32768");
> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xecec "), "var_u16 = 60652");
> +
> +out:
> +	close(fd);
> +	remove(input_file);
> +	teardown_fixture(fix);
> +}

[...]
Mykyta Yatsenko Feb. 11, 2025, 3:09 p.m. UTC | #2
On 11/02/2025 03:01, Eduard Zingerman wrote:
> On Mon, 2025-02-10 at 13:51 +0000, Mykyta Yatsenko wrote:
>
> [...]
>
>> +void test_veristat_set_global_vars_succeeds(void)
>> +{
> test_progs based tests are usually organized as a hierarchy of tests and sub-tests.
> E.g. take a look at tools/testing/selftests/bpf/prog_tests/ksyms_btf.c:
> - it defines an entry point test_ksyms_btf;
> - and a bunch of sub-tests declared as static void functions,
>    called from entry point;
> - test__start_subtest() function is used to check if sub-test has to
>    be executed.
>
>> +	char command[512];
>> +	struct fixture *fix = init_fixture();
>> +
>> +	snprintf(command, sizeof(command),
>> +		 "./veristat set_global_vars.bpf.o"\
>> +		 " -G \"var_s64 = 0xf000000000000001\" "\
>> +		 " -G \"var_u64 = 0xfedcba9876543210\" "\
>> +		 " -G \"var_s32 = -0x80000000\" "\
>> +		 " -G \"var_u32 = 0x76543210\" "\
>> +		 " -G \"var_s16 = -32768\" "\
>> +		 " -G \"var_u16 = 60652\" "\
>> +		 " -G \"var_s8 = -128\" "\
>> +		 " -G \"var_u8 = 255\" "\
>> +		 " -G \"var_ea = EA2\" "\
>> +		 " -G \"var_eb = EB2\" "\
>> +		 " -G \"var_ec = EC2\" "\
>> +		 " -G \"var_b = 1\" "\
>> +		 "-vl2 > %s", fix->tmpfile);
>> +	if (!ASSERT_EQ(0, system(command), "command"))
>> +		goto out;
> Nit: there is SYS macro in test_progs.h, it combines
>       snprintf/system/ASSERT_OK/goto.
>
>> +
>> +	read(fix->fd, fix->output, fix->sz);
> Nit: check error for read() call (same read()/write() in tests below).
>
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xf000000000000001 "),
>> +		   "var_s64 = 0xf000000000000001");
> Nit: I'd do these checks as below:
>
> #define __CHECK_STR(str, name) \
> 	if (!ASSERT_HAS_SUBSTR(fix->output, (str), (str))) goto out
>          __CHECK_STR("_w=0xf000000000000001 ");
>          ...
> #undef __CHECK_STR
>
>       this way fix->output would be printed if sub-string is not found.
>       For other tests I suggest using ASSERT_HAS_SUBSTR as well,
>       as it prints the string where sub-string was looked for.
>
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xfedcba9876543210 "),
>> +		   "var_u64 = 0xfedcba9876543210");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x80000000 "), "var_s32 = -0x80000000");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x76543210 "), "var_u32 = 0x76543210");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x8000 "), "var_s16 = -32768");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xecec "), "var_u16 = 60652");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=128 "), "var_s8 = -128");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=255 "), "var_u8 = 255");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=11 "), "var_ea = EA2");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=12 "), "var_eb = EB2");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=13 "), "var_ec = EC2");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=1 "), "var_b = 1");
>> +
>> +out:
>> +	teardown_fixture(fix);
>> +}
>> +
>> +void test_veristat_set_global_vars_from_file_succeeds(void)
>> +{
>> +	struct fixture *fix = init_fixture();
>> +	char command[512];
>> +	char input_file[80];
>> +	const char *vars = "var_s16 = -32768\nvar_u16 = 60652";
>> +	int fd;
>> +
>> +	snprintf(input_file, sizeof(input_file), "/tmp/veristat_input.XXXXXX");
>> +	fd = mkstemp(input_file);
>> +	if (!ASSERT_GT(fd, 0, "valid fd"))
> Nit: ASSERT_GE.
>
>> +		goto out;
>> +
>> +	write(fd, vars, strlen(vars));
>> +	snprintf(command, sizeof(command),
>> +		 "./veristat set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
>> +		 input_file, fix->tmpfile);
>> +
>> +	ASSERT_EQ(0, system(command), "command");
>> +	read(fix->fd, fix->output, fix->sz);
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x8000 "), "var_s16 = -32768");
>> +	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xecec "), "var_u16 = 60652");
>> +
>> +out:
>> +	close(fd);
>> +	remove(input_file);
>> +	teardown_fixture(fix);
>> +}
> [...]
Thanks, will update this patch accordingly.
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 87551628e112..c300190c59ae 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -684,6 +684,7 @@  $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
 			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
 			     $(RESOLVE_BTFIDS)				\
 			     $(TRUNNER_BPFTOOL)				\
+				 $(OUTPUT)/veristat				\
 			     | $(TRUNNER_BINARY)-extras
 	$$(call msg,BINARY,,$$@)
 	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) $$(LDFLAGS) -o $$@
diff --git a/tools/testing/selftests/bpf/prog_tests/test_veristat.c b/tools/testing/selftests/bpf/prog_tests/test_veristat.c
new file mode 100644
index 000000000000..df7d0f291c7e
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_veristat.c
@@ -0,0 +1,122 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include <string.h>
+#include <stdio.h>
+
+struct fixture {
+	char tmpfile[80];
+	int fd;
+	char *output;
+	size_t sz;
+};
+
+static struct fixture *init_fixture(void)
+{
+	struct fixture *fix = malloc(sizeof(struct fixture));
+
+	snprintf(fix->tmpfile, sizeof(fix->tmpfile), "/tmp/test_veristat.XXXXXX");
+	fix->fd = mkstemp(fix->tmpfile);
+	fix->sz = 1000000;
+	fix->output = malloc(fix->sz);
+	return fix;
+}
+
+static void teardown_fixture(struct fixture *fix)
+{
+	free(fix->output);
+	close(fix->fd);
+	remove(fix->tmpfile);
+	free(fix);
+}
+
+void test_veristat_set_global_vars_succeeds(void)
+{
+	char command[512];
+	struct fixture *fix = init_fixture();
+
+	snprintf(command, sizeof(command),
+		 "./veristat set_global_vars.bpf.o"\
+		 " -G \"var_s64 = 0xf000000000000001\" "\
+		 " -G \"var_u64 = 0xfedcba9876543210\" "\
+		 " -G \"var_s32 = -0x80000000\" "\
+		 " -G \"var_u32 = 0x76543210\" "\
+		 " -G \"var_s16 = -32768\" "\
+		 " -G \"var_u16 = 60652\" "\
+		 " -G \"var_s8 = -128\" "\
+		 " -G \"var_u8 = 255\" "\
+		 " -G \"var_ea = EA2\" "\
+		 " -G \"var_eb = EB2\" "\
+		 " -G \"var_ec = EC2\" "\
+		 " -G \"var_b = 1\" "\
+		 "-vl2 > %s", fix->tmpfile);
+	if (!ASSERT_EQ(0, system(command), "command"))
+		goto out;
+
+	read(fix->fd, fix->output, fix->sz);
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xf000000000000001 "),
+		   "var_s64 = 0xf000000000000001");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xfedcba9876543210 "),
+		   "var_u64 = 0xfedcba9876543210");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x80000000 "), "var_s32 = -0x80000000");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x76543210 "), "var_u32 = 0x76543210");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x8000 "), "var_s16 = -32768");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xecec "), "var_u16 = 60652");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=128 "), "var_s8 = -128");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=255 "), "var_u8 = 255");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=11 "), "var_ea = EA2");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=12 "), "var_eb = EB2");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=13 "), "var_ec = EC2");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=1 "), "var_b = 1");
+
+out:
+	teardown_fixture(fix);
+}
+
+void test_veristat_set_global_vars_from_file_succeeds(void)
+{
+	struct fixture *fix = init_fixture();
+	char command[512];
+	char input_file[80];
+	const char *vars = "var_s16 = -32768\nvar_u16 = 60652";
+	int fd;
+
+	snprintf(input_file, sizeof(input_file), "/tmp/veristat_input.XXXXXX");
+	fd = mkstemp(input_file);
+	if (!ASSERT_GT(fd, 0, "valid fd"))
+		goto out;
+
+	write(fd, vars, strlen(vars));
+	snprintf(command, sizeof(command),
+		 "./veristat set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
+		 input_file, fix->tmpfile);
+
+	ASSERT_EQ(0, system(command), "command");
+	read(fix->fd, fix->output, fix->sz);
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x8000 "), "var_s16 = -32768");
+	ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xecec "), "var_u16 = 60652");
+
+out:
+	close(fd);
+	remove(input_file);
+	teardown_fixture(fix);
+}
+
+void test_veristat_set_global_vars_out_of_range(void)
+{
+	struct fixture *fix = init_fixture();
+	char command[512];
+
+	snprintf(command, sizeof(command),
+		 "./veristat set_global_vars.bpf.o -G \"var_s32 = 2147483648\" -vl2 2> %s",
+		 fix->tmpfile);
+
+	if (!ASSERT_NEQ(0, system(command), "command"))
+		goto out;
+
+	read(fix->fd, fix->output, fix->sz);
+	ASSERT_NEQ(NULL, strstr(fix->output, "is out of range [-2147483648; 2147483647]"),
+		   "out of range");
+out:
+	teardown_fixture(fix);
+}
diff --git a/tools/testing/selftests/bpf/progs/set_global_vars.c b/tools/testing/selftests/bpf/progs/set_global_vars.c
new file mode 100644
index 000000000000..9adb5ba4cd4d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/set_global_vars.c
@@ -0,0 +1,47 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include "bpf_experimental.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include <stdbool.h>
+
+char _license[] SEC("license") = "GPL";
+
+enum Enum { EA1 = 0, EA2 = 11 };
+enum Enumu64 {EB1 = 0llu, EB2 = 12llu };
+enum Enums64 { EC1 = 0ll, EC2 = 13ll };
+
+const volatile __s64 var_s64 = -1;
+const volatile __u64 var_u64 = 0;
+const volatile __s32 var_s32 = -1;
+const volatile __u32 var_u32 = 0;
+const volatile __s16 var_s16 = -1;
+const volatile __u16 var_u16 = 0;
+const volatile __s8 var_s8 = -1;
+const volatile __u8 var_u8 = 0;
+const volatile enum Enum var_ea = EA1;
+const volatile enum Enumu64 var_eb = EB1;
+const volatile enum Enums64 var_ec = EC1;
+const volatile bool var_b = false;
+
+char arr[4] = {0};
+
+SEC("socket")
+int test_set_globals(void *ctx)
+{
+	volatile __s8 a;
+
+	a = var_s64;
+	a = var_u64;
+	a = var_s32;
+	a = var_u32;
+	a = var_s16;
+	a = var_u16;
+	a = var_s8;
+	a = var_u8;
+	a = var_ea;
+	a = var_eb;
+	a = var_ec;
+	a = var_b;
+	return a;
+}