summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/log_fixup.c
blob: 90a98e23be619da6aae628ae53307e938f840227 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
#include <bpf/btf.h>

#include "test_log_fixup.skel.h"

enum trunc_type {
	TRUNC_NONE,
	TRUNC_PARTIAL,
	TRUNC_FULL,
};

static void bad_core_relo(size_t log_buf_size, enum trunc_type trunc_type)
{
	char log_buf[8 * 1024];
	struct test_log_fixup* skel;
	int err;

	skel = test_log_fixup__open();
	if (!ASSERT_OK_PTR(skel, "skel_open"))
		return;

	bpf_program__set_autoload(skel->progs.bad_relo, true);
	memset(log_buf, 0, sizeof(log_buf));
	bpf_program__set_log_buf(skel->progs.bad_relo, log_buf, log_buf_size ?: sizeof(log_buf));
	bpf_program__set_log_level(skel->progs.bad_relo, 1 | 8); /* BPF_LOG_FIXED to force truncation */

	err = test_log_fixup__load(skel);
	if (!ASSERT_ERR(err, "load_fail"))
		goto cleanup;

	ASSERT_HAS_SUBSTR(log_buf,
			  "0: <invalid CO-RE relocation>\n"
			  "failed to resolve CO-RE relocation <byte_sz> ",
			  "log_buf_part1");

	switch (trunc_type) {
	case TRUNC_NONE:
		ASSERT_HAS_SUBSTR(log_buf,
				  "struct task_struct___bad.fake_field (0:1 @ offset 4)\n",
				  "log_buf_part2");
		ASSERT_HAS_SUBSTR(log_buf,
				  "max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0\n",
				  "log_buf_end");
		break;
	case TRUNC_PARTIAL:
		/* we should get full libbpf message patch */
		ASSERT_HAS_SUBSTR(log_buf,
				  "struct task_struct___bad.fake_field (0:1 @ offset 4)\n",
				  "log_buf_part2");
		/* we shouldn't get full end of BPF verifier log */
		ASSERT_NULL(strstr(log_buf, "max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0\n"),
			    "log_buf_end");
		break;
	case TRUNC_FULL:
		/* we shouldn't get second part of libbpf message patch */
		ASSERT_NULL(strstr(log_buf, "struct task_struct___bad.fake_field (0:1 @ offset 4)\n"),
			    "log_buf_part2");
		/* we shouldn't get full end of BPF verifier log */
		ASSERT_NULL(strstr(log_buf, "max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0\n"),
			    "log_buf_end");
		break;
	}

	if (env.verbosity > VERBOSE_NONE)
		printf("LOG:   \n=================\n%s=================\n", log_buf);
cleanup:
	test_log_fixup__destroy(skel);
}

static void bad_core_relo_subprog(void)
{
	char log_buf[8 * 1024];
	struct test_log_fixup* skel;
	int err;

	skel = test_log_fixup__open();
	if (!ASSERT_OK_PTR(skel, "skel_open"))
		return;

	bpf_program__set_autoload(skel->progs.bad_relo_subprog, true);
	bpf_program__set_log_buf(skel->progs.bad_relo_subprog, log_buf, sizeof(log_buf));

	err = test_log_fixup__load(skel);
	if (!ASSERT_ERR(err, "load_fail"))
		goto cleanup;

	ASSERT_HAS_SUBSTR(log_buf,
			  ": <invalid CO-RE relocation>\n"
			  "failed to resolve CO-RE relocation <byte_off> ",
			  "log_buf");
	ASSERT_HAS_SUBSTR(log_buf,
			  "struct task_struct___bad.fake_field_subprog (0:2 @ offset 8)\n",
			  "log_buf");

	if (env.verbosity > VERBOSE_NONE)
		printf("LOG:   \n=================\n%s=================\n", log_buf);

cleanup:
	test_log_fixup__destroy(skel);
}

static void missing_map(void)
{
	char log_buf[8 * 1024];
	struct test_log_fixup* skel;
	int err;

	skel = test_log_fixup__open();
	if (!ASSERT_OK_PTR(skel, "skel_open"))
		return;

	bpf_map__set_autocreate(skel->maps.missing_map, false);

	bpf_program__set_autoload(skel->progs.use_missing_map, true);
	bpf_program__set_log_buf(skel->progs.use_missing_map, log_buf, sizeof(log_buf));

	err = test_log_fixup__load(skel);
	if (!ASSERT_ERR(err, "load_fail"))
		goto cleanup;

	ASSERT_TRUE(bpf_map__autocreate(skel->maps.existing_map), "existing_map_autocreate");
	ASSERT_FALSE(bpf_map__autocreate(skel->maps.missing_map), "missing_map_autocreate");

	ASSERT_HAS_SUBSTR(log_buf,
			  ": <invalid BPF map reference>\n"
			  "BPF map 'missing_map' is referenced but wasn't created\n",
			  "log_buf");

	if (env.verbosity > VERBOSE_NONE)
		printf("LOG:   \n=================\n%s=================\n", log_buf);

cleanup:
	test_log_fixup__destroy(skel);
}

static void missing_kfunc(void)
{
	char log_buf[8 * 1024];
	struct test_log_fixup* skel;
	int err;

	skel = test_log_fixup__open();
	if (!ASSERT_OK_PTR(skel, "skel_open"))
		return;

	bpf_program__set_autoload(skel->progs.use_missing_kfunc, true);
	bpf_program__set_log_buf(skel->progs.use_missing_kfunc, log_buf, sizeof(log_buf));

	err = test_log_fixup__load(skel);
	if (!ASSERT_ERR(err, "load_fail"))
		goto cleanup;

	ASSERT_HAS_SUBSTR(log_buf,
			  "0: <invalid kfunc call>\n"
			  "kfunc 'bpf_nonexistent_kfunc' is referenced but wasn't resolved\n",
			  "log_buf");

	if (env.verbosity > VERBOSE_NONE)
		printf("LOG:   \n=================\n%s=================\n", log_buf);

cleanup:
	test_log_fixup__destroy(skel);
}

void test_log_fixup(void)
{
	if (test__start_subtest("bad_core_relo_trunc_none"))
		bad_core_relo(0, TRUNC_NONE /* full buf */);
	if (test__start_subtest("bad_core_relo_trunc_partial"))
		bad_core_relo(300, TRUNC_PARTIAL /* truncate original log a bit */);
	if (test__start_subtest("bad_core_relo_trunc_full"))
		bad_core_relo(240, TRUNC_FULL  /* truncate also libbpf's message patch */);
	if (test__start_subtest("bad_core_relo_subprog"))
		bad_core_relo_subprog();
	if (test__start_subtest("missing_map"))
		missing_map();
	if (test__start_subtest("missing_kfunc"))
		missing_kfunc();
}