File: bad_struct_ops.c

package info (click to toggle)
linux 6.16.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,724,468 kB
  • sloc: ansic: 26,560,391; asm: 271,356; sh: 143,999; python: 72,469; makefile: 57,129; perl: 36,821; xml: 19,553; cpp: 5,820; yacc: 4,915; lex: 2,955; awk: 1,667; sed: 28; ruby: 25
file content (67 lines) | stat: -rw-r--r-- 1,563 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: GPL-2.0

#include <test_progs.h>
#include "bad_struct_ops.skel.h"
#include "bad_struct_ops2.skel.h"

static void invalid_prog_reuse(void)
{
	struct bad_struct_ops *skel;
	char *log = NULL;
	int err;

	skel = bad_struct_ops__open();
	if (!ASSERT_OK_PTR(skel, "bad_struct_ops__open"))
		return;

	if (start_libbpf_log_capture())
		goto cleanup;

	err = bad_struct_ops__load(skel);
	log = stop_libbpf_log_capture();
	ASSERT_ERR(err, "bad_struct_ops__load should fail");
	ASSERT_HAS_SUBSTR(log,
		"struct_ops init_kern testmod_2 func ptr test_1: invalid reuse of prog test_1",
		"expected init_kern message");

cleanup:
	free(log);
	bad_struct_ops__destroy(skel);
}

static void unused_program(void)
{
	struct bad_struct_ops2 *skel;
	char *log = NULL;
	int err;

	skel = bad_struct_ops2__open();
	if (!ASSERT_OK_PTR(skel, "bad_struct_ops2__open"))
		return;

	/* struct_ops programs not referenced from any maps are open
	 * with autoload set to true.
	 */
	ASSERT_TRUE(bpf_program__autoload(skel->progs.foo), "foo autoload == true");

	if (start_libbpf_log_capture())
		goto cleanup;

	err = bad_struct_ops2__load(skel);
	ASSERT_ERR(err, "bad_struct_ops2__load should fail");
	log = stop_libbpf_log_capture();
	ASSERT_HAS_SUBSTR(log, "prog 'foo': failed to load",
			  "message about 'foo' failing to load");

cleanup:
	free(log);
	bad_struct_ops2__destroy(skel);
}

void test_bad_struct_ops(void)
{
	if (test__start_subtest("invalid_prog_reuse"))
		invalid_prog_reuse();
	if (test__start_subtest("unused_program"))
		unused_program();
}