summaryrefslogtreecommitdiff
path: root/tools/perf/tests/symbols.c
blob: b644d95c6be9adc87d77d75f7d23df4c809e1d8f (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// SPDX-License-Identifier: GPL-2.0
#include <linux/compiler.h>
#include <linux/string.h>
#include <sys/mman.h>
#include <inttypes.h>
#include <limits.h>
#include "debug.h"
#include "dso.h"
#include "env.h"
#include "machine.h"
#include "thread.h"
#include "symbol.h"
#include "map.h"
#include "util.h"
#include "tests.h"

struct test_info {
	struct perf_env host_env;
	struct machine *machine;
	struct thread *thread;
};

static int init_test_info(struct test_info *ti)
{
	perf_env__init(&ti->host_env);
	ti->machine = machine__new_host(&ti->host_env);
	if (!ti->machine) {
		pr_debug("machine__new_host() failed!\n");
		perf_env__exit(&ti->host_env);
		return TEST_FAIL;
	}

	/* Create a dummy thread */
	ti->thread = machine__findnew_thread(ti->machine, 100, 100);
	if (!ti->thread) {
		pr_debug("machine__findnew_thread() failed!\n");
		perf_env__exit(&ti->host_env);
		return TEST_FAIL;
	}

	return TEST_OK;
}

static void exit_test_info(struct test_info *ti)
{
	thread__put(ti->thread);
	machine__delete(ti->machine);
	perf_env__exit(&ti->host_env);
}

struct dso_map {
	struct dso *dso;
	struct map *map;
};

static int find_map_cb(struct map *map, void *d)
{
	struct dso_map *data = d;

	if (map__dso(map) != data->dso)
		return 0;
	data->map = map;
	return 1;
}

static struct map *find_module_map(struct machine *machine, struct dso *dso)
{
	struct dso_map data = { .dso = dso };

	machine__for_each_kernel_map(machine, find_map_cb, &data);

	return data.map;
}

static void get_test_dso_filename(char *filename, size_t max_sz)
{
	if (dso_to_test)
		strlcpy(filename, dso_to_test, max_sz);
	else
		perf_exe(filename, max_sz);
}

static int create_map(struct test_info *ti, char *filename, struct map **map_p)
{
	struct dso *dso = machine__findnew_dso(ti->machine, filename);

	/*
	 * If 'filename' matches a current kernel module, must use a kernel
	 * map. Find the one that already exists.
	 */
	if (dso && dso__kernel(dso) != DSO_SPACE__USER) {
		*map_p = find_module_map(ti->machine, dso);
		dso__put(dso);
		if (!*map_p) {
			pr_debug("Failed to find map for current kernel module %s",
				 filename);
			return TEST_FAIL;
		}
		map__get(*map_p);
		return TEST_OK;
	}

	dso__put(dso);

	/* Create a dummy map at 0x100000 */
	*map_p = map__new(ti->machine, 0x100000, 0xffffffff, 0, &dso_id_empty,
			  PROT_EXEC, /*flags=*/0, filename, ti->thread);
	if (!*map_p) {
		pr_debug("Failed to create map!");
		return TEST_FAIL;
	}

	return TEST_OK;
}

static int test_dso(struct dso *dso)
{
	struct symbol *last_sym = NULL;
	struct rb_node *nd;
	int ret = TEST_OK;

	/* dso__fprintf() prints all the symbols */
	if (verbose > 1)
		dso__fprintf(dso, stderr);

	for (nd = rb_first_cached(dso__symbols(dso)); nd; nd = rb_next(nd)) {
		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);

		if (symbol__type(sym) != STT_FUNC && symbol__type(sym) != STT_GNU_IFUNC)
			continue;

		/* Check for overlapping function symbols */
		if (last_sym && sym->start < last_sym->end) {
			pr_debug("Overlapping symbols:\n");
			symbol__fprintf(last_sym, stderr);
			symbol__fprintf(sym, stderr);
			ret = TEST_FAIL;
		}
		/* Check for zero-length function symbol */
		if (sym->start == sym->end) {
			pr_debug("Zero-length symbol:\n");
			symbol__fprintf(sym, stderr);
			ret = TEST_FAIL;
		}
		last_sym = sym;
	}

	return ret;
}

static int subdivided_dso_cb(struct dso *dso, struct machine *machine __maybe_unused, void *d)
{
	struct dso *text_dso = d;

	if (dso != text_dso && strstarts(dso__short_name(dso), dso__short_name(text_dso)))
		if (test_dso(dso) != TEST_OK)
			return -1;

	return 0;
}

static int process_subdivided_dso(struct machine *machine, struct dso *dso)
{
	int ret;

	ret = machine__for_each_dso(machine, subdivided_dso_cb, dso);

	return ret < 0 ? TEST_FAIL : TEST_OK;
}

static int test_file(struct test_info *ti, char *filename)
{
	struct map *map = NULL;
	int ret, nr;
	struct dso *dso;

	pr_debug("Testing %s\n", filename);

	ret = create_map(ti, filename, &map);
	if (ret != TEST_OK)
		return ret;

	dso = map__dso(map);
	nr = dso__load(dso, map);
	if (nr < 0) {
		pr_debug("dso__load() failed!\n");
		ret = TEST_FAIL;
		goto out_put;
	}

	if (nr == 0) {
		pr_debug("DSO has no symbols!\n");
		ret = TEST_SKIP;
		goto out_put;
	}

	ret = test_dso(dso);

	/* Module dso is split into many dsos by section */
	if (ret == TEST_OK && dso__kernel(dso) != DSO_SPACE__USER)
		ret = process_subdivided_dso(ti->machine, dso);
out_put:
	map__put(map);

	return ret;
}

static int test__symbols(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
	char filename[PATH_MAX];
	struct test_info ti;
	int ret;

	ret = init_test_info(&ti);
	if (ret != TEST_OK)
		return ret;

	get_test_dso_filename(filename, sizeof(filename));

	ret = test_file(&ti, filename);

	exit_test_info(&ti);

	return ret;
}

struct kallsyms_sym {
	u64 start;
	const char *name;
};

/*
 * kallsyms from an x86 host where nf_tables was loaded in the page right
 * after the last symbol of dca, plus a kernel symbol followed closely by a
 * module and a module far away from the others.
 */
static const struct kallsyms_sym kallsyms_syms[] = {
	{ 0xffffffff81000000, "_stext" },
	{ 0xffffffff81000ff0, "last_kernel_symbol" },
	{ 0xffffffff81001000, "near_module_symbol\t[near]" },
	{ 0xffffffffc0c4aa40, "dca_exit\t[dca]" },
	{ 0xffffffffc0c4aa50, "dca_sysfs_exit\t[dca]" },
	{ 0xffffffffc0c4b000, "__nft_trace_packet\t[nf_tables]" },
	{ 0xffffffffc0c4b0b0, "nft_do_chain\t[nf_tables]" },
	{ 0xffffffffc0c4b4e0, "nf_tables_core_module_exit\t[nf_tables]" },
	{ 0xffffffffc2000000, "far_module_symbol\t[far]" },
};

static int check_symbol(struct dso *dso, u64 addr, const char *name, u64 end)
{
	struct symbol *sym = dso__find_symbol_nocache(dso, addr);

	if (!sym || strcmp(sym->name, name)) {
		pr_debug("%#" PRIx64 ": expected %s, got %s\n", addr, name,
			 sym ? sym->name : "no symbol");
		return TEST_FAIL;
	}
	if (sym->end != end) {
		pr_debug("%s: expected end %#" PRIx64 ", got %#" PRIx64 "\n",
			 name, end, sym->end);
		return TEST_FAIL;
	}
	return TEST_OK;
}

static int test__kallsyms_fixup_end(struct test_suite *test __maybe_unused,
				    int subtest __maybe_unused)
{
	struct dso *dso = dso__new("[kernel.kallsyms]");
	int ret = TEST_FAIL;

	if (!dso)
		return TEST_FAIL;

	for (unsigned int i = 0; i < ARRAY_SIZE(kallsyms_syms); i++) {
		struct symbol *sym = symbol__new(kallsyms_syms[i].start, 0, 0, 0,
						 kallsyms_syms[i].name);

		if (!sym)
			goto out;
		symbols__insert(dso__symbols(dso), sym);
	}

	symbols__fixup_end(dso__symbols(dso), true);

	ret = TEST_OK;
	/* The next symbol is too close for the end of the page. */
	if (check_symbol(dso, 0xffffffff81000ff8, "last_kernel_symbol",
			 0xffffffff81001000))
		ret = TEST_FAIL;
	if (check_symbol(dso, 0xffffffffc0c4aa58, "dca_sysfs_exit\t[dca]",
			 0xffffffffc0c4b000))
		ret = TEST_FAIL;
	if (check_symbol(dso, 0xffffffffc0c4b280, "nft_do_chain\t[nf_tables]",
			 0xffffffffc0c4b4e0))
		ret = TEST_FAIL;

	/* Far from the next module, the end of the page is still used. */
	if (check_symbol(dso, 0xffffffffc0c4b4f0,
			 "nf_tables_core_module_exit\t[nf_tables]",
			 0xffffffffc0c4d000))
		ret = TEST_FAIL;
	if (check_symbol(dso, 0xffffffff81001008, "near_module_symbol\t[near]",
			 0xffffffff81002000))
		ret = TEST_FAIL;
out:
	dso__put(dso);
	return ret;
}

static struct test_case tests__symbols[] = {
	TEST_CASE("Symbols", symbols),
	TEST_CASE("Kallsyms symbol ends", kallsyms_fixup_end),
	{ .name = NULL, }
};

struct test_suite suite__symbols = {
	.desc = "Symbols",
	.test_cases = tests__symbols,
};