xref: /linux/tools/testing/selftests/bpf/test_loader.c (revision 6798668ab19557520876d0f7dfabca827ee8b524)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 #include <linux/capability.h>
4 #include <stdlib.h>
5 #include <regex.h>
6 #include <test_progs.h>
7 #include <bpf/btf.h>
8 
9 #include "autoconf_helper.h"
10 #include "disasm_helpers.h"
11 #include "unpriv_helpers.h"
12 #include "cap_helpers.h"
13 #include "jit_disasm_helpers.h"
14 
15 #define str_has_pfx(str, pfx) \
16 	(strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0)
17 
18 #define TEST_LOADER_LOG_BUF_SZ 2097152
19 
20 #define TEST_TAG_EXPECT_FAILURE "comment:test_expect_failure"
21 #define TEST_TAG_EXPECT_SUCCESS "comment:test_expect_success"
22 #define TEST_TAG_EXPECT_MSG_PFX "comment:test_expect_msg="
23 #define TEST_TAG_EXPECT_XLATED_PFX "comment:test_expect_xlated="
24 #define TEST_TAG_EXPECT_FAILURE_UNPRIV "comment:test_expect_failure_unpriv"
25 #define TEST_TAG_EXPECT_SUCCESS_UNPRIV "comment:test_expect_success_unpriv"
26 #define TEST_TAG_EXPECT_MSG_PFX_UNPRIV "comment:test_expect_msg_unpriv="
27 #define TEST_TAG_EXPECT_XLATED_PFX_UNPRIV "comment:test_expect_xlated_unpriv="
28 #define TEST_TAG_LOG_LEVEL_PFX "comment:test_log_level="
29 #define TEST_TAG_PROG_FLAGS_PFX "comment:test_prog_flags="
30 #define TEST_TAG_DESCRIPTION_PFX "comment:test_description="
31 #define TEST_TAG_RETVAL_PFX "comment:test_retval="
32 #define TEST_TAG_RETVAL_PFX_UNPRIV "comment:test_retval_unpriv="
33 #define TEST_TAG_AUXILIARY "comment:test_auxiliary"
34 #define TEST_TAG_AUXILIARY_UNPRIV "comment:test_auxiliary_unpriv"
35 #define TEST_BTF_PATH "comment:test_btf_path="
36 #define TEST_TAG_ARCH "comment:test_arch="
37 #define TEST_TAG_JITED_PFX "comment:test_jited="
38 #define TEST_TAG_JITED_PFX_UNPRIV "comment:test_jited_unpriv="
39 #define TEST_TAG_CAPS_UNPRIV "comment:test_caps_unpriv="
40 #define TEST_TAG_LOAD_MODE_PFX "comment:load_mode="
41 #define TEST_TAG_EXPECT_STDERR_PFX "comment:test_expect_stderr="
42 #define TEST_TAG_EXPECT_STDERR_PFX_UNPRIV "comment:test_expect_stderr_unpriv="
43 #define TEST_TAG_EXPECT_STDOUT_PFX "comment:test_expect_stdout="
44 #define TEST_TAG_EXPECT_STDOUT_PFX_UNPRIV "comment:test_expect_stdout_unpriv="
45 
46 /* Warning: duplicated in bpf_misc.h */
47 #define POINTER_VALUE	0xbadcafe
48 #define TEST_DATA_LEN	64
49 
50 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
51 #define EFFICIENT_UNALIGNED_ACCESS 1
52 #else
53 #define EFFICIENT_UNALIGNED_ACCESS 0
54 #endif
55 
56 static int sysctl_unpriv_disabled = -1;
57 
58 enum mode {
59 	PRIV = 1,
60 	UNPRIV = 2
61 };
62 
63 enum load_mode {
64 	JITED		= 1 << 0,
65 	NO_JITED	= 1 << 1,
66 };
67 
68 struct expect_msg {
69 	const char *substr; /* substring match */
70 	regex_t regex;
71 	bool is_regex;
72 	bool on_next_line;
73 };
74 
75 struct expected_msgs {
76 	struct expect_msg *patterns;
77 	size_t cnt;
78 };
79 
80 struct test_subspec {
81 	char *name;
82 	bool expect_failure;
83 	struct expected_msgs expect_msgs;
84 	struct expected_msgs expect_xlated;
85 	struct expected_msgs jited;
86 	struct expected_msgs stderr;
87 	struct expected_msgs stdout;
88 	int retval;
89 	bool execute;
90 	__u64 caps;
91 };
92 
93 struct test_spec {
94 	const char *prog_name;
95 	struct test_subspec priv;
96 	struct test_subspec unpriv;
97 	const char *btf_custom_path;
98 	int log_level;
99 	int prog_flags;
100 	int mode_mask;
101 	int arch_mask;
102 	int load_mask;
103 	bool auxiliary;
104 	bool valid;
105 };
106 
107 static int tester_init(struct test_loader *tester)
108 {
109 	if (!tester->log_buf) {
110 		tester->log_buf_sz = TEST_LOADER_LOG_BUF_SZ;
111 		tester->log_buf = calloc(tester->log_buf_sz, 1);
112 		if (!ASSERT_OK_PTR(tester->log_buf, "tester_log_buf"))
113 			return -ENOMEM;
114 	}
115 
116 	return 0;
117 }
118 
119 void test_loader_fini(struct test_loader *tester)
120 {
121 	if (!tester)
122 		return;
123 
124 	free(tester->log_buf);
125 }
126 
127 static void free_msgs(struct expected_msgs *msgs)
128 {
129 	int i;
130 
131 	for (i = 0; i < msgs->cnt; i++)
132 		if (msgs->patterns[i].is_regex)
133 			regfree(&msgs->patterns[i].regex);
134 	free(msgs->patterns);
135 	msgs->patterns = NULL;
136 	msgs->cnt = 0;
137 }
138 
139 static void free_test_spec(struct test_spec *spec)
140 {
141 	/* Deallocate expect_msgs arrays. */
142 	free_msgs(&spec->priv.expect_msgs);
143 	free_msgs(&spec->unpriv.expect_msgs);
144 	free_msgs(&spec->priv.expect_xlated);
145 	free_msgs(&spec->unpriv.expect_xlated);
146 	free_msgs(&spec->priv.jited);
147 	free_msgs(&spec->unpriv.jited);
148 	free_msgs(&spec->unpriv.stderr);
149 	free_msgs(&spec->priv.stderr);
150 	free_msgs(&spec->unpriv.stdout);
151 	free_msgs(&spec->priv.stdout);
152 
153 	free(spec->priv.name);
154 	free(spec->unpriv.name);
155 	spec->priv.name = NULL;
156 	spec->unpriv.name = NULL;
157 }
158 
159 /* Compiles regular expression matching pattern.
160  * Pattern has a special syntax:
161  *
162  *   pattern := (<verbatim text> | regex)*
163  *   regex := "{{" <posix extended regular expression> "}}"
164  *
165  * In other words, pattern is a verbatim text with inclusion
166  * of regular expressions enclosed in "{{" "}}" pairs.
167  * For example, pattern "foo{{[0-9]+}}" matches strings like
168  * "foo0", "foo007", etc.
169  */
170 static int compile_regex(const char *pattern, regex_t *regex)
171 {
172 	char err_buf[256], buf[256] = {}, *ptr, *buf_end;
173 	const char *original_pattern = pattern;
174 	bool in_regex = false;
175 	int err;
176 
177 	buf_end = buf + sizeof(buf);
178 	ptr = buf;
179 	while (*pattern && ptr < buf_end - 2) {
180 		if (!in_regex && str_has_pfx(pattern, "{{")) {
181 			in_regex = true;
182 			pattern += 2;
183 			continue;
184 		}
185 		if (in_regex && str_has_pfx(pattern, "}}")) {
186 			in_regex = false;
187 			pattern += 2;
188 			continue;
189 		}
190 		if (in_regex) {
191 			*ptr++ = *pattern++;
192 			continue;
193 		}
194 		/* list of characters that need escaping for extended posix regex */
195 		if (strchr(".[]\\()*+?{}|^$", *pattern)) {
196 			*ptr++ = '\\';
197 			*ptr++ = *pattern++;
198 			continue;
199 		}
200 		*ptr++ = *pattern++;
201 	}
202 	if (*pattern) {
203 		PRINT_FAIL("Regexp too long: '%s'\n", original_pattern);
204 		return -EINVAL;
205 	}
206 	if (in_regex) {
207 		PRINT_FAIL("Regexp has open '{{' but no closing '}}': '%s'\n", original_pattern);
208 		return -EINVAL;
209 	}
210 	err = regcomp(regex, buf, REG_EXTENDED | REG_NEWLINE);
211 	if (err != 0) {
212 		regerror(err, regex, err_buf, sizeof(err_buf));
213 		PRINT_FAIL("Regexp compilation error in '%s': '%s'\n", buf, err_buf);
214 		return -EINVAL;
215 	}
216 	return 0;
217 }
218 
219 static int __push_msg(const char *pattern, bool on_next_line, struct expected_msgs *msgs)
220 {
221 	struct expect_msg *msg;
222 	void *tmp;
223 	int err;
224 
225 	tmp = realloc(msgs->patterns,
226 		      (1 + msgs->cnt) * sizeof(struct expect_msg));
227 	if (!tmp) {
228 		ASSERT_FAIL("failed to realloc memory for messages\n");
229 		return -ENOMEM;
230 	}
231 	msgs->patterns = tmp;
232 	msg = &msgs->patterns[msgs->cnt];
233 	msg->on_next_line = on_next_line;
234 	msg->substr = pattern;
235 	msg->is_regex = false;
236 	if (strstr(pattern, "{{")) {
237 		err = compile_regex(pattern, &msg->regex);
238 		if (err)
239 			return err;
240 		msg->is_regex = true;
241 	}
242 	msgs->cnt += 1;
243 	return 0;
244 }
245 
246 static int clone_msgs(struct expected_msgs *from, struct expected_msgs *to)
247 {
248 	struct expect_msg *msg;
249 	int i, err;
250 
251 	for (i = 0; i < from->cnt; i++) {
252 		msg = &from->patterns[i];
253 		err = __push_msg(msg->substr, msg->on_next_line, to);
254 		if (err)
255 			return err;
256 	}
257 	return 0;
258 }
259 
260 static int push_msg(const char *substr, struct expected_msgs *msgs)
261 {
262 	return __push_msg(substr, false, msgs);
263 }
264 
265 static int push_disasm_msg(const char *regex_str, bool *on_next_line, struct expected_msgs *msgs)
266 {
267 	int err;
268 
269 	if (strcmp(regex_str, "...") == 0) {
270 		*on_next_line = false;
271 		return 0;
272 	}
273 	err = __push_msg(regex_str, *on_next_line, msgs);
274 	if (err)
275 		return err;
276 	*on_next_line = true;
277 	return 0;
278 }
279 
280 static int parse_int(const char *str, int *val, const char *name)
281 {
282 	char *end;
283 	long tmp;
284 
285 	errno = 0;
286 	if (str_has_pfx(str, "0x"))
287 		tmp = strtol(str + 2, &end, 16);
288 	else
289 		tmp = strtol(str, &end, 10);
290 	if (errno || end[0] != '\0') {
291 		PRINT_FAIL("failed to parse %s from '%s'\n", name, str);
292 		return -EINVAL;
293 	}
294 	*val = tmp;
295 	return 0;
296 }
297 
298 static int parse_caps(const char *str, __u64 *val, const char *name)
299 {
300 	int cap_flag = 0;
301 	char *token = NULL, *saveptr = NULL;
302 
303 	char *str_cpy = strdup(str);
304 	if (str_cpy == NULL) {
305 		PRINT_FAIL("Memory allocation failed\n");
306 		return -EINVAL;
307 	}
308 
309 	token = strtok_r(str_cpy, "|", &saveptr);
310 	while (token != NULL) {
311 		errno = 0;
312 		if (!strncmp("CAP_", token, sizeof("CAP_") - 1)) {
313 			PRINT_FAIL("define %s constant in bpf_misc.h, failed to parse caps\n", token);
314 			return -EINVAL;
315 		}
316 		cap_flag = strtol(token, NULL, 10);
317 		if (!cap_flag || errno) {
318 			PRINT_FAIL("failed to parse caps %s\n", name);
319 			return -EINVAL;
320 		}
321 		*val |= (1ULL << cap_flag);
322 		token = strtok_r(NULL, "|", &saveptr);
323 	}
324 
325 	free(str_cpy);
326 	return 0;
327 }
328 
329 static int parse_retval(const char *str, int *val, const char *name)
330 {
331 	/*
332 	 * INT_MIN is defined as (-INT_MAX -1), i.e. it doesn't expand to a
333 	 * single int and cannot be parsed with strtol, so we handle it
334 	 * separately here. In addition, it expands to different expressions in
335 	 * different compilers so we use a prefixed _INT_MIN instead.
336 	 */
337 	if (strcmp(str, "_INT_MIN") == 0) {
338 		*val = INT_MIN;
339 		return 0;
340 	}
341 
342 	return parse_int(str, val, name);
343 }
344 
345 static void update_flags(int *flags, int flag, bool clear)
346 {
347 	if (clear)
348 		*flags &= ~flag;
349 	else
350 		*flags |= flag;
351 }
352 
353 /* Matches a string of form '<pfx>[^=]=.*' and returns it's suffix.
354  * Used to parse btf_decl_tag values.
355  * Such values require unique prefix because compiler does not add
356  * same __attribute__((btf_decl_tag(...))) twice.
357  * Test suite uses two-component tags for such cases:
358  *
359  *   <pfx> __COUNTER__ '='
360  *
361  * For example, two consecutive __msg tags '__msg("foo") __msg("foo")'
362  * would be encoded as:
363  *
364  *   [18] DECL_TAG 'comment:test_expect_msg=0=foo' type_id=15 component_idx=-1
365  *   [19] DECL_TAG 'comment:test_expect_msg=1=foo' type_id=15 component_idx=-1
366  *
367  * And the purpose of this function is to extract 'foo' from the above.
368  */
369 static const char *skip_dynamic_pfx(const char *s, const char *pfx)
370 {
371 	const char *msg;
372 
373 	if (strncmp(s, pfx, strlen(pfx)) != 0)
374 		return NULL;
375 	msg = s + strlen(pfx);
376 	msg = strchr(msg, '=');
377 	if (!msg)
378 		return NULL;
379 	return msg + 1;
380 }
381 
382 enum arch {
383 	ARCH_UNKNOWN	= 0x1,
384 	ARCH_X86_64	= 0x2,
385 	ARCH_ARM64	= 0x4,
386 	ARCH_RISCV64	= 0x8,
387 	ARCH_S390X	= 0x10,
388 };
389 
390 static int get_current_arch(void)
391 {
392 #if defined(__x86_64__)
393 	return ARCH_X86_64;
394 #elif defined(__aarch64__)
395 	return ARCH_ARM64;
396 #elif defined(__riscv) && __riscv_xlen == 64
397 	return ARCH_RISCV64;
398 #elif defined(__s390x__)
399 	return ARCH_S390X;
400 #endif
401 	return ARCH_UNKNOWN;
402 }
403 
404 /* Uses btf_decl_tag attributes to describe the expected test
405  * behavior, see bpf_misc.h for detailed description of each attribute
406  * and attribute combinations.
407  */
408 static int parse_test_spec(struct test_loader *tester,
409 			   struct bpf_object *obj,
410 			   struct bpf_program *prog,
411 			   struct test_spec *spec)
412 {
413 	const char *description = NULL;
414 	bool has_unpriv_result = false;
415 	bool has_unpriv_retval = false;
416 	bool unpriv_xlated_on_next_line = true;
417 	bool xlated_on_next_line = true;
418 	bool unpriv_jit_on_next_line;
419 	bool jit_on_next_line;
420 	bool stderr_on_next_line = true;
421 	bool unpriv_stderr_on_next_line = true;
422 	bool stdout_on_next_line = true;
423 	bool unpriv_stdout_on_next_line = true;
424 	bool collect_jit = false;
425 	int func_id, i, err = 0;
426 	u32 arch_mask = 0;
427 	u32 load_mask = 0;
428 	struct btf *btf;
429 	enum arch arch;
430 
431 	memset(spec, 0, sizeof(*spec));
432 
433 	spec->prog_name = bpf_program__name(prog);
434 	spec->prog_flags = testing_prog_flags();
435 
436 	btf = bpf_object__btf(obj);
437 	if (!btf) {
438 		ASSERT_FAIL("BPF object has no BTF");
439 		return -EINVAL;
440 	}
441 
442 	func_id = btf__find_by_name_kind(btf, spec->prog_name, BTF_KIND_FUNC);
443 	if (func_id < 0) {
444 		ASSERT_FAIL("failed to find FUNC BTF type for '%s'", spec->prog_name);
445 		return -EINVAL;
446 	}
447 
448 	for (i = 1; i < btf__type_cnt(btf); i++) {
449 		const char *s, *val, *msg;
450 		const struct btf_type *t;
451 		bool clear;
452 		int flags;
453 
454 		t = btf__type_by_id(btf, i);
455 		if (!btf_is_decl_tag(t))
456 			continue;
457 
458 		if (t->type != func_id || btf_decl_tag(t)->component_idx != -1)
459 			continue;
460 
461 		s = btf__str_by_offset(btf, t->name_off);
462 		if (str_has_pfx(s, TEST_TAG_DESCRIPTION_PFX)) {
463 			description = s + sizeof(TEST_TAG_DESCRIPTION_PFX) - 1;
464 		} else if (strcmp(s, TEST_TAG_EXPECT_FAILURE) == 0) {
465 			spec->priv.expect_failure = true;
466 			spec->mode_mask |= PRIV;
467 		} else if (strcmp(s, TEST_TAG_EXPECT_SUCCESS) == 0) {
468 			spec->priv.expect_failure = false;
469 			spec->mode_mask |= PRIV;
470 		} else if (strcmp(s, TEST_TAG_EXPECT_FAILURE_UNPRIV) == 0) {
471 			spec->unpriv.expect_failure = true;
472 			spec->mode_mask |= UNPRIV;
473 			has_unpriv_result = true;
474 		} else if (strcmp(s, TEST_TAG_EXPECT_SUCCESS_UNPRIV) == 0) {
475 			spec->unpriv.expect_failure = false;
476 			spec->mode_mask |= UNPRIV;
477 			has_unpriv_result = true;
478 		} else if (strcmp(s, TEST_TAG_AUXILIARY) == 0) {
479 			spec->auxiliary = true;
480 			spec->mode_mask |= PRIV;
481 		} else if (strcmp(s, TEST_TAG_AUXILIARY_UNPRIV) == 0) {
482 			spec->auxiliary = true;
483 			spec->mode_mask |= UNPRIV;
484 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_MSG_PFX))) {
485 			err = push_msg(msg, &spec->priv.expect_msgs);
486 			if (err)
487 				goto cleanup;
488 			spec->mode_mask |= PRIV;
489 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_MSG_PFX_UNPRIV))) {
490 			err = push_msg(msg, &spec->unpriv.expect_msgs);
491 			if (err)
492 				goto cleanup;
493 			spec->mode_mask |= UNPRIV;
494 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_JITED_PFX))) {
495 			if (arch_mask == 0) {
496 				PRINT_FAIL("__jited used before __arch_*");
497 				goto cleanup;
498 			}
499 			if (collect_jit) {
500 				err = push_disasm_msg(msg, &jit_on_next_line,
501 						      &spec->priv.jited);
502 				if (err)
503 					goto cleanup;
504 				spec->mode_mask |= PRIV;
505 			}
506 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_JITED_PFX_UNPRIV))) {
507 			if (arch_mask == 0) {
508 				PRINT_FAIL("__unpriv_jited used before __arch_*");
509 				goto cleanup;
510 			}
511 			if (collect_jit) {
512 				err = push_disasm_msg(msg, &unpriv_jit_on_next_line,
513 						      &spec->unpriv.jited);
514 				if (err)
515 					goto cleanup;
516 				spec->mode_mask |= UNPRIV;
517 			}
518 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_XLATED_PFX))) {
519 			err = push_disasm_msg(msg, &xlated_on_next_line,
520 					      &spec->priv.expect_xlated);
521 			if (err)
522 				goto cleanup;
523 			spec->mode_mask |= PRIV;
524 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_XLATED_PFX_UNPRIV))) {
525 			err = push_disasm_msg(msg, &unpriv_xlated_on_next_line,
526 					      &spec->unpriv.expect_xlated);
527 			if (err)
528 				goto cleanup;
529 			spec->mode_mask |= UNPRIV;
530 		} else if (str_has_pfx(s, TEST_TAG_RETVAL_PFX)) {
531 			val = s + sizeof(TEST_TAG_RETVAL_PFX) - 1;
532 			err = parse_retval(val, &spec->priv.retval, "__retval");
533 			if (err)
534 				goto cleanup;
535 			spec->priv.execute = true;
536 			spec->mode_mask |= PRIV;
537 		} else if (str_has_pfx(s, TEST_TAG_RETVAL_PFX_UNPRIV)) {
538 			val = s + sizeof(TEST_TAG_RETVAL_PFX_UNPRIV) - 1;
539 			err = parse_retval(val, &spec->unpriv.retval, "__retval_unpriv");
540 			if (err)
541 				goto cleanup;
542 			spec->mode_mask |= UNPRIV;
543 			spec->unpriv.execute = true;
544 			has_unpriv_retval = true;
545 		} else if (str_has_pfx(s, TEST_TAG_LOG_LEVEL_PFX)) {
546 			val = s + sizeof(TEST_TAG_LOG_LEVEL_PFX) - 1;
547 			err = parse_int(val, &spec->log_level, "test log level");
548 			if (err)
549 				goto cleanup;
550 		} else if (str_has_pfx(s, TEST_TAG_PROG_FLAGS_PFX)) {
551 			val = s + sizeof(TEST_TAG_PROG_FLAGS_PFX) - 1;
552 
553 			clear = val[0] == '!';
554 			if (clear)
555 				val++;
556 
557 			if (strcmp(val, "BPF_F_STRICT_ALIGNMENT") == 0) {
558 				update_flags(&spec->prog_flags, BPF_F_STRICT_ALIGNMENT, clear);
559 			} else if (strcmp(val, "BPF_F_ANY_ALIGNMENT") == 0) {
560 				update_flags(&spec->prog_flags, BPF_F_ANY_ALIGNMENT, clear);
561 			} else if (strcmp(val, "BPF_F_TEST_RND_HI32") == 0) {
562 				update_flags(&spec->prog_flags, BPF_F_TEST_RND_HI32, clear);
563 			} else if (strcmp(val, "BPF_F_TEST_STATE_FREQ") == 0) {
564 				update_flags(&spec->prog_flags, BPF_F_TEST_STATE_FREQ, clear);
565 			} else if (strcmp(val, "BPF_F_SLEEPABLE") == 0) {
566 				update_flags(&spec->prog_flags, BPF_F_SLEEPABLE, clear);
567 			} else if (strcmp(val, "BPF_F_XDP_HAS_FRAGS") == 0) {
568 				update_flags(&spec->prog_flags, BPF_F_XDP_HAS_FRAGS, clear);
569 			} else if (strcmp(val, "BPF_F_TEST_REG_INVARIANTS") == 0) {
570 				update_flags(&spec->prog_flags, BPF_F_TEST_REG_INVARIANTS, clear);
571 			} else /* assume numeric value */ {
572 				err = parse_int(val, &flags, "test prog flags");
573 				if (err)
574 					goto cleanup;
575 				update_flags(&spec->prog_flags, flags, clear);
576 			}
577 		} else if (str_has_pfx(s, TEST_TAG_ARCH)) {
578 			val = s + sizeof(TEST_TAG_ARCH) - 1;
579 			if (strcmp(val, "X86_64") == 0) {
580 				arch = ARCH_X86_64;
581 			} else if (strcmp(val, "ARM64") == 0) {
582 				arch = ARCH_ARM64;
583 			} else if (strcmp(val, "RISCV64") == 0) {
584 				arch = ARCH_RISCV64;
585 			} else if (strcmp(val, "s390x") == 0) {
586 				arch = ARCH_S390X;
587 			} else {
588 				PRINT_FAIL("bad arch spec: '%s'\n", val);
589 				err = -EINVAL;
590 				goto cleanup;
591 			}
592 			arch_mask |= arch;
593 			collect_jit = get_current_arch() == arch;
594 			unpriv_jit_on_next_line = true;
595 			jit_on_next_line = true;
596 		} else if (str_has_pfx(s, TEST_BTF_PATH)) {
597 			spec->btf_custom_path = s + sizeof(TEST_BTF_PATH) - 1;
598 		} else if (str_has_pfx(s, TEST_TAG_CAPS_UNPRIV)) {
599 			val = s + sizeof(TEST_TAG_CAPS_UNPRIV) - 1;
600 			err = parse_caps(val, &spec->unpriv.caps, "test caps");
601 			if (err)
602 				goto cleanup;
603 			spec->mode_mask |= UNPRIV;
604 		} else if (str_has_pfx(s, TEST_TAG_LOAD_MODE_PFX)) {
605 			val = s + sizeof(TEST_TAG_LOAD_MODE_PFX) - 1;
606 			if (strcmp(val, "jited") == 0) {
607 				load_mask = JITED;
608 			} else if (strcmp(val, "no_jited") == 0) {
609 				load_mask = NO_JITED;
610 			} else {
611 				PRINT_FAIL("bad load spec: '%s'", val);
612 				err = -EINVAL;
613 				goto cleanup;
614 			}
615 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_STDERR_PFX))) {
616 			err = push_disasm_msg(msg, &stderr_on_next_line,
617 					      &spec->priv.stderr);
618 			if (err)
619 				goto cleanup;
620 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_STDERR_PFX_UNPRIV))) {
621 			err = push_disasm_msg(msg, &unpriv_stderr_on_next_line,
622 					      &spec->unpriv.stderr);
623 			if (err)
624 				goto cleanup;
625 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_STDOUT_PFX))) {
626 			err = push_disasm_msg(msg, &stdout_on_next_line,
627 					      &spec->priv.stdout);
628 			if (err)
629 				goto cleanup;
630 		} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_STDOUT_PFX_UNPRIV))) {
631 			err = push_disasm_msg(msg, &unpriv_stdout_on_next_line,
632 					      &spec->unpriv.stdout);
633 			if (err)
634 				goto cleanup;
635 		}
636 	}
637 
638 	spec->arch_mask = arch_mask ?: -1;
639 	spec->load_mask = load_mask ?: (JITED | NO_JITED);
640 
641 	if (spec->mode_mask == 0)
642 		spec->mode_mask = PRIV;
643 
644 	if (!description)
645 		description = spec->prog_name;
646 
647 	if (spec->mode_mask & PRIV) {
648 		spec->priv.name = strdup(description);
649 		if (!spec->priv.name) {
650 			PRINT_FAIL("failed to allocate memory for priv.name\n");
651 			err = -ENOMEM;
652 			goto cleanup;
653 		}
654 	}
655 
656 	if (spec->mode_mask & UNPRIV) {
657 		int descr_len = strlen(description);
658 		const char *suffix = " @unpriv";
659 		char *name;
660 
661 		name = malloc(descr_len + strlen(suffix) + 1);
662 		if (!name) {
663 			PRINT_FAIL("failed to allocate memory for unpriv.name\n");
664 			err = -ENOMEM;
665 			goto cleanup;
666 		}
667 
668 		strcpy(name, description);
669 		strcpy(&name[descr_len], suffix);
670 		spec->unpriv.name = name;
671 	}
672 
673 	if (spec->mode_mask & (PRIV | UNPRIV)) {
674 		if (!has_unpriv_result)
675 			spec->unpriv.expect_failure = spec->priv.expect_failure;
676 
677 		if (!has_unpriv_retval) {
678 			spec->unpriv.retval = spec->priv.retval;
679 			spec->unpriv.execute = spec->priv.execute;
680 		}
681 
682 		if (spec->unpriv.expect_msgs.cnt == 0)
683 			clone_msgs(&spec->priv.expect_msgs, &spec->unpriv.expect_msgs);
684 		if (spec->unpriv.expect_xlated.cnt == 0)
685 			clone_msgs(&spec->priv.expect_xlated, &spec->unpriv.expect_xlated);
686 		if (spec->unpriv.jited.cnt == 0)
687 			clone_msgs(&spec->priv.jited, &spec->unpriv.jited);
688 		if (spec->unpriv.stderr.cnt == 0)
689 			clone_msgs(&spec->priv.stderr, &spec->unpriv.stderr);
690 		if (spec->unpriv.stdout.cnt == 0)
691 			clone_msgs(&spec->priv.stdout, &spec->unpriv.stdout);
692 	}
693 
694 	spec->valid = true;
695 
696 	return 0;
697 
698 cleanup:
699 	free_test_spec(spec);
700 	return err;
701 }
702 
703 static void prepare_case(struct test_loader *tester,
704 			 struct test_spec *spec,
705 			 struct bpf_object *obj,
706 			 struct bpf_program *prog)
707 {
708 	int min_log_level = 0, prog_flags;
709 
710 	if (env.verbosity > VERBOSE_NONE)
711 		min_log_level = 1;
712 	if (env.verbosity > VERBOSE_VERY)
713 		min_log_level = 2;
714 
715 	bpf_program__set_log_buf(prog, tester->log_buf, tester->log_buf_sz);
716 
717 	/* Make sure we set at least minimal log level, unless test requires
718 	 * even higher level already. Make sure to preserve independent log
719 	 * level 4 (verifier stats), though.
720 	 */
721 	if ((spec->log_level & 3) < min_log_level)
722 		bpf_program__set_log_level(prog, (spec->log_level & 4) | min_log_level);
723 	else
724 		bpf_program__set_log_level(prog, spec->log_level);
725 
726 	prog_flags = bpf_program__flags(prog);
727 	bpf_program__set_flags(prog, prog_flags | spec->prog_flags);
728 
729 	tester->log_buf[0] = '\0';
730 }
731 
732 static void emit_verifier_log(const char *log_buf, bool force)
733 {
734 	if (!force && env.verbosity == VERBOSE_NONE)
735 		return;
736 	fprintf(stdout, "VERIFIER LOG:\n=============\n%s=============\n", log_buf);
737 }
738 
739 static void emit_xlated(const char *xlated, bool force)
740 {
741 	if (!force && env.verbosity == VERBOSE_NONE)
742 		return;
743 	fprintf(stdout, "XLATED:\n=============\n%s=============\n", xlated);
744 }
745 
746 static void emit_jited(const char *jited, bool force)
747 {
748 	if (!force && env.verbosity == VERBOSE_NONE)
749 		return;
750 	fprintf(stdout, "JITED:\n=============\n%s=============\n", jited);
751 }
752 
753 static void emit_stderr(const char *stderr, bool force)
754 {
755 	if (!force && env.verbosity == VERBOSE_NONE)
756 		return;
757 	fprintf(stdout, "STDERR:\n=============\n%s=============\n", stderr);
758 }
759 
760 static void emit_stdout(const char *bpf_stdout, bool force)
761 {
762 	if (!force && env.verbosity == VERBOSE_NONE)
763 		return;
764 	fprintf(stdout, "STDOUT:\n=============\n%s=============\n", bpf_stdout);
765 }
766 
767 static void validate_msgs(char *log_buf, struct expected_msgs *msgs,
768 			  void (*emit_fn)(const char *buf, bool force))
769 {
770 	const char *log = log_buf, *prev_match;
771 	regmatch_t reg_match[1];
772 	int prev_match_line;
773 	int match_line;
774 	int i, j, err;
775 
776 	prev_match_line = -1;
777 	match_line = 0;
778 	prev_match = log;
779 	for (i = 0; i < msgs->cnt; i++) {
780 		struct expect_msg *msg = &msgs->patterns[i];
781 		const char *match = NULL, *pat_status;
782 		bool wrong_line = false;
783 
784 		if (!msg->is_regex) {
785 			match = strstr(log, msg->substr);
786 			if (match)
787 				log = match + strlen(msg->substr);
788 		} else {
789 			err = regexec(&msg->regex, log, 1, reg_match, 0);
790 			if (err == 0) {
791 				match = log + reg_match[0].rm_so;
792 				log += reg_match[0].rm_eo;
793 			}
794 		}
795 
796 		if (match) {
797 			for (; prev_match < match; ++prev_match)
798 				if (*prev_match == '\n')
799 					++match_line;
800 			wrong_line = msg->on_next_line && prev_match_line >= 0 &&
801 				     prev_match_line + 1 != match_line;
802 		}
803 
804 		if (!match || wrong_line) {
805 			PRINT_FAIL("expect_msg\n");
806 			if (env.verbosity == VERBOSE_NONE)
807 				emit_fn(log_buf, true /*force*/);
808 			for (j = 0; j <= i; j++) {
809 				msg = &msgs->patterns[j];
810 				if (j < i)
811 					pat_status = "MATCHED   ";
812 				else if (wrong_line)
813 					pat_status = "WRONG LINE";
814 				else
815 					pat_status = "EXPECTED  ";
816 				msg = &msgs->patterns[j];
817 				fprintf(stderr, "%s %s: '%s'\n",
818 					pat_status,
819 					msg->is_regex ? " REGEX" : "SUBSTR",
820 					msg->substr);
821 			}
822 			if (wrong_line) {
823 				fprintf(stderr,
824 					"expecting match at line %d, actual match is at line %d\n",
825 					prev_match_line + 1, match_line);
826 			}
827 			break;
828 		}
829 
830 		prev_match_line = match_line;
831 	}
832 }
833 
834 struct cap_state {
835 	__u64 old_caps;
836 	bool initialized;
837 };
838 
839 static int drop_capabilities(struct cap_state *caps)
840 {
841 	const __u64 caps_to_drop = (1ULL << CAP_SYS_ADMIN | 1ULL << CAP_NET_ADMIN |
842 				    1ULL << CAP_PERFMON   | 1ULL << CAP_BPF);
843 	int err;
844 
845 	err = cap_disable_effective(caps_to_drop, &caps->old_caps);
846 	if (err) {
847 		PRINT_FAIL("failed to drop capabilities: %i, %s\n", err, strerror(-err));
848 		return err;
849 	}
850 
851 	caps->initialized = true;
852 	return 0;
853 }
854 
855 static int restore_capabilities(struct cap_state *caps)
856 {
857 	int err;
858 
859 	if (!caps->initialized)
860 		return 0;
861 
862 	err = cap_enable_effective(caps->old_caps, NULL);
863 	if (err)
864 		PRINT_FAIL("failed to restore capabilities: %i, %s\n", err, strerror(-err));
865 	caps->initialized = false;
866 	return err;
867 }
868 
869 static bool can_execute_unpriv(struct test_loader *tester, struct test_spec *spec)
870 {
871 	if (sysctl_unpriv_disabled < 0)
872 		sysctl_unpriv_disabled = get_unpriv_disabled() ? 1 : 0;
873 	if (sysctl_unpriv_disabled)
874 		return false;
875 	if ((spec->prog_flags & BPF_F_ANY_ALIGNMENT) && !EFFICIENT_UNALIGNED_ACCESS)
876 		return false;
877 	return true;
878 }
879 
880 static bool is_unpriv_capable_map(struct bpf_map *map)
881 {
882 	enum bpf_map_type type;
883 	__u32 flags;
884 
885 	type = bpf_map__type(map);
886 
887 	switch (type) {
888 	case BPF_MAP_TYPE_HASH:
889 	case BPF_MAP_TYPE_PERCPU_HASH:
890 	case BPF_MAP_TYPE_HASH_OF_MAPS:
891 		flags = bpf_map__map_flags(map);
892 		return !(flags & BPF_F_ZERO_SEED);
893 	case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
894 	case BPF_MAP_TYPE_ARRAY:
895 	case BPF_MAP_TYPE_RINGBUF:
896 	case BPF_MAP_TYPE_PROG_ARRAY:
897 	case BPF_MAP_TYPE_CGROUP_ARRAY:
898 	case BPF_MAP_TYPE_PERCPU_ARRAY:
899 	case BPF_MAP_TYPE_USER_RINGBUF:
900 	case BPF_MAP_TYPE_ARRAY_OF_MAPS:
901 	case BPF_MAP_TYPE_CGROUP_STORAGE:
902 	case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
903 		return true;
904 	default:
905 		return false;
906 	}
907 }
908 
909 static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts)
910 {
911 	__u8 tmp_out[TEST_DATA_LEN << 2] = {};
912 	__u8 tmp_in[TEST_DATA_LEN] = {};
913 	int err, saved_errno;
914 	LIBBPF_OPTS(bpf_test_run_opts, topts,
915 		.data_in = tmp_in,
916 		.data_size_in = sizeof(tmp_in),
917 		.data_out = tmp_out,
918 		.data_size_out = sizeof(tmp_out),
919 		.repeat = 1,
920 	);
921 
922 	if (empty_opts) {
923 		memset(&topts, 0, sizeof(struct bpf_test_run_opts));
924 		topts.sz = sizeof(struct bpf_test_run_opts);
925 	}
926 	err = bpf_prog_test_run_opts(fd_prog, &topts);
927 	saved_errno = errno;
928 
929 	if (err) {
930 		PRINT_FAIL("FAIL: Unexpected bpf_prog_test_run error: %d (%s) ",
931 			   saved_errno, strerror(saved_errno));
932 		return err;
933 	}
934 
935 	ASSERT_OK(0, "bpf_prog_test_run");
936 	*retval = topts.retval;
937 
938 	return 0;
939 }
940 
941 static bool should_do_test_run(struct test_spec *spec, struct test_subspec *subspec)
942 {
943 	if (!subspec->execute)
944 		return false;
945 
946 	if (subspec->expect_failure)
947 		return false;
948 
949 	if ((spec->prog_flags & BPF_F_ANY_ALIGNMENT) && !EFFICIENT_UNALIGNED_ACCESS) {
950 		if (env.verbosity != VERBOSE_NONE)
951 			printf("alignment prevents execution\n");
952 		return false;
953 	}
954 
955 	return true;
956 }
957 
958 /* Get a disassembly of BPF program after verifier applies all rewrites */
959 static int get_xlated_program_text(int prog_fd, char *text, size_t text_sz)
960 {
961 	struct bpf_insn *insn_start = NULL, *insn, *insn_end;
962 	__u32 insns_cnt = 0, i;
963 	char buf[64];
964 	FILE *out = NULL;
965 	int err;
966 
967 	err = get_xlated_program(prog_fd, &insn_start, &insns_cnt);
968 	if (!ASSERT_OK(err, "get_xlated_program"))
969 		goto out;
970 	out = fmemopen(text, text_sz, "w");
971 	if (!ASSERT_OK_PTR(out, "open_memstream"))
972 		goto out;
973 	insn_end = insn_start + insns_cnt;
974 	insn = insn_start;
975 	while (insn < insn_end) {
976 		i = insn - insn_start;
977 		insn = disasm_insn(insn, buf, sizeof(buf));
978 		fprintf(out, "%d: %s\n", i, buf);
979 	}
980 	fflush(out);
981 
982 out:
983 	free(insn_start);
984 	if (out)
985 		fclose(out);
986 	return err;
987 }
988 
989 /* Read the bpf stream corresponding to the stream_id */
990 static int get_stream(int stream_id, int prog_fd, char *text, size_t text_sz)
991 {
992 	LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts);
993 	int ret;
994 
995 	ret = bpf_prog_stream_read(prog_fd, stream_id, text, text_sz, &ropts);
996 	ASSERT_GT(ret, 0, "stream read");
997 	text[ret] = '\0';
998 
999 	return ret;
1000 }
1001 
1002 /* this function is forced noinline and has short generic name to look better
1003  * in test_progs output (in case of a failure)
1004  */
1005 static noinline
1006 void run_subtest(struct test_loader *tester,
1007 		 struct bpf_object_open_opts *open_opts,
1008 		 const void *obj_bytes,
1009 		 size_t obj_byte_cnt,
1010 		 struct test_spec *specs,
1011 		 struct test_spec *spec,
1012 		 bool unpriv)
1013 {
1014 	struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv;
1015 	int current_runtime = is_jit_enabled() ? JITED : NO_JITED;
1016 	struct bpf_program *tprog = NULL, *tprog_iter;
1017 	struct bpf_link *link, *links[32] = {};
1018 	struct test_spec *spec_iter;
1019 	struct cap_state caps = {};
1020 	struct bpf_object *tobj;
1021 	struct bpf_map *map;
1022 	int retval, err, i;
1023 	int links_cnt = 0;
1024 	bool should_load;
1025 
1026 	if (!test__start_subtest(subspec->name))
1027 		return;
1028 
1029 	if ((get_current_arch() & spec->arch_mask) == 0) {
1030 		test__skip();
1031 		return;
1032 	}
1033 
1034 	if ((current_runtime & spec->load_mask) == 0) {
1035 		test__skip();
1036 		return;
1037 	}
1038 
1039 	if (unpriv) {
1040 		if (!can_execute_unpriv(tester, spec)) {
1041 			test__skip();
1042 			test__end_subtest();
1043 			return;
1044 		}
1045 		if (drop_capabilities(&caps)) {
1046 			test__end_subtest();
1047 			return;
1048 		}
1049 		if (subspec->caps) {
1050 			err = cap_enable_effective(subspec->caps, NULL);
1051 			if (err) {
1052 				PRINT_FAIL("failed to set capabilities: %i, %s\n", err, strerror(-err));
1053 				goto subtest_cleanup;
1054 			}
1055 		}
1056 	}
1057 
1058 	/* Implicitly reset to NULL if next test case doesn't specify */
1059 	open_opts->btf_custom_path = spec->btf_custom_path;
1060 
1061 	tobj = bpf_object__open_mem(obj_bytes, obj_byte_cnt, open_opts);
1062 	if (!ASSERT_OK_PTR(tobj, "obj_open_mem")) /* shouldn't happen */
1063 		goto subtest_cleanup;
1064 
1065 	i = 0;
1066 	bpf_object__for_each_program(tprog_iter, tobj) {
1067 		spec_iter = &specs[i++];
1068 		should_load = false;
1069 
1070 		if (spec_iter->valid) {
1071 			if (strcmp(bpf_program__name(tprog_iter), spec->prog_name) == 0) {
1072 				tprog = tprog_iter;
1073 				should_load = true;
1074 			}
1075 
1076 			if (spec_iter->auxiliary &&
1077 			    spec_iter->mode_mask & (unpriv ? UNPRIV : PRIV))
1078 				should_load = true;
1079 		}
1080 
1081 		bpf_program__set_autoload(tprog_iter, should_load);
1082 	}
1083 
1084 	prepare_case(tester, spec, tobj, tprog);
1085 
1086 	/* By default bpf_object__load() automatically creates all
1087 	 * maps declared in the skeleton. Some map types are only
1088 	 * allowed in priv mode. Disable autoload for such maps in
1089 	 * unpriv mode.
1090 	 */
1091 	bpf_object__for_each_map(map, tobj)
1092 		bpf_map__set_autocreate(map, !unpriv || is_unpriv_capable_map(map));
1093 
1094 	err = bpf_object__load(tobj);
1095 	if (subspec->expect_failure) {
1096 		if (!ASSERT_ERR(err, "unexpected_load_success")) {
1097 			emit_verifier_log(tester->log_buf, false /*force*/);
1098 			goto tobj_cleanup;
1099 		}
1100 	} else {
1101 		if (!ASSERT_OK(err, "unexpected_load_failure")) {
1102 			emit_verifier_log(tester->log_buf, true /*force*/);
1103 			goto tobj_cleanup;
1104 		}
1105 	}
1106 	emit_verifier_log(tester->log_buf, false /*force*/);
1107 	validate_msgs(tester->log_buf, &subspec->expect_msgs, emit_verifier_log);
1108 
1109 	/* Restore capabilities because the kernel will silently ignore requests
1110 	 * for program info (such as xlated program text) if we are not
1111 	 * bpf-capable. Also, for some reason test_verifier executes programs
1112 	 * with all capabilities restored. Do the same here.
1113 	 */
1114 	if (restore_capabilities(&caps))
1115 		goto tobj_cleanup;
1116 
1117 	if (subspec->expect_xlated.cnt) {
1118 		err = get_xlated_program_text(bpf_program__fd(tprog),
1119 					      tester->log_buf, tester->log_buf_sz);
1120 		if (err)
1121 			goto tobj_cleanup;
1122 		emit_xlated(tester->log_buf, false /*force*/);
1123 		validate_msgs(tester->log_buf, &subspec->expect_xlated, emit_xlated);
1124 	}
1125 
1126 	if (subspec->jited.cnt) {
1127 		err = get_jited_program_text(bpf_program__fd(tprog),
1128 					     tester->log_buf, tester->log_buf_sz);
1129 		if (err == -EOPNOTSUPP) {
1130 			printf("%s:SKIP: jited programs disassembly is not supported,\n", __func__);
1131 			printf("%s:SKIP: tests are built w/o LLVM development libs\n", __func__);
1132 			test__skip();
1133 			goto tobj_cleanup;
1134 		}
1135 		if (!ASSERT_EQ(err, 0, "get_jited_program_text"))
1136 			goto tobj_cleanup;
1137 		emit_jited(tester->log_buf, false /*force*/);
1138 		validate_msgs(tester->log_buf, &subspec->jited, emit_jited);
1139 	}
1140 
1141 	if (should_do_test_run(spec, subspec)) {
1142 		/* Do bpf_map__attach_struct_ops() for each struct_ops map.
1143 		 * This should trigger bpf_struct_ops->reg callback on kernel side.
1144 		 */
1145 		bpf_object__for_each_map(map, tobj) {
1146 			if (!bpf_map__autocreate(map) ||
1147 			    bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
1148 				continue;
1149 			if (links_cnt >= ARRAY_SIZE(links)) {
1150 				PRINT_FAIL("too many struct_ops maps");
1151 				goto tobj_cleanup;
1152 			}
1153 			link = bpf_map__attach_struct_ops(map);
1154 			if (!link) {
1155 				PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%d\n",
1156 					   bpf_map__name(map), -errno);
1157 				goto tobj_cleanup;
1158 			}
1159 			links[links_cnt++] = link;
1160 		}
1161 
1162 		if (tester->pre_execution_cb) {
1163 			err = tester->pre_execution_cb(tobj);
1164 			if (err) {
1165 				PRINT_FAIL("pre_execution_cb failed: %d\n", err);
1166 				goto tobj_cleanup;
1167 			}
1168 		}
1169 
1170 		err = do_prog_test_run(bpf_program__fd(tprog), &retval,
1171 				       bpf_program__type(tprog) == BPF_PROG_TYPE_SYSCALL ? true : false);
1172 		if (!err && retval != subspec->retval && subspec->retval != POINTER_VALUE) {
1173 			PRINT_FAIL("Unexpected retval: %d != %d\n", retval, subspec->retval);
1174 			goto tobj_cleanup;
1175 		}
1176 
1177 		if (subspec->stderr.cnt) {
1178 			err = get_stream(2, bpf_program__fd(tprog),
1179 					 tester->log_buf, tester->log_buf_sz);
1180 			if (err <= 0) {
1181 				PRINT_FAIL("Unexpected retval from get_stream(): %d, errno = %d\n",
1182 					   err, errno);
1183 				goto tobj_cleanup;
1184 			}
1185 			emit_stderr(tester->log_buf, false /*force*/);
1186 			validate_msgs(tester->log_buf, &subspec->stderr, emit_stderr);
1187 		}
1188 
1189 		if (subspec->stdout.cnt) {
1190 			err = get_stream(1, bpf_program__fd(tprog),
1191 					 tester->log_buf, tester->log_buf_sz);
1192 			if (err <= 0) {
1193 				PRINT_FAIL("Unexpected retval from get_stream(): %d, errno = %d\n",
1194 					   err, errno);
1195 				goto tobj_cleanup;
1196 			}
1197 			emit_stdout(tester->log_buf, false /*force*/);
1198 			validate_msgs(tester->log_buf, &subspec->stdout, emit_stdout);
1199 		}
1200 
1201 		/* redo bpf_map__attach_struct_ops for each test */
1202 		while (links_cnt > 0)
1203 			bpf_link__destroy(links[--links_cnt]);
1204 	}
1205 
1206 tobj_cleanup:
1207 	while (links_cnt > 0)
1208 		bpf_link__destroy(links[--links_cnt]);
1209 	bpf_object__close(tobj);
1210 subtest_cleanup:
1211 	test__end_subtest();
1212 	restore_capabilities(&caps);
1213 }
1214 
1215 static void process_subtest(struct test_loader *tester,
1216 			    const char *skel_name,
1217 			    skel_elf_bytes_fn elf_bytes_factory)
1218 {
1219 	LIBBPF_OPTS(bpf_object_open_opts, open_opts, .object_name = skel_name);
1220 	struct test_spec *specs = NULL;
1221 	struct bpf_object *obj = NULL;
1222 	struct bpf_program *prog;
1223 	const void *obj_bytes;
1224 	int err, i, nr_progs;
1225 	size_t obj_byte_cnt;
1226 
1227 	if (tester_init(tester) < 0)
1228 		return; /* failed to initialize tester */
1229 
1230 	obj_bytes = elf_bytes_factory(&obj_byte_cnt);
1231 	obj = bpf_object__open_mem(obj_bytes, obj_byte_cnt, &open_opts);
1232 	if (!ASSERT_OK_PTR(obj, "obj_open_mem"))
1233 		return;
1234 
1235 	nr_progs = 0;
1236 	bpf_object__for_each_program(prog, obj)
1237 		++nr_progs;
1238 
1239 	specs = calloc(nr_progs, sizeof(struct test_spec));
1240 	if (!ASSERT_OK_PTR(specs, "specs_alloc"))
1241 		return;
1242 
1243 	i = 0;
1244 	bpf_object__for_each_program(prog, obj) {
1245 		/* ignore tests for which  we can't derive test specification */
1246 		err = parse_test_spec(tester, obj, prog, &specs[i++]);
1247 		if (err)
1248 			PRINT_FAIL("Can't parse test spec for program '%s'\n",
1249 				   bpf_program__name(prog));
1250 	}
1251 
1252 	i = 0;
1253 	bpf_object__for_each_program(prog, obj) {
1254 		struct test_spec *spec = &specs[i++];
1255 
1256 		if (!spec->valid || spec->auxiliary)
1257 			continue;
1258 
1259 		if (spec->mode_mask & PRIV)
1260 			run_subtest(tester, &open_opts, obj_bytes, obj_byte_cnt,
1261 				    specs, spec, false);
1262 		if (spec->mode_mask & UNPRIV)
1263 			run_subtest(tester, &open_opts, obj_bytes, obj_byte_cnt,
1264 				    specs, spec, true);
1265 
1266 	}
1267 
1268 	for (i = 0; i < nr_progs; ++i)
1269 		free_test_spec(&specs[i]);
1270 	free(specs);
1271 	bpf_object__close(obj);
1272 }
1273 
1274 void test_loader__run_subtests(struct test_loader *tester,
1275 			       const char *skel_name,
1276 			       skel_elf_bytes_fn elf_bytes_factory)
1277 {
1278 	/* see comment in run_subtest() for why we do this function nesting */
1279 	process_subtest(tester, skel_name, elf_bytes_factory);
1280 }
1281