1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #define _GNU_SOURCE
5 #include "test_progs.h"
6 #include "cgroup_helpers.h"
7 #include "bpf_rlimit.h"
8 #include <argp.h>
9 #include <pthread.h>
10 #include <sched.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <execinfo.h> /* backtrace */
14 
15 /* defined in test_progs.h */
16 struct test_env env = {};
17 
18 struct prog_test_def {
19 	const char *test_name;
20 	int test_num;
21 	void (*run_test)(void);
22 	bool force_log;
23 	int error_cnt;
24 	int skip_cnt;
25 	bool tested;
26 	bool need_cgroup_cleanup;
27 
28 	char *subtest_name;
29 	int subtest_num;
30 
31 	/* store counts before subtest started */
32 	int old_error_cnt;
33 };
34 
35 /* Override C runtime library's usleep() implementation to ensure nanosleep()
36  * is always called. Usleep is frequently used in selftests as a way to
37  * trigger kprobe and tracepoints.
38  */
39 int usleep(useconds_t usec)
40 {
41 	struct timespec ts = {
42 		.tv_sec = usec / 1000000,
43 		.tv_nsec = (usec % 1000000) * 1000,
44 	};
45 
46 	return syscall(__NR_nanosleep, &ts, NULL);
47 }
48 
49 static bool should_run(struct test_selector *sel, int num, const char *name)
50 {
51 	int i;
52 
53 	for (i = 0; i < sel->blacklist.cnt; i++) {
54 		if (strstr(name, sel->blacklist.strs[i]))
55 			return false;
56 	}
57 
58 	for (i = 0; i < sel->whitelist.cnt; i++) {
59 		if (strstr(name, sel->whitelist.strs[i]))
60 			return true;
61 	}
62 
63 	if (!sel->whitelist.cnt && !sel->num_set)
64 		return true;
65 
66 	return num < sel->num_set_len && sel->num_set[num];
67 }
68 
69 static void dump_test_log(const struct prog_test_def *test, bool failed)
70 {
71 	if (stdout == env.stdout)
72 		return;
73 
74 	fflush(stdout); /* exports env.log_buf & env.log_cnt */
75 
76 	if (env.verbosity > VERBOSE_NONE || test->force_log || failed) {
77 		if (env.log_cnt) {
78 			env.log_buf[env.log_cnt] = '\0';
79 			fprintf(env.stdout, "%s", env.log_buf);
80 			if (env.log_buf[env.log_cnt - 1] != '\n')
81 				fprintf(env.stdout, "\n");
82 		}
83 	}
84 
85 	fseeko(stdout, 0, SEEK_SET); /* rewind */
86 }
87 
88 static void skip_account(void)
89 {
90 	if (env.test->skip_cnt) {
91 		env.skip_cnt++;
92 		env.test->skip_cnt = 0;
93 	}
94 }
95 
96 static void stdio_restore(void);
97 
98 /* A bunch of tests set custom affinity per-thread and/or per-process. Reset
99  * it after each test/sub-test.
100  */
101 static void reset_affinity() {
102 
103 	cpu_set_t cpuset;
104 	int i, err;
105 
106 	CPU_ZERO(&cpuset);
107 	for (i = 0; i < env.nr_cpus; i++)
108 		CPU_SET(i, &cpuset);
109 
110 	err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
111 	if (err < 0) {
112 		stdio_restore();
113 		fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
114 		exit(-1);
115 	}
116 	err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
117 	if (err < 0) {
118 		stdio_restore();
119 		fprintf(stderr, "Failed to reset thread affinity: %d!\n", err);
120 		exit(-1);
121 	}
122 }
123 
124 void test__end_subtest()
125 {
126 	struct prog_test_def *test = env.test;
127 	int sub_error_cnt = test->error_cnt - test->old_error_cnt;
128 
129 	if (sub_error_cnt)
130 		env.fail_cnt++;
131 	else
132 		env.sub_succ_cnt++;
133 	skip_account();
134 
135 	dump_test_log(test, sub_error_cnt);
136 
137 	fprintf(env.stdout, "#%d/%d %s:%s\n",
138 	       test->test_num, test->subtest_num,
139 	       test->subtest_name, sub_error_cnt ? "FAIL" : "OK");
140 
141 	reset_affinity();
142 
143 	free(test->subtest_name);
144 	test->subtest_name = NULL;
145 }
146 
147 bool test__start_subtest(const char *name)
148 {
149 	struct prog_test_def *test = env.test;
150 
151 	if (test->subtest_name)
152 		test__end_subtest();
153 
154 	test->subtest_num++;
155 
156 	if (!name || !name[0]) {
157 		fprintf(env.stderr,
158 			"Subtest #%d didn't provide sub-test name!\n",
159 			test->subtest_num);
160 		return false;
161 	}
162 
163 	if (!should_run(&env.subtest_selector, test->subtest_num, name))
164 		return false;
165 
166 	test->subtest_name = strdup(name);
167 	if (!test->subtest_name) {
168 		fprintf(env.stderr,
169 			"Subtest #%d: failed to copy subtest name!\n",
170 			test->subtest_num);
171 		return false;
172 	}
173 	env.test->old_error_cnt = env.test->error_cnt;
174 
175 	return true;
176 }
177 
178 void test__force_log() {
179 	env.test->force_log = true;
180 }
181 
182 void test__skip(void)
183 {
184 	env.test->skip_cnt++;
185 }
186 
187 void test__fail(void)
188 {
189 	env.test->error_cnt++;
190 }
191 
192 int test__join_cgroup(const char *path)
193 {
194 	int fd;
195 
196 	if (!env.test->need_cgroup_cleanup) {
197 		if (setup_cgroup_environment()) {
198 			fprintf(stderr,
199 				"#%d %s: Failed to setup cgroup environment\n",
200 				env.test->test_num, env.test->test_name);
201 			return -1;
202 		}
203 
204 		env.test->need_cgroup_cleanup = true;
205 	}
206 
207 	fd = create_and_get_cgroup(path);
208 	if (fd < 0) {
209 		fprintf(stderr,
210 			"#%d %s: Failed to create cgroup '%s' (errno=%d)\n",
211 			env.test->test_num, env.test->test_name, path, errno);
212 		return fd;
213 	}
214 
215 	if (join_cgroup(path)) {
216 		fprintf(stderr,
217 			"#%d %s: Failed to join cgroup '%s' (errno=%d)\n",
218 			env.test->test_num, env.test->test_name, path, errno);
219 		return -1;
220 	}
221 
222 	return fd;
223 }
224 
225 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
226 {
227 	struct bpf_map *map;
228 
229 	map = bpf_object__find_map_by_name(obj, name);
230 	if (!map) {
231 		fprintf(stdout, "%s:FAIL:map '%s' not found\n", test, name);
232 		test__fail();
233 		return -1;
234 	}
235 	return bpf_map__fd(map);
236 }
237 
238 static bool is_jit_enabled(void)
239 {
240 	const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
241 	bool enabled = false;
242 	int sysctl_fd;
243 
244 	sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
245 	if (sysctl_fd != -1) {
246 		char tmpc;
247 
248 		if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
249 			enabled = (tmpc != '0');
250 		close(sysctl_fd);
251 	}
252 
253 	return enabled;
254 }
255 
256 int compare_map_keys(int map1_fd, int map2_fd)
257 {
258 	__u32 key, next_key;
259 	char val_buf[PERF_MAX_STACK_DEPTH *
260 		     sizeof(struct bpf_stack_build_id)];
261 	int err;
262 
263 	err = bpf_map_get_next_key(map1_fd, NULL, &key);
264 	if (err)
265 		return err;
266 	err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
267 	if (err)
268 		return err;
269 
270 	while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
271 		err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
272 		if (err)
273 			return err;
274 
275 		key = next_key;
276 	}
277 	if (errno != ENOENT)
278 		return -1;
279 
280 	return 0;
281 }
282 
283 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
284 {
285 	__u32 key, next_key, *cur_key_p, *next_key_p;
286 	char *val_buf1, *val_buf2;
287 	int i, err = 0;
288 
289 	val_buf1 = malloc(stack_trace_len);
290 	val_buf2 = malloc(stack_trace_len);
291 	cur_key_p = NULL;
292 	next_key_p = &key;
293 	while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
294 		err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
295 		if (err)
296 			goto out;
297 		err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
298 		if (err)
299 			goto out;
300 		for (i = 0; i < stack_trace_len; i++) {
301 			if (val_buf1[i] != val_buf2[i]) {
302 				err = -1;
303 				goto out;
304 			}
305 		}
306 		key = *next_key_p;
307 		cur_key_p = &key;
308 		next_key_p = &next_key;
309 	}
310 	if (errno != ENOENT)
311 		err = -1;
312 
313 out:
314 	free(val_buf1);
315 	free(val_buf2);
316 	return err;
317 }
318 
319 int extract_build_id(char *build_id, size_t size)
320 {
321 	FILE *fp;
322 	char *line = NULL;
323 	size_t len = 0;
324 
325 	fp = popen("readelf -n ./urandom_read | grep 'Build ID'", "r");
326 	if (fp == NULL)
327 		return -1;
328 
329 	if (getline(&line, &len, fp) == -1)
330 		goto err;
331 	fclose(fp);
332 
333 	if (len > size)
334 		len = size;
335 	memcpy(build_id, line, len);
336 	build_id[len] = '\0';
337 	free(line);
338 	return 0;
339 err:
340 	fclose(fp);
341 	return -1;
342 }
343 
344 /* extern declarations for test funcs */
345 #define DEFINE_TEST(name) extern void test_##name(void);
346 #include <prog_tests/tests.h>
347 #undef DEFINE_TEST
348 
349 static struct prog_test_def prog_test_defs[] = {
350 #define DEFINE_TEST(name) {		\
351 	.test_name = #name,		\
352 	.run_test = &test_##name,	\
353 },
354 #include <prog_tests/tests.h>
355 #undef DEFINE_TEST
356 };
357 const int prog_test_cnt = ARRAY_SIZE(prog_test_defs);
358 
359 const char *argp_program_version = "test_progs 0.1";
360 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
361 const char argp_program_doc[] = "BPF selftests test runner";
362 
363 enum ARG_KEYS {
364 	ARG_TEST_NUM = 'n',
365 	ARG_TEST_NAME = 't',
366 	ARG_TEST_NAME_BLACKLIST = 'b',
367 	ARG_VERIFIER_STATS = 's',
368 	ARG_VERBOSE = 'v',
369 };
370 
371 static const struct argp_option opts[] = {
372 	{ "num", ARG_TEST_NUM, "NUM", 0,
373 	  "Run test number NUM only " },
374 	{ "name", ARG_TEST_NAME, "NAMES", 0,
375 	  "Run tests with names containing any string from NAMES list" },
376 	{ "name-blacklist", ARG_TEST_NAME_BLACKLIST, "NAMES", 0,
377 	  "Don't run tests with names containing any string from NAMES list" },
378 	{ "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
379 	  "Output verifier statistics", },
380 	{ "verbose", ARG_VERBOSE, "LEVEL", OPTION_ARG_OPTIONAL,
381 	  "Verbose output (use -vv or -vvv for progressively verbose output)" },
382 	{},
383 };
384 
385 static int libbpf_print_fn(enum libbpf_print_level level,
386 			   const char *format, va_list args)
387 {
388 	if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG)
389 		return 0;
390 	vfprintf(stdout, format, args);
391 	return 0;
392 }
393 
394 static void free_str_set(const struct str_set *set)
395 {
396 	int i;
397 
398 	if (!set)
399 		return;
400 
401 	for (i = 0; i < set->cnt; i++)
402 		free((void *)set->strs[i]);
403 	free(set->strs);
404 }
405 
406 static int parse_str_list(const char *s, struct str_set *set)
407 {
408 	char *input, *state = NULL, *next, **tmp, **strs = NULL;
409 	int cnt = 0;
410 
411 	input = strdup(s);
412 	if (!input)
413 		return -ENOMEM;
414 
415 	set->cnt = 0;
416 	set->strs = NULL;
417 
418 	while ((next = strtok_r(state ? NULL : input, ",", &state))) {
419 		tmp = realloc(strs, sizeof(*strs) * (cnt + 1));
420 		if (!tmp)
421 			goto err;
422 		strs = tmp;
423 
424 		strs[cnt] = strdup(next);
425 		if (!strs[cnt])
426 			goto err;
427 
428 		cnt++;
429 	}
430 
431 	set->cnt = cnt;
432 	set->strs = (const char **)strs;
433 	free(input);
434 	return 0;
435 err:
436 	free(strs);
437 	free(input);
438 	return -ENOMEM;
439 }
440 
441 extern int extra_prog_load_log_flags;
442 
443 static error_t parse_arg(int key, char *arg, struct argp_state *state)
444 {
445 	struct test_env *env = state->input;
446 
447 	switch (key) {
448 	case ARG_TEST_NUM: {
449 		char *subtest_str = strchr(arg, '/');
450 
451 		if (subtest_str) {
452 			*subtest_str = '\0';
453 			if (parse_num_list(subtest_str + 1,
454 					   &env->subtest_selector.num_set,
455 					   &env->subtest_selector.num_set_len)) {
456 				fprintf(stderr,
457 					"Failed to parse subtest numbers.\n");
458 				return -EINVAL;
459 			}
460 		}
461 		if (parse_num_list(arg, &env->test_selector.num_set,
462 				   &env->test_selector.num_set_len)) {
463 			fprintf(stderr, "Failed to parse test numbers.\n");
464 			return -EINVAL;
465 		}
466 		break;
467 	}
468 	case ARG_TEST_NAME: {
469 		char *subtest_str = strchr(arg, '/');
470 
471 		if (subtest_str) {
472 			*subtest_str = '\0';
473 			if (parse_str_list(subtest_str + 1,
474 					   &env->subtest_selector.whitelist))
475 				return -ENOMEM;
476 		}
477 		if (parse_str_list(arg, &env->test_selector.whitelist))
478 			return -ENOMEM;
479 		break;
480 	}
481 	case ARG_TEST_NAME_BLACKLIST: {
482 		char *subtest_str = strchr(arg, '/');
483 
484 		if (subtest_str) {
485 			*subtest_str = '\0';
486 			if (parse_str_list(subtest_str + 1,
487 					   &env->subtest_selector.blacklist))
488 				return -ENOMEM;
489 		}
490 		if (parse_str_list(arg, &env->test_selector.blacklist))
491 			return -ENOMEM;
492 		break;
493 	}
494 	case ARG_VERIFIER_STATS:
495 		env->verifier_stats = true;
496 		break;
497 	case ARG_VERBOSE:
498 		env->verbosity = VERBOSE_NORMAL;
499 		if (arg) {
500 			if (strcmp(arg, "v") == 0) {
501 				env->verbosity = VERBOSE_VERY;
502 				extra_prog_load_log_flags = 1;
503 			} else if (strcmp(arg, "vv") == 0) {
504 				env->verbosity = VERBOSE_SUPER;
505 				extra_prog_load_log_flags = 2;
506 			} else {
507 				fprintf(stderr,
508 					"Unrecognized verbosity setting ('%s'), only -v and -vv are supported\n",
509 					arg);
510 				return -EINVAL;
511 			}
512 		}
513 		break;
514 	case ARGP_KEY_ARG:
515 		argp_usage(state);
516 		break;
517 	case ARGP_KEY_END:
518 		break;
519 	default:
520 		return ARGP_ERR_UNKNOWN;
521 	}
522 	return 0;
523 }
524 
525 static void stdio_hijack(void)
526 {
527 #ifdef __GLIBC__
528 	env.stdout = stdout;
529 	env.stderr = stderr;
530 
531 	if (env.verbosity > VERBOSE_NONE) {
532 		/* nothing to do, output to stdout by default */
533 		return;
534 	}
535 
536 	/* stdout and stderr -> buffer */
537 	fflush(stdout);
538 
539 	stdout = open_memstream(&env.log_buf, &env.log_cnt);
540 	if (!stdout) {
541 		stdout = env.stdout;
542 		perror("open_memstream");
543 		return;
544 	}
545 
546 	stderr = stdout;
547 #endif
548 }
549 
550 static void stdio_restore(void)
551 {
552 #ifdef __GLIBC__
553 	if (stdout == env.stdout)
554 		return;
555 
556 	fclose(stdout);
557 	free(env.log_buf);
558 
559 	env.log_buf = NULL;
560 	env.log_cnt = 0;
561 
562 	stdout = env.stdout;
563 	stderr = env.stderr;
564 #endif
565 }
566 
567 /*
568  * Determine if test_progs is running as a "flavored" test runner and switch
569  * into corresponding sub-directory to load correct BPF objects.
570  *
571  * This is done by looking at executable name. If it contains "-flavor"
572  * suffix, then we are running as a flavored test runner.
573  */
574 int cd_flavor_subdir(const char *exec_name)
575 {
576 	/* General form of argv[0] passed here is:
577 	 * some/path/to/test_progs[-flavor], where -flavor part is optional.
578 	 * First cut out "test_progs[-flavor]" part, then extract "flavor"
579 	 * part, if it's there.
580 	 */
581 	const char *flavor = strrchr(exec_name, '/');
582 
583 	if (!flavor)
584 		return 0;
585 	flavor++;
586 	flavor = strrchr(flavor, '-');
587 	if (!flavor)
588 		return 0;
589 	flavor++;
590 	fprintf(stdout, "Switching to flavor '%s' subdirectory...\n", flavor);
591 	return chdir(flavor);
592 }
593 
594 #define MAX_BACKTRACE_SZ 128
595 void crash_handler(int signum)
596 {
597 	void *bt[MAX_BACKTRACE_SZ];
598 	size_t sz;
599 
600 	sz = backtrace(bt, ARRAY_SIZE(bt));
601 
602 	if (env.test)
603 		dump_test_log(env.test, true);
604 	if (env.stdout)
605 		stdio_restore();
606 
607 	fprintf(stderr, "Caught signal #%d!\nStack trace:\n", signum);
608 	backtrace_symbols_fd(bt, sz, STDERR_FILENO);
609 }
610 
611 int main(int argc, char **argv)
612 {
613 	static const struct argp argp = {
614 		.options = opts,
615 		.parser = parse_arg,
616 		.doc = argp_program_doc,
617 	};
618 	struct sigaction sigact = {
619 		.sa_handler = crash_handler,
620 		.sa_flags = SA_RESETHAND,
621 	};
622 	int err, i;
623 
624 	sigaction(SIGSEGV, &sigact, NULL);
625 
626 	err = argp_parse(&argp, argc, argv, 0, NULL, &env);
627 	if (err)
628 		return err;
629 
630 	err = cd_flavor_subdir(argv[0]);
631 	if (err)
632 		return err;
633 
634 	libbpf_set_print(libbpf_print_fn);
635 
636 	srand(time(NULL));
637 
638 	env.jit_enabled = is_jit_enabled();
639 	env.nr_cpus = libbpf_num_possible_cpus();
640 	if (env.nr_cpus < 0) {
641 		fprintf(stderr, "Failed to get number of CPUs: %d!\n",
642 			env.nr_cpus);
643 		return -1;
644 	}
645 
646 	stdio_hijack();
647 	for (i = 0; i < prog_test_cnt; i++) {
648 		struct prog_test_def *test = &prog_test_defs[i];
649 
650 		env.test = test;
651 		test->test_num = i + 1;
652 
653 		if (!should_run(&env.test_selector,
654 				test->test_num, test->test_name))
655 			continue;
656 
657 		test->run_test();
658 		/* ensure last sub-test is finalized properly */
659 		if (test->subtest_name)
660 			test__end_subtest();
661 
662 		test->tested = true;
663 		if (test->error_cnt)
664 			env.fail_cnt++;
665 		else
666 			env.succ_cnt++;
667 		skip_account();
668 
669 		dump_test_log(test, test->error_cnt);
670 
671 		fprintf(env.stdout, "#%d %s:%s\n",
672 			test->test_num, test->test_name,
673 			test->error_cnt ? "FAIL" : "OK");
674 
675 		reset_affinity();
676 		if (test->need_cgroup_cleanup)
677 			cleanup_cgroup_environment();
678 	}
679 	stdio_restore();
680 	fprintf(stdout, "Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
681 		env.succ_cnt, env.sub_succ_cnt, env.skip_cnt, env.fail_cnt);
682 
683 	free_str_set(&env.test_selector.blacklist);
684 	free_str_set(&env.test_selector.whitelist);
685 	free(env.test_selector.num_set);
686 	free_str_set(&env.subtest_selector.blacklist);
687 	free_str_set(&env.subtest_selector.whitelist);
688 	free(env.subtest_selector.num_set);
689 
690 	return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
691 }
692