Skip to contentSkip to conversation

selftests/bpf: implement setting global variables in veristat

PATCH Superseded Mykyta Yatsenko 2 participants 6 messages Feb 19, 2025 – Feb 20, 2025

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover selftests/bpf: implement setting global variables in veristat

From: Mykyta Yatsenko <[email protected]>

To better verify some complex BPF programs by veristat, it would be useful to preset global variables. This patch set implements this functionality and introduces tests for veristat.

v2->v3:
* Reworked parsing of the presets, using sscanf to split into variable and value, but
still use strtoll/strtoull to support range checks when parsing integers
* Fix test failures for no_alu32 & cpuv4 by checking if veristat binary is in parent folder
* Introduce __CHECK_STR macro for simplifying checks in test
* Modify tests into sub-tests

Mykyta Yatsenko (2):
selftests/bpf: implement setting global variables in veristat selftests/bpf: introduce veristat test

 tools/testing/selftests/bpf/Makefile          |   1 +
 .../selftests/bpf/prog_tests/test_veristat.c  | 136 +++++++++
 .../selftests/bpf/progs/set_global_vars.c     |  47 +++
 tools/testing/selftests/bpf/test_progs.h      |   8 +
 tools/testing/selftests/bpf/veristat.c        | 282 ++++++++++++++++++
 5 files changed, 474 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
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/2 selftests/bpf: implement setting global variables in veristat

From: Mykyta Yatsenko <[email protected]>

To better verify some complex BPF programs we'd like to preset global variables.
This patch introduces CLI argument --set-global-vars or -G to veristat, that allows presetting values to global variables defined in BPF program. For example:

prog.c:

```
enum Enum { ELEMENT1 = 0, ELEMENT2 = 5 };
const volatile __s64 a = 5;
const volatile __u8 b = 5;
const volatile enum Enum c = ELEMENT2;
const volatile bool d = false;

char arr[4] = {0};

SEC("tp_btf/sched_switch")
int BPF_PROG(...)
{
	bpf_printk("%c\n", arr[a]);
	bpf_printk("%c\n", arr[b]);
	bpf_printk("%c\n", arr[c]);
	bpf_printk("%c\n", arr[d]);
	return 0;
}
```
By default verification of the program fails:
```
./veristat prog.bpf.o
```
By presetting global variables, we can make verification pass:
```
./veristat wq.bpf.o  -G "a = 0" -G "b = 1" -G "c = 2" -G "d = 3"
```
---
 tools/testing/selftests/bpf/veristat.c | 282 +++++++++++++++++++++++++
 1 file changed, 282 insertions(+)
tools/testing/selftests/bpf/veristat.c +282 -0
index 06af5029885b..8a540c4b4bba 100644
@@ -154,6 +154,16 @@ struct filter {
 	bool abs;
 };
 
+struct var_preset {
+	char *name;
+	enum { INTEGRAL, NAME } type;
+	union {
+		long long ivalue;
+		char *svalue;
+	};
+	bool applied;
+};
+
 static struct env {
 	char **filenames;
 	int filename_cnt;
@@ -195,6 +205,8 @@ static struct env {
 	int progs_processed;
 	int progs_skipped;
 	int top_src_lines;
+	struct var_preset *presets;
+	int npresets;
 } env;
 
 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
@@ -246,12 +258,15 @@ static const struct argp_option opts[] = {
 	{ "test-reg-invariants", 'r', NULL, 0,
 	  "Force BPF verifier failure on register invariant violation (BPF_F_TEST_REG_INVARIANTS program flag)" },
 	{ "top-src-lines", 'S', "N", 0, "Emit N most frequent source code lines" },
+	{ "set-global-vars", 'G', "GLOBALS", 0, "Set global variables provided in the expression, for example \"var1 = 1\"" },
 	{},
 };
 
 static int parse_stats(const char *stats_str, struct stat_specs *specs);
 static int append_filter(struct filter **filters, int *cnt, const char *str);
 static int append_filter_file(const char *path);
+static int append_var_preset(struct var_preset **presets, int *cnt, const char *expr);
+static int append_var_preset_file(const char *filename);
 
 static error_t parse_arg(int key, char *arg, struct argp_state *state)
 {
@@ -363,6 +378,19 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
 			return -ENOMEM;
 		env.filename_cnt++;
 		break;
+	case 'G': {
+		if (arg[0] == '@') {
+			if (append_var_preset_file(arg + 1)) {
+				fprintf(stderr, "Could not parse global variables preset: %s\n",
+					arg);
+				argp_usage(state);
+			}
+		} else if (append_var_preset(&env.presets, &env.npresets, arg)) {
+			fprintf(stderr, "Could not parse global variables preset: %s\n", arg);
+			argp_usage(state);
+		}
+		break;
+	}
 	default:
 		return ARGP_ERR_UNKNOWN;
 	}
@@ -1292,6 +1320,249 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
 	return 0;
 };
 
+static int append_var_preset(struct var_preset **presets, int *cnt, const char *expr)
+{
+	void *tmp;
+	struct var_preset *cur;
+	char var[256], val[256];
+
+	tmp = realloc(*presets, (*cnt + 1) * sizeof(**presets));
+	if (!tmp)
+		return -ENOMEM;
+	*presets = tmp;
+	cur = &(*presets)[*cnt];
+	cur->applied = false;
+
+	if (sscanf(expr, "%s = %s\n", var, val) != 2) {
+		fprintf(stderr, "Could not parse expression %s\n", expr);
+		return -EINVAL;
+	}
+
+	if (isalpha(*val)) {
+		cur->svalue = strdup(val);
+		cur->type = NAME;
+	} else if (*val == '-' || isdigit(*val)) {
+		long long value;
+
+		errno = 0;
+		value = strtoll(val, NULL, 0);
+		if (errno == ERANGE) {
+			errno = 0;
+			value = strtoull(val, NULL, 0);
+		}
+		cur->ivalue = value;
+		cur->type = INTEGRAL;
+		if (errno) {
+			fprintf(stderr, "Could not parse integer value %s\n", val);
+			return -EINVAL;
+		}
+	} else {
+		fprintf(stderr, "Could not parse value %s\n", val);
+		return -EINVAL;
+	}
+	cur->name = strdup(var);
+	(*cnt)++;
+	return 0;
+}
+
+static int append_var_preset_file(const char *filename)
+{
+	char buf[1024];
+	FILE *f;
+	int err = 0;
+
+	f = fopen(filename, "rt");
+	if (!f) {
+		err = -errno;
+		fprintf(stderr, "Failed to open presets in '%s': %d\n", filename, err);
+		return -EINVAL;
+	}
+
+	while (fscanf(f, " %1023[^\n]\n", buf) == 1) {
+		if (buf[0] == '\0' || buf[0] == '#')
+			continue;
+
+		err = append_var_preset(&env.presets, &env.npresets, buf);
+		if (err)
+			goto cleanup;
+	}
+
+cleanup:
+	fclose(f);
+	return err;
+}
+
+static bool is_signed_type(const struct btf_type *t)
+{
+	if (btf_is_int(t))
+		return btf_int_encoding(t) & BTF_INT_SIGNED;
+	if (btf_is_any_enum(t))
+		return btf_kflag(t);
+	return true;
+}
+
+static int enum_value_from_name(const struct btf *btf, const struct btf_type *t,
+				const char *evalue, long long *retval)
+{
+	if (btf_is_enum(t)) {
+		struct btf_enum *e = btf_enum(t);
+		int i, n = btf_vlen(t);
+
+		for (i = 0; i < n; ++i, ++e) {
+			const char *cur_name = btf__name_by_offset(btf, e->name_off);
+
+			if (strcmp(cur_name, evalue) == 0) {
+				*retval = e->val;
+				return 0;
+			}
+		}
+	} else if (btf_is_enum64(t)) {
+		struct btf_enum64 *e = btf_enum64(t);
+		int i, n = btf_vlen(t);
+
+		for (i = 0; i < n; ++i, ++e) {
+			const char *cur_name = btf__name_by_offset(btf, e->name_off);
+			__u64 value =  btf_enum64_value(e);
+
+			if (strcmp(cur_name, evalue) == 0) {
+				*retval = value;
+				return 0;
+			}
+		}
+	}
+	return -EINVAL;
+}
+
+static bool is_preset_supported(const struct btf_type *t)
+{
+	return btf_is_int(t) || btf_is_enum(t) || btf_is_enum64(t);
+}
+
+static int set_global_var(struct bpf_object *obj, struct btf *btf, const struct btf_type *t,
+			  struct bpf_map *map, struct btf_var_secinfo *sinfo,
+			  struct var_preset *preset)
+{
+	const struct btf_type *base_type;
+	void *ptr;
+	long long value = preset->ivalue;
+	size_t size;
+
+	base_type = btf__type_by_id(btf, btf__resolve_type(btf, t->type));
+	if (!base_type) {
+		fprintf(stderr, "Could not resolve type %d\n", t->type);
+		return -EINVAL;
+	}
+	if (!is_preset_supported(base_type)) {
+		fprintf(stderr, "Setting global variable for btf kind %d is not supported\n",
+			btf_kind(base_type));
+		return -EINVAL;
+	}
+
+	if (preset->type == NAME && btf_is_any_enum(base_type)) {
+		if (enum_value_from_name(btf, base_type, preset->svalue, &value) != 0) {
+			fprintf(stderr, "Could not find integer value for enum element %s\n",
+				preset->svalue);
+			return -EINVAL;
+		}
+	}
+
+	/* Check if value fits into the target variable size */
+	if  (sinfo->size < sizeof(preset->ivalue)) {
+		bool is_signed = is_signed_type(base_type);
+		__u32 unsigned_bits = sinfo->size * 8 - (is_signed ? 1 : 0);
+		long long max_val = 1ll << unsigned_bits;
+
+		if (preset->ivalue >= max_val || preset->ivalue < -max_val) {
+			fprintf(stderr,
+				"Variable %s value %lld is out of range [%lld; %lld]\n",
+				btf__name_by_offset(btf, t->name_off), preset->ivalue,
+				is_signed ? -max_val : 0, max_val - 1);
+			return -EINVAL;
+		}
+	}
+
+	ptr = (void *)bpf_map__initial_value(map, &size);
+	if (!ptr || (sinfo->offset + sinfo->size > size))
+		return -EINVAL;
+
+	if (__BYTE_ORDER == __LITTLE_ENDIAN) {
+		memcpy(ptr + sinfo->offset, &value, sinfo->size);
+	} else if (__BYTE_ORDER == __BIG_ENDIAN) {
+		__u8 src_offset = sizeof(value) - sinfo->size;
+
+		memcpy(ptr + sinfo->offset, (void *)&value + src_offset, sinfo->size);
+	}
+	return 0;
+}
+
+static int set_global_vars(struct bpf_object *obj, struct var_preset *presets, int npresets)
+{
+	struct btf_var_secinfo *sinfo;
+	const char *sec_name;
+	const struct btf_type *t;
+	struct bpf_map *map;
+	struct btf *btf;
+	int i, j, k, n, cnt, err = 0;
+
+	if (npresets == 0)
+		return 0;
+
+	btf = bpf_object__btf(obj);
+	if (!btf)
+		return -EINVAL;
+
+	cnt = btf__type_cnt(btf);
+	for (i = 1; i != cnt; ++i) {
+		t = btf__type_by_id(btf, i);
+
+		if (!btf_is_datasec(t))
+			continue;
+
+		sinfo = btf_var_secinfos(t);
+		sec_name = btf__name_by_offset(btf, t->name_off);
+		map = bpf_object__find_map_by_name(obj, sec_name);
+		if (!map)
+			continue;
+
+		n = btf_vlen(t);
+		for (j = 0; j < n; ++j, ++sinfo) {
+			const struct btf_type *var_type = btf__type_by_id(btf, sinfo->type);
+			const char *var_name;
+
+			if (!btf_is_var(var_type))
+				continue;
+
+			var_name = btf__name_by_offset(btf, var_type->name_off);
+
+			for (k = 0; k < npresets; ++k) {
+				if (strcmp(var_name, presets[k].name) != 0)
+					continue;
+
+				if (presets[k].applied) {
+					fprintf(stderr, "Variable %s is set more than once",
+						var_name);
+					return -EINVAL;
+				}
+
+				err = set_global_var(obj, btf, var_type, map, sinfo, presets + k);
+				if (err)
+					return err;
+
+				presets[k].applied = true;
+				break;
+			}
+		}
+	}
+	for (i = 0; i < npresets; ++i) {
+		if (!presets[i].applied) {
+			fprintf(stderr, "Global variable preset %s has not been applied\n",
+				presets[i].name);
+		}
+		presets[i].applied = false;
+	}
+	return err;
+}
+
 static int process_obj(const char *filename)
 {
 	const char *base_filename = basename(strdupa(filename));
@@ -1341,6 +1612,11 @@ static int process_obj(const char *filename)
 	if (prog_cnt == 1) {
 		prog = bpf_object__next_program(obj, NULL);
 		bpf_program__set_autoload(prog, true);
+		err = set_global_vars(obj, env.presets, env.npresets);
+		if (err) {
+			fprintf(stderr, "Failed to set global variables\n");
+			goto cleanup;
+		}
 		process_prog(filename, obj, prog);
 		goto cleanup;
 	}
@@ -1355,6 +1631,12 @@ static int process_obj(const char *filename)
 			goto cleanup;
 		}
 
+		err = set_global_vars(tobj, env.presets, env.npresets);
+		if (err) {
+			fprintf(stderr, "Failed to set global variables\n");
+			goto cleanup;
+		}
+
 		lprog = NULL;
 		bpf_object__for_each_program(tprog, tobj) {
 			const char *tprog_name = bpf_program__name(tprog);
Signature · 1 line
2.48.1
maintainer Acked-by on v3 1/2
#2 lore raw
Quoted 37 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

To better verify some complex BPF programs we'd like to preset global variables.
This patch introduces CLI argument --set-global-vars or -G to veristat, that allows presetting values to global variables defined in BPF program. For example:

prog.c:

```
enum Enum { ELEMENT1 = 0, ELEMENT2 = 5 };
const volatile __s64 a = 5;
const volatile __u8 b = 5;
const volatile enum Enum c = ELEMENT2;
const volatile bool d = false;

char arr[4] = {0};

SEC("tp_btf/sched_switch")
int BPF_PROG(...)
{
	bpf_printk("%c\n", arr[a]);
	bpf_printk("%c\n", arr[b]);
	bpf_printk("%c\n", arr[c]);
	bpf_printk("%c\n", arr[d]);
	return 0;
}
```
By default verification of the program fails:
```
./veristat prog.bpf.o
```
By presetting global variables, we can make verification pass:
```
./veristat wq.bpf.o  -G "a = 0" -G "b = 1" -G "c = 2" -G "d = 3"
```
Quoted 2 lines

---

[...]

Quoted 14 lines
tools/testing/selftests/bpf/veristat.c +10 -0
@@ -1292,6 +1320,249 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
 	return 0;
 };
 
+static int append_var_preset(struct var_preset **presets, int *cnt, const char *expr)
+{
+	void *tmp;
+	struct var_preset *cur;
+	char var[256], val[256];
+
+	tmp = realloc(*presets, (*cnt + 1) * sizeof(**presets));
+	if (!tmp)
+		return -ENOMEM;
+	*presets = tmp;

tools/testing/selftests/bpf/veristat.c static int process_prog(const char *filename, struct bpf_object *obj, struct bpf · line 1342

Quoted 10 lines
tools/testing/selftests/bpf/veristat.c +10 -0
+	cur = &(*presets)[*cnt];
+	cur->applied = false;
+
+	if (sscanf(expr, "%s = %s\n", var, val) != 2) {
+		fprintf(stderr, "Could not parse expression %s\n", expr);
+		return -EINVAL;
+	}
+
+	if (isalpha(*val)) {
+		cur->svalue = strdup(val);

Nit: when I run veristat under valgrind, it complains that
fields allocated in this function are never freed. Grepping for 'free', it look like veristat.c prefers to free any allocated memory before exit.

Quoted 25 lines
tools/testing/selftests/bpf/veristat.c +25 -0
+		cur->type = NAME;
+	} else if (*val == '-' || isdigit(*val)) {
+		long long value;
+
+		errno = 0;
+		value = strtoll(val, NULL, 0);
+		if (errno == ERANGE) {
+			errno = 0;
+			value = strtoull(val, NULL, 0);
+		}
+		cur->ivalue = value;
+		cur->type = INTEGRAL;
+		if (errno) {
+			fprintf(stderr, "Could not parse integer value %s\n", val);
+			return -EINVAL;
+		}
+	} else {
+		fprintf(stderr, "Could not parse value %s\n", val);
+		return -EINVAL;
+	}
+	cur->name = strdup(var);
+	(*cnt)++;
+	return 0;
+}
+

tools/testing/selftests/bpf/veristat.c static int process_prog(const char *filename, struct bpf_object *obj, struct bpf · line 1377

Quoted 10 lines
tools/testing/selftests/bpf/veristat.c +10 -0
+static int append_var_preset_file(const char *filename)
+{
+	char buf[1024];
+	FILE *f;
+	int err = 0;
+
+	f = fopen(filename, "rt");
+	if (!f) {
+		err = -errno;
+		fprintf(stderr, "Failed to open presets in '%s': %d\n", filename, err);

Nit: fprintf(stderr, "Failed to open presets in '%s': %s\n",
filename, strerror(errno));

Would make this a bit more friendly
(and it prints error code if string representation does not exist).

Quoted 16 lines
tools/testing/selftests/bpf/veristat.c +16 -0
+		return -EINVAL;
+	}
+
+	while (fscanf(f, " %1023[^\n]\n", buf) == 1) {
+		if (buf[0] == '\0' || buf[0] == '#')
+			continue;
+
+		err = append_var_preset(&env.presets, &env.npresets, buf);
+		if (err)
+			goto cleanup;
+	}
+
+cleanup:
+	fclose(f);
+	return err;
+}

[...]

#3 lore raw

PATCH v3 2/2 selftests/bpf: introduce veristat test

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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

---
 tools/testing/selftests/bpf/Makefile          |   1 +
 .../selftests/bpf/prog_tests/test_veristat.c  | 136 ++++++++++++++++++
 .../selftests/bpf/progs/set_global_vars.c     |  47 ++++++
 tools/testing/selftests/bpf/test_progs.h      |   8 ++
 4 files changed, 192 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
tools/testing/selftests/bpf/Makefile +1 -0
index 0d552bfcfe7d..5e9d3c91c6db 100644
@@ -690,6 +690,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 $$@
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +136 -0

136 lines not on this page · raw message

tools/testing/selftests/bpf/progs/set_global_vars.c new file +47 -0

47 lines not on this page · raw message

tools/testing/selftests/bpf/test_progs.h +8 -0
index 404d0d4915d5..870694f2a359 100644
@@ -427,6 +427,14 @@ void hexdump(const char *prefix, const void *buf, size_t len);
 			goto goto_label;				\
 	})
 
+#define SYS_FAIL(goto_label, fmt, ...)					\
+	({								\
+		char cmd[1024];						\
+		snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);		\
+		if (!ASSERT_NEQ(0, system(cmd), cmd))			\
+			goto goto_label;				\
+	})
+
 #define ALL_TO_DEV_NULL " >/dev/null 2>&1"
 
 #define SYS_NOFAIL(fmt, ...)						\
Signature · 1 line
2.48.1
maintainer Acked-by on v3 2/2
#4 lore raw

Re: [PATCH bpf-next v3 2/2] selftests/bpf: introduce veristat test

Quoted 8 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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

---

[...]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +0 -0
new file mode 100644
index 000000000000..eff79bf55fe3

[...]

tools/testing/selftests/bpf/prog_tests/test_veristat.c line 29

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +9 -0
+static struct fixture *init_fixture(void)
+{
+	struct fixture *fix = malloc(sizeof(struct fixture));
+
+	if (access("./veristat", F_OK) == 0)
+		strcpy(fix->veristat, "./veristat");
+	/* for no_alu32 and cpuv4 veristat is in parent folder */
+	if (access("../veristat", F_OK) == 0)
+		strcpy(fix->veristat, "../veristat");

Nit: 'else PRINT_FAIL("Can't find veristat binary");' ?

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +7 -0
+
+	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;
+}

[...]

tools/testing/selftests/bpf/prog_tests/test_veristat.c line 96

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +13 -0
+static void test_set_global_vars_from_file_succeeds(void)
+{
+	struct fixture *fix = init_fixture();
+	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_GE(fd, 0, "valid fd"))
+		goto out;
+
+	write(fd, vars, strlen(vars));

Nit: 'syncfs(fd);' ?

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +11 -0
+	SYS(out, "%s set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
+	    fix->veristat, input_file, fix->tmpfile);
+	read(fix->fd, fix->output, fix->sz);
+	__CHECK_STR("_w=0x8000 ", "var_s16 = -32768");
+	__CHECK_STR("_w=0xecec ", "var_u16 = 60652");
+
+out:
+	close(fd);
+	remove(input_file);
+	teardown_fixture(fix);
+}

[...]

#5 lore raw

Re: [PATCH bpf-next v3 2/2] selftests/bpf: introduce veristat test

Quoted 40 lines · Eduard Zingerman
Quoted 8 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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

---

[...]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +0 -0
new file mode 100644
index 000000000000..eff79bf55fe3

[...]

tools/testing/selftests/bpf/prog_tests/test_veristat.c line 29

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +9 -0
+static struct fixture *init_fixture(void)
+{
+	struct fixture *fix = malloc(sizeof(struct fixture));
+
+	if (access("./veristat", F_OK) == 0)
+		strcpy(fix->veristat, "./veristat");
+	/* for no_alu32 and cpuv4 veristat is in parent folder */
+	if (access("../veristat", F_OK) == 0)
+		strcpy(fix->veristat, "../veristat");

Nit: 'else PRINT_FAIL("Can't find veristat binary");' ?

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +7 -0
+
+	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;
+}

[...]

Quoted 14 lines

tools/testing/selftests/bpf/prog_tests/test_veristat.c line 96

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +13 -0
+static void test_set_global_vars_from_file_succeeds(void)
+{
+	struct fixture *fix = init_fixture();
+	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_GE(fd, 0, "valid fd"))
+		goto out;
+
+	write(fd, vars, strlen(vars));

Nit: 'syncfs(fd);' ?

Thanks, makes sense. I'll include these in the next version, along with CI failures fixes.

Quoted 13 lines
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/test_veristat.c new file +11 -0
+	SYS(out, "%s set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
+	    fix->veristat, input_file, fix->tmpfile);
+	read(fix->fd, fix->output, fix->sz);
+	__CHECK_STR("_w=0x8000 ", "var_s16 = -32768");
+	__CHECK_STR("_w=0xecec ", "var_u16 = 60652");
+
+out:
+	close(fd);
+	remove(input_file);
+	teardown_fixture(fix);
+}

[...]